123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Admin\Security\Acl\Permission;
- use Sonata\AdminBundle\Security\Handler\AclSecurityHandler;
- use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
- use Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder;
- class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
- {
- public function testAcl()
- {
- $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->any())
- ->method('getCode')
- ->will($this->returnValue('test'));
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
- $securityContext->expects($this->any())
- ->method('isGranted')
- ->will($this->returnValue(true));
- $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
- $this->assertTrue($handler->isGranted($admin, array('TOTO')));
- $this->assertTrue($handler->isGranted($admin, 'TOTO'));
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
- $securityContext->expects($this->any())
- ->method('isGranted')
- ->will($this->returnValue(false));
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
- $this->assertFalse($handler->isGranted($admin, array('TOTO')));
- $this->assertFalse($handler->isGranted($admin, 'TOTO'));
- }
- public function testBuildInformation()
- {
- $informations = array(
- 'EDIT' => array('EDIT')
- );
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
- $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $admin->expects($this->once())
- ->method('getCode')
- ->will($this->returnValue('test'));
- $admin->expects($this->once())
- ->method('getSecurityInformation')
- ->will($this->returnValue($informations));
- $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
- $results = $handler->buildSecurityInformation($admin);
- $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
- }
- public function testWithAuthenticationCredentialsNotFoundException()
- {
- $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
- $securityContext->expects($this->any())
- ->method('isGranted')
- ->will($this->throwException(new AuthenticationCredentialsNotFoundException('FAIL')));
- $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
- $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
- }
- /**
- * @expectedException RuntimeException
- */
- public function testWithNonAuthenticationCredentialsNotFoundException()
- {
- $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
- $securityContext->expects($this->any())
- ->method('isGranted')
- ->will($this->throwException(new \RunTimeException('FAIL')));
- $aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
- $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
- }
- }
|