7021aa0027fafbd2140a3b1349ee5695eae9d073
[yaffs-website] / web / core / modules / language / tests / src / Functional / LanguageUrlRewritingTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Functional;
4
5 use Drupal\Core\Language\Language;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Url;
8 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
9 use Drupal\Tests\BrowserTestBase;
10 use Symfony\Component\HttpFoundation\Request;
11
12 /**
13  * Tests that URL rewriting works as expected.
14  *
15  * @group language
16  */
17 class LanguageUrlRewritingTest extends BrowserTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['language', 'language_test'];
25
26   /**
27    * An user with permissions to administer languages.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $webUser;
32
33   protected function setUp() {
34     parent::setUp();
35
36     // Create and log in user.
37     $this->webUser = $this->drupalCreateUser(['administer languages', 'access administration pages']);
38     $this->drupalLogin($this->webUser);
39
40     // Install French language.
41     $edit = [];
42     $edit['predefined_langcode'] = 'fr';
43     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
44
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'));
48
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.');
52   }
53
54   /**
55    * Check that non-installed languages are not considered.
56    */
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');
61
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');
65   }
66
67   /**
68    * Check URL rewriting for the given language.
69    *
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.
73    *
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.
80    */
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;
88
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;
95
96     $this->drupalGet("$prefix/$path");
97     $this->assertResponse(404, $message2);
98   }
99
100   /**
101    * Check URL rewriting when using a domain name and a non-standard port.
102    */
103   public function testDomainNameNegotiationPort() {
104     global $base_url;
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);
108     $edit = [
109       'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
110       'domain[en]' => $base_url_host,
111       'domain[fr]' => $language_domain
112     ];
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();
117
118     // Enable domain configuration.
119     $this->config('language.negotiation')
120       ->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN)
121       ->save();
122
123     // Reset static caching.
124     $this->container->get('language_manager')->reset();
125
126     // In case index.php is part of the URLs, we need to adapt the asserted
127     // URLs as well.
128     $index_php = strpos(\Drupal::url('<front>', [], ['absolute' => TRUE]), 'index.php') !== FALSE;
129
130     $request = Request::createFromGlobals();
131     $server = $request->server->all();
132     $request = $this->prepareRequestForGenerator(TRUE, ['HTTP_HOST' => $server['HTTP_HOST'] . ':88']);
133
134     // Create an absolute French link.
135     $language = \Drupal::languageManager()->getLanguage('fr');
136     $url = Url::fromRoute('<front>', [], [
137       'absolute' => TRUE,
138       'language' => $language,
139     ])->toString();
140
141     $expected = ($index_php ? 'http://example.fr:88/index.php' : 'http://example.fr:88') . rtrim(base_path(), '/') . '/';
142
143     $this->assertEqual($url, $expected, 'The right port is used.');
144
145     // If we set the port explicitly, it should not be overridden.
146     $url = Url::fromRoute('<front>', [], [
147       'absolute' => TRUE,
148       'language' => $language,
149       'base_url' => $request->getBaseUrl() . ':90',
150     ])->toString();
151
152     $expected = $index_php ? 'http://example.fr:90/index.php' : 'http://example.fr:90' . rtrim(base_path(), '/') . '/';
153
154     $this->assertEqual($url, $expected, 'A given port is not overridden.');
155
156   }
157
158 }