From cf1c8eca0b4d0b32a1b98906def641537c6abfc0 Mon Sep 17 00:00:00 2001 From: Andrey Shatrov Date: Thu, 4 May 2017 14:47:50 +0400 Subject: [PATCH 1/2] [373] Cannot get a name and parameters for RoleProviderPluginManager https://github.com/ZF-Commons/zfc-rbac/issues/373 --- src/ZfcRbac/Factory/RoleServiceFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ZfcRbac/Factory/RoleServiceFactory.php b/src/ZfcRbac/Factory/RoleServiceFactory.php index 6eae608..acfb364 100644 --- a/src/ZfcRbac/Factory/RoleServiceFactory.php +++ b/src/ZfcRbac/Factory/RoleServiceFactory.php @@ -56,6 +56,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o $pluginManager = $container->get('ZfcRbac\Role\RoleProviderPluginManager'); /* @var \ZfcRbac\Role\RoleProviderInterface $roleProvider */ + reset($roleProviderConfig); $roleProvider = $pluginManager->get(key($roleProviderConfig), current($roleProviderConfig)); /* @var \Rbac\Traversal\Strategy\TraversalStrategyInterface $traversalStrategy */ From 6dea80bf39fd0c34735f8e4a4ee9e906912f90be Mon Sep 17 00:00:00 2001 From: Andrey Shatrov Date: Thu, 4 May 2017 17:38:48 +0400 Subject: [PATCH 2/2] [373] Cannot get a name and parameters for RoleProviderPluginManager (Tests) --- .../Factory/RoleServiceFactoryTest.php | 90 ++++++++++++++++--- 1 file changed, 78 insertions(+), 12 deletions(-) diff --git a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php index f420242..9ffeb73 100644 --- a/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php +++ b/tests/ZfcRbacTest/Factory/RoleServiceFactoryTest.php @@ -40,22 +40,37 @@ public function testFactory() ] ]); - $serviceManager = new ServiceManager(); - $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); - $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', new RoleProviderPluginManager($serviceManager)); - $serviceManager->setService( - 'ZfcRbac\Identity\AuthenticationProvider', - $this->getMock('ZfcRbac\Identity\IdentityProviderInterface') - ); - $traversalStrategy = $this->getMock('Rbac\Traversal\Strategy\TraversalStrategyInterface'); - $rbac = $this->getMock('Rbac\Rbac', [], [], '', false); + $roleProvider = $this->getMock('\ZfcRbac\Role\RoleProviderInterface'); - $rbac->expects($this->once())->method('getTraversalStrategy')->will($this->returnValue($traversalStrategy)); + $rbac = $this + ->getMockBuilder('Rbac\Rbac') + ->disableOriginalConstructor() + ->getMock(); + $rbac->expects($this->once()) + ->method('getTraversalStrategy') + ->will($this->returnValue( + $traversalStrategy + )); + $pluginManager = $this + ->getMockBuilder('\ZfcRbac\Role\RoleProviderPluginManager') + ->disableOriginalConstructor() + ->getMock(); + $pluginManager->expects($this->once()) + ->method('get') + ->with('ZfcRbac\Role\InMemoryRoleProvider', ['foo']) + ->will($this->returnValue( + $roleProvider + )); + + $serviceManager = new ServiceManager(); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); $serviceManager->setService('Rbac\Rbac', $rbac); + $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); + $serviceManager->setService('ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface')); - $factory = new RoleServiceFactory(); + $factory = new RoleServiceFactory(); $roleService = $factory->createService($serviceManager); $this->assertInstanceOf('ZfcRbac\Service\RoleService', $roleService); @@ -63,6 +78,57 @@ public function testFactory() $this->assertAttributeSame($traversalStrategy, 'traversalStrategy', $roleService); } + public function testIfRoleArrayPointerBeyondArrayEnd() + { + $options = new ModuleOptions([ + 'identity_provider' => 'ZfcRbac\Identity\AuthenticationProvider', + 'guest_role' => 'guest', + 'role_provider' => [ + 'ZfcRbac\Role\InMemoryRoleProvider' => [ + 'foo' + ] + ] + ]); + + // Simulate if array pointer beyond end of array. E.g after 'while(next($roleProvider)) { //do }' + $roleProvider = $options->getRoleProvider(); + next($roleProvider); + $options->setRoleProvider($roleProvider); + + $traversalStrategy = $this->getMock('Rbac\Traversal\Strategy\TraversalStrategyInterface'); + $roleProvider = $this->getMock('\ZfcRbac\Role\RoleProviderInterface'); + + $rbac = $this + ->getMockBuilder('Rbac\Rbac') + ->disableOriginalConstructor() + ->getMock(); + $rbac->expects($this->once()) + ->method('getTraversalStrategy') + ->will($this->returnValue( + $traversalStrategy + )); + + $pluginManager = $this + ->getMockBuilder('\ZfcRbac\Role\RoleProviderPluginManager') + ->disableOriginalConstructor() + ->getMock(); + $pluginManager->expects($this->once()) + ->method('get') + ->with('ZfcRbac\Role\InMemoryRoleProvider', ['foo']) + ->will($this->returnValue( + $roleProvider + )); + + $serviceManager = new ServiceManager(); + $serviceManager->setService('ZfcRbac\Options\ModuleOptions', $options); + $serviceManager->setService('Rbac\Rbac', $rbac); + $serviceManager->setService('ZfcRbac\Role\RoleProviderPluginManager', $pluginManager); + $serviceManager->setService('ZfcRbac\Identity\AuthenticationProvider', $this->getMock('ZfcRbac\Identity\IdentityProviderInterface')); + + $factory = new RoleServiceFactory(); + $factory->createService($serviceManager); + } + public function testThrowExceptionIfNoRoleProvider() { $this->setExpectedException('ZfcRbac\Exception\RuntimeException'); @@ -81,6 +147,6 @@ public function testThrowExceptionIfNoRoleProvider() ); $factory = new RoleServiceFactory(); - $roleService = $factory->createService($serviceManager); + $factory->createService($serviceManager); } }