You are here

function LanguageUILanguageNegotiationTest::testUrlLanguageFallback in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/src/Tests/LanguageUILanguageNegotiationTest.php \Drupal\language\Tests\LanguageUILanguageNegotiationTest::testUrlLanguageFallback()

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

File

core/modules/language/src/Tests/LanguageUILanguageNegotiationTest.php, line 372
Contains \Drupal\language\Tests\LanguageUILanguageNegotiationTest.

Class

LanguageUILanguageNegotiationTest
Tests the language UI for language switching.

Namespace

Drupal\language\Tests

Code

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 = array(
    'prefix[en]' => 'en',
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));

  // Enable browser and URL language detection.
  $edit = array(
    '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, array(
    '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 = array(
    "Accept-Language: {$langcode_browser_fallback};q=1",
  );
  $language = new Language(array(
    'id' => '',
  ));
  $this
    ->drupalGet('', array(
    'language' => $language,
  ), $http_header);

  // Check that the language switcher active link matches the given browser
  // language.
  $args = array(
    ':id' => 'block-test-language-block',
    ':url' => \Drupal::url('<front>') . $langcode_browser_fallback,
  );
  $fields = $this
    ->xpath('//div[@id=:id]//a[@class="language-link is-active" and starts-with(@href, :url)]', $args);
  $this
    ->assertTrue($fields[0] == $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
    ->assertTrue($fields[0] == 'Drupal', 'URLs are rewritten using the browser language.');
}