3 namespace Drupal\Tests\language\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests browser language detection.
12 class LanguageBrowserDetectionTest extends BrowserTestBase {
14 public static $modules = ['language'];
17 * Tests for adding, editing and deleting mappings between browser language
18 * codes and Drupal language codes.
20 public function testUIBrowserLanguageMappings() {
21 // User to manage languages.
22 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
23 $this->drupalLogin($admin_user);
25 // Check that the configure link exists.
26 $this->drupalGet('admin/config/regional/language/detection');
27 $this->assertLinkByHref('admin/config/regional/language/detection/browser');
29 // Check that defaults are loaded from language.mappings.yml.
30 $this->drupalGet('admin/config/regional/language/detection/browser');
31 $this->assertField('edit-mappings-zh-cn-browser-langcode', 'zh-cn', 'Chinese browser language code found.');
32 $this->assertField('edit-mappings-zh-cn-drupal-langcode', 'zh-hans-cn', 'Chinese Drupal language code found.');
34 // Delete zh-cn language code.
35 $browser_langcode = 'zh-cn';
36 $this->drupalGet('admin/config/regional/language/detection/browser/delete/' . $browser_langcode);
37 $message = t('Are you sure you want to delete @browser_langcode?', [
38 '@browser_langcode' => $browser_langcode,
40 $this->assertRaw($message);
42 // Confirm the delete.
44 $this->drupalPostForm('admin/config/regional/language/detection/browser/delete/' . $browser_langcode, $edit, t('Confirm'));
46 // We need raw here because %browser will add HTML.
48 '%browser' => $browser_langcode,
50 $this->assertRaw(t('The mapping for the %browser browser language code has been deleted.', $t_args), 'The test browser language code has been deleted.');
52 // Check we went back to the browser negotiation mapping overview.
53 $this->assertUrl(\Drupal::url('language.negotiation_browser', [], ['absolute' => TRUE]));
54 // Check that ch-zn no longer exists.
55 $this->assertNoField('edit-mappings-zh-cn-browser-langcode', 'Chinese browser language code no longer exists.');
57 // Add a new custom mapping.
59 'new_mapping[browser_langcode]' => 'xx',
60 'new_mapping[drupal_langcode]' => 'en',
62 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
63 $this->assertUrl(\Drupal::url('language.negotiation_browser', [], ['absolute' => TRUE]));
64 $this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
65 $this->assertField('edit-mappings-xx-drupal-langcode', 'en', 'Drupal language code found.');
67 // Add the same custom mapping again.
68 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
69 $this->assertText('Browser language codes must be unique.');
71 // Change browser language code of our custom mapping to zh-sg.
73 'mappings[xx][browser_langcode]' => 'zh-sg',
74 'mappings[xx][drupal_langcode]' => 'en',
76 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
77 $this->assertText(t('Browser language codes must be unique.'));
79 // Change Drupal language code of our custom mapping to zh-hans.
81 'mappings[xx][browser_langcode]' => 'xx',
82 'mappings[xx][drupal_langcode]' => 'zh-hans',
84 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
85 $this->assertUrl(\Drupal::url('language.negotiation_browser', [], ['absolute' => TRUE]));
86 $this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
87 $this->assertField('edit-mappings-xx-drupal-langcode', 'zh-hans', 'Drupal language code found.');