diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index f54f0636a5f0c..1d4a85f0bc3fa 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -49,6 +49,9 @@ use OCP\Mail\IMailer; use OCP\Security\ICrypto; use OCP\Security\ISecureRandom; +use function array_filter; +use function count; +use function reset; /** * Class LostController @@ -383,12 +386,12 @@ protected function findUserByIdOrMail($input) { 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; diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index de8e8cfbcefbe..60c225a2832b4 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -807,28 +807,38 @@ public function testTwoUsersWithSameEmail() { $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) { $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') @@ -838,14 +848,7 @@ public function testTwoUsersWithSameEmailOneDisabled() { ->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); } }