function LanguageUILanguageNegotiationTest::testUrlLanguageFallback

Same name in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testUrlLanguageFallback()
  2. 10 core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testUrlLanguageFallback()
  3. 11.x core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testUrlLanguageFallback()

Test URL language detection when the requested URL has no language.

File

core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php, line 426

Class

LanguageUILanguageNegotiationTest
Tests the language UI for language switching.

Namespace

Drupal\Tests\language\Functional

Code

public function testUrlLanguageFallback() {
    // Add the Italian language.
    $langcode_browser_fallback = 'it';
    ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save();
    $languages = $this->container
        ->get('language_manager')
        ->getLanguages();
    // Enable the path prefix for the default language: this way any unprefixed
    // URL must have a valid fallback value.
    $edit = [
        'prefix[en]' => 'en',
    ];
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
    // Enable browser and URL language detection.
    $edit = [
        'language_interface[enabled][language-browser]' => TRUE,
        'language_interface[enabled][language-url]' => TRUE,
        'language_interface[weight][language-browser]' => -8,
        'language_interface[weight][language-url]' => -10,
    ];
    $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
    $this->drupalGet('admin/config/regional/language/detection');
    // Enable the language switcher block.
    $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
        'id' => 'test_language_block',
    ]);
    // Log out, because for anonymous users, the "active" class is set by PHP
    // (which means we can easily test it here), whereas for authenticated users
    // it is set by JavaScript.
    $this->drupalLogout();
    // Place a site branding block in the header region.
    $this->drupalPlaceBlock('system_branding_block', [
        'region' => 'header',
    ]);
    // Access the front page without specifying any valid URL language prefix
    // and having as browser language preference a non-default language.
    $http_header = [
        "Accept-Language" => "{$langcode_browser_fallback};q=1",
    ];
    $language = new Language([
        'id' => '',
    ]);
    $this->drupalGet('', [
        'language' => $language,
    ], $http_header);
    // Check that the language switcher active link matches the given browser
    // language.
    $args = [
        ':id' => 'block-test-language-block',
        ':url' => Url::fromRoute('<front>')->toString() . $langcode_browser_fallback,
    ];
    $fields = $this->xpath('//div[@id=:id]//a[@class="language-link is-active" and starts-with(@href, :url)]', $args);
    $this->assertSame($fields[0]->getText(), $languages[$langcode_browser_fallback]->getName(), 'The browser language is the URL active language');
    // Check that URLs are rewritten using the given browser language.
    $fields = $this->xpath('//div[@class="site-name"]/a[@rel="home" and @href=:url]', $args);
    $this->assertSame($fields[0]->getText(), 'Drupal', 'URLs are rewritten using the browser language.');
}

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