public function testNoRolesAllowed() { $this->routeMatch->setParam('action', 'noRoles'); $listener = new Authorized(); $listener->onDispach($this->event); $this->assertNull($this->event->getParam('exception')); }
public static function resolveController(RouteMatch $routeMatch) { if (($extension = $routeMatch->getParam('extension')) && ($manifestName = $routeMatch->getParam('manifestName'))) { if ($endpoint = $routeMatch->getParam('endpoint')) { $routeMatch->setParam('controller', implode('.', [$extension, $manifestName, $endpoint])); } else { $routeMatch->setParam('controller', implode('.', [$extension, $manifestName])); } } }
public function testResizeActionFileNotFound() { $this->routeMatch->setParam('action', 'resize'); $this->routeMatch->setParam('file', 'img/test/fileNotFound'); $this->routeMatch->setParam('extension', 'jpg'); $this->routeMatch->setParam('command', 'thumb,80,40'); $result = $this->controller->dispatch($this->request); $response = $this->controller->getResponse(); $this->assertEquals(404, $response->getStatusCode()); }
public static function resolveController(RouteMatch $routeMatch) { if ($routeMatch->getMatchedRouteName() != 'rest') { return; } if ($endpoint = $routeMatch->getParam('endpoint')) { $routeMatch->setParam('controller', 'shard.rest.' . $endpoint); } else { $routeMatch->setParam('controller', 'shard.rest'); } }
protected function setUp() { $serviceManagerGrabber = new ServiceManagerGrabber(); $this->serviceManager = $serviceManagerGrabber->getServiceManager(); $this->serviceManager->setAllowOverride(true); $this->serviceManager->setService('doctrine.entitymanager.orm_default', $this->getEntityManagerMock()); $config = $this->serviceManager->get('Config'); $this->request = new PhpEnviromentRequest(); $this->router = HttpRouter::factory(isset($config['router']) ? $config['router'] : array()); $this->routeMatch = new RouteMatch(array('controller' => 'index')); $this->routeMatch->setParam('lang', 'it'); $this->event = new MvcEvent(); $this->event->setRouter($this->router); $this->event->setRouteMatch($this->routeMatch); }
/** * @covers \Project\Controller\ProjectController::searchAction */ public function testCanSearchProjects() { $this->routeMatch->setParam('action', 'search'); $queryParams = new Parameters(['search_item' => 'test', 'max_rows' => 12]); $this->request->setQuery($queryParams); $this->controller->dispatch($this->request, $this->response); $this->assertEquals(200, $this->controller->getResponse()->getStatusCode()); }
/** * @covers \Project\Controller\IdeaController::updateDescriptionAction */ public function testUpdateWrongIdeaDescriptionGives404() { $this->routeMatch->setParam('action', 'update-description'); $queryParams = new Parameters(['id' => 'description-0', 'value' => 'This is the new description']); $this->request->setPost($queryParams); $this->controller->dispatch($this->request, $this->response); $this->assertEquals(404, $this->controller->getResponse()->getStatusCode()); }
/** * @depends testGetCategory * @param int $id */ public function testDeleteCategory($id) { $this->getRequest()->setMethod(Request::METHOD_DELETE); $this->routeMatch->setParam('id', $id); $jsonModel = $this->dispatchRequest(); $this->assertEquals(200, $this->controller->getResponse()->getStatusCode()); $this->assertEquals('success', $jsonModel->getVariable('message')['type']); }
/** * @depends testGetListing * * @param int $id */ public function testDeleteListing($id) { $this->getRequest()->setMethod(Request::METHOD_POST); $this->routeMatch->setParam('action', 'deleteAjax'); $this->getRequest()->getPost()->fromArray(['ids' => $id]); $jsonModel = $this->dispatchRequest(); $this->assertEquals(200, $this->controller->getResponse()->getStatusCode()); $this->assertEquals('success', $jsonModel->getVariable('message')['type']); }
public function testIsActiveReturnsFalseWhenMatchingRouteButNonMatchingParams() { $page = new Page\Mvc(array('label' => 'foo', 'route' => 'bar', 'action' => 'baz')); $routeMatch = new RouteMatch(array()); $routeMatch->setMatchedRouteName('bar'); $routeMatch->setParam('action', 'qux'); $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); }
public function testCanAllowAccess() { $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $eventManager->expects($this->never())->method('trigger'); $application = $this->getMock('Zend\\Mvc\\Application', array(), array(), '', false); $application->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager)); $routeMatch = new RouteMatch(array()); $routeMatch->setParam("controller", "index"); $routeMatch->setParam("action", "update"); $event = new MvcEvent(); $event->setRouteMatch($routeMatch); $event->setApplication($application); $controllerGuardMock = $this->getMockBuilder('UghAuthorization\\Guards\\Guard', array('isGranted'))->disableOriginalConstructor()->getMock(); $controllerGuardMock->expects($this->any())->method('isGranted')->will($this->returnValue(true)); $controllerGuardListener = new ControllerGuardListener($controllerGuardMock); $controllerGuardListener->onGuard($event); $this->assertFalse($event->propagationIsStopped()); $this->assertEmpty($event->getError()); }
public function testListActionWithFilledConfiguration() { $this->markTestIncomplete(); $configuration = array('name_to_configuration_path' => array('locator_one' => __FILE__, 'locator_two' => __FILE__)); $console = $this->console; $console->shouldReceive('writeLine')->with('locator: locator_one with configuration file "' . __FILE__ . '"')->once(); $console->shouldReceive('writeLine')->with('locator: locator_two with configuration file "' . __FILE__ . '"')->once(); $this->controller->setConfiguration($configuration); $this->routeMatch->setParam('action', 'list'); $this->controller->dispatch($this->request); }
public function onDispatchError(MvcEvent $e) { if ($e->getError() !== Zf2Application::ERROR_ROUTER_NO_MATCH) { return; } $routeMatch = new RouteMatch(array()); $routeMatch->setParam('controller', 'Zf1Module\\DispatchController'); $e->setError(null); $e->setRouteMatch($routeMatch); return $routeMatch; }
/** * Test Page Check Controller with Existing Page * * @return void * * @covers \Rcm\Controller\PageCheckController */ public function testPageCheckControllerGetMethodWithExistingPage() { $this->mockPageValidator->expects($this->any())->method('isValid')->will($this->returnValue(false)); $this->mockPageValidator->expects($this->any())->method('getMessages')->will($this->returnValue(["pageExists" => 'Page Exists'])); $this->routeMatch->setParam('pageId', 'page-exists'); /** @var \Zend\View\Model\JsonModel $result */ $result = $this->controller->dispatch($this->request); $this->assertInstanceOf('Zend\\View\\Model\\JsonModel', $result); $response = $this->controller->getResponse(); $this->assertEquals(409, $response->getStatusCode()); $result = $result->getVariables(); $expected = ['valid' => false, 'error' => ['pageExists' => 'Page Exists']]; $this->assertEquals($expected, $result); }
/** * Inject regex matches into the route matches * * @param RouteMatch $routeMatches */ protected function injectRouteMatches(RouteMatch $routeMatches, $matches) { if (!class_exists('\\ZF\\Apigility\\Admin\\Module', false)) { $vendor = $matches['zf_ver_vendor']; $version = $matches['zf_ver_version']; $controllerTest = $vendor . '\\V' . $version; if (strpos($routeMatches->getParam('controller'), $controllerTest) !== 0) { $controllerParts = explode('\\', $routeMatches->getParam('controller')); $controllerParts[0] = $vendor; $controllerParts[1] = 'V' . $version; $controller = implode('\\', $controllerParts); $routeMatches->setParam('controller', $controller); } } }
public function testErrorCodes() { $this->routeMatch->setParam('quiet', true); $this->config['diagnostics']['group']['test1'] = $check1 = new AlwaysSuccessCheck(); $result = $this->controller->dispatch(new ConsoleRequest()); $this->assertInstanceOf('Zend\\View\\Model\\ConsoleModel', $result); $this->assertEquals(0, $result->getErrorLevel()); $this->config['diagnostics']['group']['test1'] = $check1 = new ReturnThisCheck(new Failure()); $result = $this->controller->dispatch(new ConsoleRequest()); $this->assertInstanceOf('Zend\\View\\Model\\ConsoleModel', $result); $this->assertEquals(1, $result->getErrorLevel()); $this->config['diagnostics']['group']['test1'] = $check1 = new ReturnThisCheck(new Warning()); $result = $this->controller->dispatch(new ConsoleRequest()); $this->assertInstanceOf('Zend\\View\\Model\\ConsoleModel', $result); $this->assertEquals(0, $result->getErrorLevel()); }
/** * @param int $editedUserID The ID of the user being edited * @param UserEntity $currentUser * @param int $newRole The new role of the edited user * @return mixed|null|\Zend\Stdlib\ResponseInterface */ protected function dispatchEdit($editedUserID, $currentUser, $newRole = 1) { if (!is_int($newRole)) { throw new \InvalidArgumentException(); } $this->getRequest()->setMethod(Request::METHOD_PUT); $this->routeMatch->setParam('id', $editedUserID); $form = new User($currentUser, $this->controller->getServiceLocator()->get('entity-manager')); $this->getRequest()->setContent('uname=admin3&email=ventsi2@mail.com&role=' . $newRole . '&user_csrf=' . $form->get('user_csrf')->getValue()); $jsonModel = null; try { $jsonModel = $this->controller->dispatch($this->getRequest()); } catch (\Exception $e) { var_dump($e->getMessage()); } return $jsonModel; }
/** * @depends testGetLanguage * @var int $id */ public function testDeleteLanguage($id) { $entityManager = $this->controller->getServiceLocator()->get('entity-manager'); $insertedLanguage = $this->getInsertedLanguage($id); //change the status to '1' manually $insertedLanguage->setStatus(1); $entityManager->persist($insertedLanguage); $entityManager->flush(); //test can delete active language (with status=1) $this->request->setMethod(Request::METHOD_DELETE); $this->routeMatch->setParam('id', $id); try { $jsonModel = $this->controller->dispatch($this->request); } catch (\Exception $e) { var_dump($e->getMessage()); exit; } $this->assertEquals('success', $jsonModel->getVariable('message')['type']); }
/** * Test the index controller with Layout Template override * * @return null * @covers Rcm\Controller\IndexController */ public function testIndexActionWithLayoutOverride() { $this->layoutOverride = 'newLayout'; $this->pageData = $this->getPageData(42, 'my-test', 443, 'z', false, $this->layoutOverride); $this->routeMatch->setParam('action', 'index'); $this->routeMatch->setParam('page', 'my-test'); $this->routeMatch->setParam('pageType', 'z'); $this->routeMatch->setParam('revision', 443); $this->mockUserServicePlugin->expects($this->any())->method('__invoke')->will($this->returnValue(true)); $this->mockShouldShowRevisions->expects($this->any())->method('__invoke')->will($this->returnValue(true)); $this->mockIsPageAllowed->expects($this->any())->method('__invoke')->will($this->returnValue(true)); $result = $this->controller->dispatch($this->request); /** @var \Zend\Http\Response $response */ $response = $this->controller->getResponse(); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('my-test', $this->controller->pageName); $this->assertEquals('z', $this->controller->pageType); $this->assertEquals(443, $this->controller->pageRevisionId); $this->assertEquals('layout/' . $this->layoutOverride, $this->controller->layout()->getTemplate()); }
/** * Test the index controller with Layout Template override * * @return null * @covers Rcm\Controller\CmsController */ public function testIndexActionWithLayoutOverride() { $mockPage = $this->getMockBuilder('\\Rcm\\Entity\\Page')->disableOriginalConstructor()->getMock(); $mockPage->expects($this->any())->method('getName')->will($this->returnValue('my-test')); $mockPage->expects($this->any())->method('getPageType')->will($this->returnValue('z')); $mockRevision = $this->getMockBuilder('\\Rcm\\Entity\\Revision')->disableOriginalConstructor()->getMock(); $mockRevision->expects($this->any())->method('')->will($this->returnValue(443)); $mockPage->expects($this->any())->method('getRevisionById')->will($this->returnValue($mockRevision)); $mockPage->expects($this->any())->method('getCurrentRevision')->will($this->returnValue($mockRevision)); $mockPage->expects($this->any())->method('getSiteLayoutOverride')->will($this->returnValue('newLayout')); $this->layoutOverride = 'newLayout'; $this->routeMatch->setParam('action', 'index'); $this->routeMatch->setParam('page', $mockPage); $this->mockUserServicePlugin->expects($this->any())->method('__invoke')->will($this->returnValue(true)); $this->mockShouldShowRevisions->expects($this->any())->method('__invoke')->will($this->returnValue(true)); $this->mockIsPageAllowed->expects($this->any())->method('__invoke')->will($this->returnValue(true)); $result = $this->controller->dispatch($this->request); /** @var \Zend\Http\Response $response */ $response = $this->controller->getResponse(); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('layout/' . $this->layoutOverride, $this->controller->layout()->getTemplate()); }
/** * Translate a routematch * Will be used by the Module bootstrap * * @param RouteMatch $routeMatch * @return void */ public function translateRouteMatch(RouteMatch $routeMatch) { $params = $routeMatch->getParams(); $routeName = $routeMatch->getMatchedRouteName(); $result = $this->translate($routeName, self::NAMESPACE_ROUTE_MATCH); if ($result === $routeName) { return; } foreach ($params as $key => $value) { $translateKey = "{$routeName}.{$key}.{$value}"; $result = $this->translate($translateKey, self::NAMESPACE_ROUTE_MATCH); if ($result !== $translateKey) { $routeMatch->setParam($key, $result); } } }
/** * Inject regex matches into the route matches * * @param RouteMatch $routeMatches * @param array $matches */ protected function injectRouteMatches(RouteMatch $routeMatches, array $matches) { foreach ($matches as $key => $value) { if (is_numeric($key) || is_int($key) || $value === '') { continue; } $routeMatches->setParam($key, $value); } }
public function testSetParam() { $match = new RouteMatch(array()); $match->setParam('foo', 'bar'); $this->assertEquals(array('foo' => 'bar'), $match->getParams()); }
public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization() { $event = new MvcEvent(); $routeMatch = new RouteMatch([]); $application = $this->getMock('Zend\\Mvc\\Application', [], [], '', false); $eventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface'); $application->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager)); $eventManager->expects($this->once())->method('trigger')->with(MvcEvent::EVENT_DISPATCH_ERROR); $routeMatch->setParam('controller', 'MyController'); $routeMatch->setParam('action', 'delete'); $event->setRouteMatch($routeMatch); $event->setApplication($application); $identityProvider = $this->getMock('ZfjRbac\\Identity\\IdentityProviderInterface'); $identityProvider->expects($this->any())->method('getIdentityRoles')->will($this->returnValue('member')); $roleProvider = new InMemoryRoleProvider(['member']); $roleService = new RoleService($identityProvider, $roleProvider, new RecursiveRoleIteratorStrategy()); $routeGuard = new ControllerGuard($roleService, [['controller' => 'MyController', 'actions' => 'edit', 'roles' => 'member']]); $routeGuard->onResult($event); $this->assertTrue($event->propagationIsStopped()); $this->assertEquals(ControllerGuard::GUARD_UNAUTHORIZED, $event->getError()); $this->assertInstanceOf('ZfjRbac\\Exception\\UnauthorizedException', $event->getParam('exception')); }
public function testListLocales() { $this->routeMatch->setParam('action', 'listLocales'); $this->controller->dispatch($this->request); $this->assertEquals(200, $this->controller->getResponse()->getStatusCode()); }