function LanguageSwitchingTest::testLanguageSessionSwitchLinks

Same name and namespace in other branches
  1. 9 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 415

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->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
    // Enable session language detection and selection.
    $edit = [
        'language_interface[enabled][language-url]' => FALSE,
        'language_interface[enabled][language-session]' => TRUE,
    ];
    $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('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(t('French'));
    // There should be a query parameter to set the session language.
    $this->assertUrl('user/2', [
        'query' => [
            'language' => 'fr',
        ],
    ]);
    // Click on the 'Home' Link.
    $this->clickLink(t('Home'));
    // There should be no query parameter.
    $this->assertUrl('user/2');
    // Click on the French link.
    $this->clickLink(t('French'));
    // There should be no query parameter.
    $this->assertUrl('user/2');
}

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