annotate core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php @ 19:fa3358dc1485 tip

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