]> source.dussan.org Git - nextcloud-server.git/commitdiff
Return first value from $users 16308/head
authorDaniel Kesselberg <mail@danielkesselberg.de>
Tue, 9 Jul 2019 09:58:14 +0000 (11:58 +0200)
committerDaniel Kesselberg <mail@danielkesselberg.de>
Tue, 9 Jul 2019 17:29:14 +0000 (19:29 +0200)
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
core/Controller/LostController.php
tests/Core/Controller/LostControllerTest.php

index 96018555ec3c408d8b3fb5925919463ef311b980..5ac0557e5d6bf5a4118eb8687f46fa77259635bd 100644 (file)
@@ -50,6 +50,9 @@ use OCP\IUserManager;
 use OCP\Mail\IMailer;
 use OCP\Security\ICrypto;
 use OCP\Security\ISecureRandom;
+use function array_filter;
+use function count;
+use function reset;
 
 /**
  * Class LostController
@@ -389,12 +392,12 @@ class LostController extends Controller {
                        return $user;
                }
 
-               $users = \array_filter($this->userManager->getByEmail($input), function (IUser $user) {
+               $users = array_filter($this->userManager->getByEmail($input), function (IUser $user) {
                        return $user->isEnabled();
                });
 
-               if (\count($users) === 1) {
-                       return $users[0];
+               if (count($users) === 1) {
+                       return reset($users);
                }
 
                throw $userNotFound;
index 8635616a9d52664dba6d13a4108e1dcf9530355b..8500819a9cae812c906608fc5bf6802932ed945b 100644 (file)
@@ -821,28 +821,38 @@ class LostControllerTest extends \Test\TestCase {
                $this->assertEquals($expectedResponse, $response);
        }
 
-       public function testTwoUsersWithSameEmailOneDisabled() {
+
+       /**
+        * @return array
+        */
+       public function dataTwoUserswithSameEmailOneDisabled(): array {
+               return [
+                       ['user1' => true, 'user2' => false],
+                       ['user1' => false, 'user2' => true]
+               ];
+       }
+
+       /**
+        * @dataProvider dataTwoUserswithSameEmailOneDisabled
+        * @param bool $userEnabled1
+        * @param bool $userEnabled2
+        */
+       public function testTwoUsersWithSameEmailOneDisabled(bool $userEnabled1, bool $userEnabled2): void {
                $user1 = $this->createMock(IUser::class);
-               $user1->expects($this->any())
-                       ->method('getEMailAddress')
+               $user1->method('getEMailAddress')
                        ->willReturn('test@example.com');
-               $user1->expects($this->any())
-                       ->method('getUID')
+               $user1->method('getUID')
                        ->willReturn('User1');
-               $user1->expects($this->any())
-                       ->method('isEnabled')
-                       ->willReturn(true);
+               $user1->method('isEnabled')
+                       ->willReturn($userEnabled1);
 
                $user2 = $this->createMock(IUser::class);
-               $user2->expects($this->any())
-                       ->method('getEMailAddress')
+               $user2->method('getEMailAddress')
                        ->willReturn('test@example.com');
-               $user2->expects($this->any())
-                       ->method('getUID')
+               $user2->method('getUID')
                        ->willReturn('User2');
-               $user2->expects($this->any())
-                       ->method('isEnabled')
-                       ->willReturn(false);
+               $user2->method('isEnabled')
+                       ->willReturn($userEnabled2);
 
                $this->userManager
                        ->method('get')
@@ -852,14 +862,7 @@ class LostControllerTest extends \Test\TestCase {
                        ->method('getByEmail')
                        ->willReturn([$user1, $user2]);
 
-               // request password reset for test@example.com
-               $response = $this->lostController->email('test@example.com');
-
-               $expectedResponse = new JSONResponse([
-                       'status' => 'success'
-               ]);
-               $expectedResponse->throttle();
-
-               $this->assertEquals($expectedResponse, $response);
+               $result = self::invokePrivate($this->lostController, 'findUserByIdOrMail', ['test@example.com']);
+               $this->assertInstanceOf(IUser::class, $result);
        }
 }