/** * {@inheritDoc} */ public function setUp() { $this->serviceManager = Bootstrap::getServiceManager(); $this->program = new Program(); $this->program->setId(1); $this->program->setProgram('This is the program'); $this->authorizeService = $this->serviceManager->get('BjyAuthorize\\Service\\Authorize'); if (!$this->authorizeService->getAcl()->hasResource($this->program)) { $this->authorizeService->getAcl()->addResource($this->program); $this->authorizeService->getAcl()->allow([], $this->program, []); } /** * Add the resource on the fly */ if (!$this->authorizeService->getAcl()->hasResource(new Program())) { $this->authorizeService->getAcl()->addResource(new Program()); } $this->authorizeService->getAcl()->allow([], new Program(), []); $this->programLink = $this->serviceManager->get('viewhelpermanager')->get('programlink'); $routeMatch = new RouteMatch(['program' => 1]); $routeMatch->setMatchedRouteName('route-program_entity_program'); $this->programLink->setRouteMatch($routeMatch); /** * Bootstrap the application to have the other information available */ $application = $this->serviceManager->get('application'); $application->bootstrap(); }
public function testMatchedRouteNameIsOverridenOnMerge() { $match = new RouteMatch(array()); $match->setMatchedRouteName('foo'); $subMatch = new RouteMatch(array()); $subMatch->setMatchedRouteName('bar'); $match->merge($subMatch); $this->assertEquals('bar', $match->getMatchedRouteName()); }
public function testAddBodyClass() { $bodyClassHelper = new BodyClass(); $listener = new BodyClassListener($bodyClassHelper); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('my/SOME/route'); $event = new \Zend\Mvc\MvcEvent(); $event->setRouteMatch($routeMatch); $listener->addBodyClass($event); $this->assertEquals('my-some-route', (string) $bodyClassHelper); }
protected function createMvcEvent() { $this->mvcEvent = new MvcEvent(); $request = new Request(); $response = new Response(); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('test/route'); $routeMatch->setParam('controller', 'Application\\Controller\\Index'); $routeMatch->setParam('action', 'index'); $this->mvcEvent->setRouteMatch($routeMatch); $this->mvcEvent->setRequest($request); $this->mvcEvent->setResponse($response); }
/** * Match a given request. * * @param Request $request Request to match * * @return RouteMatch */ public function match(Request $request) { // Get command line arguments and present working directory from // server superglobal: $filename = $request->getScriptName(); $pwd = CLI_DIR; // Convert base filename (minus .php extension and underscores) and // containing directory name into action and controller, respectively: $baseFilename = str_replace('_', '', basename($filename)); $baseFilename = substr($baseFilename, 0, strlen($baseFilename) - 4); $baseDirname = basename(dirname(realpath($pwd . '/' . $filename))); $routeMatch = new RouteMatch(array('controller' => $baseDirname, 'action' => $baseFilename), 1); // Override standard routing: $routeMatch->setMatchedRouteName('default'); return $routeMatch; }
/** * If the system is offline, set up a handler to override the routing output. * * @return void */ protected function initSystemStatus() { // If the system is unavailable, forward to a different place: if (isset($this->config->System->available) && !$this->config->System->available) { $callback = function ($e) { $routeMatch = new RouteMatch(['controller' => 'Error', 'action' => 'Unavailable'], 1); $routeMatch->setMatchedRouteName('error-unavailable'); $e->setRouteMatch($routeMatch); }; $this->events->attach('route', $callback); } }
/** * Legacy handling for scripts: Match a given request. * * @param Request $request Request to match * * @return RouteMatch */ public function match(Request $request) { // Get command line arguments and present working directory from // server superglobal: $filename = $request->getScriptName(); // WARNING: cwd is $VUFIND_HOME, so that throws off realpath! // // Convert base filename (minus .php extension and underscores) and // containing directory name into action and controller, respectively: $base = basename($filename, ".php"); $actionName = str_replace('_', '', $base); // action is the easy part $dir = dirname($filename); if ($dir == false || $dir == '' || $dir == '.' || basename($dir) == '.') { // legacy style: cd to subdir, but set CLI_DIR $dir = $this->getCliDir(); $path = $dir . '/' . $filename; } else { // modern style: invoked as, e.g. $base=util/ping.php, already has path $level1 = basename(dirname($filename)); // but we need to re-orient relative to VUFIND_HOME $path = $level1 . '/' . basename($filename); } $controller = basename($dir); // the last directory part // Special case: if user is accessing index.php directly, we expect // controller and action as first two parameters. if ($controller == 'public' && $actionName == 'index') { list($controller, $actionName) = $this->extractFromCommandLine(); } $routeMatch = new RouteMatch(['controller' => $controller, 'action' => $actionName], 1); // Override standard routing: $routeMatch->setMatchedRouteName('default'); return $routeMatch; }
/** * @dataProvider dataProviderForTestCheckRedirect_WhenShouldRedirectToRecognizedLanguageIsEnabled * * @param string $recognizedLang * @param string $routeMatchLang * @param string $routeMatchLangAfterRedirect * @param array $expectedResult */ public function testCheckRedirect_WhenShouldRedirectToRecognizedLanguageIsEnabled($recognizedLang, $routeMatchLang, $routeMatchLangAfterRedirect, $expectedResult) { $this->prepareTestedObject($this->prepareConfig(true)); $paramsBefore = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'TestController', 'action' => 'index', 'lang' => $routeMatchLang ? $routeMatchLang : '', '__CONTROLLER__' => 'test']; $paramsAfter = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'test', 'action' => null, 'lang' => $routeMatchLangAfterRedirect, '__CONTROLLER__' => 'test']; $routeMatch = new RouteMatch($paramsBefore); $matchedRouteName = 'some\\test'; $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); $event->setResponse(new Response()); $event->setRouteMatch($routeMatch); $uriLangPrefix = $routeMatchLangAfterRedirect ? '/' . $routeMatchLangAfterRedirect : ''; $expectedUri = $uriLangPrefix . '/some/test'; if ($expectedResult) { $this->routeMock->expects($this->once())->method('assemble')->with($paramsAfter, ['name' => $matchedRouteName])->willReturn($expectedUri); } $event->setRouter($this->routeMock); $result = $this->testedObject->checkRedirect($event, $recognizedLang, $routeMatchLang); $this->assertSame($expectedResult, $result); }
/** * @dataProvider dataProviderForTestRecognize_WhenShouldRedirectToRecognizedLanguageIsEnabled * * @param string $browserLangLocale * @param string $routeMatchLang * @param array $expectedResult */ public function testRecognize_WhenShouldRedirectToRecognizedLanguageIsEnabled($browserLangLocale, $routeMatchLang, $expectedResult) { $this->prepareTestedObject($this->prepareConfig(true)); $request = new Request(); $request->getHeaders()->addHeaderLine('Accept-Language', $browserLangLocale); $paramsBefore = ['__NAMESPACE__' => 'Module\\Some', 'controller' => 'TestController', 'action' => 'index', 'lang' => $routeMatchLang, '__CONTROLLER__' => 'test']; $routeMatch = new RouteMatch($paramsBefore); $matchedRouteName = 'some\\test'; $routeMatch->setMatchedRouteName($matchedRouteName); $event = new MvcEvent(); $event->setRequest($request); $event->setResponse(new Response()); $event->setRouteMatch($routeMatch); $result = $this->testedObject->recognize($event); $this->assertSame($expectedResult, $result); }