Chris@0: webUser = $this->drupalCreateUser(['administer languages', 'access administration pages']); Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: // Install French language. Chris@0: $edit = []; Chris@0: $edit['predefined_langcode'] = 'fr'; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@0: Chris@0: // Enable URL language detection and selection. Chris@0: $edit = ['language_interface[enabled][language-url]' => 1]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Check that drupalSettings contains path prefix. Chris@0: $this->drupalGet('fr/admin/config/regional/language/detection'); Chris@0: $this->assertRaw('"pathPrefix":"fr\/"', 'drupalSettings path prefix contains language code.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check that non-installed languages are not considered. Chris@0: */ Chris@0: public function testUrlRewritingEdgeCases() { Chris@0: // Check URL rewriting with a non-installed language. Chris@0: $non_existing = new Language(['id' => $this->randomMachineName()]); Chris@0: $this->checkUrl($non_existing, 'Path language is ignored if language is not installed.', 'URL language negotiation does not work with non-installed languages'); Chris@0: Chris@0: // Check that URL rewriting is not applied to subrequests. Chris@0: $this->drupalGet('language_test/subrequest'); Chris@18: $this->assertText($this->webUser->getAccountName(), 'Page correctly retrieved'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check URL rewriting for the given language. Chris@0: * Chris@0: * The test is performed with a fixed URL (the default front page) to simply Chris@0: * check that language prefixes are not added to it and that the prefixed URL Chris@0: * is actually not working. Chris@0: * Chris@0: * @param \Drupal\Core\Language\LanguageInterface $language Chris@0: * The language object. Chris@0: * @param string $message1 Chris@0: * Message to display in assertion that language prefixes are not added. Chris@0: * @param string $message2 Chris@0: * The message to display confirming prefixed URL is not working. Chris@0: */ Chris@0: private function checkUrl(LanguageInterface $language, $message1, $message2) { Chris@0: $options = ['language' => $language, 'script' => '']; Chris@0: $base_path = trim(base_path(), '/'); Chris@18: $rewritten_path = trim(str_replace($base_path, '', Url::fromRoute('', [], $options)->toString()), '/'); Chris@0: $segments = explode('/', $rewritten_path, 2); Chris@0: $prefix = $segments[0]; Chris@0: $path = isset($segments[1]) ? $segments[1] : $prefix; Chris@0: Chris@0: // If the rewritten URL has not a language prefix we pick a random prefix so Chris@0: // we can always check the prefixed URL. Chris@0: $prefixes = language_negotiation_url_prefixes(); Chris@0: $stored_prefix = isset($prefixes[$language->getId()]) ? $prefixes[$language->getId()] : $this->randomMachineName(); Chris@0: $this->assertNotEqual($stored_prefix, $prefix, $message1); Chris@0: $prefix = $stored_prefix; Chris@0: Chris@0: $this->drupalGet("$prefix/$path"); Chris@0: $this->assertResponse(404, $message2); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check URL rewriting when using a domain name and a non-standard port. Chris@0: */ Chris@0: public function testDomainNameNegotiationPort() { Chris@0: global $base_url; Chris@0: $language_domain = 'example.fr'; Chris@0: // Get the current host URI we're running on. Chris@0: $base_url_host = parse_url($base_url, PHP_URL_HOST); Chris@0: $edit = [ Chris@0: 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN, Chris@0: 'domain[en]' => $base_url_host, Chris@17: 'domain[fr]' => $language_domain, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: // Rebuild the container so that the new language gets picked up by services Chris@0: // that hold the list of languages. Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: // Enable domain configuration. Chris@0: $this->config('language.negotiation') Chris@0: ->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN) Chris@0: ->save(); Chris@0: Chris@0: // Reset static caching. Chris@0: $this->container->get('language_manager')->reset(); Chris@0: Chris@0: // In case index.php is part of the URLs, we need to adapt the asserted Chris@0: // URLs as well. Chris@18: $index_php = strpos(Url::fromRoute('', [], ['absolute' => TRUE])->toString(), 'index.php') !== FALSE; Chris@0: Chris@0: $request = Request::createFromGlobals(); Chris@0: $server = $request->server->all(); Chris@0: $request = $this->prepareRequestForGenerator(TRUE, ['HTTP_HOST' => $server['HTTP_HOST'] . ':88']); Chris@0: Chris@0: // Create an absolute French link. Chris@0: $language = \Drupal::languageManager()->getLanguage('fr'); Chris@0: $url = Url::fromRoute('', [], [ Chris@0: 'absolute' => TRUE, Chris@0: 'language' => $language, Chris@0: ])->toString(); Chris@0: Chris@0: $expected = ($index_php ? 'http://example.fr:88/index.php' : 'http://example.fr:88') . rtrim(base_path(), '/') . '/'; Chris@0: Chris@0: $this->assertEqual($url, $expected, 'The right port is used.'); Chris@0: Chris@0: // If we set the port explicitly, it should not be overridden. Chris@0: $url = Url::fromRoute('', [], [ Chris@0: 'absolute' => TRUE, Chris@0: 'language' => $language, Chris@0: 'base_url' => $request->getBaseUrl() . ':90', Chris@0: ])->toString(); Chris@0: Chris@0: $expected = $index_php ? 'http://example.fr:90/index.php' : 'http://example.fr:90' . rtrim(base_path(), '/') . '/'; Chris@0: Chris@0: $this->assertEqual($url, $expected, 'A given port is not overridden.'); Chris@0: Chris@0: } Chris@0: Chris@0: }