3 namespace Drupal\language\Tests;
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;
12 * Functional tests for the language switching feature.
16 class LanguageSwitchingTest extends WebTestBase {
23 public static $modules = ['locale', 'locale_test', 'language', 'block', 'language_test', 'menu_ui'];
25 protected function setUp() {
28 // Create and log in user.
29 $admin_user = $this->drupalCreateUser(['administer blocks', 'administer languages', 'access administration pages']);
30 $this->drupalLogin($admin_user);
34 * Functional tests for the language switcher block.
36 public function testLanguageBlock() {
39 'predefined_langcode' => 'fr',
41 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
43 // Set the native language name.
44 $this->saveNativeLanguageName('fr', 'français');
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'));
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) . '×',
57 $this->doTestLanguageBlockAuthenticated($block->label());
58 $this->doTestLanguageBlockAnonymous($block->label());
62 * For authenticated users, the "active" class is set by JavaScript.
64 * @param string $block_label
65 * The label of the language switching block.
67 * @see testLanguageBlock()
69 protected function doTestLanguageBlockAuthenticated($block_label) {
70 // Assert that the language switching block is displayed on the frontpage.
72 $this->assertText($block_label, 'Language switcher block found.');
74 // Assert that each list item and anchor element has the appropriate data-
76 list($language_switcher) = $this->xpath('//div[@id=:id]', [':id' => 'block-test-language-block']);
80 foreach ($language_switcher->ul->li as $list_item) {
81 $classes = explode(" ", (string) $list_item['class']);
82 list($langcode) = array_intersect($classes, ['en', 'fr']);
84 'langcode_class' => $langcode,
85 'data-drupal-link-system-path' => (string) $list_item['data-drupal-link-system-path'],
88 'hreflang' => (string) $list_item->a['hreflang'],
89 'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'],
91 $labels[] = (string) $list_item->a;
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'],
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.');
99 0 => ['hreflang' => 'en', 'data-drupal-link-system-path' => 'user/2'],
100 1 => ['hreflang' => 'fr', 'data-drupal-link-system-path' => 'user/2'],
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.');
111 * For anonymous users, the "active" class is set by PHP.
113 * @param string $block_label
114 * The label of the language switching block.
116 * @see testLanguageBlock()
118 protected function doTestLanguageBlockAnonymous($block_label) {
119 $this->drupalLogout();
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.');
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']);
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;
144 $links['inactive'][] = $langcode;
146 $anchor_classes = explode(" ", (string) $link->a['class']);
147 if (in_array('is-active', $anchor_classes)) {
148 $anchors['active'][] = $langcode;
151 $anchors['inactive'][] = $langcode;
153 $labels[] = (string) $link->a;
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.');
161 * Test language switcher links for domain based negotiation.
163 public function testLanguageBlockWithDomain() {
164 // Add the Italian language.
165 ConfigurableLanguage::createFromLangcode('it')->save();
167 // Rebuild the container so that the new language is picked up by services
168 // that hold a list of languages.
169 $this->rebuildContainer();
171 $languages = $this->container->get('language_manager')->getLanguages();
173 // Enable browser and URL language detection.
175 'language_interface[enabled][language-url]' => TRUE,
176 'language_interface[weight][language-url]' => -10,
178 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
180 // Do not allow blank domain.
182 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
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.');
188 // Change the domain for the Italian language.
190 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
191 'domain[en]' => \Drupal::request()->getHost(),
192 'domain[it]' => 'it.example.com',
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.');
197 // Enable the language switcher block.
198 $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, ['id' => 'test_language_block']);
200 $this->drupalGet('');
202 /** @var \Drupal\Core\Routing\UrlGenerator $generator */
203 $generator = $this->container->get('url_generator');
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',
210 $english_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['en']]);
211 $this->assertEqual($english_url, (string) $english_link['href']);
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',
218 $italian_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['it']]);
219 $this->assertEqual($italian_url, (string) $italian_link['href']);
223 * Test active class on links when switching languages.
225 public function testLanguageLinkActiveClass() {
228 'predefined_langcode' => 'fr',
230 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
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'));
236 $this->doTestLanguageLinkActiveClassAuthenticated();
237 $this->doTestLanguageLinkActiveClassAnonymous();
241 * Check the path-admin class, as same as on default language.
243 public function testLanguageBodyClass() {
244 $searched_class = 'path-admin';
248 'predefined_langcode' => 'fr',
250 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
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'));
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.'));
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.'));
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();
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');
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');
284 * For authenticated users, the "active" class is set by JavaScript.
286 * @see testLanguageLinkActiveClass()
288 protected function doTestLanguageLinkActiveClassAuthenticated() {
289 $function_name = '#type link';
290 $path = 'language_test/type-link-active-class';
292 // Test links generated by the link generator on an English page.
293 $current_language = 'English';
294 $this->drupalGet($path);
296 // Language code 'none' link should be active.
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]));
301 // Language code 'en' link should be active.
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]));
306 // Language code 'fr' link should not be active.
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]));
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.');
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');
321 // Language code 'none' link should be active.
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]));
326 // Language code 'en' link should not be active.
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]));
331 // Language code 'fr' link should be active.
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]));
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.');
344 * For anonymous users, the "active" class is set by PHP.
346 * @see testLanguageLinkActiveClass()
348 protected function doTestLanguageLinkActiveClassAnonymous() {
349 $function_name = '#type link';
351 $this->drupalLogout();
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');
357 // Language code 'none' link should be active.
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]));
362 // Language code 'en' link should be active.
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]));
367 // Language code 'fr' link should not be active.
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]));
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');
376 // Language code 'none' link should be active.
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]));
381 // Language code 'en' link should not be active.
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]));
386 // Language code 'fr' link should be active.
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]));
393 * Tests language switcher links for session based negotiation.
395 public function testLanguageSessionSwitchLinks() {
398 'predefined_langcode' => 'fr',
400 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
402 // Enable session language detection and selection.
404 'language_interface[enabled][language-url]' => FALSE,
405 'language_interface[enabled][language-session]' => TRUE,
407 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
409 // Enable the language switching block.
410 $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
411 'id' => 'test_language_block',
414 // Enable the main menu block.
415 $this->drupalPlaceBlock('system_menu_block:main', [
419 // Add a link to the homepage.
420 $link = MenuLinkContent::create([
422 'menu_name' => 'main',
423 'bundle' => 'menu_link_content',
424 'link' => [['uri' => 'entity:user/2']],
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');
445 * Saves the native name of a language entity in configuration as a label.
447 * @param string $langcode
448 * The language code of the language.
449 * @param string $label
450 * The native name of the language.
452 protected function saveNativeLanguageName($langcode, $label) {
453 \Drupal::service('language.config_factory_override')
454 ->getOverride($langcode, 'language.entity.' . $langcode)->set('label', $label)->save();