/** * 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.'); }
/** * Test domain language negotiation. * * @dataProvider providerTestDomain */ public function testDomain($http_host, $domains, $expected_langcode) { $config_data = array('source' => LanguageNegotiationUrl::CONFIG_DOMAIN, 'domains' => $domains); $config_object = $this->getMockBuilder('Drupal\\Core\\Config\\Config')->disableOriginalConstructor()->getMock(); $config_object->expects($this->any())->method('get')->with('url')->will($this->returnValue($config_data)); $config = $this->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface'); $config->expects($this->any())->method('get')->with('language.negotiation')->will($this->returnValue($config_object)); $request = Request::create('', 'GET', array(), array(), array(), array('HTTP_HOST' => $http_host)); $method = new LanguageNegotiationUrl(); $method->setLanguageManager($this->languageManager); $method->setConfig($config); $method->setCurrentUser($this->user); $this->assertEquals($expected_langcode, $method->getLangcode($request)); }
/** * Test domain language negotiation and outbound path processing. * * @dataProvider providerTestDomain */ public function testDomain($http_host, $domains, $expected_langcode) { $this->languageManager->expects($this->any())->method('getCurrentLanguage')->will($this->returnValue($this->languages['en'])); $config = $this->getConfigFactoryStub(['language.negotiation' => ['url' => ['source' => LanguageNegotiationUrl::CONFIG_DOMAIN, 'domains' => $domains]]]); $request = Request::create('', 'GET', array(), array(), array(), array('HTTP_HOST' => $http_host)); $method = new LanguageNegotiationUrl(); $method->setLanguageManager($this->languageManager); $method->setConfig($config); $method->setCurrentUser($this->user); $this->assertEquals($expected_langcode, $method->getLangcode($request)); $cacheability = new BubbleableMetadata(); $options = []; $this->assertSame('foo', $method->processOutbound('foo', $options, $request, $cacheability)); $expected_cacheability = new BubbleableMetadata(); if ($expected_langcode !== FALSE && count($domains) > 1) { $expected_cacheability->setCacheMaxAge(Cache::PERMANENT)->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL, 'url.site']); } $this->assertEquals($expected_cacheability, $cacheability); }