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