annotate core/modules/language/tests/src/Functional/LanguageSwitchingTest.php @ 19:fa3358dc1485 tip

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