public function testIsPredefined()
 {
     $name = 'Predefined role';
     $role = new AccountUserRole($name);
     $this->assertTrue($role->isPredefined());
     $role->setAccount(new Account());
     $this->assertFalse($role->isPredefined());
 }
 /**
  * @param ObjectManager $manager
  * @param string $roleLabel
  * @param string $account
  */
 protected function loadRoleWithAccount(ObjectManager $manager, $roleLabel, $account)
 {
     $entity = new AccountUserRole();
     $entity->setLabel($roleLabel);
     /** @var Account $account */
     $account = $this->getReference($account);
     $entity->setAccount($account);
     $entity->setOrganization($account->getOrganization());
     $this->setReference($entity->getLabel(), $entity);
     $manager->persist($entity);
 }
 /**
  * @param AccountUser|null $accountUser
  * @param bool             $isGranted
  * @param int              $accountId
  * @param int              $loggedUserAccountId
  * @param int              $expected
  * @param bool             $failAccountUserRole
  * @dataProvider attributeFrontendUpdateViewDataProvider
  */
 public function testVoteFrontendView($accountUser, $isGranted, $accountId, $loggedUserAccountId, $expected, $failAccountUserRole = false)
 {
     /** @var Account $roleAccount */
     $roleAccount = $this->createEntity('OroB2B\\Bundle\\AccountBundle\\Entity\\Account', $accountId);
     /** @var Account $userAccount */
     $userAccount = $this->createEntity('OroB2B\\Bundle\\AccountBundle\\Entity\\Account', $loggedUserAccountId);
     if ($failAccountUserRole) {
         $accountUserRole = new \stdClass();
     } else {
         $accountUserRole = new AccountUserRole();
         $accountUserRole->setAccount($roleAccount);
     }
     if ($accountUser) {
         $accountUser->setAccount($userAccount);
     }
     $this->getMocksForVote($accountUserRole);
     if (!$failAccountUserRole) {
         $this->getMockForUpdateAndView($accountUser, $isGranted, 'VIEW');
     }
     /** @var \PHPUnit_Framework_MockObject_MockObject|TokenInterface $token */
     $token = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface');
     $this->assertEquals($expected, $this->voter->vote($token, $accountUserRole, [AccountUserRoleVoter::ATTRIBUTE_FRONTEND_ACCOUNT_ROLE_VIEW]));
 }
 /**
  * @param AccountUserRole $role
  * @param Account|null    $newAccount
  * @param AccountUser[]   $appendUsers
  * @param AccountUser[]   $removedUsers
  * @param AccountUser[]   $assignedUsers
  * @param AccountUser[]   $expectedUsersWithRole
  * @param AccountUser[]   $expectedUsersWithoutRole
  * @param bool            $changeAccountProcessed
  * @dataProvider processWithAccountProvider
  */
 public function testProcessWithAccount(AccountUserRole $role, $newAccount, array $appendUsers, array $removedUsers, array $assignedUsers, array $expectedUsersWithRole, array $expectedUsersWithoutRole, $changeAccountProcessed = true)
 {
     // Array of persisted users
     /** @var AccountUser[] $persistedUsers */
     $persistedUsers = [];
     $request = new Request();
     $request->setMethod('POST');
     $appendForm = $this->getMock('Symfony\\Component\\Form\\FormInterface');
     $appendForm->expects($this->once())->method('getData')->willReturn($appendUsers);
     $removeForm = $this->getMock('Symfony\\Component\\Form\\FormInterface');
     $removeForm->expects($this->once())->method('getData')->willReturn($removedUsers);
     $form = $this->getMock('Symfony\\Component\\Form\\FormInterface');
     $form->expects($this->once())->method('submit')->with($request)->willReturnCallback(function () use($role, $newAccount) {
         $role->setAccount($newAccount);
         $role->setOrganization($newAccount->getOrganization());
     });
     $form->expects($this->once())->method('isValid')->willReturn(true);
     $form->expects($this->any())->method('get')->willReturnMap([['appendUsers', $appendForm], ['removeUsers', $removeForm]]);
     $this->formFactory->expects($this->once())->method('create')->willReturn($form);
     $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager');
     $objectManager->expects($this->any())->method('persist')->willReturnCallback(function ($entity) use(&$persistedUsers) {
         if ($entity instanceof AccountUser) {
             $persistedUsers[spl_object_hash($entity)] = $entity;
         }
     });
     $this->managerRegistry->expects($this->any())->method('getManagerForClass')->with(get_class($role))->willReturn($objectManager);
     $this->roleRepository->expects($changeAccountProcessed ? $this->once() : $this->never())->method('getAssignedUsers')->with($role)->willReturn($assignedUsers);
     /** @var \PHPUnit_Framework_MockObject_MockObject|AccountUserRoleUpdateHandler $handler */
     $handler = $this->getMockBuilder('\\OroB2B\\Bundle\\AccountBundle\\Form\\Handler\\AccountUserRoleUpdateHandler')->setMethods(['processPrivileges'])->setConstructorArgs([$this->formFactory, $this->privilegeConfig])->getMock();
     $this->setRequirementsForHandler($handler);
     $handler->setRequest($request);
     $handler->createForm($role);
     $handler->process($role);
     foreach ($expectedUsersWithRole as $expectedUser) {
         $this->assertEquals($persistedUsers[spl_object_hash($expectedUser)]->getRole($role->getRole()), $role);
     }
     foreach ($expectedUsersWithoutRole as $expectedUser) {
         $this->assertEquals($persistedUsers[spl_object_hash($expectedUser)]->getRole($role->getRole()), null);
     }
 }