3 namespace Drupal\Tests\language\Functional;
5 use Drupal\Core\Language\Language;
6 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
9 use Drupal\Tests\BrowserTestBase;
10 use Symfony\Component\HttpFoundation\Request;
13 * Tests that URL rewriting works as expected.
17 class LanguageUrlRewritingTest extends BrowserTestBase {
24 public static $modules = ['language', 'language_test'];
27 * An user with permissions to administer languages.
29 * @var \Drupal\user\UserInterface
33 protected function setUp() {
36 // Create and log in user.
37 $this->webUser = $this->drupalCreateUser(['administer languages', 'access administration pages']);
38 $this->drupalLogin($this->webUser);
40 // Install French language.
42 $edit['predefined_langcode'] = 'fr';
43 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
45 // Enable URL language detection and selection.
46 $edit = ['language_interface[enabled][language-url]' => 1];
47 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
49 // Check that drupalSettings contains path prefix.
50 $this->drupalGet('fr/admin/config/regional/language/detection');
51 $this->assertRaw('"pathPrefix":"fr\/"', 'drupalSettings path prefix contains language code.');
55 * Check that non-installed languages are not considered.
57 public function testUrlRewritingEdgeCases() {
58 // Check URL rewriting with a non-installed language.
59 $non_existing = new Language(['id' => $this->randomMachineName()]);
60 $this->checkUrl($non_existing, 'Path language is ignored if language is not installed.', 'URL language negotiation does not work with non-installed languages');
62 // Check that URL rewriting is not applied to subrequests.
63 $this->drupalGet('language_test/subrequest');
64 $this->assertText($this->webUser->getUsername(), 'Page correctly retrieved');
68 * Check URL rewriting for the given language.
70 * The test is performed with a fixed URL (the default front page) to simply
71 * check that language prefixes are not added to it and that the prefixed URL
72 * is actually not working.
74 * @param \Drupal\Core\Language\LanguageInterface $language
75 * The language object.
76 * @param string $message1
77 * Message to display in assertion that language prefixes are not added.
78 * @param string $message2
79 * The message to display confirming prefixed URL is not working.
81 private function checkUrl(LanguageInterface $language, $message1, $message2) {
82 $options = ['language' => $language, 'script' => ''];
83 $base_path = trim(base_path(), '/');
84 $rewritten_path = trim(str_replace($base_path, '', \Drupal::url('<front>', [], $options)), '/');
85 $segments = explode('/', $rewritten_path, 2);
86 $prefix = $segments[0];
87 $path = isset($segments[1]) ? $segments[1] : $prefix;
89 // If the rewritten URL has not a language prefix we pick a random prefix so
90 // we can always check the prefixed URL.
91 $prefixes = language_negotiation_url_prefixes();
92 $stored_prefix = isset($prefixes[$language->getId()]) ? $prefixes[$language->getId()] : $this->randomMachineName();
93 $this->assertNotEqual($stored_prefix, $prefix, $message1);
94 $prefix = $stored_prefix;
96 $this->drupalGet("$prefix/$path");
97 $this->assertResponse(404, $message2);
101 * Check URL rewriting when using a domain name and a non-standard port.
103 public function testDomainNameNegotiationPort() {
105 $language_domain = 'example.fr';
106 // Get the current host URI we're running on.
107 $base_url_host = parse_url($base_url, PHP_URL_HOST);
109 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
110 'domain[en]' => $base_url_host,
111 'domain[fr]' => $language_domain
113 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
114 // Rebuild the container so that the new language gets picked up by services
115 // that hold the list of languages.
116 $this->rebuildContainer();
118 // Enable domain configuration.
119 $this->config('language.negotiation')
120 ->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN)
123 // Reset static caching.
124 $this->container->get('language_manager')->reset();
126 // In case index.php is part of the URLs, we need to adapt the asserted
128 $index_php = strpos(\Drupal::url('<front>', [], ['absolute' => TRUE]), 'index.php') !== FALSE;
130 $request = Request::createFromGlobals();
131 $server = $request->server->all();
132 $request = $this->prepareRequestForGenerator(TRUE, ['HTTP_HOST' => $server['HTTP_HOST'] . ':88']);
134 // Create an absolute French link.
135 $language = \Drupal::languageManager()->getLanguage('fr');
136 $url = Url::fromRoute('<front>', [], [
138 'language' => $language,
141 $expected = ($index_php ? 'http://example.fr:88/index.php' : 'http://example.fr:88') . rtrim(base_path(), '/') . '/';
143 $this->assertEqual($url, $expected, 'The right port is used.');
145 // If we set the port explicitly, it should not be overridden.
146 $url = Url::fromRoute('<front>', [], [
148 'language' => $language,
149 'base_url' => $request->getBaseUrl() . ':90',
152 $expected = $index_php ? 'http://example.fr:90/index.php' : 'http://example.fr:90' . rtrim(base_path(), '/') . '/';
154 $this->assertEqual($url, $expected, 'A given port is not overridden.');