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', '*****@*****.**')));
 }
 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 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 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 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 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());
 }
 /**
  * Updates the failure report
  */
 public function updateReport()
 {
     if (null === $this->start) {
         $this->start = new \DateTime();
         $this->attemptCount++;
         $event = new AuthenticationAttemptEvent($this->relatedUser);
         $event->setReport($this->createReport());
         DomainEvents::raise($event);
     } else {
         $intervalTimestamp = $this->start->getTimestamp();
         if (time() - $intervalTimestamp > 86400) {
             $this->start = new \DateTime();
             $this->attemptCount = 1;
         } else {
             $this->attemptCount++;
             if (time() - $intervalTimestamp >= 21600) {
                 $event = new AuthenticationAttemptEvent($this->relatedUser);
                 $event->setReport($this->createReport());
                 DomainEvents::raise($event);
             }
         }
     }
 }
    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/');
    }
 /**
  * Resets the password
  *
  * @param ActivationKeyCodeGeneratorInterface $stringGenerator
  */
 public function resetPassword(ActivationKeyCodeGeneratorInterface $stringGenerator)
 {
     $this->ensureActivated();
     $password = $stringGenerator->generateKey(8);
     $this->credentials = $this->getFactory()->modifyValueObject($this->credentials, ['password' => new Password($password, true)]);
     $event = new ResetPasswordEvent($this);
     $event->setNewRawPassword($password);
     DomainEvents::raise($event);
 }
 protected function setUp()
 {
     DomainEvents::setEventDispatcher(new EventDispatcher());
     $createUserDTO = new CreateUserDTO('Ma27', 'test-password', '*****@*****.**');
     $this->user = User::fromDTO($createUserDTO);
 }
 protected function setUp()
 {
     DomainEvents::setEventDispatcher(new EventDispatcher());
 }
 protected function setUp()
 {
     DomainEvents::setEventDispatcher(new EventDispatcher());
     $this->user = User::fromDTO(new CreateUserDTO('Ma27', 'test-password', '*****@*****.**'));
 }
 /**
  * @expectedException \LogicException
  * @expectedExceptionMessage Invalid event object (Symfony\Component\EventDispatcher\Event) given!
  */
 public function testRaiseWithInvalidClass()
 {
     DomainEvents::setEventDispatcher(new EventDispatcher());
     DomainEvents::raise(new Event());
 }
 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]));
 }
 /**
  * Injects the dispatcher into the domain event classes
  */
 public function injectDispatcherIntoDomainEvents()
 {
     DomainEvents::setEventDispatcher($this->dispatcher);
 }