PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Symfony/Tests/Component/Security/Acl/Dbal/AclProviderTest.php

http://github.com/fabpot/symfony
PHP | 263 lines | 207 code | 39 blank | 17 comment | 1 complexity | e20364267110d6fd314a10fca3e47589 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Security\Acl\Dbal;
  11. use Symfony\Component\Security\Acl\Dbal\AclProvider;
  12. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  13. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  14. use Symfony\Component\Security\Acl\Dbal\Schema;
  15. use Doctrine\DBAL\DriverManager;
  16. class AclProviderTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected $con;
  19. protected $insertClassStmt;
  20. protected $insertEntryStmt;
  21. protected $insertOidStmt;
  22. protected $insertOidAncestorStmt;
  23. protected $insertSidStmt;
  24. /**
  25. * @expectedException Symfony\Component\Security\Acl\Exception\AclNotFoundException
  26. * @expectedMessage There is no ACL for the given object identity.
  27. */
  28. public function testFindAclThrowsExceptionWhenNoAclExists()
  29. {
  30. $this->getProvider()->findAcl(new ObjectIdentity('foo', 'foo'));
  31. }
  32. public function testFindAclsThrowsExceptionUnlessAnACLIsFoundForEveryOID()
  33. {
  34. $oids = array();
  35. $oids[] = new ObjectIdentity('1', 'foo');
  36. $oids[] = new ObjectIdentity('foo', 'foo');
  37. try {
  38. $this->getProvider()->findAcls($oids);
  39. $this->fail('Provider did not throw an expected exception.');
  40. } catch (\Exception $ex) {
  41. $this->assertInstanceOf('Symfony\Component\Security\Acl\Exception\AclNotFoundException', $ex);
  42. $this->assertInstanceOf('Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException', $ex);
  43. $partialResult = $ex->getPartialResult();
  44. $this->assertTrue($partialResult->contains($oids[0]));
  45. $this->assertFalse($partialResult->contains($oids[1]));
  46. }
  47. }
  48. public function testFindAcls()
  49. {
  50. $oids = array();
  51. $oids[] = new ObjectIdentity('1', 'foo');
  52. $oids[] = new ObjectIdentity('2', 'foo');
  53. $provider = $this->getProvider();
  54. $acls = $provider->findAcls($oids);
  55. $this->assertInstanceOf('SplObjectStorage', $acls);
  56. $this->assertEquals(2, count($acls));
  57. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Acl', $acl0 = $acls->offsetGet($oids[0]));
  58. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Acl', $acl1 = $acls->offsetGet($oids[1]));
  59. $this->assertTrue($oids[0]->equals($acl0->getObjectIdentity()));
  60. $this->assertTrue($oids[1]->equals($acl1->getObjectIdentity()));
  61. }
  62. public function testFindAclCachesAclInMemory()
  63. {
  64. $oid = new ObjectIdentity('1', 'foo');
  65. $provider = $this->getProvider();
  66. $acl = $provider->findAcl($oid);
  67. $this->assertSame($acl, $cAcl = $provider->findAcl($oid));
  68. $cAces = $cAcl->getObjectAces();
  69. foreach ($acl->getObjectAces() as $index => $ace) {
  70. $this->assertSame($ace, $cAces[$index]);
  71. }
  72. }
  73. public function testFindAcl()
  74. {
  75. $oid = new ObjectIdentity('1', 'foo');
  76. $provider = $this->getProvider();
  77. $acl = $provider->findAcl($oid);
  78. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Acl', $acl);
  79. $this->assertTrue($oid->equals($acl->getObjectIdentity()));
  80. $this->assertEquals(4, $acl->getId());
  81. $this->assertEquals(0, count($acl->getClassAces()));
  82. $this->assertEquals(0, count($this->getField($acl, 'classFieldAces')));
  83. $this->assertEquals(3, count($acl->getObjectAces()));
  84. $this->assertEquals(0, count($this->getField($acl, 'objectFieldAces')));
  85. $aces = $acl->getObjectAces();
  86. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Entry', $aces[0]);
  87. $this->assertTrue($aces[0]->isGranting());
  88. $this->assertTrue($aces[0]->isAuditSuccess());
  89. $this->assertTrue($aces[0]->isAuditFailure());
  90. $this->assertEquals('all', $aces[0]->getStrategy());
  91. $this->assertSame(2, $aces[0]->getMask());
  92. // check ACE are in correct order
  93. $i = 0;
  94. foreach ($aces as $index => $ace) {
  95. $this->assertEquals($i, $index);
  96. $i++;
  97. }
  98. $sid = $aces[0]->getSecurityIdentity();
  99. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\UserSecurityIdentity', $sid);
  100. $this->assertEquals('john.doe', $sid->getUsername());
  101. $this->assertEquals('SomeClass', $sid->getClass());
  102. }
  103. protected function setUp()
  104. {
  105. if (!class_exists('Doctrine\DBAL\DriverManager')) {
  106. $this->markTestSkipped('The Doctrine2 DBAL is required for this test');
  107. }
  108. $this->con = DriverManager::getConnection(array(
  109. 'driver' => 'pdo_sqlite',
  110. 'memory' => true,
  111. ));
  112. // import the schema
  113. $schema = new Schema($options = $this->getOptions());
  114. foreach ($schema->toSql($this->con->getDatabasePlatform()) as $sql) {
  115. $this->con->exec($sql);
  116. }
  117. // populate the schema with some test data
  118. $this->insertClassStmt = $this->con->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)');
  119. foreach ($this->getClassData() as $data) {
  120. $this->insertClassStmt->execute($data);
  121. }
  122. $this->insertSidStmt = $this->con->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)');
  123. foreach ($this->getSidData() as $data) {
  124. $this->insertSidStmt->execute($data);
  125. }
  126. $this->insertOidStmt = $this->con->prepare('INSERT INTO acl_object_identities (id, class_id, object_identifier, parent_object_identity_id, entries_inheriting) VALUES (?, ?, ?, ?, ?)');
  127. foreach ($this->getOidData() as $data) {
  128. $this->insertOidStmt->execute($data);
  129. }
  130. $this->insertEntryStmt = $this->con->prepare('INSERT INTO acl_entries (id, class_id, object_identity_id, field_name, ace_order, security_identity_id, mask, granting, granting_strategy, audit_success, audit_failure) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
  131. foreach ($this->getEntryData() as $data) {
  132. $this->insertEntryStmt->execute($data);
  133. }
  134. $this->insertOidAncestorStmt = $this->con->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)');
  135. foreach ($this->getOidAncestorData() as $data) {
  136. $this->insertOidAncestorStmt->execute($data);
  137. }
  138. }
  139. protected function tearDown()
  140. {
  141. $this->con = null;
  142. }
  143. protected function getField($object, $field)
  144. {
  145. $reflection = new \ReflectionProperty($object, $field);
  146. $reflection->setAccessible(true);
  147. return $reflection->getValue($object);
  148. }
  149. protected function getEntryData()
  150. {
  151. // id, cid, oid, field, order, sid, mask, granting, strategy, a success, a failure
  152. return array(
  153. array(1, 1, 1, null, 0, 1, 1, 1, 'all', 1, 1),
  154. array(2, 1, 1, null, 1, 2, 1 << 2 | 1 << 1, 0, 'any', 0, 0),
  155. array(3, 3, 4, null, 0, 1, 2, 1, 'all', 1, 1),
  156. array(4, 3, 4, null, 2, 2, 1, 1, 'all', 1, 1),
  157. array(5, 3, 4, null, 1, 3, 1, 1, 'all', 1, 1),
  158. );
  159. }
  160. protected function getOidData()
  161. {
  162. // id, cid, oid, parent_oid, entries_inheriting
  163. return array(
  164. array(1, 1, '123', null, 1),
  165. array(2, 2, '123', 1, 1),
  166. array(3, 2, 'i:3:123', 1, 1),
  167. array(4, 3, '1', 2, 1),
  168. array(5, 3, '2', 2, 1),
  169. );
  170. }
  171. protected function getOidAncestorData()
  172. {
  173. return array(
  174. array(1, 1),
  175. array(2, 1),
  176. array(2, 2),
  177. array(3, 1),
  178. array(3, 3),
  179. array(4, 2),
  180. array(4, 1),
  181. array(4, 4),
  182. array(5, 2),
  183. array(5, 1),
  184. array(5, 5),
  185. );
  186. }
  187. protected function getSidData()
  188. {
  189. return array(
  190. array(1, 'SomeClass-john.doe', 1),
  191. array(2, 'MyClass-john.doe@foo.com', 1),
  192. array(3, 'FooClass-123', 1),
  193. array(4, 'MooClass-ROLE_USER', 1),
  194. array(5, 'ROLE_USER', 0),
  195. array(6, 'IS_AUTHENTICATED_FULLY', 0),
  196. );
  197. }
  198. protected function getClassData()
  199. {
  200. return array(
  201. array(1, 'Bundle\SomeVendor\MyBundle\Entity\SomeEntity'),
  202. array(2, 'Bundle\MyBundle\Entity\AnotherEntity'),
  203. array(3, 'foo'),
  204. );
  205. }
  206. protected function getOptions()
  207. {
  208. return array(
  209. 'oid_table_name' => 'acl_object_identities',
  210. 'oid_ancestors_table_name' => 'acl_object_identity_ancestors',
  211. 'class_table_name' => 'acl_classes',
  212. 'sid_table_name' => 'acl_security_identities',
  213. 'entry_table_name' => 'acl_entries',
  214. );
  215. }
  216. protected function getStrategy()
  217. {
  218. return new PermissionGrantingStrategy();
  219. }
  220. protected function getProvider()
  221. {
  222. return new AclProvider($this->con, $this->getStrategy(), $this->getOptions());
  223. }
  224. }