AclSecurityHandlerTest.php 3.1 KB

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