Przeglądaj źródła

Improved NoopSecurityHandlerTest and RoleSecurityHandlerTest for full test coverage.

Andrej Hudec 11 lat temu
rodzic
commit
3781f662d8

+ 18 - 2
Tests/Security/Handler/NoopSecurityHandlerTest.php

@@ -11,11 +11,12 @@
 namespace Sonata\AdminBundle\Tests\Security\Handler;
 namespace Sonata\AdminBundle\Tests\Security\Handler;
 
 
 use Sonata\AdminBundle\Security\Handler\NoopSecurityHandler;
 use Sonata\AdminBundle\Security\Handler\NoopSecurityHandler;
+use Sonata\AdminBundle\Admin\AdminInterface;
 
 
 class NoopSecurityHandlerTest extends \PHPUnit_Framework_TestCase
 class NoopSecurityHandlerTest extends \PHPUnit_Framework_TestCase
 {
 {
     /**
     /**
-     * @var Sonata\AdminBundle\Security\Handler\NoopSecurityHandler
+     * @var NoopSecurityHandler
      */
      */
     private $handler = null;
     private $handler = null;
 
 
@@ -35,8 +36,23 @@ class NoopSecurityHandlerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(array(), $this->handler->buildSecurityInformation($this->getSonataAdminObject()));
         $this->assertEquals(array(), $this->handler->buildSecurityInformation($this->getSonataAdminObject()));
     }
     }
 
 
+    public function testCreateObjectSecurity()
+    {
+        $this->assertNull($this->handler->createObjectSecurity($this->getSonataAdminObject(), new \stdClass()));
+    }
+
+    public function testDeleteObjectSecurity()
+    {
+        $this->assertNull($this->handler->deleteObjectSecurity($this->getSonataAdminObject(), new \stdClass()));
+    }
+
+    public function testGetBaseRole()
+    {
+        $this->assertEquals('', $this->handler->getBaseRole($this->getSonataAdminObject()));
+    }
+
     /**
     /**
-     * @return Sonata\AdminBundle\Admin\AdminInterface
+     * @return AdminInterface
      */
      */
     private function getSonataAdminObject()
     private function getSonataAdminObject()
     {
     {

+ 59 - 1
Tests/Security/Handler/RoleSecurityHandlerTest.php

@@ -14,6 +14,7 @@ namespace Sonata\AdminBundle\Tests\Security\Handler;
 use Symfony\Component\Security\Core\SecurityContextInterface;
 use Symfony\Component\Security\Core\SecurityContextInterface;
 use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Security\Handler\RoleSecurityHandler;
 use Sonata\AdminBundle\Security\Handler\RoleSecurityHandler;
+use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
 
 
 /**
 /**
  * Test for RoleSecurityHandler
  * Test for RoleSecurityHandler
@@ -68,7 +69,7 @@ class RoleSecurityHandlerTest extends \PHPUnit_Framework_TestCase
      */
      */
     public function testIsGranted($expected, array $superAdminRoles, $adminCode, $operation, $object = null)
     public function testIsGranted($expected, array $superAdminRoles, $adminCode, $operation, $object = null)
     {
     {
-        $handler = new RoleSecurityHandler($this->securityContext, $superAdminRoles);
+        $handler = $this->getRoleSecurityHandler($superAdminRoles);
 
 
         $this->admin->expects($this->any())
         $this->admin->expects($this->any())
             ->method('getCode')
             ->method('getCode')
@@ -86,6 +87,10 @@ class RoleSecurityHandlerTest extends \PHPUnit_Framework_TestCase
                     return true;
                     return true;
                 }
                 }
 
 
+                if (in_array('ROLE_AUTH_EXCEPTION', $attributes)) {
+                    throw new AuthenticationCredentialsNotFoundException();
+                }
+
                 if (in_array('ROLE_FOO_BAR_ABC', $attributes)) {
                 if (in_array('ROLE_FOO_BAR_ABC', $attributes)) {
                     return true;
                     return true;
                 }
                 }
@@ -162,6 +167,59 @@ class RoleSecurityHandlerTest extends \PHPUnit_Framework_TestCase
             array(false, array(), 'foo.bar', 'BAZ', new \stdClass()),
             array(false, array(), 'foo.bar', 'BAZ', new \stdClass()),
             array(false, array(), 'foo.bar.baz.xyz', 'BAZ', new \stdClass()),
             array(false, array(), 'foo.bar.baz.xyz', 'BAZ', new \stdClass()),
             array(false, array(), 'foo.bar.baz.xyz', array('BAZ'), new \stdClass()),
             array(false, array(), 'foo.bar.baz.xyz', array('BAZ'), new \stdClass()),
+            array(false, array('ROLE_AUTH_EXCEPTION'), 'foo.bar.baz.xyz', array('BAZ'), new \stdClass()),
         );
         );
     }
     }
+
+    public function testIsGrantedWithException()
+    {
+        $this->setExpectedException('RuntimeException', 'Something is wrong');
+
+        $this->admin->expects($this->any())
+            ->method('getCode')
+            ->will($this->returnValue('foo.bar'));
+
+        $this->securityContext->expects($this->any())
+            ->method('isGranted')
+            ->will($this->returnCallback(function(array $attributes, $object) {
+                throw new \RuntimeException('Something is wrong');
+            }));
+
+        $handler = $this->getRoleSecurityHandler(array('ROLE_BATMAN'));
+        $handler->isGranted($this->admin, 'BAZ');
+    }
+
+    public function testCreateObjectSecurity()
+    {
+        $handler = $this->getRoleSecurityHandler(array('ROLE_FOO'));
+        $this->assertNull($handler->createObjectSecurity($this->getSonataAdminObject(), new \stdClass()));
+    }
+
+    public function testDeleteObjectSecurity()
+    {
+        $handler = $this->getRoleSecurityHandler(array('ROLE_FOO'));
+        $this->assertNull($handler->deleteObjectSecurity($this->getSonataAdminObject(), new \stdClass()));
+    }
+
+    public function testBuildSecurityInformation()
+    {
+        $handler = $this->getRoleSecurityHandler(array('ROLE_FOO'));
+        $this->assertEquals(array(), $handler->buildSecurityInformation($this->getSonataAdminObject()));
+    }
+
+    /**
+     * @return RoleSecurityHandler
+     */
+    private function getRoleSecurityHandler(array $superAdminRoles)
+    {
+        return new RoleSecurityHandler($this->securityContext, $superAdminRoles);
+    }
+
+    /**
+     * @return AdminInterface
+     */
+    private function getSonataAdminObject()
+    {
+        return $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
+    }
 }
 }