AclSecurityHandlerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Security\Handler;
  11. use Sonata\AdminBundle\Security\Handler\AclSecurityHandler;
  12. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  13. class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function getTokenStorageMock()
  16. {
  17. // Set the SecurityContext for Symfony <2.6
  18. // TODO: Remove conditional return when bumping requirements to SF 2.6+
  19. if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
  20. return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
  21. $this->authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
  22. }
  23. return $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  24. }
  25. public function getAuthorizationCheckerMock()
  26. {
  27. // Set the SecurityContext for Symfony <2.6
  28. // TODO: Remove conditional return when bumping requirements to SF 2.6+
  29. if (interface_exists('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')) {
  30. return $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
  31. }
  32. return $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  33. }
  34. public function testAcl()
  35. {
  36. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  37. $admin->expects($this->any())
  38. ->method('getCode')
  39. ->will($this->returnValue('test'));
  40. $authorizationChecker = $this->getAuthorizationCheckerMock();
  41. $authorizationChecker->expects($this->any())
  42. ->method('isGranted')
  43. ->will($this->returnValue(true));
  44. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  45. $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  46. $this->assertTrue($handler->isGranted($admin, array('TOTO')));
  47. $this->assertTrue($handler->isGranted($admin, 'TOTO'));
  48. $authorizationChecker = $this->getAuthorizationCheckerMock();
  49. $authorizationChecker->expects($this->any())
  50. ->method('isGranted')
  51. ->will($this->returnValue(false));
  52. $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  53. $this->assertFalse($handler->isGranted($admin, array('TOTO')));
  54. $this->assertFalse($handler->isGranted($admin, 'TOTO'));
  55. }
  56. public function testBuildInformation()
  57. {
  58. $informations = array(
  59. 'EDIT' => array('EDIT'),
  60. );
  61. $authorizationChecker = $this->getAuthorizationCheckerMock();
  62. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  63. $admin->expects($this->once())
  64. ->method('getCode')
  65. ->will($this->returnValue('test'));
  66. $admin->expects($this->once())
  67. ->method('getSecurityInformation')
  68. ->will($this->returnValue($informations));
  69. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  70. $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  71. $results = $handler->buildSecurityInformation($admin);
  72. $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
  73. }
  74. public function testWithAuthenticationCredentialsNotFoundException()
  75. {
  76. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  77. $authorizationChecker = $this->getAuthorizationCheckerMock();
  78. $authorizationChecker->expects($this->any())
  79. ->method('isGranted')
  80. ->will($this->throwException(new AuthenticationCredentialsNotFoundException('FAIL')));
  81. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  82. $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  83. $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
  84. }
  85. /**
  86. * @expectedException \RuntimeException
  87. */
  88. public function testWithNonAuthenticationCredentialsNotFoundException()
  89. {
  90. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  91. $authorizationChecker = $this->getAuthorizationCheckerMock();
  92. $authorizationChecker->expects($this->any())
  93. ->method('isGranted')
  94. ->will($this->throwException(new \RuntimeException('FAIL')));
  95. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  96. $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  97. $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
  98. }
  99. }