function LanguageSwitchingTest::testLanguageSessionSwitchLinks

Same name and namespace in other branches
  1. 8.9.x 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()
  3. 11.x 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 452

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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.