/** * Checks whether the given path is an administrative one. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * * @return bool * TRUE if the path is administrative, FALSE otherwise. */ protected function isAdminPath(Request $request) { $result = FALSE; if ($request && $this->adminContext) { // If called from an event subscriber, the request may not have the route // object yet (it is still being built), so use the router to look up // based on the path. $route_match = $this->stackedRouteMatch->getRouteMatchFromRequest($request); if ($route_match && !($route_object = $route_match->getRouteObject())) { try { // Process the path as an inbound path. This will remove any language // prefixes and other path components that inbound processing would // clear out, so we can attempt to load the route clearly. $path = $this->pathProcessorManager->processInbound(urldecode(rtrim($request->getPathInfo(), '/')), $request); $attributes = $this->router->match($path); } catch (ResourceNotFoundException $e) { return FALSE; } catch (AccessDeniedHttpException $e) { return FALSE; } $route_object = $attributes[RouteObjectInterface::ROUTE_OBJECT]; } $result = $this->adminContext->isAdminRoute($route_object); } return $result; }
function setUp() { $routes = new RouteCollection(); $first_route = new Route('/test/one'); $second_route = new Route('/test/two/{narf}'); $third_route = new Route('/test/two/'); $fourth_route = new Route('/test/four', array(), array('_scheme' => 'https')); $routes->add('test_1', $first_route); $routes->add('test_2', $second_route); $routes->add('test_3', $third_route); $routes->add('test_4', $fourth_route); // Create a route provider stub. $provider = $this->getMockBuilder('Drupal\\Core\\Routing\\RouteProvider')->disableOriginalConstructor()->getMock(); // We need to set up return value maps for both the getRouteByName() and the // getRoutesByNames() method calls on the route provider. The parameters // are not passed in and default to an empty array. $route_name_return_map = $routes_names_return_map = array(); $return_map_values = array(array('route_name' => 'test_1', 'return' => $first_route), array('route_name' => 'test_2', 'return' => $second_route), array('route_name' => 'test_3', 'return' => $third_route), array('route_name' => 'test_4', 'return' => $fourth_route)); foreach ($return_map_values as $values) { $route_name_return_map[] = array($values['route_name'], array(), $values['return']); $routes_names_return_map[] = array(array($values['route_name']), array(), $values['return']); } $provider->expects($this->any())->method('getRouteByName')->will($this->returnValueMap($route_name_return_map)); $provider->expects($this->any())->method('getRoutesByNames')->will($this->returnValueMap($routes_names_return_map)); // Create an alias manager stub. $alias_manager = $this->getMockBuilder('Drupal\\Core\\Path\\AliasManager')->disableOriginalConstructor()->getMock(); $alias_manager->expects($this->any())->method('getAliasByPath')->will($this->returnCallback(array($this, 'aliasManagerCallback'))); $this->aliasManager = $alias_manager; $context = new RequestContext(); $context->fromRequest($request = Request::create('/some/path')); $processor = new PathProcessorAlias($this->aliasManager); $processor_manager = new PathProcessorManager(); $processor_manager->addOutbound($processor, 1000); $this->routeProcessorManager = $this->getMockBuilder('Drupal\\Core\\RouteProcessor\\RouteProcessorManager')->disableOriginalConstructor()->getMock(); $config_factory_stub = $this->getConfigFactoryStub(array('system.filter' => array('protocols' => array('http', 'https')))); $requestStack = new RequestStack(); $requestStack->push($request); $generator = new UrlGenerator($provider, $processor_manager, $this->routeProcessorManager, $config_factory_stub, new Settings(array()), NULL, $requestStack); $generator->setContext($context); $this->generator = $generator; // Second generator for mixed-mode sessions. $generator = new UrlGenerator($provider, $processor_manager, $this->routeProcessorManager, $config_factory_stub, new Settings(array('mixed_mode_sessions' => TRUE)), NULL, $requestStack); $generator->setContext($context); $this->generatorMixedMode = $generator; }
/** * Tests resolving the inbound path to the system path. */ function testProcessInbound() { // Create an alias manager stub. $alias_manager = $this->getMockBuilder('Drupal\\Core\\Path\\AliasManager')->disableOriginalConstructor()->getMock(); $system_path_map = array(array('/foo', NULL, '/user/1'), array('/fr/foo', NULL, '/fr/foo'), array('/fr', NULL, '/fr'), array('/user/login', NULL, '/user/login')); $alias_manager->expects($this->any())->method('getPathByAlias')->will($this->returnValueMap($system_path_map)); // Create a stub config factory with all config settings that will be checked // during this test. $config_factory_stub = $this->getConfigFactoryStub(array('system.site' => array('page.front' => '/user/login'), 'language.negotiation' => array('url' => array('prefixes' => array('fr' => 'fr'))))); // Create a language negotiator stub. $negotiator = $this->getMockBuilder('Drupal\\language\\LanguageNegotiatorInterface')->getMock(); $negotiator->expects($this->any())->method('getNegotiationMethods')->will($this->returnValue(array(LanguageNegotiationUrl::METHOD_ID => array('class' => 'Drupal\\language\\Plugin\\LanguageNegotiation\\LanguageNegotiationUrl')))); $method = new LanguageNegotiationUrl(); $method->setConfig($config_factory_stub); $method->setLanguageManager($this->languageManager); $negotiator->expects($this->any())->method('getNegotiationMethodInstance')->will($this->returnValue($method)); // Create a user stub. $current_user = $this->getMockBuilder('Drupal\\Core\\Session\\AccountInterface')->getMock(); // Create the processors. $alias_processor = new PathProcessorAlias($alias_manager); $decode_processor = new PathProcessorDecode(); $front_processor = new PathProcessorFront($config_factory_stub); $language_processor = new PathProcessorLanguage($config_factory_stub, $this->languageManager, $negotiator, $current_user); // First, test the processor manager with the processors in the incorrect // order. The alias processor will run before the language processor, meaning // aliases will not be found. $priorities = array(1000 => $alias_processor, 500 => $decode_processor, 300 => $front_processor, 200 => $language_processor); // Create the processor manager and add the processors. $processor_manager = new PathProcessorManager(); foreach ($priorities as $priority => $processor) { $processor_manager->addInbound($processor, $priority); } // Test resolving the French homepage using the incorrect processor order. $test_path = '/fr'; $request = Request::create($test_path); $processed = $processor_manager->processInbound($test_path, $request); $this->assertEquals('/', $processed, 'Processing in the incorrect order fails to resolve the system path from the empty path'); // Test resolving an existing alias using the incorrect processor order. $test_path = '/fr/foo'; $request = Request::create($test_path); $processed = $processor_manager->processInbound($test_path, $request); $this->assertEquals('/foo', $processed, 'Processing in the incorrect order fails to resolve the system path from an alias'); // Now create a new processor manager and add the processors, this time in // the correct order. $processor_manager = new PathProcessorManager(); $priorities = array(1000 => $decode_processor, 500 => $language_processor, 300 => $front_processor, 200 => $alias_processor); foreach ($priorities as $priority => $processor) { $processor_manager->addInbound($processor, $priority); } // Test resolving the French homepage using the correct processor order. $test_path = '/fr'; $request = Request::create($test_path); $processed = $processor_manager->processInbound($test_path, $request); $this->assertEquals('/user/login', $processed, 'Processing in the correct order resolves the system path from the empty path.'); // Test resolving an existing alias using the correct processor order. $test_path = '/fr/foo'; $request = Request::create($test_path); $processed = $processor_manager->processInbound($test_path, $request); $this->assertEquals('/user/1', $processed, 'Processing in the correct order resolves the system path from an alias.'); }
/** * {@inheritdoc} */ protected function setUp() { $cache_contexts_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')->disableOriginalConstructor()->getMock(); $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE); $container = new ContainerBuilder(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container); $routes = new RouteCollection(); $first_route = new Route('/test/one'); $second_route = new Route('/test/two/{narf}'); $third_route = new Route('/test/two/'); $fourth_route = new Route('/test/four', [], [], [], '', ['https']); $none_route = new Route('', [], [], ['_no_path' => TRUE]); $routes->add('test_1', $first_route); $routes->add('test_2', $second_route); $routes->add('test_3', $third_route); $routes->add('test_4', $fourth_route); $routes->add('<none>', $none_route); // Create a route provider stub. $provider = $this->getMockBuilder('Drupal\\Core\\Routing\\RouteProvider')->disableOriginalConstructor()->getMock(); // We need to set up return value maps for both the getRouteByName() and the // getRoutesByNames() method calls on the route provider. The parameters // are not passed in and default to an empty array. $route_name_return_map = $routes_names_return_map = array(); $return_map_values = array(['route_name' => 'test_1', 'return' => $first_route], ['route_name' => 'test_2', 'return' => $second_route], ['route_name' => 'test_3', 'return' => $third_route], ['route_name' => 'test_4', 'return' => $fourth_route], ['route_name' => '<none>', 'return' => $none_route]); foreach ($return_map_values as $values) { $route_name_return_map[] = array($values['route_name'], $values['return']); $routes_names_return_map[] = array(array($values['route_name']), $values['return']); } $provider->expects($this->any())->method('getRouteByName')->will($this->returnValueMap($route_name_return_map)); $provider->expects($this->any())->method('getRoutesByNames')->will($this->returnValueMap($routes_names_return_map)); // Create an alias manager stub. $alias_manager = $this->getMockBuilder('Drupal\\Core\\Path\\AliasManager')->disableOriginalConstructor()->getMock(); $alias_manager->expects($this->any())->method('getAliasByPath')->will($this->returnCallback(array($this, 'aliasManagerCallback'))); $this->aliasManager = $alias_manager; $this->requestStack = new RequestStack(); $request = Request::create('/some/path'); $this->requestStack->push($request); $context = new RequestContext(); $context->fromRequestStack($this->requestStack); $processor = new PathProcessorAlias($this->aliasManager); $processor_manager = new PathProcessorManager(); $processor_manager->addOutbound($processor, 1000); $this->routeProcessorManager = $this->getMockBuilder('Drupal\\Core\\RouteProcessor\\RouteProcessorManager')->disableOriginalConstructor()->getMock(); $generator = new UrlGenerator($provider, $processor_manager, $this->routeProcessorManager, $this->requestStack, ['http', 'https']); $generator->setContext($context); $this->generator = $generator; }