diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e2b291852a..0bbb3bc90f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -12225,16 +12225,6 @@ parameters: count: 1 path: src/lib/MVC/Symfony/Security/InteractiveLoginToken.php - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProvider\\:\\:loadUserByUsername\\(\\) has parameter \\$user with no type specified\\.$#" - count: 1 - path: src/lib/MVC/Symfony/Security/User/EmailProvider.php - - - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProvider\\:\\:loadUserByUsername\\(\\) has parameter \\$user with no type specified\\.$#" - count: 1 - path: src/lib/MVC/Symfony/Security/User/UsernameProvider.php - - message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\UserInterface\\:\\:setAPIUser\\(\\) has no return type specified\\.$#" count: 1 @@ -46905,116 +46895,6 @@ parameters: count: 1 path: tests/lib/MVC/Symfony/Security/InteractiveLoginTokenTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:supportsClassProvider\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByAPIUser\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsername\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsernameAlreadyUserObject\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testLoadUserByUsernameUserNotFound\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUser\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUserNotFound\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testRefreshUserNotSupported\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$class with no type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\EmailProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$supports with no type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:supportsClassProvider\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByAPIUser\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsername\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsernameAlreadyUserObject\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testLoadUserByUsernameUserNotFound\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUser\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUserNotFound\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testRefreshUserNotSupported\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$class with no type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\MVC\\\\Symfony\\\\Security\\\\User\\\\UsernameProviderTest\\:\\:testSupportsClass\\(\\) has parameter \\$supports with no type specified\\.$#" - count: 1 - path: tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php - - message: "#^Parameter \\#1 \\$expirationDate of class Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\PasswordInfo constructor expects DateTimeImmutable\\|null, DateTimeImmutable\\|false given\\.$#" count: 1 diff --git a/src/contracts/Repository/UserService.php b/src/contracts/Repository/UserService.php index faf485aca0..58aec8f8f8 100644 --- a/src/contracts/Repository/UserService.php +++ b/src/contracts/Repository/UserService.php @@ -165,6 +165,7 @@ public function loadUser(int $userId, array $prioritizedLanguages = []): User; * @return \Ibexa\Contracts\Core\Repository\Values\User\User * * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException if a user with the given credentials was not found + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException */ public function loadUserByLogin(string $login, array $prioritizedLanguages = []): User; @@ -187,6 +188,7 @@ public function checkUserCredentials(User $user, string $credentials): bool; * @return \Ibexa\Contracts\Core\Repository\Values\User\User * * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException */ public function loadUserByEmail(string $email, array $prioritizedLanguages = []): User; diff --git a/src/lib/MVC/Symfony/Security/User/EmailProvider.php b/src/lib/MVC/Symfony/Security/User/EmailProvider.php index 79aae22ed1..4976c77933 100644 --- a/src/lib/MVC/Symfony/Security/User/EmailProvider.php +++ b/src/lib/MVC/Symfony/Security/User/EmailProvider.php @@ -8,27 +8,26 @@ namespace Ibexa\Core\MVC\Symfony\Security\User; +use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; -use Ibexa\Core\MVC\Symfony\Security\UserInterface; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; +use Symfony\Component\Security\Core\Exception\UserNotFoundException; +use Symfony\Component\Security\Core\User\UserInterface; final class EmailProvider extends BaseProvider { - public function loadUserByUsername($user) + public function loadUserByIdentifier(string $identifier): UserInterface { try { - // SecurityContext always tries to authenticate anonymous users when checking granted access. - // In that case $user is an instance of \Ibexa\Core\MVC\Symfony\Security\User. - // We don't need to reload the user here. - if ($user instanceof UserInterface) { - return $user; - } - return $this->createSecurityUser( - $this->userService->loadUserByEmail($user) + $this->userService->loadUserByEmail($identifier) ); - } catch (NotFoundException $e) { - throw new UsernameNotFoundException($e->getMessage(), 0, $e); + } catch (NotFoundException|InvalidArgumentException $e) { + throw new UserNotFoundException($e->getMessage(), 0, $e); } } + + public function loadUserByUsername(string $username): UserInterface + { + return $this->loadUserByIdentifier($username); + } } diff --git a/src/lib/MVC/Symfony/Security/User/UsernameProvider.php b/src/lib/MVC/Symfony/Security/User/UsernameProvider.php index f9adacf79a..865777eee7 100644 --- a/src/lib/MVC/Symfony/Security/User/UsernameProvider.php +++ b/src/lib/MVC/Symfony/Security/User/UsernameProvider.php @@ -8,27 +8,26 @@ namespace Ibexa\Core\MVC\Symfony\Security\User; +use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; -use Ibexa\Core\MVC\Symfony\Security\UserInterface; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; +use Symfony\Component\Security\Core\Exception\UserNotFoundException; +use Symfony\Component\Security\Core\User\UserInterface; final class UsernameProvider extends BaseProvider { - public function loadUserByUsername($user) + public function loadUserByIdentifier(string $identifier): UserInterface { try { - // SecurityContext always tries to authenticate anonymous users when checking granted access. - // In that case $user is an instance of \Ibexa\Core\MVC\Symfony\Security\User. - // We don't need to reload the user here. - if ($user instanceof UserInterface) { - return $user; - } - return $this->createSecurityUser( - $this->userService->loadUserByLogin($user) + $this->userService->loadUserByLogin($identifier) ); - } catch (NotFoundException $e) { - throw new UsernameNotFoundException($e->getMessage(), 0, $e); + } catch (NotFoundException|InvalidArgumentException $e) { + throw new UserNotFoundException($e->getMessage(), 0, $e); } } + + public function loadUserByUsername(string $username): UserInterface + { + return $this->loadUserByIdentifier($username); + } } diff --git a/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php b/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php new file mode 100644 index 0000000000..3cada1549e --- /dev/null +++ b/tests/lib/MVC/Symfony/Security/User/BaseProviderTestCase.php @@ -0,0 +1,195 @@ +userService = $this->createMock(UserService::class); + $this->permissionResolver = $this->createMock(PermissionResolver::class); + $this->userProvider = $this->buildProvider(); + } + + /** + * @phpstan-return list, bool}> + */ + public function supportsClassProvider(): array + { + return [ + [SymfonyUserInterface::class, false], + [MVCUser::class, true], + [get_class($this->createMock(MVCUser::class)), true], + ]; + } + + /** + * @dataProvider supportsClassProvider + * + * @phpstan-param class-string<\Symfony\Component\Security\Core\User\UserInterface> $class + */ + public function testSupportsClass(string $class, bool $supports): void + { + self::assertSame($supports, $this->userProvider->supportsClass($class)); + } + + public function testLoadUserByAPIUser(): void + { + $apiUser = $this->createMock(APIUser::class); + + $user = $this->userProvider->loadUserByAPIUser($apiUser); + + self::assertInstanceOf(MVCUser::class, $user); + self::assertSame($apiUser, $user->getAPIUser()); + self::assertSame(['ROLE_USER'], $user->getRoles()); + } + + public function testRefreshUserNotFound(): void + { + $userId = 123; + $apiUser = $this->buildUserValueObjectStub($userId); + $user = $this->createMock(UserInterface::class); + $user + ->expects(self::once()) + ->method('getAPIUser') + ->willReturn($apiUser); + + $this->userService + ->expects(self::once()) + ->method('loadUser') + ->with($userId) + ->willThrowException(new NotFoundException('user', 'foo')); + + $this->expectException(UserNotFoundException::class); + $this->userProvider->refreshUser($user); + } + + public function testRefreshUserNotSupported(): void + { + $user = $this->createMock(SymfonyUserInterface::class); + + $this->expectException(UnsupportedUserException::class); + $this->userProvider->refreshUser($user); + } + + protected function createUserWrapperMockFromAPIUser(User $apiUser, int $userId): UserInterface & MockObject + { + $refreshedAPIUser = clone $apiUser; + $user = $this->createMock(UserInterface::class); + $user + ->expects(self::once()) + ->method('getAPIUser') + ->willReturn($apiUser) + ; + $user + ->expects(self::once()) + ->method('setAPIUser') + ->with($refreshedAPIUser) + ; + + $this->userService + ->expects(self::once()) + ->method('loadUser') + ->with($userId) + ->willReturn($refreshedAPIUser) + ; + + return $user; + } + + public function testRefreshUser(): void + { + $userId = 123; + $apiUser = $this->buildUserValueObjectStub($userId); + $user = $this->createUserWrapperMockFromAPIUser($apiUser, $userId); + + $this->permissionResolver + ->expects(self::once()) + ->method('setCurrentUserReference') + ->with(new UserReference($apiUser->getUserId())); + + self::assertSame($user, $this->userProvider->refreshUser($user)); + } + + public function testLoadUserByUsername(): void + { + $username = $this->getUserIdentifier(); + $apiUser = $this->createMock(APIUser::class); + + $this->userService + ->expects(self::once()) + ->method($this->getUserServiceMethod()) + ->with($username) + ->willReturn($apiUser); + + $user = $this->userProvider->loadUserByIdentifier($username); + self::assertInstanceOf(UserInterface::class, $user); + self::assertSame($apiUser, $user->getAPIUser()); + self::assertSame(['ROLE_USER'], $user->getRoles()); + } + + public function testLoadUserByUsernameUserNotFound(): void + { + $username = $this->getUserIdentifier(); + $this->userService + ->expects(self::once()) + ->method($this->getUserServiceMethod()) + ->with($username) + ->willThrowException(new NotFoundException('user', $username)); + + $this->expectException(UserNotFoundException::class); + $this->userProvider->loadUserByIdentifier($username); + } + + final protected function buildUserValueObjectStub(int $userId): User + { + return new User( + [ + 'content' => new Content( + [ + 'versionInfo' => new VersionInfo( + ['contentInfo' => new ContentInfo(['id' => $userId])] + ), + ] + ), + ] + ); + } +} diff --git a/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php b/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php index 1579cd7a37..261609b26e 100644 --- a/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php +++ b/tests/lib/MVC/Symfony/Security/User/EmailProviderTest.php @@ -8,181 +8,26 @@ namespace Ibexa\Tests\Core\MVC\Symfony\Security\User; -use Ibexa\Contracts\Core\Repository\PermissionResolver; -use Ibexa\Contracts\Core\Repository\UserService; -use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; -use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser; -use Ibexa\Core\Base\Exceptions\NotFoundException; -use Ibexa\Core\MVC\Symfony\Security\User as MVCUser; +use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider; use Ibexa\Core\MVC\Symfony\Security\User\EmailProvider; -use Ibexa\Core\MVC\Symfony\Security\UserInterface; -use Ibexa\Core\Repository\Values\Content\Content; -use Ibexa\Core\Repository\Values\Content\VersionInfo; -use Ibexa\Core\Repository\Values\User\User; -use Ibexa\Core\Repository\Values\User\UserReference; -use PHPUnit\Framework\TestCase; -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; -use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; -class EmailProviderTest extends TestCase +/** + * @covers \Ibexa\Core\MVC\Symfony\Security\User\EmailProvider + */ +final class EmailProviderTest extends BaseProviderTestCase { - /** @var \Ibexa\Contracts\Core\Repository\UserService|\PHPUnit\Framework\MockObject\MockObject */ - private $userService; - - /** @var \Ibexa\Contracts\Core\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */ - private $permissionResolver; - - /** @var \Ibexa\Core\MVC\Symfony\Security\User\EmailProvider */ - private $userProvider; - - protected function setUp(): void - { - parent::setUp(); - $this->userService = $this->createMock(UserService::class); - $this->permissionResolver = $this->createMock(PermissionResolver::class); - $this->userProvider = new EmailProvider($this->userService, $this->permissionResolver); - } - - public function testLoadUserByUsernameAlreadyUserObject() - { - $user = $this->createMock(UserInterface::class); - self::assertSame($user, $this->userProvider->loadUserByUsername($user)); - } - - public function testLoadUserByUsernameUserNotFound() - { - $this->expectException(UsernameNotFoundException::class); - - $username = 'foobar@example.org'; - $this->userService - ->expects(self::once()) - ->method('loadUserByEmail') - ->with($username) - ->will(self::throwException(new NotFoundException('user', $username))); - $this->userProvider->loadUserByUsername($username); - } - - public function testLoadUserByUsername() - { - $username = 'foobar@example.org'; - $apiUser = $this->createMock(APIUser::class); - - $this->userService - ->expects(self::once()) - ->method('loadUserByEmail') - ->with($username) - ->will(self::returnValue($apiUser)); - - $user = $this->userProvider->loadUserByUsername($username); - self::assertInstanceOf(UserInterface::class, $user); - self::assertSame($apiUser, $user->getAPIUser()); - self::assertSame(['ROLE_USER'], $user->getRoles()); - } - - public function testRefreshUserNotSupported() + protected function buildProvider(): BaseProvider { - $this->expectException(UnsupportedUserException::class); - - $user = $this->createMock(SymfonyUserInterface::class); - $this->userProvider->refreshUser($user); + return new EmailProvider($this->userService, $this->permissionResolver); } - public function testRefreshUser() + protected function getUserIdentifier(): string { - $userId = 123; - $apiUser = new User( - [ - 'content' => new Content( - [ - 'versionInfo' => new VersionInfo( - ['contentInfo' => new ContentInfo(['id' => $userId])] - ), - ] - ), - ] - ); - $refreshedAPIUser = clone $apiUser; - $user = $this->createMock(UserInterface::class); - $user - ->expects(self::once()) - ->method('getAPIUser') - ->will(self::returnValue($apiUser)); - $user - ->expects(self::once()) - ->method('setAPIUser') - ->with($refreshedAPIUser); - - $this->userService - ->expects(self::once()) - ->method('loadUser') - ->with($userId) - ->will(self::returnValue($refreshedAPIUser)); - - $this->permissionResolver - ->expects(self::once()) - ->method('setCurrentUserReference') - ->with(new UserReference($apiUser->getUserId())); - - self::assertSame($user, $this->userProvider->refreshUser($user)); + return 'foobar@example.org'; } - public function testRefreshUserNotFound() + protected function getUserServiceMethod(): string { - $this->expectException(UsernameNotFoundException::class); - - $userId = 123; - $apiUser = new User( - [ - 'content' => new Content( - [ - 'versionInfo' => new VersionInfo( - ['contentInfo' => new ContentInfo(['id' => $userId])] - ), - ] - ), - ] - ); - $user = $this->createMock(UserInterface::class); - $user - ->expects(self::once()) - ->method('getAPIUser') - ->will(self::returnValue($apiUser)); - - $this->userService - ->expects(self::once()) - ->method('loadUser') - ->with($userId) - ->will(self::throwException(new NotFoundException('user', 'foo'))); - - $this->userProvider->refreshUser($user); - } - - /** - * @dataProvider supportsClassProvider - */ - public function testSupportsClass($class, $supports) - { - self::assertSame($supports, $this->userProvider->supportsClass($class)); - } - - public function supportsClassProvider() - { - return [ - [SymfonyUserInterface::class, false], - [MVCUser::class, true], - [get_class($this->createMock(MVCUser::class)), true], - ]; - } - - public function testLoadUserByAPIUser() - { - $apiUser = $this->createMock(APIUser::class); - - $user = $this->userProvider->loadUserByAPIUser($apiUser); - - self::assertInstanceOf(MVCUser::class, $user); - self::assertSame($apiUser, $user->getAPIUser()); - self::assertSame(['ROLE_USER'], $user->getRoles()); + return 'loadUserByEmail'; } } diff --git a/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php b/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php index 6fe995cb08..5886976ba4 100644 --- a/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php +++ b/tests/lib/MVC/Symfony/Security/User/UsernameProviderTest.php @@ -8,181 +8,26 @@ namespace Ibexa\Tests\Core\MVC\Symfony\Security\User; -use Ibexa\Contracts\Core\Repository\PermissionResolver; -use Ibexa\Contracts\Core\Repository\UserService; -use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; -use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser; -use Ibexa\Core\Base\Exceptions\NotFoundException; -use Ibexa\Core\MVC\Symfony\Security\User as MVCUser; +use Ibexa\Core\MVC\Symfony\Security\User\BaseProvider; use Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider; -use Ibexa\Core\MVC\Symfony\Security\UserInterface; -use Ibexa\Core\Repository\Values\Content\Content; -use Ibexa\Core\Repository\Values\Content\VersionInfo; -use Ibexa\Core\Repository\Values\User\User; -use Ibexa\Core\Repository\Values\User\UserReference; -use PHPUnit\Framework\TestCase; -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; -use Symfony\Component\Security\Core\User\UserInterface as SymfonyUserInterface; -class UsernameProviderTest extends TestCase +/** + * @covers \Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider + */ +final class UsernameProviderTest extends BaseProviderTestCase { - /** @var \Ibexa\Contracts\Core\Repository\UserService|\PHPUnit\Framework\MockObject\MockObject */ - private $userService; - - /** @var \Ibexa\Contracts\Core\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject */ - private $permissionResolver; - - /** @var \Ibexa\Core\MVC\Symfony\Security\User\UsernameProvider */ - private $userProvider; - - protected function setUp(): void - { - parent::setUp(); - $this->userService = $this->createMock(UserService::class); - $this->permissionResolver = $this->createMock(PermissionResolver::class); - $this->userProvider = new UsernameProvider($this->userService, $this->permissionResolver); - } - - public function testLoadUserByUsernameAlreadyUserObject() - { - $user = $this->createMock(UserInterface::class); - self::assertSame($user, $this->userProvider->loadUserByUsername($user)); - } - - public function testLoadUserByUsernameUserNotFound() - { - $this->expectException(UsernameNotFoundException::class); - - $username = 'foobar'; - $this->userService - ->expects(self::once()) - ->method('loadUserByLogin') - ->with($username) - ->will(self::throwException(new NotFoundException('user', $username))); - $this->userProvider->loadUserByUsername($username); - } - - public function testLoadUserByUsername() - { - $username = 'foobar'; - $apiUser = $this->createMock(APIUser::class); - - $this->userService - ->expects(self::once()) - ->method('loadUserByLogin') - ->with($username) - ->will(self::returnValue($apiUser)); - - $user = $this->userProvider->loadUserByUsername($username); - self::assertInstanceOf(UserInterface::class, $user); - self::assertSame($apiUser, $user->getAPIUser()); - self::assertSame(['ROLE_USER'], $user->getRoles()); - } - - public function testRefreshUserNotSupported() + protected function buildProvider(): BaseProvider { - $this->expectException(UnsupportedUserException::class); - - $user = $this->createMock(SymfonyUserInterface::class); - $this->userProvider->refreshUser($user); + return new UsernameProvider($this->userService, $this->permissionResolver); } - public function testRefreshUser() + protected function getUserIdentifier(): string { - $userId = 123; - $apiUser = new User( - [ - 'content' => new Content( - [ - 'versionInfo' => new VersionInfo( - ['contentInfo' => new ContentInfo(['id' => $userId])] - ), - ] - ), - ] - ); - $refreshedAPIUser = clone $apiUser; - $user = $this->createMock(UserInterface::class); - $user - ->expects(self::once()) - ->method('getAPIUser') - ->will(self::returnValue($apiUser)); - $user - ->expects(self::once()) - ->method('setAPIUser') - ->with($refreshedAPIUser); - - $this->userService - ->expects(self::once()) - ->method('loadUser') - ->with($userId) - ->will(self::returnValue($refreshedAPIUser)); - - $this->permissionResolver - ->expects(self::once()) - ->method('setCurrentUserReference') - ->with(new UserReference($apiUser->getUserId())); - - self::assertSame($user, $this->userProvider->refreshUser($user)); + return 'foobar'; } - public function testRefreshUserNotFound() + protected function getUserServiceMethod(): string { - $this->expectException(UsernameNotFoundException::class); - - $userId = 123; - $apiUser = new User( - [ - 'content' => new Content( - [ - 'versionInfo' => new VersionInfo( - ['contentInfo' => new ContentInfo(['id' => $userId])] - ), - ] - ), - ] - ); - $user = $this->createMock(UserInterface::class); - $user - ->expects(self::once()) - ->method('getAPIUser') - ->will(self::returnValue($apiUser)); - - $this->userService - ->expects(self::once()) - ->method('loadUser') - ->with($userId) - ->will(self::throwException(new NotFoundException('user', 'foo'))); - - $this->userProvider->refreshUser($user); - } - - /** - * @dataProvider supportsClassProvider - */ - public function testSupportsClass($class, $supports) - { - self::assertSame($supports, $this->userProvider->supportsClass($class)); - } - - public function supportsClassProvider() - { - return [ - [SymfonyUserInterface::class, false], - [MVCUser::class, true], - [get_class($this->createMock(MVCUser::class)), true], - ]; - } - - public function testLoadUserByAPIUser() - { - $apiUser = $this->createMock(APIUser::class); - - $user = $this->userProvider->loadUserByAPIUser($apiUser); - - self::assertInstanceOf(MVCUser::class, $user); - self::assertSame($apiUser, $user->getAPIUser()); - self::assertSame(['ROLE_USER'], $user->getRoles()); + return 'loadUserByLogin'; } }