AclSecurityHandlerTest.php 5.6 KB

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