Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / language / src / Tests / LanguageSwitchingTest.php
1 <?php
2
3 namespace Drupal\language\Tests;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
7 use Drupal\menu_link_content\Entity\MenuLinkContent;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\simpletest\WebTestBase;
10
11 /**
12  * Functional tests for the language switching feature.
13  *
14  * @group language
15  */
16 class LanguageSwitchingTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['locale', 'locale_test', 'language', 'block', 'language_test', 'menu_ui'];
24
25   protected function setUp() {
26     parent::setUp();
27
28     // Create and log in user.
29     $admin_user = $this->drupalCreateUser(['administer blocks', 'administer languages', 'access administration pages']);
30     $this->drupalLogin($admin_user);
31   }
32
33   /**
34    * Functional tests for the language switcher block.
35    */
36   public function testLanguageBlock() {
37     // Add language.
38     $edit = [
39       'predefined_langcode' => 'fr',
40     ];
41     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
42
43     // Set the native language name.
44     $this->saveNativeLanguageName('fr', 'français');
45
46     // Enable URL language detection and selection.
47     $edit = ['language_interface[enabled][language-url]' => '1'];
48     $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
49
50     // Enable the language switching block.
51     $block = $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
52       'id' => 'test_language_block',
53       // Ensure a 2-byte UTF-8 sequence is in the tested output.
54       'label' => $this->randomMachineName(8) . '×',
55     ]);
56
57     $this->doTestLanguageBlockAuthenticated($block->label());
58     $this->doTestLanguageBlockAnonymous($block->label());
59   }
60
61   /**
62    * For authenticated users, the "active" class is set by JavaScript.
63    *
64    * @param string $block_label
65    *   The label of the language switching block.
66    *
67    * @see testLanguageBlock()
68    */
69   protected function doTestLanguageBlockAuthenticated($block_label) {
70     // Assert that the language switching block is displayed on the frontpage.
71     $this->drupalGet('');
72     $this->assertText($block_label, 'Language switcher block found.');
73
74     // Assert that each list item and anchor element has the appropriate data-
75     // attributes.
76     list($language_switcher) = $this->xpath('//div[@id=:id]', [':id' => 'block-test-language-block']);
77     $list_items = [];
78     $anchors = [];
79     $labels = [];
80     foreach ($language_switcher->ul->li as $list_item) {
81       $classes = explode(" ", (string) $list_item['class']);
82       list($langcode) = array_intersect($classes, ['en', 'fr']);
83       $list_items[] = [
84         'langcode_class' => $langcode,
85         'data-drupal-link-system-path' => (string) $list_item['data-drupal-link-system-path'],
86       ];
87       $anchors[] = [
88         'hreflang' => (string) $list_item->a['hreflang'],
89         'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'],
90       ];
91       $labels[] = (string) $list_item->a;
92     }
93     $expected_list_items = [
94       0 => ['langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'],
95       1 => ['langcode_class' => 'fr', 'data-drupal-link-system-path' => 'user/2'],
96     ];
97     $this->assertIdentical($list_items, $expected_list_items, 'The list items have the correct attributes that will allow the drupal.active-link library to mark them as active.');
98     $expected_anchors = [
99       0 => ['hreflang' => 'en', 'data-drupal-link-system-path' => 'user/2'],
100       1 => ['hreflang' => 'fr', 'data-drupal-link-system-path' => 'user/2'],
101     ];
102     $this->assertIdentical($anchors, $expected_anchors, 'The anchors have the correct attributes that will allow the drupal.active-link library to mark them as active.');
103     $settings = $this->getDrupalSettings();
104     $this->assertIdentical($settings['path']['currentPath'], 'user/2', 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.');
105     $this->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.');
106     $this->assertIdentical($settings['path']['currentLanguage'], 'en', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
107     $this->assertIdentical($labels, ['English', 'français'], 'The language links labels are in their own language on the language switcher block.');
108   }
109
110   /**
111    * For anonymous users, the "active" class is set by PHP.
112    *
113    * @param string $block_label
114    *   The label of the language switching block.
115    *
116    * @see testLanguageBlock()
117    */
118   protected function doTestLanguageBlockAnonymous($block_label) {
119     $this->drupalLogout();
120
121     // Assert that the language switching block is displayed on the frontpage
122     // and ensure that the active class is added when query params are present.
123     $this->drupalGet('', ['query' => ['foo' => 'bar']]);
124     $this->assertText($block_label, 'Language switcher block found.');
125
126     // Assert that only the current language is marked as active.
127     list($language_switcher) = $this->xpath('//div[@id=:id]', [':id' => 'block-test-language-block']);
128     $links = [
129       'active' => [],
130       'inactive' => [],
131     ];
132     $anchors = [
133       'active' => [],
134       'inactive' => [],
135     ];
136     $labels = [];
137     foreach ($language_switcher->ul->li as $link) {
138       $classes = explode(" ", (string) $link['class']);
139       list($langcode) = array_intersect($classes, ['en', 'fr']);
140       if (in_array('is-active', $classes)) {
141         $links['active'][] = $langcode;
142       }
143       else {
144         $links['inactive'][] = $langcode;
145       }
146       $anchor_classes = explode(" ", (string) $link->a['class']);
147       if (in_array('is-active', $anchor_classes)) {
148         $anchors['active'][] = $langcode;
149       }
150       else {
151         $anchors['inactive'][] = $langcode;
152       }
153       $labels[] = (string) $link->a;
154     }
155     $this->assertIdentical($links, ['active' => ['en'], 'inactive' => ['fr']], 'Only the current language list item is marked as active on the language switcher block.');
156     $this->assertIdentical($anchors, ['active' => ['en'], 'inactive' => ['fr']], 'Only the current language anchor is marked as active on the language switcher block.');
157     $this->assertIdentical($labels, ['English', 'français'], 'The language links labels are in their own language on the language switcher block.');
158   }
159
160   /**
161    * Test language switcher links for domain based negotiation.
162    */
163   public function testLanguageBlockWithDomain() {
164     // Add the Italian language.
165     ConfigurableLanguage::createFromLangcode('it')->save();
166
167     // Rebuild the container so that the new language is picked up by services
168     // that hold a list of languages.
169     $this->rebuildContainer();
170
171     $languages = $this->container->get('language_manager')->getLanguages();
172
173     // Enable browser and URL language detection.
174     $edit = [
175       'language_interface[enabled][language-url]' => TRUE,
176       'language_interface[weight][language-url]' => -10,
177     ];
178     $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
179
180     // Do not allow blank domain.
181     $edit = [
182       'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
183       'domain[en]' => '',
184     ];
185     $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
186     $this->assertText(t('The domain may not be left blank for English'), 'The form does not allow blank domains.');
187
188     // Change the domain for the Italian language.
189     $edit = [
190       'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
191       'domain[en]' => \Drupal::request()->getHost(),
192       'domain[it]' => 'it.example.com',
193     ];
194     $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
195     $this->assertText(t('The configuration options have been saved'), 'Domain configuration is saved.');
196
197     // Enable the language switcher block.
198     $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, ['id' => 'test_language_block']);
199
200     $this->drupalGet('');
201
202     /** @var \Drupal\Core\Routing\UrlGenerator $generator */
203     $generator = $this->container->get('url_generator');
204
205     // Verify the English URL is correct
206     list($english_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [
207       ':id' => 'block-test-language-block',
208       ':hreflang' => 'en',
209     ]);
210     $english_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['en']]);
211     $this->assertEqual($english_url, (string) $english_link['href']);
212
213     // Verify the Italian URL is correct
214     list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [
215       ':id' => 'block-test-language-block',
216       ':hreflang' => 'it',
217     ]);
218     $italian_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['it']]);
219     $this->assertEqual($italian_url, (string) $italian_link['href']);
220   }
221
222   /**
223    * Test active class on links when switching languages.
224    */
225   public function testLanguageLinkActiveClass() {
226     // Add language.
227     $edit = [
228       'predefined_langcode' => 'fr',
229     ];
230     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
231
232     // Enable URL language detection and selection.
233     $edit = ['language_interface[enabled][language-url]' => '1'];
234     $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
235
236     $this->doTestLanguageLinkActiveClassAuthenticated();
237     $this->doTestLanguageLinkActiveClassAnonymous();
238   }
239
240   /**
241    * Check the path-admin class, as same as on default language.
242    */
243   public function testLanguageBodyClass() {
244     $searched_class = 'path-admin';
245
246     // Add language.
247     $edit = [
248       'predefined_langcode' => 'fr',
249     ];
250     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
251
252     // Enable URL language detection and selection.
253     $edit = ['language_interface[enabled][language-url]' => '1'];
254     $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
255
256     // Check if the default (English) admin/config page has the right class.
257     $this->drupalGet('admin/config');
258     $class = $this->xpath('//body[contains(@class, :class)]', [':class' => $searched_class]);
259     $this->assertTrue(isset($class[0]), t('The path-admin class appears on default language.'));
260
261     // Check if the French admin/config page has the right class.
262     $this->drupalGet('fr/admin/config');
263     $class = $this->xpath('//body[contains(@class, :class)]', [':class' => $searched_class]);
264     $this->assertTrue(isset($class[0]), t('The path-admin class same as on default language.'));
265
266     // The testing profile sets the user/login page as the frontpage. That
267     // redirects authenticated users to their profile page, so check with an
268     // anonymous user instead.
269     $this->drupalLogout();
270
271     // Check if the default (English) frontpage has the right class.
272     $this->drupalGet('<front>');
273     $class = $this->xpath('//body[contains(@class, :class)]', [':class' => 'path-frontpage']);
274     $this->assertTrue(isset($class[0]), 'path-frontpage class found on the body tag');
275
276     // Check if the French frontpage has the right class.
277     $this->drupalGet('fr');
278     $class = $this->xpath('//body[contains(@class, :class)]', [':class' => 'path-frontpage']);
279     $this->assertTrue(isset($class[0]), 'path-frontpage class found on the body tag with french as the active language');
280
281   }
282
283   /**
284    * For authenticated users, the "active" class is set by JavaScript.
285    *
286    * @see testLanguageLinkActiveClass()
287    */
288   protected function doTestLanguageLinkActiveClassAuthenticated() {
289     $function_name = '#type link';
290     $path = 'language_test/type-link-active-class';
291
292     // Test links generated by the link generator on an English page.
293     $current_language = 'English';
294     $this->drupalGet($path);
295
296     // Language code 'none' link should be active.
297     $langcode = 'none';
298     $links = $this->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', [':id' => 'no_lang_link', ':path' => $path]);
299     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
300
301     // Language code 'en' link should be active.
302     $langcode = 'en';
303     $links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'en_link', ':lang' => 'en', ':path' => $path]);
304     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
305
306     // Language code 'fr' link should not be active.
307     $langcode = 'fr';
308     $links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'fr_link', ':lang' => 'fr', ':path' => $path]);
309     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to NOT mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
310
311     // Verify that drupalSettings contains the correct values.
312     $settings = $this->getDrupalSettings();
313     $this->assertIdentical($settings['path']['currentPath'], $path, 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.');
314     $this->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.');
315     $this->assertIdentical($settings['path']['currentLanguage'], 'en', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
316
317     // Test links generated by the link generator on a French page.
318     $current_language = 'French';
319     $this->drupalGet('fr/language_test/type-link-active-class');
320
321     // Language code 'none' link should be active.
322     $langcode = 'none';
323     $links = $this->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', [':id' => 'no_lang_link', ':path' => $path]);
324     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
325
326     // Language code 'en' link should not be active.
327     $langcode = 'en';
328     $links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'en_link', ':lang' => 'en', ':path' => $path]);
329     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to NOT mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
330
331     // Language code 'fr' link should be active.
332     $langcode = 'fr';
333     $links = $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', [':id' => 'fr_link', ':lang' => 'fr', ':path' => $path]);
334     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode has the correct attributes that will allow the drupal.active-link library to mark it as active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
335
336     // Verify that drupalSettings contains the correct values.
337     $settings = $this->getDrupalSettings();
338     $this->assertIdentical($settings['path']['currentPath'], $path, 'drupalSettings.path.currentPath is set correctly to allow drupal.active-link to mark the correct links as active.');
339     $this->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.');
340     $this->assertIdentical($settings['path']['currentLanguage'], 'fr', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.');
341   }
342
343   /**
344    * For anonymous users, the "active" class is set by PHP.
345    *
346    * @see testLanguageLinkActiveClass()
347    */
348   protected function doTestLanguageLinkActiveClassAnonymous() {
349     $function_name = '#type link';
350
351     $this->drupalLogout();
352
353     // Test links generated by the link generator on an English page.
354     $current_language = 'English';
355     $this->drupalGet('language_test/type-link-active-class');
356
357     // Language code 'none' link should be active.
358     $langcode = 'none';
359     $links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'no_lang_link', ':class' => 'is-active']);
360     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is marked active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
361
362     // Language code 'en' link should be active.
363     $langcode = 'en';
364     $links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'en_link', ':class' => 'is-active']);
365     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is marked active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
366
367     // Language code 'fr' link should not be active.
368     $langcode = 'fr';
369     $links = $this->xpath('//a[@id = :id and not(contains(@class, :class))]', [':id' => 'fr_link', ':class' => 'is-active']);
370     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is NOT marked active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
371
372     // Test links generated by the link generator on a French page.
373     $current_language = 'French';
374     $this->drupalGet('fr/language_test/type-link-active-class');
375
376     // Language code 'none' link should be active.
377     $langcode = 'none';
378     $links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'no_lang_link', ':class' => 'is-active']);
379     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is marked active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
380
381     // Language code 'en' link should not be active.
382     $langcode = 'en';
383     $links = $this->xpath('//a[@id = :id and not(contains(@class, :class))]', [':id' => 'en_link', ':class' => 'is-active']);
384     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is NOT marked active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
385
386     // Language code 'fr' link should be active.
387     $langcode = 'fr';
388     $links = $this->xpath('//a[@id = :id and contains(@class, :class)]', [':id' => 'fr_link', ':class' => 'is-active']);
389     $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is marked active.', [':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode]));
390   }
391
392   /**
393    * Tests language switcher links for session based negotiation.
394    */
395   public function testLanguageSessionSwitchLinks() {
396     // Add language.
397     $edit = [
398       'predefined_langcode' => 'fr',
399     ];
400     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
401
402     // Enable session language detection and selection.
403     $edit = [
404       'language_interface[enabled][language-url]' => FALSE,
405       'language_interface[enabled][language-session]' => TRUE,
406     ];
407     $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
408
409     // Enable the language switching block.
410     $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
411       'id' => 'test_language_block',
412     ]);
413
414     // Enable the main menu block.
415     $this->drupalPlaceBlock('system_menu_block:main', [
416       'id' => 'test_menu',
417     ]);
418
419     // Add a link to the homepage.
420     $link = MenuLinkContent::create([
421       'title' => 'Home',
422       'menu_name' => 'main',
423       'bundle' => 'menu_link_content',
424       'link' => [['uri' => 'entity:user/2']],
425     ]);
426     $link->save();
427
428     // Go to the homepage.
429     $this->drupalGet('');
430     // Click on the French link.
431     $this->clickLink(t('French'));
432     // There should be a query parameter to set the session language.
433     $this->assertUrl('user/2', ['query' => ['language' => 'fr']]);
434     // Click on the 'Home' Link.
435     $this->clickLink(t('Home'));
436     // There should be no query parameter.
437     $this->assertUrl('user/2');
438     // Click on the French link.
439     $this->clickLink(t('French'));
440     // There should be no query parameter.
441     $this->assertUrl('user/2');
442   }
443
444   /**
445    * Saves the native name of a language entity in configuration as a label.
446    *
447    * @param string $langcode
448    *   The language code of the language.
449    * @param string $label
450    *   The native name of the language.
451    */
452   protected function saveNativeLanguageName($langcode, $label) {
453     \Drupal::service('language.config_factory_override')
454       ->getOverride($langcode, 'language.entity.' . $langcode)->set('label', $label)->save();
455   }
456
457 }