1 locale.test LocaleUILanguageNegotiationTest::testUrlLanguageFallback()

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

File

core/modules/locale/tests/locale.test, line 2259
Tests for locale.module.

Class

LocaleUILanguageNegotiationTest
Test UI language negotiation

Code

function testUrlLanguageFallback() {
  // Add the Italian language.
  $langcode_browser_fallback = 'it';
  $language = (object) array(
    'langcode' => $langcode_browser_fallback,
  );
  language_save($language);
  $languages = language_list();

  // 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->backdropPost('admin/config/regional/language/detection/url', $edit, t('Save configuration'));

  // Enable browser and URL language detection.
  $edit = array(
    'language[locale-browser][enabled]' => TRUE,
    'language[locale-browser][weight]' => -8,
    'language[locale-url][enabled]' => TRUE,
    'language[locale-url][weight]' => -10,
  );
  $this->backdropPost('admin/config/regional/language/detection', $edit, t('Save settings'));
  $this->backdropGet('admin/config/regional/language/detection');

  // Enable the language switcher block.
  $layout = layout_load('home');
  $layout->addBlock('locale', 'language', 'top');
  $layout->save();

  // Use Stark for expected markup checking.
  theme_enable(array('stark'));
  config_set('system.core', 'theme_default', 'stark');

  // Access the home 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 = (object) array('langcode' => '');
  $this->backdropGet('', array('language' => $language), $http_header);

  // Check that the language switcher active link matches the given browser
  // language.
  $args = array(':url' => base_path() . (config_get('system.core', 'clean_url') ? $langcode_browser_fallback : "?q=$langcode_browser_fallback"));
  $fields = $this->xpath('//*[contains(@class,"block-locale-language")]//a[@class="language-link active" and @href=:url]', $args);
  $this->assertTrue($fields[0] == $languages[$langcode_browser_fallback]->native, 'The browser language is the URL active language');

  // Check that URLs are rewritten using the given browser language.
  $fields = $this->xpath('//*[contains(@class,"site-name")]//a[@rel="home" and @href=:url]//span', $args);
  $this->assertTrue($fields[0] == 'Backdrop CMS', 'URLs are rewritten using the browser language.');
}