You are here

function LanguageUILanguageNegotiationTest::testUILanguageNegotiation 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::testUILanguageNegotiation()

Tests for language switching by URL path.

File

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

Class

LanguageUILanguageNegotiationTest
Tests the language UI for language switching.

Namespace

Drupal\language\Tests

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",
  );

  // Setup the site languages by installing two languages.
  // 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.
  $default_language = \Drupal::languageManager()
    ->getDefaultLanguage();
  ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)
    ->save();
  $this
    ->config('system.site')
    ->set('default_langcode', $langcode_browser_fallback)
    ->save();
  ConfigurableLanguage::createFromLangcode($langcode)
    ->save();

  // We will look for this string in the admin/config screen to see if the
  // corresponding translated string is shown.
  $default_string = 'Hide descriptions';

  // First visit this page to make sure our target string is searchable.
  $this
    ->drupalGet('admin/config');

  // Now the t()'ed string is in db so switch the language back to default.
  // This will rebuild the container so we need to rebuild the container in
  // the test environment.
  $this
    ->config('system.site')
    ->set('default_langcode', $default_language
    ->getId())
    ->save();
  $this
    ->config('language.negotiation')
    ->set('url.prefixes.en', '')
    ->save();
  $this
    ->rebuildContainer();

  // 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.
  $search = array(
    'string' => $default_string,
    'langcode' => $langcode_browser_fallback,
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $language_browser_fallback_string,
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
  $search = array(
    'string' => $default_string,
    'langcode' => $langcode,
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $language_string,
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));

  // Configure selected language negotiation to use zh-hans.
  $edit = array(
    'selected_langcode' => $langcode,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection/selected', $edit, t('Save configuration'));
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $language_string,
    'expected_method_id' => LanguageNegotiationSelected::METHOD_ID,
    'http_header' => $http_header_browser_fallback,
    'message' => 'SELECTED: UI language is switched based on selected language.',
  );
  $this
    ->runTest($test);

  // An invalid language is selected.
  $this
    ->config('language.negotiation')
    ->set('selected_langcode', NULL)
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $default_string,
    'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
    'http_header' => $http_header_browser_fallback,
    'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
  );
  $this
    ->runTest($test);

  // No selected language is available.
  $this
    ->config('language.negotiation')
    ->set('selected_langcode', $langcode_unknown)
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $default_string,
    'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
    'http_header' => $http_header_browser_fallback,
    'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
  );
  $this
    ->runTest($test);
  $tests = array(
    // Default, browser preference should have no influence.
    array(
      'language_negotiation' => array(
        LanguageNegotiationUrl::METHOD_ID,
        LanguageNegotiationSelected::METHOD_ID,
      ),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
      '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(
        LanguageNegotiationUrl::METHOD_ID,
        LanguageNegotiationSelected::METHOD_ID,
      ),
      'path' => "{$langcode}/admin/config",
      'expect' => $language_string,
      'expected_method_id' => LanguageNegotiationUrl::METHOD_ID,
      '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(
        LanguageNegotiationUrl::METHOD_ID,
        LanguageNegotiationBrowser::METHOD_ID,
      ),
      'path' => 'admin/config',
      'expect' => $language_browser_fallback_string,
      'expected_method_id' => LanguageNegotiationBrowser::METHOD_ID,
      '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(
        LanguageNegotiationUrl::METHOD_ID,
        LanguageNegotiationBrowser::METHOD_ID,
      ),
      'path' => "{$langcode}/admin/config",
      'expect' => $language_string,
      'expected_method_id' => LanguageNegotiationUrl::METHOD_ID,
      'http_header' => $http_header_browser_fallback,
      'message' => 'URL (PATH) > BROWSER: with language prefix, UI language is based on path prefix',
    ),
    // Default, browser language preference is not one of site's lang.
    array(
      'language_negotiation' => array(
        LanguageNegotiationUrl::METHOD_ID,
        LanguageNegotiationBrowser::METHOD_ID,
        LanguageNegotiationSelected::METHOD_ID,
      ),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
      '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.
  $definitions = \Drupal::languageManager()
    ->getNegotiator()
    ->getNegotiationMethods();

  // Enable only methods, which are either not limited to a specific language
  // type or are supporting the interface language type.
  $language_interface_method_definitions = array_filter($definitions, function ($method_definition) {
    return !isset($method_definition['types']) || isset($method_definition['types']) && in_array(LanguageInterface::TYPE_INTERFACE, $method_definition['types']);
  });
  $this
    ->config('language.types')
    ->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled', array_flip(array_keys($language_interface_method_definitions)))
    ->save();
  $this
    ->drupalGet("{$langcode_unknown}/admin/config", array(), $http_header_browser_fallback);
  $this
    ->assertResponse(404, "Unknown language path prefix should return 404");

  // Set preferred langcode for user to NULL.
  $account = $this->loggedInUser;
  $account->preferred_langcode = NULL;
  $account
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationUser::METHOD_ID,
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $default_string,
    'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
    'http_header' => array(),
    'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default',
  );
  $this
    ->runTest($test);

  // Set preferred langcode for user to unknown language.
  $account = $this->loggedInUser;
  $account->preferred_langcode = $langcode_unknown;
  $account
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationUser::METHOD_ID,
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $default_string,
    'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
    'http_header' => array(),
    'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default',
  );
  $this
    ->runTest($test);

  // Set preferred langcode for user to non default.
  $account->preferred_langcode = $langcode;
  $account
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationUser::METHOD_ID,
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $language_string,
    'expected_method_id' => LanguageNegotiationUser::METHOD_ID,
    'http_header' => array(),
    'message' => 'USER > DEFAULT: defined preferred user language setting, the UI language is based on user setting',
  );
  $this
    ->runTest($test);

  // Set preferred admin langcode for user to NULL.
  $account->preferred_admin_langcode = NULL;
  $account
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationUserAdmin::METHOD_ID,
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $default_string,
    'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
    'http_header' => array(),
    'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default',
  );
  $this
    ->runTest($test);

  // Set preferred admin langcode for user to unknown language.
  $account->preferred_admin_langcode = $langcode_unknown;
  $account
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationUserAdmin::METHOD_ID,
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $default_string,
    'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
    'http_header' => array(),
    'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default',
  );
  $this
    ->runTest($test);

  // Set preferred admin langcode for user to non default.
  $account->preferred_admin_langcode = $langcode;
  $account
    ->save();
  $test = array(
    'language_negotiation' => array(
      LanguageNegotiationUserAdmin::METHOD_ID,
      LanguageNegotiationSelected::METHOD_ID,
    ),
    'path' => 'admin/config',
    'expect' => $language_string,
    'expected_method_id' => LanguageNegotiationUserAdmin::METHOD_ID,
    'http_header' => array(),
    'message' => 'USER ADMIN > DEFAULT: defined preferred user admin language setting, the UI language is based on user setting',
  );
  $this
    ->runTest($test);

  // Go by session preference.
  $language_negotiation_session_param = $this
    ->randomMachineName();
  $edit = array(
    'language_negotiation_session_param' => $language_negotiation_session_param,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/detection/session', $edit, t('Save configuration'));
  $tests = array(
    array(
      'language_negotiation' => array(
        LanguageNegotiationSession::METHOD_ID,
      ),
      'path' => "admin/config",
      'expect' => $default_string,
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
      'http_header' => $http_header_browser_fallback,
      'message' => 'SESSION > DEFAULT: no language given, the UI language is default',
    ),
    array(
      'language_negotiation' => array(
        LanguageNegotiationSession::METHOD_ID,
      ),
      'path' => 'admin/config',
      'path_options' => [
        'query' => [
          $language_negotiation_session_param => $langcode,
        ],
      ],
      'expect' => $language_string,
      'expected_method_id' => LanguageNegotiationSession::METHOD_ID,
      'http_header' => $http_header_browser_fallback,
      'message' => 'SESSION > DEFAULT: language given, UI language is determined by session language preference',
    ),
  );
  foreach ($tests as $test) {
    $this
      ->runTest($test);
  }
}