annotate core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\language\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Url;
Chris@0 6 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 7 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser;
Chris@0 8 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
Chris@0 9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession;
Chris@0 10 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
Chris@0 11 use Drupal\Tests\BrowserTestBase;
Chris@0 12 use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser;
Chris@0 13 use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin;
Chris@0 14 use Drupal\Core\Language\Language;
Chris@0 15 use Drupal\Core\Language\LanguageInterface;
Chris@0 16 use Symfony\Component\HttpFoundation\Request;
Chris@0 17 use Drupal\language\LanguageNegotiatorInterface;
Chris@0 18 use Drupal\block\Entity\Block;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Tests the language UI for language switching.
Chris@0 22 *
Chris@0 23 * The uses cases that get tested, are:
Chris@0 24 * - URL (path) > default: Test that the URL prefix setting gets precedence over
Chris@0 25 * the default language. The browser language preference does not have any
Chris@0 26 * influence.
Chris@0 27 * - URL (path) > browser > default: Test that the URL prefix setting gets
Chris@0 28 * precedence over the browser language preference, which in turn gets
Chris@0 29 * precedence over the default language.
Chris@0 30 * - URL (domain) > default: Tests that the URL domain setting gets precedence
Chris@0 31 * over the default language.
Chris@0 32 *
Chris@0 33 * The paths that are used for each of these, are:
Chris@0 34 * - admin/config: Tests the UI using the precedence rules.
Chris@0 35 * - zh-hans/admin/config: Tests the UI in Chinese.
Chris@0 36 * - blah-blah/admin/config: Tests the 404 page.
Chris@0 37 *
Chris@0 38 * @group language
Chris@0 39 */
Chris@0 40 class LanguageUILanguageNegotiationTest extends BrowserTestBase {
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Modules to enable.
Chris@0 44 *
Chris@0 45 * We marginally use interface translation functionality here, so need to use
Chris@0 46 * the locale module instead of language only, but the 90% of the test is
Chris@0 47 * about the negotiation process which is solely in language module.
Chris@0 48 *
Chris@0 49 * @var array
Chris@0 50 */
Chris@0 51 public static $modules = ['locale', 'language_test', 'block', 'user', 'content_translation'];
Chris@0 52
Chris@0 53 protected function setUp() {
Chris@0 54 parent::setUp();
Chris@0 55
Chris@0 56 $admin_user = $this->drupalCreateUser(['administer languages', 'translate interface', 'access administration pages', 'administer blocks']);
Chris@0 57 $this->drupalLogin($admin_user);
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Tests for language switching by URL path.
Chris@0 62 */
Chris@0 63 public function testUILanguageNegotiation() {
Chris@0 64 // A few languages to switch to.
Chris@0 65 // This one is unknown, should get the default lang version.
Chris@0 66 $langcode_unknown = 'blah-blah';
Chris@0 67 // For testing browser lang preference.
Chris@0 68 $langcode_browser_fallback = 'vi';
Chris@0 69 // For testing path prefix.
Chris@0 70 $langcode = 'zh-hans';
Chris@0 71 // For setting browser language preference to 'vi'.
Chris@0 72 $http_header_browser_fallback = ["Accept-Language" => "$langcode_browser_fallback;q=1"];
Chris@0 73 // For setting browser language preference to some unknown.
Chris@0 74 $http_header_blah = ["Accept-Language" => "blah;q=1"];
Chris@0 75
Chris@0 76 // Setup the site languages by installing two languages.
Chris@0 77 // Set the default language in order for the translated string to be registered
Chris@0 78 // into database when seen by t(). Without doing this, our target string
Chris@0 79 // is for some reason not found when doing translate search. This might
Chris@0 80 // be some bug.
Chris@0 81 $default_language = \Drupal::languageManager()->getDefaultLanguage();
Chris@0 82 ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save();
Chris@0 83 $this->config('system.site')->set('default_langcode', $langcode_browser_fallback)->save();
Chris@0 84 ConfigurableLanguage::createFromLangcode($langcode)->save();
Chris@0 85
Chris@0 86 // We will look for this string in the admin/config screen to see if the
Chris@0 87 // corresponding translated string is shown.
Chris@0 88 $default_string = 'Hide descriptions';
Chris@0 89
Chris@0 90 // First visit this page to make sure our target string is searchable.
Chris@0 91 $this->drupalGet('admin/config');
Chris@0 92
Chris@0 93 // Now the t()'ed string is in db so switch the language back to default.
Chris@0 94 // This will rebuild the container so we need to rebuild the container in
Chris@0 95 // the test environment.
Chris@0 96 $this->config('system.site')->set('default_langcode', $default_language->getId())->save();
Chris@0 97 $this->config('language.negotiation')->set('url.prefixes.en', '')->save();
Chris@0 98 $this->rebuildContainer();
Chris@0 99
Chris@0 100 // Translate the string.
Chris@0 101 $language_browser_fallback_string = "In $langcode_browser_fallback In $langcode_browser_fallback In $langcode_browser_fallback";
Chris@0 102 $language_string = "In $langcode In $langcode In $langcode";
Chris@0 103 // Do a translate search of our target string.
Chris@0 104 $search = [
Chris@0 105 'string' => $default_string,
Chris@0 106 'langcode' => $langcode_browser_fallback,
Chris@0 107 ];
Chris@0 108 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 109 $textarea = current($this->xpath('//textarea'));
Chris@0 110 $lid = $textarea->getAttribute('name');
Chris@0 111 $edit = [
Chris@0 112 $lid => $language_browser_fallback_string,
Chris@0 113 ];
Chris@0 114 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 115
Chris@0 116 $search = [
Chris@0 117 'string' => $default_string,
Chris@0 118 'langcode' => $langcode,
Chris@0 119 ];
Chris@0 120 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 121 $textarea = current($this->xpath('//textarea'));
Chris@0 122 $lid = $textarea->getAttribute('name');
Chris@0 123 $edit = [
Chris@0 124 $lid => $language_string,
Chris@0 125 ];
Chris@0 126 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 127
Chris@0 128 // Configure selected language negotiation to use zh-hans.
Chris@0 129 $edit = ['selected_langcode' => $langcode];
Chris@0 130 $this->drupalPostForm('admin/config/regional/language/detection/selected', $edit, t('Save configuration'));
Chris@0 131 $test = [
Chris@0 132 'language_negotiation' => [LanguageNegotiationSelected::METHOD_ID],
Chris@0 133 'path' => 'admin/config',
Chris@0 134 'expect' => $language_string,
Chris@0 135 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID,
Chris@0 136 'http_header' => $http_header_browser_fallback,
Chris@0 137 'message' => 'SELECTED: UI language is switched based on selected language.',
Chris@0 138 ];
Chris@0 139 $this->doRunTest($test);
Chris@0 140
Chris@0 141 // An invalid language is selected.
Chris@0 142 $this->config('language.negotiation')->set('selected_langcode', NULL)->save();
Chris@0 143 $test = [
Chris@0 144 'language_negotiation' => [LanguageNegotiationSelected::METHOD_ID],
Chris@0 145 'path' => 'admin/config',
Chris@0 146 'expect' => $default_string,
Chris@0 147 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 148 'http_header' => $http_header_browser_fallback,
Chris@0 149 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
Chris@0 150 ];
Chris@0 151 $this->doRunTest($test);
Chris@0 152
Chris@0 153 // No selected language is available.
Chris@0 154 $this->config('language.negotiation')->set('selected_langcode', $langcode_unknown)->save();
Chris@0 155 $test = [
Chris@0 156 'language_negotiation' => [LanguageNegotiationSelected::METHOD_ID],
Chris@0 157 'path' => 'admin/config',
Chris@0 158 'expect' => $default_string,
Chris@0 159 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 160 'http_header' => $http_header_browser_fallback,
Chris@0 161 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
Chris@0 162 ];
Chris@0 163 $this->doRunTest($test);
Chris@0 164
Chris@0 165 $tests = [
Chris@0 166 // Default, browser preference should have no influence.
Chris@0 167 [
Chris@0 168 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 169 'path' => 'admin/config',
Chris@0 170 'expect' => $default_string,
Chris@0 171 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 172 'http_header' => $http_header_browser_fallback,
Chris@0 173 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.',
Chris@0 174 ],
Chris@0 175 // Language prefix.
Chris@0 176 [
Chris@0 177 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 178 'path' => "$langcode/admin/config",
Chris@0 179 'expect' => $language_string,
Chris@0 180 'expected_method_id' => LanguageNegotiationUrl::METHOD_ID,
Chris@0 181 'http_header' => $http_header_browser_fallback,
Chris@0 182 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix',
Chris@0 183 ],
Chris@0 184 // Default, go by browser preference.
Chris@0 185 [
Chris@0 186 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID],
Chris@0 187 'path' => 'admin/config',
Chris@0 188 'expect' => $language_browser_fallback_string,
Chris@0 189 'expected_method_id' => LanguageNegotiationBrowser::METHOD_ID,
Chris@0 190 'http_header' => $http_header_browser_fallback,
Chris@0 191 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference',
Chris@0 192 ],
Chris@0 193 // Prefix, switch to the language.
Chris@0 194 [
Chris@0 195 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID],
Chris@0 196 'path' => "$langcode/admin/config",
Chris@0 197 'expect' => $language_string,
Chris@0 198 'expected_method_id' => LanguageNegotiationUrl::METHOD_ID,
Chris@0 199 'http_header' => $http_header_browser_fallback,
Chris@0 200 'message' => 'URL (PATH) > BROWSER: with language prefix, UI language is based on path prefix',
Chris@0 201 ],
Chris@0 202 // Default, browser language preference is not one of site's lang.
Chris@0 203 [
Chris@0 204 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 205 'path' => 'admin/config',
Chris@0 206 'expect' => $default_string,
Chris@0 207 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 208 'http_header' => $http_header_blah,
Chris@0 209 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language',
Chris@0 210 ],
Chris@0 211 ];
Chris@0 212
Chris@0 213 foreach ($tests as $test) {
Chris@0 214 $this->doRunTest($test);
Chris@0 215 }
Chris@0 216
Chris@0 217 // Unknown language prefix should return 404.
Chris@0 218 $definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods();
Chris@0 219 // Enable only methods, which are either not limited to a specific language
Chris@0 220 // type or are supporting the interface language type.
Chris@0 221 $language_interface_method_definitions = array_filter($definitions, function ($method_definition) {
Chris@0 222 return !isset($method_definition['types']) || (isset($method_definition['types']) && in_array(LanguageInterface::TYPE_INTERFACE, $method_definition['types']));
Chris@0 223 });
Chris@0 224 $this->config('language.types')
Chris@0 225 ->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled', array_flip(array_keys($language_interface_method_definitions)))
Chris@0 226 ->save();
Chris@0 227 $this->drupalGet("$langcode_unknown/admin/config", [], $http_header_browser_fallback);
Chris@0 228 $this->assertResponse(404, "Unknown language path prefix should return 404");
Chris@0 229
Chris@0 230 // Set preferred langcode for user to NULL.
Chris@0 231 $account = $this->loggedInUser;
Chris@0 232 $account->preferred_langcode = NULL;
Chris@0 233 $account->save();
Chris@0 234
Chris@0 235 $test = [
Chris@0 236 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 237 'path' => 'admin/config',
Chris@0 238 'expect' => $default_string,
Chris@0 239 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 240 'http_header' => [],
Chris@0 241 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default',
Chris@0 242 ];
Chris@0 243 $this->doRunTest($test);
Chris@0 244
Chris@0 245 // Set preferred langcode for user to unknown language.
Chris@0 246 $account = $this->loggedInUser;
Chris@0 247 $account->preferred_langcode = $langcode_unknown;
Chris@0 248 $account->save();
Chris@0 249
Chris@0 250 $test = [
Chris@0 251 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 252 'path' => 'admin/config',
Chris@0 253 'expect' => $default_string,
Chris@0 254 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 255 'http_header' => [],
Chris@0 256 'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default',
Chris@0 257 ];
Chris@0 258 $this->doRunTest($test);
Chris@0 259
Chris@0 260 // Set preferred langcode for user to non default.
Chris@0 261 $account->preferred_langcode = $langcode;
Chris@0 262 $account->save();
Chris@0 263
Chris@0 264 $test = [
Chris@0 265 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 266 'path' => 'admin/config',
Chris@0 267 'expect' => $language_string,
Chris@0 268 'expected_method_id' => LanguageNegotiationUser::METHOD_ID,
Chris@0 269 'http_header' => [],
Chris@0 270 'message' => 'USER > DEFAULT: defined preferred user language setting, the UI language is based on user setting',
Chris@0 271 ];
Chris@0 272 $this->doRunTest($test);
Chris@0 273
Chris@0 274 // Set preferred admin langcode for user to NULL.
Chris@0 275 $account->preferred_admin_langcode = NULL;
Chris@0 276 $account->save();
Chris@0 277
Chris@0 278 $test = [
Chris@0 279 'language_negotiation' => [LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 280 'path' => 'admin/config',
Chris@0 281 'expect' => $default_string,
Chris@0 282 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 283 'http_header' => [],
Chris@0 284 'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default',
Chris@0 285 ];
Chris@0 286 $this->doRunTest($test);
Chris@0 287
Chris@0 288 // Set preferred admin langcode for user to unknown language.
Chris@0 289 $account->preferred_admin_langcode = $langcode_unknown;
Chris@0 290 $account->save();
Chris@0 291
Chris@0 292 $test = [
Chris@0 293 'language_negotiation' => [LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 294 'path' => 'admin/config',
Chris@0 295 'expect' => $default_string,
Chris@0 296 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 297 'http_header' => [],
Chris@0 298 'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default',
Chris@0 299 ];
Chris@0 300 $this->doRunTest($test);
Chris@0 301
Chris@0 302 // Set preferred admin langcode for user to non default.
Chris@0 303 $account->preferred_admin_langcode = $langcode;
Chris@0 304 $account->save();
Chris@0 305
Chris@0 306 $test = [
Chris@0 307 'language_negotiation' => [LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID],
Chris@0 308 'path' => 'admin/config',
Chris@0 309 'expect' => $language_string,
Chris@0 310 'expected_method_id' => LanguageNegotiationUserAdmin::METHOD_ID,
Chris@0 311 'http_header' => [],
Chris@0 312 'message' => 'USER ADMIN > DEFAULT: defined preferred user admin language setting, the UI language is based on user setting',
Chris@0 313 ];
Chris@0 314 $this->doRunTest($test);
Chris@0 315
Chris@0 316 // Go by session preference.
Chris@0 317 $language_negotiation_session_param = $this->randomMachineName();
Chris@0 318 $edit = ['language_negotiation_session_param' => $language_negotiation_session_param];
Chris@0 319 $this->drupalPostForm('admin/config/regional/language/detection/session', $edit, t('Save configuration'));
Chris@0 320 $tests = [
Chris@0 321 [
Chris@0 322 'language_negotiation' => [LanguageNegotiationSession::METHOD_ID],
Chris@0 323 'path' => "admin/config",
Chris@0 324 'expect' => $default_string,
Chris@0 325 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
Chris@0 326 'http_header' => $http_header_browser_fallback,
Chris@0 327 'message' => 'SESSION > DEFAULT: no language given, the UI language is default',
Chris@0 328 ],
Chris@0 329 [
Chris@0 330 'language_negotiation' => [LanguageNegotiationSession::METHOD_ID],
Chris@0 331 'path' => 'admin/config',
Chris@0 332 'path_options' => ['query' => [$language_negotiation_session_param => $langcode]],
Chris@0 333 'expect' => $language_string,
Chris@0 334 'expected_method_id' => LanguageNegotiationSession::METHOD_ID,
Chris@0 335 'http_header' => $http_header_browser_fallback,
Chris@0 336 'message' => 'SESSION > DEFAULT: language given, UI language is determined by session language preference',
Chris@0 337 ],
Chris@0 338 ];
Chris@0 339 foreach ($tests as $test) {
Chris@0 340 $this->doRunTest($test);
Chris@0 341 }
Chris@0 342 }
Chris@0 343
Chris@0 344 protected function doRunTest($test) {
Chris@0 345 $test += ['path_options' => []];
Chris@0 346 if (!empty($test['language_negotiation'])) {
Chris@0 347 $method_weights = array_flip($test['language_negotiation']);
Chris@0 348 $this->container->get('language_negotiator')->saveConfiguration(LanguageInterface::TYPE_INTERFACE, $method_weights);
Chris@0 349 }
Chris@0 350 if (!empty($test['language_negotiation_url_part'])) {
Chris@0 351 $this->config('language.negotiation')
Chris@0 352 ->set('url.source', $test['language_negotiation_url_part'])
Chris@0 353 ->save();
Chris@0 354 }
Chris@0 355 if (!empty($test['language_test_domain'])) {
Chris@0 356 \Drupal::state()->set('language_test.domain', $test['language_test_domain']);
Chris@0 357 }
Chris@0 358 $this->container->get('language_manager')->reset();
Chris@0 359 $this->drupalGet($test['path'], $test['path_options'], $test['http_header']);
Chris@0 360 $this->assertText($test['expect'], $test['message']);
Chris@0 361 $this->assertText(t('Language negotiation method: @name', ['@name' => $test['expected_method_id']]));
Chris@0 362 }
Chris@0 363
Chris@0 364 /**
Chris@0 365 * Test URL language detection when the requested URL has no language.
Chris@0 366 */
Chris@0 367 public function testUrlLanguageFallback() {
Chris@0 368 // Add the Italian language.
Chris@0 369 $langcode_browser_fallback = 'it';
Chris@0 370 ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save();
Chris@0 371 $languages = $this->container->get('language_manager')->getLanguages();
Chris@0 372
Chris@0 373 // Enable the path prefix for the default language: this way any unprefixed
Chris@0 374 // URL must have a valid fallback value.
Chris@0 375 $edit = ['prefix[en]' => 'en'];
Chris@0 376 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0 377
Chris@0 378 // Enable browser and URL language detection.
Chris@0 379 $edit = [
Chris@0 380 'language_interface[enabled][language-browser]' => TRUE,
Chris@0 381 'language_interface[enabled][language-url]' => TRUE,
Chris@0 382 'language_interface[weight][language-browser]' => -8,
Chris@0 383 'language_interface[weight][language-url]' => -10,
Chris@0 384 ];
Chris@0 385 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 386 $this->drupalGet('admin/config/regional/language/detection');
Chris@0 387
Chris@0 388 // Enable the language switcher block.
Chris@0 389 $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, ['id' => 'test_language_block']);
Chris@0 390
Chris@0 391 // Log out, because for anonymous users, the "active" class is set by PHP
Chris@0 392 // (which means we can easily test it here), whereas for authenticated users
Chris@0 393 // it is set by JavaScript.
Chris@0 394 $this->drupalLogout();
Chris@0 395
Chris@0 396 // Place a site branding block in the header region.
Chris@0 397 $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
Chris@0 398
Chris@0 399 // Access the front page without specifying any valid URL language prefix
Chris@0 400 // and having as browser language preference a non-default language.
Chris@0 401 $http_header = ["Accept-Language" => "$langcode_browser_fallback;q=1"];
Chris@0 402 $language = new Language(['id' => '']);
Chris@0 403 $this->drupalGet('', ['language' => $language], $http_header);
Chris@0 404
Chris@0 405 // Check that the language switcher active link matches the given browser
Chris@0 406 // language.
Chris@0 407 $args = [':id' => 'block-test-language-block', ':url' => \Drupal::url('<front>') . $langcode_browser_fallback];
Chris@0 408 $fields = $this->xpath('//div[@id=:id]//a[@class="language-link is-active" and starts-with(@href, :url)]', $args);
Chris@0 409 $this->assertSame($fields[0]->getText(), $languages[$langcode_browser_fallback]->getName(), 'The browser language is the URL active language');
Chris@0 410
Chris@0 411 // Check that URLs are rewritten using the given browser language.
Chris@0 412 $fields = $this->xpath('//div[@class="site-name"]/a[@rel="home" and @href=:url]', $args);
Chris@0 413 $this->assertSame($fields[0]->getText(), 'Drupal', 'URLs are rewritten using the browser language.');
Chris@0 414 }
Chris@0 415
Chris@0 416 /**
Chris@0 417 * Tests URL handling when separate domains are used for multiple languages.
Chris@0 418 */
Chris@0 419 public function testLanguageDomain() {
Chris@0 420 global $base_url;
Chris@0 421
Chris@0 422 // Get the current host URI we're running on.
Chris@0 423 $base_url_host = parse_url($base_url, PHP_URL_HOST);
Chris@0 424
Chris@0 425 // Add the Italian language.
Chris@0 426 ConfigurableLanguage::createFromLangcode('it')->save();
Chris@0 427
Chris@0 428 $languages = $this->container->get('language_manager')->getLanguages();
Chris@0 429
Chris@0 430 // Enable browser and URL language detection.
Chris@0 431 $edit = [
Chris@0 432 'language_interface[enabled][language-url]' => TRUE,
Chris@0 433 'language_interface[weight][language-url]' => -10,
Chris@0 434 ];
Chris@0 435 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 436
Chris@0 437 // Do not allow blank domain.
Chris@0 438 $edit = [
Chris@0 439 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
Chris@0 440 'domain[en]' => '',
Chris@0 441 ];
Chris@0 442 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0 443 $this->assertText('The domain may not be left blank for English', 'The form does not allow blank domains.');
Chris@0 444 $this->rebuildContainer();
Chris@0 445
Chris@0 446 // Change the domain for the Italian language.
Chris@0 447 $edit = [
Chris@0 448 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
Chris@0 449 'domain[en]' => $base_url_host,
Chris@0 450 'domain[it]' => 'it.example.com',
Chris@0 451 ];
Chris@0 452 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0 453 $this->assertText('The configuration options have been saved', 'Domain configuration is saved.');
Chris@0 454 $this->rebuildContainer();
Chris@0 455
Chris@0 456 // Try to use an invalid domain.
Chris@0 457 $edit = [
Chris@0 458 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
Chris@0 459 'domain[en]' => $base_url_host,
Chris@0 460 'domain[it]' => 'it.example.com/',
Chris@0 461 ];
Chris@0 462 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0 463 $this->assertRaw(t('The domain for %language may only contain the domain name, not a trailing slash, protocol and/or port.', ['%language' => 'Italian']));
Chris@0 464
Chris@0 465 // Build the link we're going to test.
Chris@0 466 $link = 'it.example.com' . rtrim(base_path(), '/') . '/admin';
Chris@0 467
Chris@0 468 // Test URL in another language: http://it.example.com/admin.
Chris@0 469 // Base path gives problems on the testbot, so $correct_link is hard-coded.
Chris@0 470 // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test).
Chris@0 471 $italian_url = Url::fromRoute('system.admin', [], ['language' => $languages['it']])->toString();
Chris@0 472 $url_scheme = \Drupal::request()->isSecure() ? 'https://' : 'http://';
Chris@0 473 $correct_link = $url_scheme . $link;
Chris@0 474 $this->assertEqual($italian_url, $correct_link, format_string('The right URL (@url) in accordance with the chosen language', ['@url' => $italian_url]));
Chris@0 475
Chris@0 476 // Test HTTPS via options.
Chris@0 477 $italian_url = Url::fromRoute('system.admin', [], ['https' => TRUE, 'language' => $languages['it']])->toString();
Chris@0 478 $correct_link = 'https://' . $link;
Chris@0 479 $this->assertTrue($italian_url == $correct_link, format_string('The right HTTPS URL (via options) (@url) in accordance with the chosen language', ['@url' => $italian_url]));
Chris@0 480
Chris@0 481 // Test HTTPS via current URL scheme.
Chris@0 482 $request = Request::create('', 'GET', [], [], [], ['HTTPS' => 'on']);
Chris@0 483 $this->container->get('request_stack')->push($request);
Chris@0 484 $italian_url = Url::fromRoute('system.admin', [], ['language' => $languages['it']])->toString();
Chris@0 485 $correct_link = 'https://' . $link;
Chris@0 486 $this->assertTrue($italian_url == $correct_link, format_string('The right URL (via current URL scheme) (@url) in accordance with the chosen language', ['@url' => $italian_url]));
Chris@0 487 }
Chris@0 488
Chris@0 489 /**
Chris@0 490 * Tests persistence of negotiation settings for the content language type.
Chris@0 491 */
Chris@0 492 public function testContentCustomization() {
Chris@0 493 // Customize content language settings from their defaults.
Chris@0 494 $edit = [
Chris@0 495 'language_content[configurable]' => TRUE,
Chris@0 496 'language_content[enabled][language-url]' => FALSE,
Chris@0 497 'language_content[enabled][language-session]' => TRUE,
Chris@0 498 ];
Chris@0 499 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 500
Chris@0 501 // Check if configurability persisted.
Chris@0 502 $config = $this->config('language.types');
Chris@0 503 $this->assertTrue(in_array('language_interface', $config->get('configurable')), 'Interface language is configurable.');
Chris@0 504 $this->assertTrue(in_array('language_content', $config->get('configurable')), 'Content language is configurable.');
Chris@0 505
Chris@0 506 // Ensure configuration was saved.
Chris@0 507 $this->assertFalse(array_key_exists('language-url', $config->get('negotiation.language_content.enabled')), 'URL negotiation is not enabled for content.');
Chris@0 508 $this->assertTrue(array_key_exists('language-session', $config->get('negotiation.language_content.enabled')), 'Session negotiation is enabled for content.');
Chris@0 509 }
Chris@0 510
Chris@0 511 /**
Chris@0 512 * Tests if the language switcher block gets deleted when a language type has been made not configurable.
Chris@0 513 */
Chris@0 514 public function testDisableLanguageSwitcher() {
Chris@0 515 $block_id = 'test_language_block';
Chris@0 516
Chris@0 517 // Enable the language switcher block.
Chris@0 518 $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_CONTENT, ['id' => $block_id]);
Chris@0 519
Chris@0 520 // Check if the language switcher block has been created.
Chris@0 521 $block = Block::load($block_id);
Chris@0 522 $this->assertTrue($block, 'Language switcher block was created.');
Chris@0 523
Chris@0 524 // Make sure language_content is not configurable.
Chris@0 525 $edit = [
Chris@0 526 'language_content[configurable]' => FALSE,
Chris@0 527 ];
Chris@0 528 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@0 529 $this->assertResponse(200);
Chris@0 530
Chris@0 531 // Check if the language switcher block has been removed.
Chris@0 532 $block = Block::load($block_id);
Chris@0 533 $this->assertFalse($block, 'Language switcher block was removed.');
Chris@0 534 }
Chris@0 535
Chris@0 536 }