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));
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $formBuilder, array $options)
 {
     /*
      * The form shows only roles that the current user can edit for the targeted user. Now we still need to persist
      * all other roles. It is not possible to alter those values inside an event listener as the selected
      * key will be validated. So we use a Transformer to alter the value and an listener to catch the original values
      *
      * The transformer will then append non editable roles to the user ...
      */
     $transformer = new RestoreRolesTransformer($this->rolesBuilder);
     // GET METHOD
     $formBuilder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($transformer) {
         $transformer->setOriginalRoles($event->getData());
     });
     // POST METHOD
     $formBuilder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($transformer) {
         $transformer->setOriginalRoles($event->getForm()->getData());
     });
     $formBuilder->addModelTransformer($transformer);
 }
 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);
 }