|
@@ -16,6 +16,29 @@ use Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder;
|
|
|
|
|
|
class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
+ public function getTokenStorageMock()
|
|
|
+ {
|
|
|
+ // Set the SecurityContext for Symfony <2.6
|
|
|
+ // TODO: Remove conditional return when bumping requirements to SF 2.6+
|
|
|
+ if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
|
|
|
+ return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
|
|
|
+ $this->authorizationChecker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getAuthorizationCheckerMock()
|
|
|
+ {
|
|
|
+ // Set the SecurityContext for Symfony <2.6
|
|
|
+ // TODO: Remove conditional return when bumping requirements to SF 2.6+
|
|
|
+ if (interface_exists('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface')) {
|
|
|
+ return $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
+ }
|
|
|
+
|
|
|
public function testAcl()
|
|
|
{
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
@@ -23,24 +46,24 @@ class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
->method('getCode')
|
|
|
->will($this->returnValue('test'));
|
|
|
|
|
|
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
- $securityContext->expects($this->any())
|
|
|
+ $authorizationChecker = $this->getAuthorizationCheckerMock();
|
|
|
+ $authorizationChecker->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());
|
|
|
+ $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $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())
|
|
|
+ $authorizationChecker = $this->getAuthorizationCheckerMock();
|
|
|
+ $authorizationChecker->expects($this->any())
|
|
|
->method('isGranted')
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
|
|
|
+ $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
|
|
|
|
|
|
$this->assertFalse($handler->isGranted($admin, array('TOTO')));
|
|
|
$this->assertFalse($handler->isGranted($admin, 'TOTO'));
|
|
@@ -52,7 +75,7 @@ class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
'EDIT' => array('EDIT')
|
|
|
);
|
|
|
|
|
|
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
+ $authorizationChecker = $this->getAuthorizationCheckerMock();
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
$admin->expects($this->once())
|
|
|
->method('getCode')
|
|
@@ -64,7 +87,7 @@ class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
|
|
$aclProvider = $this->getMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface');
|
|
|
|
|
|
- $handler = new AclSecurityHandler($securityContext, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
|
|
|
+ $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
|
|
|
|
|
|
$results = $handler->buildSecurityInformation($admin);
|
|
|
|
|
@@ -75,14 +98,14 @@ class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
|
|
|
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
- $securityContext->expects($this->any())
|
|
|
+ $authorizationChecker = $this->getAuthorizationCheckerMock();
|
|
|
+ $authorizationChecker->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());
|
|
|
+ $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
|
|
|
|
|
|
$this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
|
|
|
}
|
|
@@ -94,14 +117,14 @@ class AclSecurityHandlerTest extends \PHPUnit_Framework_TestCase
|
|
|
{
|
|
|
$admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
|
|
|
|
|
|
- $securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
|
|
- $securityContext->expects($this->any())
|
|
|
+ $authorizationChecker = $this->getAuthorizationCheckerMock();
|
|
|
+ $authorizationChecker->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());
|
|
|
+ $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, 'Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder', array());
|
|
|
|
|
|
$this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
|
|
|
}
|