Wednesday, 15 June 2011

zend framework - Guards BjyAuthorize Always 200 (which should be 403) Status Code in Unit Test -



zend framework - Guards BjyAuthorize Always 200 (which should be 403) Status Code in Unit Test -

i'm trying create unit test using bjyauthorize, guard works in browser ( homecoming 403 ) doesn't work in unit test because it's homecoming 200 (which should 403).

here codes :

bjyauthohrize.global.php

<?php homecoming [ 'bjyauthorize' => [ // set 'guest' role default (must defined in role provider) 'default_role' => 'guest', 'identity_provider' => \bjyauthorize\provider\identity\zfcuserzenddb::class, // using authentication identity provider, reads roles auth service's identity // 'identity_provider' => \bjyauthorize\provider\identity\authenticationidentityprovider::class, 'role_providers' => array( \bjyauthorize\provider\role\zenddb::class => [ 'table' => 'user_role', 'identifier_field_name' => 'id', 'role_id_field' => 'role_id', 'parent_role_field' => 'parent_id', ], // using object repository (entity repository) load roles our acl // bjyauthorize\provider\role\objectrepositoryprovider::class => array( // 'object_manager' => 'doctrine.entitymanager.orm_default', // 'role_entity_class' => 'user\entity\role', // ), ), /* currently, controller , route guards exist * * consider enabling either controller or route guard depending on needs. */ 'guards' => [ \bjyauthorize\guard\route::class => [ ['route' => 'zfcuser', 'roles' => ['user']], ['route' => 'zfcuser/logout', 'roles' => ['user','administrator']], ['route' => 'zfcuser/login', 'roles' => ['guest']], ['route' => 'zfcuser/register', 'roles' => ['guest']], // below default index action used zendskeletonapplication ['route' => 'home', 'roles' => ['guest', 'user']], ['route' => 'user/default', 'roles' => ['user']], ], ], ],

];

bootstrap :

public static function init() { $zf2modulepaths = array(dirname(dirname(__dir__))); if (($path = static::findparentpath('vendor'))) { $zf2modulepaths[] = $path; } if (($path = static::findparentpath('module')) !== $zf2modulepaths[0]) { $zf2modulepaths[] = $path; } static::initautoloader(); // utilize modulemanager load module , it's dependencies $testconfig = include __dir__ . '/testconfig.php'; $baseconfig = array( 'module_listener_options' => array( 'module_paths' => $zf2modulepaths, ), ); $config = arrayutils::merge($testconfig, $baseconfig); $servicemanager = new servicemanager(new servicemanagerconfig()); $servicemanager->setservice('applicationconfig', $config); $servicemanager->get('modulemanager')->loadmodules(); static::$servicemanager = $servicemanager; static::$config = $config; }

setup() :

protected $servicemanager; protected $controller; protected $event; protected $routematch; protected $request; public function setup() { $this->servicemanager = bootstrap::getservicemanager(); $this->controller = new indexcontroller($this->servicemanager->get('doctrine.entitymanager.orm_default')); $this->routematch = new routematch(array('controller' => 'user\controller\index')); $this->request = new request(); $this->event = new mvcevent(); $config = $this->servicemanager->get('config'); $routerconfig = isset($config['router']) ? $config['router'] : array(); $router = httprouter::factory($routerconfig); $this->event->setrouter($router); $this->event->setroutematch($this->routematch); $this->controller->setevent($this->event); $this->controller->setservicelocator($this->servicemanager); $this->mockzfclogin(); $this->mockbjy(); parent::setup(); }

testaction:

public function testupdateprofileactioncanbeaccessed() { $this->mockbjy('dodol'); $this->routematch->setparam('action', 'updateprofile'); $result = $this->controller->dispatch($this->request); $response = $this->controller->getresponse(); $this->assertequals(403, $response->getstatuscode()); }

here mockbjy , mockzfc :

protected function mockbjy($role = 'guest') { $authorizemock = $this ->getmockbuilder('bjyauthorize\provider\identity\providerinterface') ->disableoriginalconstructor() ->getmock(); $authorizemock ->expects($this->any()) ->method('getidentityroles') ->will($this->returnvalue($role)); $this->servicemanager->setallowoverride(true) ->setservice('bjyauthorize\provider\identity\providerinterface', $authorizemock); } protected function mockzfclogin() { $zfcusermock = $this->getmock('zfcuser\entity\user'); $zfcusermock->expects($this->any()) ->method('getid') ->will($this->returnvalue('10')); $authmock = $this->getmock('zfcuser\controller\plugin\zfcuserauthentication'); $authmock->expects($this->any()) ->method('hasidentity') -> will($this->returnvalue(true)); $authmock->expects($this->any()) ->method('getidentity') -> will($this->returnvalue($zfcusermock)); $this->controller->getpluginmanager() ->setservice('zfcuserauthentication', $authmock); }

controller:

public function updateprofileaction() { debug::dump($this->zfcuserauthentication()->getidentity()->getid()); $authorize = $this->getservicelocator()->get('bjyauthorize\provider\identity\providerinterface'); $roles = $authorize->getidentityroles(); debug::dump($roles); }

result :

configuration read /home/mockie/importants/htdocs/hommate/module/user/test/phpunit.xml string(2) "10" string(5) "guest" time: 122 ms, memory: 7.00mb ok (1 test, 5 assertions)

unit-testing zend-framework bjyauthorize

No comments:

Post a Comment