DoctrineAclCacheTest.php 3.1 KB

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