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
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Tests browser language detection.
*
* @group language
*/
class LanguageBrowserDetectionTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['language'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests mappings between browser language codes and Drupal language codes.
*/
public function testUIBrowserLanguageMappings(): void {
// User to manage languages.
$admin_user = $this->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this->drupalLogin($admin_user);
// Check that the configure link exists.
$this->drupalGet('admin/config/regional/language/detection');
$this->assertSession()->linkByHrefExists('admin/config/regional/language/detection/browser');
// Check that defaults are loaded from language.mappings.yml.
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->assertSession()->fieldValueEquals('edit-mappings-zh-cn-browser-langcode', 'zh-cn');
$this->assertSession()->fieldValueEquals('edit-mappings-zh-cn-drupal-langcode', 'zh-hans');
// Delete zh-cn language code.
$browser_langcode = 'zh-cn';
$this->drupalGet('admin/config/regional/language/detection/browser/delete/' . $browser_langcode);
$this->assertSession()->pageTextContains("Are you sure you want to delete {$browser_langcode}?");
// Confirm the delete.
$edit = [];
$this->drupalGet('admin/config/regional/language/detection/browser/delete/' . $browser_langcode);
$this->submitForm($edit, 'Confirm');
$this->assertSession()->statusMessageContains("The mapping for the {$browser_langcode} browser language code has been deleted.", 'status');
// Check we went back to the browser negotiation mapping overview.
$this->assertSession()->addressEquals(Url::fromRoute('language.negotiation_browser'));
// Check that Chinese browser language code no longer exists.
$this->assertSession()->fieldNotExists('edit-mappings-zh-cn-browser-langcode');
// Add a new custom mapping.
$edit = [
'new_mapping[browser_langcode]' => 'xx',
'new_mapping[drupal_langcode]' => 'en',
];
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->submitForm($edit, 'Save configuration');
$this->assertSession()->addressEquals(Url::fromRoute('language.negotiation_browser'));
$this->assertSession()->fieldValueEquals('edit-mappings-xx-browser-langcode', 'xx');
$this->assertSession()->fieldValueEquals('edit-mappings-xx-drupal-langcode', 'en');
// Add the same custom mapping again.
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->submitForm($edit, 'Save configuration');
$this->assertSession()->statusMessageContains('Browser language codes must be unique.', 'error');
// Change browser language code of our custom mapping to zh-sg.
$edit = [
'mappings[xx][browser_langcode]' => 'zh-sg',
'mappings[xx][drupal_langcode]' => 'en',
];
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->submitForm($edit, 'Save configuration');
$this->assertSession()->statusMessageContains('Browser language codes must be unique.', 'error');
// Change Drupal language code of our custom mapping to zh-hans.
$edit = [
'mappings[xx][browser_langcode]' => 'xx',
'mappings[xx][drupal_langcode]' => 'zh-hans',
];
$this->drupalGet('admin/config/regional/language/detection/browser');
$this->submitForm($edit, 'Save configuration');
$this->assertSession()->addressEquals(Url::fromRoute('language.negotiation_browser'));
$this->assertSession()->fieldValueEquals('edit-mappings-xx-browser-langcode', 'xx');
$this->assertSession()->fieldValueEquals('edit-mappings-xx-drupal-langcode', 'zh-hans');
}
}
|