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