/** * @test */ public function setParentRolesMakesSureThatParentRolesDontContainDuplicates() { $role = new Role('Acme.Demo:Test'); $role->initializeObject(); $parentRole1 = new Role('Acme.Demo:Parent1'); $parentRole2 = new Role('Acme.Demo:Parent2'); $parentRole2->addParentRole($parentRole1); $role->setParentRoles(array($parentRole1, $parentRole2, $parentRole2, $parentRole1)); $expectedParentRoles = array('Acme.Demo:Parent1' => $parentRole1, 'Acme.Demo:Parent2' => $parentRole2); // Internally, parentRoles might contain duplicates which Doctrine will try // to persist - even though getParentRoles() will return an array which // does not contain duplicates: $internalParentRolesCollection = ObjectAccess::getProperty($role, 'parentRoles', TRUE); $this->assertEquals(2, count($internalParentRolesCollection->toArray())); $this->assertEquals($expectedParentRoles, $role->getParentRoles()); }
/** * @test */ public function hasRoleWorksWithRecursiveRoles() { $everybodyRole = new Role('Everybody', Role::SOURCE_SYSTEM); $testRole1 = new Role('Acme.Demo:TestRole1'); $testRole2 = new Role('Acme.Demo:TestRole2'); // Set parents $testRole1->setParentRoles(array($testRole2)); $account = new \TYPO3\Flow\Security\Account(); $account->setRoles(array($testRole1)); $mockAuthenticationManager = $this->getMock('TYPO3\\Flow\\Security\\Authentication\\AuthenticationManagerInterface'); $mockAuthenticationManager->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(TRUE)); $mockToken = $this->getMock('TYPO3\\Flow\\Security\\Authentication\\TokenInterface'); $mockToken->expects($this->atLeastOnce())->method('isAuthenticated')->will($this->returnValue(TRUE)); $mockToken->expects($this->atLeastOnce())->method('getAccount')->will($this->returnValue($account)); $mockPolicyService = $this->getAccessibleMock('TYPO3\\Flow\\Security\\Policy\\PolicyService', array('getRole', 'initializeRolesFromPolicy')); $mockPolicyService->expects($this->atLeastOnce())->method('getRole')->will($this->returnCallback(function ($roleIdentifier) use($everybodyRole) { switch ($roleIdentifier) { case 'Everybody': return $everybodyRole; } })); $securityContext = $this->getAccessibleMock('TYPO3\\Flow\\Security\\Context', array('initialize', 'getAccount')); $securityContext->expects($this->any())->method('getAccount')->will($this->returnValue($account)); $securityContext->_set('activeTokens', array($mockToken)); $securityContext->_set('policyService', $mockPolicyService); $securityContext->_set('authenticationManager', $mockAuthenticationManager); $this->assertTrue($securityContext->hasRole('Acme.Demo:TestRole2')); }