/vendor/propel/propel-bundle/Propel/PropelBundle/Tests/Security/Acl/Domain/AclTest.php

https://gitlab.com/Isaki/le331.fr · PHP · 206 lines · 148 code · 41 blank · 17 comment · 0 complexity · 4abe91e4476144d45f0b154f63d2f299 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the PropelBundle package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. namespace Propel\PropelBundle\Tests\Security\Acl\Domain;
  10. use Propel\PropelBundle\Model\Acl\Entry;
  11. use Propel\PropelBundle\Model\Acl\SecurityIdentity;
  12. use Propel\PropelBundle\Security\Acl\Domain\Acl;
  13. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  14. use Propel\PropelBundle\Tests\AclTestCase;
  15. /**
  16. * @author Toni Uebernickel <tuebernickel@gmail.com>
  17. */
  18. class AclTest extends AclTestCase
  19. {
  20. public function testConstructorInvalidCollection()
  21. {
  22. $collection = new \PropelObjectCollection();
  23. $collection->setModel('Propel\PropelBundle\Model\Acl\AclClass');
  24. $this->setExpectedException('Symfony\Component\Security\Acl\Exception\Exception');
  25. new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  26. }
  27. public function testConstructorEmptyCollection()
  28. {
  29. $collection = new \PropelObjectCollection();
  30. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  31. $aclObj = $this->getAclObjectIdentity();
  32. $acl = new Acl($collection, $aclObj, new PermissionGrantingStrategy());
  33. $this->assertEmpty($acl->getClassAces());
  34. $this->assertEmpty($acl->getObjectAces());
  35. $this->assertEmpty($acl->getFields());
  36. $this->assertNull($acl->getParentAcl());
  37. $this->assertSame($aclObj, $acl->getObjectIdentity());
  38. $this->assertTrue($acl->isEntriesInheriting());
  39. }
  40. /**
  41. * @depends testConstructorEmptyCollection
  42. */
  43. public function testConstructorWithAces()
  44. {
  45. $collection = new \PropelObjectCollection();
  46. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  47. $obj = $this->createModelObjectIdentity(1);
  48. // object based ACE
  49. $entry = $this->createEntry();
  50. $entry
  51. ->setObjectIdentity($obj)
  52. ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity()))
  53. ->setAclClass($this->getAclClass())
  54. ;
  55. $collection->append($entry);
  56. // object field based ACE
  57. $entry = $this->createEntry();
  58. $entry
  59. ->setObjectIdentity($obj)
  60. ->setFieldName('name')
  61. ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity()))
  62. ->setAclClass($this->getAclClass())
  63. ;
  64. $collection->append($entry);
  65. // class based ACE
  66. $entry = $this->createEntry();
  67. $entry
  68. ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity()))
  69. ->setAclClass($this->getAclClass())
  70. ;
  71. $collection->append($entry);
  72. // class field based ACE
  73. $entry = $this->createEntry();
  74. $entry
  75. ->setFieldName('name')
  76. ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity()))
  77. ->setAclClass($this->getAclClass())
  78. ;
  79. $collection->append($entry);
  80. $acl = new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  81. $this->assertNotEmpty($acl->getClassAces());
  82. $this->assertNotEmpty($acl->getObjectAces());
  83. $this->assertEquals(array('name'), $acl->getFields());
  84. $this->assertNotEmpty($acl->getClassFieldAces('name'));
  85. $this->assertNotEmpty($acl->getObjectFieldAces('name'));
  86. $classAces = $acl->getClassAces();
  87. $objectAces = $acl->getObjectAces();
  88. $classFieldAces = $acl->getClassFieldAces('name');
  89. $objectFieldAces = $acl->getObjectFieldAces('name');
  90. $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\Entry', $classAces[0]);
  91. $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\Entry', $objectAces[0]);
  92. $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\FieldEntry', $classFieldAces[0]);
  93. $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\FieldEntry', $objectFieldAces[0]);
  94. $this->assertSame($acl, $classAces[0]->getAcl());
  95. $this->assertSame($acl, $objectAces[0]->getAcl());
  96. $this->assertSame($acl, $classFieldAces[0]->getAcl());
  97. $this->assertSame($acl, $objectFieldAces[0]->getAcl());
  98. $this->assertEquals('name', $classFieldAces[0]->getField());
  99. $this->assertEquals('name', $objectFieldAces[0]->getField());
  100. }
  101. public function testIsSidLoadedNoneLoaded()
  102. {
  103. $collection = new \PropelObjectCollection();
  104. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  105. $acl = new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  106. $this->assertFalse($acl->isSidLoaded($this->getRoleSecurityIdentity()));
  107. }
  108. public function testIsSidLoadedInvalid()
  109. {
  110. $collection = new \PropelObjectCollection();
  111. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  112. $aclObj = $this->getAclObjectIdentity();
  113. $acl = new Acl($collection, $aclObj, new PermissionGrantingStrategy());
  114. $this->setExpectedException('InvalidArgumentException');
  115. $acl->isSidLoaded('foo');
  116. }
  117. public function testIsGrantedNoAces()
  118. {
  119. $collection = new \PropelObjectCollection();
  120. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  121. $acl = new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  122. $this->setExpectedException('Symfony\Component\Security\Acl\Exception\NoAceFoundException');
  123. $acl->isGranted(array(64), array($this->getRoleSecurityIdentity()));
  124. }
  125. public function testIsGrantedNoMatchingSecurityIdentity()
  126. {
  127. $collection = new \PropelObjectCollection();
  128. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  129. $entry = $this->createEntry();
  130. $entry
  131. ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity('ROLE_ADMIN')))
  132. ->setAclClass($this->getAclClass())
  133. ;
  134. $collection->append($entry);
  135. $acl = new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  136. $this->setExpectedException('Symfony\Component\Security\Acl\Exception\NoAceFoundException');
  137. $acl->isGranted(array(64), array($this->getRoleSecurityIdentity('ROLE_USER')));
  138. }
  139. public function testIsFieldGrantedNoAces()
  140. {
  141. $collection = new \PropelObjectCollection();
  142. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  143. $acl = new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  144. $this->setExpectedException('Symfony\Component\Security\Acl\Exception\NoAceFoundException');
  145. $acl->isFieldGranted('name', array(64), array($this->getRoleSecurityIdentity()));
  146. }
  147. public function testSerializeUnserialize()
  148. {
  149. $collection = new \PropelObjectCollection();
  150. $collection->setModel('Propel\PropelBundle\Model\Acl\Entry');
  151. $entry = $this->createEntry();
  152. $entry
  153. ->setSecurityIdentity(SecurityIdentity::fromAclIdentity($this->getRoleSecurityIdentity('ROLE_ADMIN')))
  154. ->setAclClass($this->getAclClass())
  155. ;
  156. $collection->append($entry);
  157. $acl = new Acl($collection, $this->getAclObjectIdentity(), new PermissionGrantingStrategy());
  158. $serialized = serialize($acl);
  159. $unserialized = unserialize($serialized);
  160. $this->assertNotEmpty($serialized);
  161. $this->assertNotEmpty($unserialized);
  162. $this->assertInstanceOf('Propel\PropelBundle\Security\Acl\Domain\Acl', $unserialized);
  163. $this->assertEquals($serialized, serialize($unserialized));
  164. }
  165. }