/** * Test new user creation */ public function testOnFlushCreateUser() { $args = new OnFlushEventArgs($this->em); $user = new User(); $org1 = new Organization(); ReflectionUtil::setId($org1, 1); $org2 = new Organization(); ReflectionUtil::setId($org2, 2); $user->setOrganization($org1); $user->addOrganization($org1); $user->addOrganization($org2); $newCalendar1 = new Calendar(); $newCalendar1->setOwner($user)->setOrganization($org1); $newCalendar2 = new Calendar(); $newCalendar2->setOwner($user)->setOrganization($org2); $calendarMetadata = new ClassMetadata(get_class($newCalendar1)); $this->uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue([$user])); $this->uow->expects($this->once())->method('getScheduledCollectionUpdates')->will($this->returnValue([])); $this->em->expects($this->at(1))->method('persist')->with($this->equalTo($newCalendar1)); $this->em->expects($this->at(2))->method('getClassMetadata')->with('Oro\\Bundle\\CalendarBundle\\Entity\\Calendar')->will($this->returnValue($calendarMetadata)); $this->em->expects($this->at(3))->method('persist')->with($this->equalTo($newCalendar2)); $this->uow->expects($this->at(1))->method('computeChangeSet')->with($calendarMetadata, $newCalendar1); $this->uow->expects($this->at(2))->method('computeChangeSet')->with($calendarMetadata, $newCalendar2); $this->listener->onFlush($args); }
/** * Test existing user modification */ public function testOnFlushUpdateUser() { $args = new OnFlushEventArgs($this->em); $user = new User(); $org = new Organization(); $org->setId(1); $org->setName('test'); $user->addOrganization($org); $newCalendar = new Calendar(); $newCalendar->setOwner($user); $newCalendar->setOrganization($org); $newConnection = new CalendarConnection($newCalendar); $newCalendar->addConnection($newConnection); $calendarMetadata = new ClassMetadata(get_class($newCalendar)); $connectionMetadata = new ClassMetadata(get_class($newConnection)); $this->em->expects($this->any())->method('getClassMetadata')->will($this->returnValueMap([['Oro\\Bundle\\CalendarBundle\\Entity\\Calendar', $calendarMetadata], ['Oro\\Bundle\\CalendarBundle\\Entity\\CalendarConnection', $connectionMetadata]])); $calendarRepo = $this->getMockBuilder('\\Doctrine\\ORM\\EntityRepository')->disableOriginalConstructor()->getMock(); $calendarRepo->expects($this->any())->method('findDefaultCalendar')->will($this->returnValue(false)); $this->em->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($this->uow)); $this->uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue([])); $this->uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue([$user])); $this->em->expects($this->any())->method('getRepository')->with('OroCalendarBundle:Calendar')->will($this->returnValue($calendarRepo)); $this->em->expects($this->at(2))->method('persist')->with($this->equalTo($newCalendar)); $this->em->expects($this->at(3))->method('persist')->with($this->equalTo($newConnection)); $this->uow->expects($this->at(2))->method('computeChangeSet')->with($calendarMetadata, $newCalendar); $this->uow->expects($this->at(3))->method('computeChangeSet')->with($connectionMetadata, $newConnection); $this->listener->onFlush($args); }
public function testTokenShouldBeAuthenticated() { $token = new OAuthToken('token'); $token->setResourceOwnerName('google'); $organization = new Organization(); $organization->setEnabled(true); $token->setOrganizationContext($organization); $userResponse = $this->getMock('HWI\\Bundle\\OAuthBundle\\OAuth\\Response\\UserResponseInterface'); $resourceOwner = $this->getMock('HWI\\Bundle\\OAuthBundle\\OAuth\\ResourceOwnerInterface'); $resourceOwner->expects($this->any())->method('getName')->will($this->returnValue('google')); $resourceOwner->expects($this->any())->method('getUserInformation')->will($this->returnValue($userResponse)); $this->resourceOwnerMap->expects($this->any())->method('getResourceOwnerByName')->will($this->returnValue($resourceOwner)); $user = new User(); $user->addOrganization($organization); $this->userProvider->expects($this->any())->method('loadUserByOAuthUserResponse')->with($userResponse)->will($this->returnValue($user)); $resultToken = $this->oauthProvider->authenticate($token); $this->assertInstanceOf('Oro\\Bundle\\SSOBundle\\Security\\OAuthToken', $resultToken); $this->assertSame($user, $resultToken->getUser()); $this->assertEquals('google', $resultToken->getResourceOwnerName()); $this->assertTrue($resultToken->isAuthenticated()); }
public function testSetUsernameAndOrganizationName() { $this->assertEmpty($this->securityContext->getToken()); $username = '******'; $user = new User(); $user->setUsername($username); $organizationName = 'test_organization'; $organization = new Organization(); $organization->setName($organizationName); $organization->setEnabled(true); $user->addOrganization($organization); $event = $this->getEvent(); /** @var \PHPUnit_Framework_MockObject_MockObject $input */ $input = $event->getInput(); $input->expects($this->at(0))->method('getParameterOption')->with('--' . ConsoleContextListener::OPTION_USER)->will($this->returnValue($username)); $input->expects($this->at(1))->method('getParameterOption')->with('--' . ConsoleContextListener::OPTION_ORGANIZATION)->will($this->returnValue($organizationName)); $this->userManager->expects($this->once())->method('findUserByUsernameOrEmail')->with($username)->will($this->returnValue($user)); $this->organizationRepository->expects($this->once())->method('findOneBy')->with(['name' => $organizationName])->will($this->returnValue($organization)); $this->listener->onConsoleCommand($event); /** @var ConsoleToken $token */ $token = $this->securityContext->getToken(); $this->assertNotEmpty($token); $this->assertInstanceOf('Oro\\Bundle\\SecurityBundle\\Authentication\\Token\\ConsoleToken', $token); $this->assertEquals($user, $token->getUser()); $this->assertEquals($organization, $token->getOrganizationContext()); }
public function testOrganizations() { $user = new User(); $disabledOrganization = new Organization(); $organization = new Organization(); $organization->setEnabled(true); $user->setOrganizations(new ArrayCollection(array($organization))); $this->assertContains($organization, $user->getOrganizations()); $user->removeOrganization($organization); $this->assertNotContains($organization, $user->getOrganizations()); $user->addOrganization($organization); $this->assertContains($organization, $user->getOrganizations()); $user->addOrganization($disabledOrganization); $result = $user->getOrganizations(true); $this->assertTrue($result->count() == 1); $this->assertSame($result->first(), $organization); }
/** * @param User $user * @param array $options * @throws InvalidArgumentException * @return $this */ protected function setOrganizations(User $user, $options) { if (!empty($options['user-organizations'])) { foreach ($options['user-organizations'] as $organizationName) { try { $organization = $this->getEntityManager()->getRepository('OroOrganizationBundle:Organization')->getOrganizationByName($organizationName); } catch (NoResultException $e) { throw new InvalidArgumentException('Invalid organization "' . $organizationName . '" in "--user-organizations" parameter'); } $user->addOrganization($organization); } } return $this; }