Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / language / tests / src / Functional / LanguageBrowserDetectionTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests browser language detection.
9  *
10  * @group language
11  */
12 class LanguageBrowserDetectionTest extends BrowserTestBase {
13
14   public static $modules = ['language'];
15
16   /**
17    * Tests for adding, editing and deleting mappings between browser language
18    * codes and Drupal language codes.
19    */
20   public function testUIBrowserLanguageMappings() {
21     // User to manage languages.
22     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
23     $this->drupalLogin($admin_user);
24
25     // Check that the configure link exists.
26     $this->drupalGet('admin/config/regional/language/detection');
27     $this->assertLinkByHref('admin/config/regional/language/detection/browser');
28
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.');
33
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,
39     ]);
40     $this->assertRaw($message);
41
42     // Confirm the delete.
43     $edit = [];
44     $this->drupalPostForm('admin/config/regional/language/detection/browser/delete/' . $browser_langcode, $edit, t('Confirm'));
45
46     // We need raw here because %browser will add HTML.
47     $t_args = [
48       '%browser' => $browser_langcode,
49     ];
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.');
51
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.');
56
57     // Add a new custom mapping.
58     $edit = [
59       'new_mapping[browser_langcode]' => 'xx',
60       'new_mapping[drupal_langcode]' => 'en',
61     ];
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.');
66
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.');
70
71     // Change browser language code of our custom mapping to zh-sg.
72     $edit = [
73       'mappings[xx][browser_langcode]' => 'zh-sg',
74       'mappings[xx][drupal_langcode]' => 'en',
75     ];
76     $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
77     $this->assertText(t('Browser language codes must be unique.'));
78
79     // Change Drupal language code of our custom mapping to zh-hans.
80     $edit = [
81       'mappings[xx][browser_langcode]' => 'xx',
82       'mappings[xx][drupal_langcode]' => 'zh-hans',
83     ];
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.');
88   }
89
90 }