AclSecurityHandlerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $handler = new AclSecurityHandler($securityContext, array());
  26. $this->assertTrue($handler->isGranted($admin, array('TOTO')));
  27. $this->assertTrue($handler->isGranted($admin, 'TOTO'));
  28. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  29. $securityContext->expects($this->any())
  30. ->method('isGranted')
  31. ->will($this->returnValue(false));
  32. $handler = new AclSecurityHandler($securityContext, array());
  33. $this->assertFalse($handler->isGranted($admin, array('TOTO')));
  34. $this->assertFalse($handler->isGranted($admin, 'TOTO'));
  35. }
  36. public function testBuildInformation()
  37. {
  38. $informations = array(
  39. 'EDIT' => array('EDIT')
  40. );
  41. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  42. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  43. $admin->expects($this->once())
  44. ->method('getCode')
  45. ->will($this->returnValue('test'));
  46. $admin->expects($this->once())
  47. ->method('getSecurityInformation')
  48. ->will($this->returnValue($informations));
  49. $handler = new AclSecurityHandler($securityContext, array());
  50. $results = $handler->buildSecurityInformation($admin);
  51. $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
  52. }
  53. public function testWithAuthenticationCredentialsNotFoundException()
  54. {
  55. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  56. $admin->expects($this->once())
  57. ->method('getCode')
  58. ->will($this->returnValue('test'));
  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. $handler = new AclSecurityHandler($securityContext, 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. $admin->expects($this->once())
  73. ->method('getCode')
  74. ->will($this->returnValue('test'));
  75. $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
  76. $securityContext->expects($this->any())
  77. ->method('isGranted')
  78. ->will($this->throwException(new \RunTimeException('FAIL')));
  79. $handler = new AclSecurityHandler($securityContext, array());
  80. $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
  81. }
  82. }