AclSecurityHandlerTest.php 4.0 KB

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