DoctrineAclCacheTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Acl\Domain;
  3. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  4. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  5. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  6. use Symfony\Component\Security\Acl\Domain\Acl;
  7. use Symfony\Component\Security\Acl\Domain\DoctrineAclCache;
  8. use Doctrine\Common\Cache\ArrayCache;
  9. class DoctrineAclCacheTest extends \PHPUnit_Framework_TestCase
  10. {
  11. protected $permissionGrantingStrategy;
  12. /**
  13. * @expectedException \InvalidArgumentException
  14. * @dataProvider getEmptyValue
  15. */
  16. public function testConstructorDoesNotAcceptEmptyPrefix($empty)
  17. {
  18. new DoctrineAclCache(new ArrayCache(), $this->getPermissionGrantingStrategy(), $empty);
  19. }
  20. public function getEmptyValue()
  21. {
  22. return array(
  23. array(null),
  24. array(false),
  25. array(''),
  26. );
  27. }
  28. public function test()
  29. {
  30. $cache = $this->getCache();
  31. $aclWithParent = $this->getAcl(1);
  32. $acl = $this->getAcl();
  33. $cache->putInCache($aclWithParent);
  34. $cache->putInCache($acl);
  35. $cachedAcl = $cache->getFromCacheByIdentity($acl->getObjectIdentity());
  36. $this->assertEquals($acl->getId(), $cachedAcl->getId());
  37. $this->assertNull($acl->getParentAcl());
  38. $cachedAclWithParent = $cache->getFromCacheByIdentity($aclWithParent->getObjectIdentity());
  39. $this->assertEquals($aclWithParent->getId(), $cachedAclWithParent->getId());
  40. $this->assertNotNull($cachedParentAcl = $cachedAclWithParent->getParentAcl());
  41. $this->assertEquals($aclWithParent->getParentAcl()->getId(), $cachedParentAcl->getId());
  42. }
  43. protected function getAcl($depth = 0)
  44. {
  45. static $id = 1;
  46. $acl = new Acl($id, new ObjectIdentity($id, 'foo'), $this->getPermissionGrantingStrategy(), array(), $depth > 0);
  47. // insert some ACEs
  48. $sid = new UserSecurityIdentity('johannes', 'Foo');
  49. $acl->insertClassAce($sid, 1);
  50. $acl->insertClassFieldAce('foo', $sid, 1);
  51. $acl->insertObjectAce($sid, 1);
  52. $acl->insertObjectFieldAce('foo', $sid, 1);
  53. $id++;
  54. if ($depth > 0) {
  55. $acl->setParentAcl($this->getAcl($depth - 1));
  56. }
  57. return $acl;
  58. }
  59. protected function getPermissionGrantingStrategy()
  60. {
  61. if (null === $this->permissionGrantingStrategy) {
  62. $this->permissionGrantingStrategy = new PermissionGrantingStrategy();
  63. }
  64. return $this->permissionGrantingStrategy;
  65. }
  66. protected function getCache($cacheDriver = null, $prefix = DoctrineAclCache::PREFIX)
  67. {
  68. if (null === $cacheDriver) {
  69. $cacheDriver = new ArrayCache();
  70. }
  71. return new DoctrineAclCache($cacheDriver, $this->getPermissionGrantingStrategy(), $prefix);
  72. }
  73. }