You are here

public function LanguageSwitchingTest::testLanguageSessionSwitchLinks in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/tests/src/Functional/LanguageSwitchingTest.php \Drupal\Tests\language\Functional\LanguageSwitchingTest::testLanguageSessionSwitchLinks()
  2. 10 core/modules/language/tests/src/Functional/LanguageSwitchingTest.php \Drupal\Tests\language\Functional\LanguageSwitchingTest::testLanguageSessionSwitchLinks()

Tests language switcher links for session based negotiation.

File

core/modules/language/tests/src/Functional/LanguageSwitchingTest.php, line 396

Class

LanguageSwitchingTest
Functional tests for the language switching feature.

Namespace

Drupal\Tests\language\Functional

Code

public function testLanguageSessionSwitchLinks() {

  // Add language.
  $edit = [
    'predefined_langcode' => 'fr',
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add language');

  // Enable session language detection and selection.
  $edit = [
    'language_interface[enabled][language-url]' => FALSE,
    'language_interface[enabled][language-session]' => TRUE,
  ];
  $this
    ->drupalGet('admin/config/regional/language/detection');
  $this
    ->submitForm($edit, 'Save settings');

  // Enable the language switching block.
  $this
    ->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
    'id' => 'test_language_block',
  ]);

  // Enable the main menu block.
  $this
    ->drupalPlaceBlock('system_menu_block:main', [
    'id' => 'test_menu',
  ]);

  // Add a link to the homepage.
  $link = MenuLinkContent::create([
    'title' => 'Home',
    'menu_name' => 'main',
    'bundle' => 'menu_link_content',
    'link' => [
      [
        'uri' => 'entity:user/2',
      ],
    ],
  ]);
  $link
    ->save();

  // Go to the homepage.
  $this
    ->drupalGet('');

  // Click on the French link.
  $this
    ->clickLink('French');

  // There should be a query parameter to set the session language.
  $this
    ->assertSession()
    ->addressEquals('user/2?language=fr');

  // Click on the 'Home' Link.
  $this
    ->clickLink('Home');

  // There should be no query parameter.
  $this
    ->assertSession()
    ->addressEquals('user/2');

  // Click on the French link.
  $this
    ->clickLink('French');

  // There should be no query parameter.
  $this
    ->assertSession()
    ->addressEquals('user/2');
}