AclSecurityHandlerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Admin\Security\Acl\Permission;
  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 testAcl()
  17. {
  18. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  19. $admin->expects($this->any())
  20. ->method('getCode')
  21. ->will($this->returnValue('test'));
  22. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  23. $securityContext->expects($this->any())
  24. ->method('isGranted')
  25. ->will($this->returnValue(true));
  26. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  27. $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  28. $this->assertTrue($handler->isGranted($admin, array('TOTO')));
  29. $this->assertTrue($handler->isGranted($admin, 'TOTO'));
  30. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  31. $securityContext->expects($this->any())
  32. ->method('isGranted')
  33. ->will($this->returnValue(false));
  34. $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  35. $this->assertFalse($handler->isGranted($admin, array('TOTO')));
  36. $this->assertFalse($handler->isGranted($admin, 'TOTO'));
  37. }
  38. public function testBuildInformation()
  39. {
  40. $informations = array(
  41. 'EDIT' => array('EDIT')
  42. );
  43. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  44. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  45. $admin->expects($this->once())
  46. ->method('getCode')
  47. ->will($this->returnValue('test'));
  48. $admin->expects($this->once())
  49. ->method('getSecurityInformation')
  50. ->will($this->returnValue($informations));
  51. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  52. $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  53. $results = $handler->buildSecurityInformation($admin);
  54. $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
  55. }
  56. public function testWithAuthenticationCredentialsNotFoundException()
  57. {
  58. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  59. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  60. $securityContext->expects($this->any())
  61. ->method('isGranted')
  62. ->will($this->throwException(new AuthenticationCredentialsNotFoundException('FAIL')));
  63. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  64. $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  65. $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
  66. }
  67. /**
  68. * @expectedException RuntimeException
  69. */
  70. public function testWithNonAuthenticationCredentialsNotFoundException()
  71. {
  72. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  73. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  74. $securityContext->expects($this->any())
  75. ->method('isGranted')
  76. ->will($this->throwException(new \RunTimeException('FAIL')));
  77. $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
  78. $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
  79. $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
  80. }
  81. }