You are here

public function LanguageUILanguageNegotiationTest::testUrlLanguageFallback in Drupal 9

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

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
    ->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',
  ]);

  // 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[@class='site-name']/a[@rel='home' and @href='{$href}']", 'Drupal');
}