function LanguageUILanguageNegotiationTest::testUrlLanguageFallback

Same name and namespace in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php \Drupal\Tests\language\Functional\LanguageUILanguageNegotiationTest::testUrlLanguageFallback()
  2. 8.9.x 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()

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

File

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

Class

LanguageUILanguageNegotiationTest
Tests the language UI for language switching.

Namespace

Drupal\Tests\language\Functional

Code

public function testUrlLanguageFallback() : void {
  // 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->drupalGet('admin/config/regional/language/detection/url');
  $this->submitForm($edit, '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->drupalGet('admin/config/regional/language/detection');
  $this->submitForm($edit, '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',
    'id' => 'site_branding',
  ]);
  // 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.
  $href = Url::fromRoute('<front>')->toString() . $langcode_browser_fallback;
  $this->assertSession()
    ->elementTextEquals('xpath', "//div[@id='block-test-language-block']//a[@class='language-link is-active' and starts-with(@href, '{$href}')]", $languages[$langcode_browser_fallback]->getName());
  // Check that URLs are rewritten using the given browser language.
  $this->assertSession()
    ->elementTextEquals('xpath', "//div[@id='block-site-branding']/a[@rel='home' and @href='{$href}'][2]", 'Drupal');
}

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