public function testPersistEntityWithRoles() { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('foobar', '123456', '*****@*****.**', 'Ma27', 'en', ['ROLE_USER'])); $persisted = $this->repository->add($user); $this->assertCount(1, $persisted->getRoles()); }
/** * Builds a new analyse entity * * @param integer $dispatchTimes * * @return AuthenticationFailure */ private function buildEntity($dispatchTimes = 2) { $dispatcher = $this->getMock(EventDispatcher::class); $dispatcher->expects($this->exactly($dispatchTimes))->method('dispatch'); DomainEvents::setEventDispatcher($dispatcher); return new AuthenticationFailure(User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**'))); }
/** * @expectedException \Sententiaregum\User\Application\Exception\FollowerException */ public function testLoadRandomUsersBecauseOfEmptyResult() { $this->user->follow(User::fromDTO(new CreateUserDTO('test', 'foo123', '*****@*****.**'))); $userRepository = $this->getMock(UserRepositoryInterface::class); $userRepository->expects($this->once())->method('findFollowingByFollowing')->will($this->returnValue([])); $service = new FollowerService($userRepository); $service->adviceFollowersByUser($this->user); }
public function testLoadUserByUsernameWithMappedRoles() { $userMock = User::fromDTO(new CreateUserDTO('foobar', '123456', '*****@*****.**', '*****@*****.**', 'en', ['ROLE_USER'])); $repository = $this->getMock(UserRepositoryInterface::class); $repository->expects($this->any())->method('findOneByName')->will($this->returnValue($userMock)); $userProvider = new UserProvider($repository); $this->assertSameUser($userMock, $userProvider->loadUserByUsername('foobar')); $this->assertInstanceOf(Role::class, $userProvider->loadUserByUsername('foobar')->getRoles()[0]); }
public function testReturnValidUser() { DomainEvents::setEventDispatcher(new EventDispatcher()); $tokenStorage = $this->getMock(TokenStorageInterface::class); $repository = $this->getMock(UserRepositoryInterface::class); $tokenStorage->expects($this->any())->method('getToken')->will($this->returnValue(new UsernamePasswordToken(new User('foo', 'bar'), 'ROLE_USER', 'unknown'))); $repository->expects($this->any())->method('findOneByName')->will($this->returnValue(SystemUser::fromDTO(new CreateUserDTO('foo', 'bar', '*****@*****.**')))); $fetcher = new UserFetcher($repository, $tokenStorage); $fetcher->resolve(); }
public function testSendNotificationAfterPasswordReset() { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $publisher = $this->getMock(BackendInterface::class); $publisher->expects($this->once())->method('createAndPublish')->with('sen_mailer', ['user' => $user, 'content' => 'notification.password_reset', 'parameters' => ['translation_defaults' => ['%new_password%' => 'new password']], 'override_locale' => true]); $event = new ResetPasswordEvent($user); $event->setNewRawPassword('new password'); $listener = new PasswordResetListener($publisher); $listener->onPasswordReset($event); }
public function testPublishEmail() { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**'), (new UserActivation())->buildKeyCode(new ActivationKeyCodeGenerator())); $publisher = $this->getMock(BackendInterface::class); $publisher->expects($this->once())->method('createAndPublish')->with('sen_mailer', ['user' => $user, 'config_path' => 'SententiaregumApp:registration', 'parameters' => ['code' => $user->getActivation()->getKeyCode(), 'name' => 'Ma27'], 'override_locale' => true]); $event = new CreateUserEvent($user); $event->setActivationKey($user->getActivation()->getKeyCode()); $listener = new PostRegistrationListener($publisher); $listener->postUserRegistration($event); }
public function testDeleteApiKey() { $serviceMock = $this->getMock(ApiKeyFactoryInterface::class); $serviceMock->expects($this->any())->method('generateKeyCode')->will($this->returnValue(new ApiKey('api key'))); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $user->authenticateToken(new AuthDTO('Ma27', 'test-password'), $serviceMock); $userRepository = $this->getMock(UserRepositoryInterface::class); $userRepository->expects($this->once())->method('modify'); $service = new TokenService($userRepository, $serviceMock); $service->purgeToken($user); $this->assertNull($user->getToken()); }
public function testResetPassword() { $userRepository = $this->getMock(UserRepositoryInterface::class); $userRepository->expects($this->once())->method('modify'); $generator = new ActivationKeyCodeGenerator(); $service = new PasswordService($userRepository, $generator); DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $service->resetPassword($user); $this->assertTrue($user->getCredentials()->getPassword()->isHashed()); $this->assertFalse($user->getCredentials()->getPassword()->compare('test-password')); }
public function testIsUserMarked() { $comment = new Comment(User::fromDTO(new CreateUserDTO('Ma27', '123456', '*****@*****.**')), 'foo!'); $user = User::fromDTO(new CreateUserDTO('foo', 'bar', '*****@*****.**')); $reflection = new \ReflectionProperty(User::class, 'identifier'); $reflection->setAccessible(true); $reflection->setValue($user, 1); $analyser = $this->getMock(ContentAnalyserInterface::class); $analyser->expects($this->any())->method('analyse')->will($this->returnValue(new PostMetadata([$user], [], []))); $comment->analyse($analyser); $this->assertTrue($comment->isMarked($user->getId())); }
public function testActivateUser() { DomainEvents::setEventDispatcher(new EventDispatcher()); $generator = new ActivationKeyCodeGenerator(); $activation = new UserActivation(); $activation->buildKeyCode($generator); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**'), $activation); $userRepository = $this->getMock(UserRepositoryInterface::class); $userRepository->expects($this->once())->method('modify'); $service = new UserService($userRepository, $generator); $service->activateUser($user, new ActivateUserDTO($activation->getKeyCode())); }
public function testModifyUser() { /** @var BaseUserController $controller */ $controller = $this->getMockForAbstractClass(BaseUserController::class); $repository = $this->getServiceMockBuilder('sen.user.repository')->getMock(); $repository->expects($this->once())->method('modify'); $container = new Container(); $container->set('sen.user.repository', $repository); $controller->setContainer($container); DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $this->callRestrictedMethod($controller, 'modifyUser', [$user]); }
/** * @dataProvider sortProvider */ public function testSortArrayCollection(array $items, array $expected) { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', '123456', '*****@*****.**')); $collection = new EntryCollection($items); $collection->sort($user); /** @var SortableInterface[] $result */ $result = $collection->toArray(); foreach ($expected as $expected => $key) { $this->assertArrayHasKey($key, $items); $this->assertArrayHasKey($expected, $result); $this->assertSame($items[$key], $result[$expected]); } }
public function testUpdate() { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $fetcher = $this->getMock(UserFetcherInterface::class); $fetcher->expects($this->once())->method('resolve')->will($this->returnValue($user)); $repository = $this->getMock(UserRepositoryInterface::class); $repository->expects($this->once())->method('modify'); $request = Request::create('/'); $time = time(); $request->server->set('REQUEST_TIME', $time); $listener = new LastActionUpdateEventListener($fetcher, $repository); $event = $this->createPostResponseEvent($request); $listener->onRequestTermination($event); }
public function testSendNotification() { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $publisher = $this->getMock(BackendInterface::class); $publisher->expects($this->once())->method('createAndPublish')->with('sen_mailer', ['override_locale' => true, 'user' => $user, 'content' => 'notification.auth.failures', 'parameters' => ['translation_defaults' => ['%times%' => 2]]]); $hook = new AuthenticationReportListener($publisher); $entity = new AuthenticationFailure($user); $entity->updateReport(); $entity->updateReport(); $entityReport = $entity->createReport(); $event = new AuthenticationAttemptEvent($user); $event->setReport($entityReport); $hook->onAuthenticationFailure($event); }
/** * @dataProvider getCredentialSet */ public function testTokenAuthentication($apiKey, $providerKey) { DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('username', 'password', '*****@*****.**')); $token = $this->getMock(TokenInterface::class); $token->expects($this->any())->method('getCredentials')->will($this->returnValue($apiKey)); $mock = $this->getMock(UserRepositoryInterface::class); $mock->expects($this->once())->method('findOneByApiKey')->willReturn($user); $provider = new UserProvider($mock); $authenticator = new ApiKeyAuthenticator(); /** @var PreAuthenticatedToken $token */ $token = $authenticator->authenticateToken($token, $provider, $providerKey); $this->assertInstanceOf(PreAuthenticatedToken::class, $token); $this->assertSame($user->getCredentials()->getUsername(), $token->getUser()->getUsername()); $this->assertSame($user->getCredentials()->getPassword()->getHash(), $token->getUser()->getPassword()); $this->assertSame($apiKey, $token->getCredentials()); $this->assertSame($providerKey, $token->getProviderKey()); }
public function testExtractDataFromPostContent() { DomainEvents::setEventDispatcher(new EventDispatcher()); $userRepository = $this->getMock(UserRepositoryInterface::class); $userRepository->expects($this->at(0))->method('findOneByName')->with('foo')->will($this->returnValue(User::fromDTO(new CreateUserDTO('foo', '123456', '*****@*****.**')))); $userRepository->expects($this->at(1))->method('findOneByName')->with('bar')->will($this->returnValue(null)); $anaylser = new ContentAnalyser($userRepository); $postContent = <<<EOF hello word! this is a link: http://google.de and another: ftp://ftp.uni-bayreuth.de/ the user @foo is valid, but the user @bar not and of course a #hashtag EOF; $metadata = $anaylser->analyse($postContent); $this->assertInstanceOf(PostMetadata::class, $metadata); // users $this->assertCount(1, $metadata->getMarkedUsers()); $first = $metadata->getMarkedUsers()->toArray()[0]; $this->assertInstanceOf(User::class, $first); /** @var $first User */ $this->assertSame('foo', $first->getCredentials()->getUsername()); // tags $tags = $metadata->getHashtags(); $this->assertCount(1, $tags); $firstTag = $tags->toArray()[0]; /** @var $firstTag \Sententiaregum\Dashboard\Value\Hashtag */ $this->assertInstanceOf(Hashtag::class, $firstTag); $this->assertSame('hashtag', $firstTag->getName()); // urls $urls = $metadata->getLinks(); $this->assertCount(2, $urls); /** @var Link $second */ $second = $urls->toArray()[1]; $this->assertInstanceOf(Link::class, $second); $this->assertSame($second->getTarget(), 'ftp://ftp.uni-bayreuth.de/'); }
public function testEqualCookie() { $checker = $this->getAuthorizationChecker(); $user = User::fromDTO(new CreateUserDTO('Ma27', 'foobar', '*****@*****.**')); $fetcher = $this->getResolver($user); $request = Request::create('/'); $request->attributes->set('_route', 'sen_user.request_api_key'); $request->cookies->set('locale', 'en'); $event = new FilterResponseEvent($this->getKernel(), $request, KernelInterface::MASTER_REQUEST, new Response('<h1>hello world!</h1>')); $listener = new ProfileLanguageListener($fetcher, $this->getMock(UserRepositoryInterface::class), $checker); $listener->checkLocaleAfterAuthentication($event); $response = $event->getResponse(); $cookies = $response->headers->getCookies(); $this->assertCount(0, $cookies); }
public function testPasswordReset() { $user = User::fromDTO(new CreateUserDTO('Ma27', '123456', '*****@*****.**')); $this->assertTrue($user->getCredentials()->getPassword()->compare('123456')); $user->resetPassword(new ActivationKeyCodeGenerator()); $this->assertFalse($user->getCredentials()->getPassword()->compare('123456')); }
public function testResolveUserFromService() { /** @var BaseController $controller */ $controller = $this->getMockForAbstractClass(BaseController::class); $userService = $this->getServiceMockBuilder('sen.user.service.user')->getMock(); DomainEvents::setEventDispatcher(new EventDispatcher()); $user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**')); $userService->expects($this->any())->method('getUserById')->will($this->returnValue($user)); $container = new Container(); $container->set('sen.user.service.user', $userService); $controller->setContainer($container); $this->assertSame($user, $this->callRestrictedMethod($controller, 'resolveUser', [1])); }
/** * Creates a new user * * @param CreateUserDTO $newUserData * * @return User */ public function createUser(CreateUserDTO $newUserData) { $activation = new UserActivation(); $activation->buildKeyCode($this->activationKeyCodeGenerator); return $this->userRepository->add(User::fromDTO($newUserData, $activation)); }
/** * @expectedException \LogicException * @expectedExceptionMessage The user "1" has already shared this post! */ public function testPostIsAlreadySharedByUser() { $user = User::fromDTO(new CreateUserDTO('Ma27', '123456', '*****@*****.**')); $reflection = new \ReflectionProperty(User::class, 'identifier'); $reflection->setAccessible(true); $reflection->setValue($user, 1); $post = new Post(User::fromDTO(new CreateUserDTO('foo', 'bar', '*****@*****.**')), 'content'); $post->share($user); $post->share($user); }