annotate core/modules/language/tests/src/Functional/LanguageBrowserDetectionTest.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@18 5 use Drupal\Core\Url;
Chris@0 6 use Drupal\Tests\BrowserTestBase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests browser language detection.
Chris@0 10 *
Chris@0 11 * @group language
Chris@0 12 */
Chris@0 13 class LanguageBrowserDetectionTest extends BrowserTestBase {
Chris@0 14
Chris@0 15 public static $modules = ['language'];
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Tests for adding, editing and deleting mappings between browser language
Chris@0 19 * codes and Drupal language codes.
Chris@0 20 */
Chris@0 21 public function testUIBrowserLanguageMappings() {
Chris@0 22 // User to manage languages.
Chris@0 23 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@0 24 $this->drupalLogin($admin_user);
Chris@0 25
Chris@0 26 // Check that the configure link exists.
Chris@0 27 $this->drupalGet('admin/config/regional/language/detection');
Chris@0 28 $this->assertLinkByHref('admin/config/regional/language/detection/browser');
Chris@0 29
Chris@0 30 // Check that defaults are loaded from language.mappings.yml.
Chris@0 31 $this->drupalGet('admin/config/regional/language/detection/browser');
Chris@0 32 $this->assertField('edit-mappings-zh-cn-browser-langcode', 'zh-cn', 'Chinese browser language code found.');
Chris@0 33 $this->assertField('edit-mappings-zh-cn-drupal-langcode', 'zh-hans-cn', 'Chinese Drupal language code found.');
Chris@0 34
Chris@0 35 // Delete zh-cn language code.
Chris@0 36 $browser_langcode = 'zh-cn';
Chris@0 37 $this->drupalGet('admin/config/regional/language/detection/browser/delete/' . $browser_langcode);
Chris@0 38 $message = t('Are you sure you want to delete @browser_langcode?', [
Chris@0 39 '@browser_langcode' => $browser_langcode,
Chris@0 40 ]);
Chris@0 41 $this->assertRaw($message);
Chris@0 42
Chris@0 43 // Confirm the delete.
Chris@0 44 $edit = [];
Chris@0 45 $this->drupalPostForm('admin/config/regional/language/detection/browser/delete/' . $browser_langcode, $edit, t('Confirm'));
Chris@0 46
Chris@0 47 // We need raw here because %browser will add HTML.
Chris@0 48 $t_args = [
Chris@0 49 '%browser' => $browser_langcode,
Chris@0 50 ];
Chris@0 51 $this->assertRaw(t('The mapping for the %browser browser language code has been deleted.', $t_args), 'The test browser language code has been deleted.');
Chris@0 52
Chris@0 53 // Check we went back to the browser negotiation mapping overview.
Chris@18 54 $this->assertUrl(Url::fromRoute('language.negotiation_browser', [], ['absolute' => TRUE])->toString());
Chris@0 55 // Check that ch-zn no longer exists.
Chris@0 56 $this->assertNoField('edit-mappings-zh-cn-browser-langcode', 'Chinese browser language code no longer exists.');
Chris@0 57
Chris@0 58 // Add a new custom mapping.
Chris@0 59 $edit = [
Chris@0 60 'new_mapping[browser_langcode]' => 'xx',
Chris@0 61 'new_mapping[drupal_langcode]' => 'en',
Chris@0 62 ];
Chris@0 63 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
Chris@18 64 $this->assertUrl(Url::fromRoute('language.negotiation_browser', [], ['absolute' => TRUE])->toString());
Chris@0 65 $this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
Chris@0 66 $this->assertField('edit-mappings-xx-drupal-langcode', 'en', 'Drupal language code found.');
Chris@0 67
Chris@0 68 // Add the same custom mapping again.
Chris@0 69 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
Chris@0 70 $this->assertText('Browser language codes must be unique.');
Chris@0 71
Chris@0 72 // Change browser language code of our custom mapping to zh-sg.
Chris@0 73 $edit = [
Chris@0 74 'mappings[xx][browser_langcode]' => 'zh-sg',
Chris@0 75 'mappings[xx][drupal_langcode]' => 'en',
Chris@0 76 ];
Chris@0 77 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
Chris@0 78 $this->assertText(t('Browser language codes must be unique.'));
Chris@0 79
Chris@0 80 // Change Drupal language code of our custom mapping to zh-hans.
Chris@0 81 $edit = [
Chris@0 82 'mappings[xx][browser_langcode]' => 'xx',
Chris@0 83 'mappings[xx][drupal_langcode]' => 'zh-hans',
Chris@0 84 ];
Chris@0 85 $this->drupalPostForm('admin/config/regional/language/detection/browser', $edit, t('Save configuration'));
Chris@18 86 $this->assertUrl(Url::fromRoute('language.negotiation_browser', [], ['absolute' => TRUE])->toString());
Chris@0 87 $this->assertField('edit-mappings-xx-browser-langcode', 'xx', 'Browser language code found.');
Chris@0 88 $this->assertField('edit-mappings-xx-drupal-langcode', 'zh-hans', 'Drupal language code found.');
Chris@0 89 }
Chris@0 90
Chris@0 91 }