1.20.x locale.test LocaleUILanguageNegotiationTest::testUILanguageNegotiation()

Tests for language switching by URL path.

File

modules/locale/tests/locale.test, line 2083
Tests for locale.module.

Class

LocaleUILanguageNegotiationTest
Test UI language negotiation

Code

function testUILanguageNegotiation() {
  // A few languages to switch to.
  // This one is unknown, should get the default lang version.
  $langcode_unknown = 'blah-blah';
  // For testing browser lang preference.
  $langcode_browser_fallback = 'vi';
  // For testing path prefix.
  $langcode = 'zh-hans';
  // For setting browser language preference to 'vi'.
  $http_header_browser_fallback = array("Accept-Language: $langcode_browser_fallback;q=1");
  // For setting browser language preference to some unknown.
  $http_header_blah = array("Accept-Language: blah;q=1");

  // This domain should switch the UI to Chinese.
  $language_domain = 'example.cn';

  // Setup the site languages by installing two languages.
  require_once BACKDROP_ROOT . '/core/includes/locale.inc';
  $language = (object) array(
    'langcode' => $langcode_browser_fallback,
  );
  language_save($language);
  $language = (object) array(
    'langcode' => $langcode,
  );
  language_save($language);

  // We will look for this string in the admin/config screen to see if the
  // corresponding translated string is shown.
  $default_string = 'Configure languages for content and the user interface';

  // Set the default language in order for the translated string to be registered
  // into database when seen by t(). Without doing this, our target string
  // is for some reason not found when doing translate search. This might
  // be some bug.
  backdrop_static_reset('language_list');
  $core_config = config('system.core');
  $languages = language_list(TRUE);
  $default_langcode = $core_config->get('language_default');
  $core_config->set('language_default', $languages['vi']->langcode);
  $core_config->save();
  // First visit this page to make sure our target string is searchable.
  $this->backdropGet('admin/config');
  // Now the t()'ed string is in db so switch the language back to default.
  $core_config->set('language_default', $default_langcode);
  $core_config->save();

  // Translate the string.
  $language_browser_fallback_string = "In $langcode_browser_fallback In $langcode_browser_fallback In $langcode_browser_fallback";
  $language_string = "In $langcode In $langcode In $langcode";
  // Do a translate search of our target string.
  $edit = array('string' => $default_string);
  $this->backdropPost('admin/config/regional/translate/translate', $edit, t('Filter'));
  // Should find the string and now click edit to post translated string.
  $this->clickLink('Edit');
  $edit = array(
    "translations[$langcode_browser_fallback]" => $language_browser_fallback_string,
    "translations[$langcode]" => $language_string,
  );
  $this->backdropPost(NULL, $edit, t('Save translations'));

  // Configure URL language rewrite.
  config_get('locale.settings', 'language_negotiation_url_type', LANGUAGE_TYPE_INTERFACE);

  $tests = array(
    // Default, browser preference should have no influence.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_DEFAULT,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.',
    ),
    // Language prefix.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
      'path' => "$langcode/admin/config",
      'expect' => $language_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_URL,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix',
    ),
    // Default, go by browser preference.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER),
      'path' => 'admin/config',
      'expect' => $language_browser_fallback_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_BROWSER,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference',
    ),
    // Prefix, switch to the language.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER),
      'path' => "$langcode/admin/config",
      'expect' => $language_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_URL,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (PATH) > BROWSER: with langage prefix, UI language is based on path prefix',
    ),
    // Default, browser language preference is not one of site's lang.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_DEFAULT),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_DEFAULT,
      'http_header' => $http_header_blah,
      'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language',
    ),
  );

  foreach ($tests as $test) {
    $this->runTest($test);
  }

  // Unknown language prefix should return 404.
  config_set('language.settings', 'language_negotiation.' . LANGUAGE_TYPE_INTERFACE, array_keys(locale_language_negotiation_info()));
  $this->backdropGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback);
  $this->assertResponse(404, "Unknown language path prefix should return 404");

  // Setup for domain negotiation, first configure the language to have domain
  // URL. We use HTTPS and a port to make sure that only the domain name is used.
  $edit = array("domain[$langcode]" => "https://$language_domain:99");
  $this->backdropPost("admin/config/regional/language/detection/url", $edit, t('Save configuration'));
  // Set the site to use domain language negotiation.

  $tests = array(
    // Default domain, browser preference should have no influence.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
      'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_DEFAULT,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language',
    ),
    // Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in
    // locale_test.module hook_boot() to simulate this.
    array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_DEFAULT),
      'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
      'locale_test_domain' => $language_domain . ':88',
      'path' => 'admin/config',
      'expect' => $language_string,
      'expected_provider' => LANGUAGE_NEGOTIATION_URL,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (DOMAIN) > DEFAULT: domain example.cn should switch to Chinese',
    ),
  );

  foreach ($tests as $test) {
    $this->runTest($test);
  }
}