public function testReverseTransformAllowEmptyOriginalRoles() { $roleBuilder = $this->getMockBuilder('Sonata\\UserBundle\\Security\\EditableRolesBuilder')->disableOriginalConstructor()->getMock(); $roleBuilder->expects($this->once())->method('getRoles')->will($this->returnValue(array(array(), array()))); $transformer = new RestoreRolesTransformer($roleBuilder); $transformer->setOriginalRoles(null); $data = array('ROLE_FOO'); $this->assertEquals(array('ROLE_FOO'), $transformer->reverseTransform($data)); }
public function testReverseTransformRevokedHierarchicalRole() { $roleBuilder = $this->getMockBuilder('Sonata\\UserBundle\\Security\\EditableRolesBuilder')->disableOriginalConstructor()->getMock(); $availableRoles = array('ROLE_SONATA_ADMIN' => 'ROLE_SONATA_ADMIN', 'ROLE_COMPANY_ADMIN' => 'ROLE_COMPANY_ADMIN: ROLE_COMPANY_PERSONAL_MODERATOR, ROLE_COMPANY_NEWS_MODERATOR, ROLE_COMPANY_BOOKKEEPER', 'ROLE_COMPANY_PERSONAL_MODERATOR' => 'ROLE_COMPANY_PERSONAL_MODERATOR: ROLE_COMPANY_USER', 'ROLE_COMPANY_NEWS_MODERATOR' => 'ROLE_COMPANY_NEWS_MODERATOR: ROLE_COMPANY_USER', 'ROLE_COMPANY_BOOKKEEPER' => 'ROLE_COMPANY_BOOKKEEPER: ROLE_COMPANY_USER', 'ROLE_COMPANY_USER' => 'ROLE_USER', 'ROLE_USER' => 'ROLE_USER'); $roleBuilder->expects($this->once())->method('getRoles')->will($this->returnValue(array($availableRoles, array()))); // user has thease roles $userRoles = array('ROLE_COMPANY_PERSONAL_MODERATOR', 'ROLE_COMPANY_NEWS_MODERATOR', 'ROLE_COMPANY_BOOKKEEPER'); $transformer = new RestoreRolesTransformer($roleBuilder); $transformer->setOriginalRoles($userRoles); // now we want to revoke role ROLE_COMPANY_PERSONAL_MODERATOR $revokedRole = array_shift($userRoles); $processedRoles = $transformer->reverseTransform($userRoles); $this->assertNotContains($revokedRole, $processedRoles); }
public function testReverseTransformHiddenRole() { $roleBuilder = $this->getMockBuilder('Sonata\\UserBundle\\Security\\EditableRolesBuilder')->disableOriginalConstructor()->getMock(); $availableRoles = array('ROLE_SONATA_ADMIN' => 'ROLE_SONATA_ADMIN', 'ROLE_ADMIN' => 'ROLE_ADMIN: ROLE_USER ROLE_COMPANY_ADMIN'); $roleBuilder->expects($this->once())->method('getRoles')->will($this->returnValue(array($availableRoles, array()))); // user roles $userRoles = array('ROLE_USER', 'ROLE_SUPER_ADMIN'); $transformer = new RestoreRolesTransformer($roleBuilder); $transformer->setOriginalRoles($userRoles); // add a new role array_push($userRoles, 'ROLE_SONATA_ADMIN'); // remove existing user role that is not availableRoles unset($userRoles[array_search('ROLE_SUPER_ADMIN', $userRoles)]); $processedRoles = $transformer->reverseTransform($userRoles); $this->assertContains('ROLE_SUPER_ADMIN', $processedRoles); }