From c5eb7d8b9377a2f4af35b94650746c79c40ba035 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Tue, 25 Jun 2024 16:04:58 +0200 Subject: [PATCH] provided OnAuthenticationTokenCreatedRepositoryUserSubscriber unit test --- ...kenCreatedRepositoryUserSubscriberTest.php | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/lib/MVC/Symfony/Security/Authentication/EventSubscriber/OnAuthenticationTokenCreatedRepositoryUserSubscriberTest.php diff --git a/tests/lib/MVC/Symfony/Security/Authentication/EventSubscriber/OnAuthenticationTokenCreatedRepositoryUserSubscriberTest.php b/tests/lib/MVC/Symfony/Security/Authentication/EventSubscriber/OnAuthenticationTokenCreatedRepositoryUserSubscriberTest.php new file mode 100644 index 0000000000..29c3d68685 --- /dev/null +++ b/tests/lib/MVC/Symfony/Security/Authentication/EventSubscriber/OnAuthenticationTokenCreatedRepositoryUserSubscriberTest.php @@ -0,0 +1,85 @@ +createMock(PermissionResolver::class) + ); + + self::assertEquals( + [ + AuthenticationTokenCreatedEvent::class => ['onAuthenticationTokenCreated', 10], + ], + $subscriber->getSubscribedEvents() + ); + } + + /** + * @dataProvider dataProviderForTestSettingCurrentUserReference + */ + public function testSettingCurrentUserReference( + UserInterface $user, + bool $isPermissionResolverInvoked + ): void { + $permissionResolver = $this->createMock(PermissionResolver::class); + $permissionResolver + ->expects($isPermissionResolverInvoked === true ? self::once() : self::never()) + ->method('setCurrentUserReference'); + + $subscriber = new OnAuthenticationTokenCreatedRepositoryUserSubscriber($permissionResolver); + + $subscriber->onAuthenticationTokenCreated( + $this->getAuthenticationTokenCreatedEvent($user) + ); + } + + /** + * @return iterable + */ + public function dataProviderForTestSettingCurrentUserReference(): iterable + { + yield 'authorizing Ibexa user' => [ + new User($this->createMock(ApiUser::class)), + true, + ]; + + yield 'authorizing non-Ibexa user' => [ + new InMemoryUser('foo', 'bar'), + false, + ]; + } + + private function getAuthenticationTokenCreatedEvent(UserInterface $user): AuthenticationTokenCreatedEvent + { + return new AuthenticationTokenCreatedEvent( + new UsernamePasswordToken($user, 'test_firewall'), + new Passport( + new UserBadge('foo'), + new PasswordCredentials('bar') + ) + ); + } +}