annotate core/modules/language/tests/src/Functional/LanguageConfigurationTest.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\language\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 7 use Drupal\Tests\BrowserTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Adds and configures languages to check negotiation changes.
Chris@0 11 *
Chris@0 12 * @group language
Chris@0 13 */
Chris@0 14 class LanguageConfigurationTest extends BrowserTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['language'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Functional tests for adding, editing and deleting languages.
Chris@0 25 */
Chris@0 26 public function testLanguageConfiguration() {
Chris@0 27 // Ensure the after installing the language module the weight of the English
Chris@0 28 // language is still 0.
Chris@0 29 $this->assertEqual(ConfigurableLanguage::load('en')->getWeight(), 0, 'The English language has a weight of 0.');
Chris@0 30
Chris@0 31 // User to add and remove language.
Chris@0 32 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@0 33 $this->drupalLogin($admin_user);
Chris@0 34
Chris@0 35 // Check if the Default English language has no path prefix.
Chris@0 36 $this->drupalGet('admin/config/regional/language/detection/url');
Chris@0 37 $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
Chris@0 38
Chris@0 39 // Check that Add language is a primary button.
Chris@0 40 $this->drupalGet('admin/config/regional/language/add');
Chris@0 41 $this->assertFieldByXPath('//input[contains(@class, "button--primary")]', 'Add language', 'Add language is a primary button');
Chris@0 42
Chris@0 43 // Add predefined language.
Chris@0 44 $edit = [
Chris@0 45 'predefined_langcode' => 'fr',
Chris@0 46 ];
Chris@0 47 $this->drupalPostForm(NULL, $edit, 'Add language');
Chris@0 48 $this->assertText('French');
Chris@0 49 $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
Chris@0 50 // Langcode for Languages is always 'en'.
Chris@0 51 $language = $this->config('language.entity.fr')->get();
Chris@0 52 $this->assertEqual($language['langcode'], 'en');
Chris@0 53
Chris@0 54 // Check if the Default English language has no path prefix.
Chris@0 55 $this->drupalGet('admin/config/regional/language/detection/url');
Chris@0 56 $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
Chris@0 57 // Check if French has a path prefix.
Chris@0 58 $this->drupalGet('admin/config/regional/language/detection/url');
Chris@0 59 $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.');
Chris@0 60
Chris@0 61 // Check if we can change the default language.
Chris@0 62 $this->drupalGet('admin/config/regional/language');
Chris@0 63 $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
Chris@0 64
Chris@0 65 // Change the default language.
Chris@0 66 $edit = [
Chris@0 67 'site_default_language' => 'fr',
Chris@0 68 ];
Chris@0 69 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
Chris@0 70 $this->rebuildContainer();
Chris@0 71 $this->assertFieldChecked('edit-site-default-language-fr', 'Default language updated.');
Chris@0 72 $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'langcode' => 'fr']), [], 'Correct page redirection.');
Chris@0 73
Chris@0 74 // Check if a valid language prefix is added after changing the default
Chris@0 75 // language.
Chris@0 76 $this->drupalGet('admin/config/regional/language/detection/url');
Chris@0 77 $this->assertFieldByXPath('//input[@name="prefix[en]"]', 'en', 'A valid path prefix has been added to the previous default language.');
Chris@0 78 // Check if French still has a path prefix.
Chris@0 79 $this->drupalGet('admin/config/regional/language/detection/url');
Chris@0 80 $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French still has a path prefix.');
Chris@0 81
Chris@0 82 // Check that prefix can be changed.
Chris@0 83 $edit = [
Chris@0 84 'prefix[fr]' => 'french',
Chris@0 85 ];
Chris@0 86 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
Chris@0 87 $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'french', 'French path prefix has changed.');
Chris@0 88
Chris@0 89 // Check that the prefix can be removed.
Chris@0 90 $edit = [
Chris@0 91 'prefix[fr]' => '',
Chris@0 92 ];
Chris@0 93 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
Chris@0 94 $this->assertNoText(t('The prefix may only be left blank for the selected detection fallback language.'), 'The path prefix can be removed for the default language');
Chris@0 95
Chris@0 96 // Change default negotiation language.
Chris@0 97 $this->config('language.negotiation')->set('selected_langcode', 'fr')->save();
Chris@0 98 // Check that the prefix of a language that is not the negotiation one
Chris@0 99 // cannot be changed to empty string.
Chris@0 100 $edit = [
Chris@0 101 'prefix[en]' => '',
Chris@0 102 ];
Chris@0 103 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
Chris@0 104 $this->assertText(t('The prefix may only be left blank for the selected detection fallback language.'));
Chris@0 105
Chris@0 106 // Check that prefix cannot be changed to contain a slash.
Chris@0 107 $edit = [
Chris@0 108 'prefix[en]' => 'foo/bar',
Chris@0 109 ];
Chris@0 110 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
Chris@0 111 $this->assertText(t('The prefix may not contain a slash.'), 'English prefix cannot be changed to contain a slash.');
Chris@0 112
Chris@0 113 // Remove English language and add a new Language to check if langcode of
Chris@0 114 // Language entity is 'en'.
Chris@0 115 $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
Chris@0 116 $this->rebuildContainer();
Chris@0 117 $this->assertRaw(t('The %language (%langcode) language has been removed.', ['%language' => 'English', '%langcode' => 'en']));
Chris@0 118
Chris@0 119 // Ensure that French language has a weight of 1 after being created through
Chris@0 120 // the UI.
Chris@0 121 $french = ConfigurableLanguage::load('fr');
Chris@0 122 $this->assertEqual($french->getWeight(), 1, 'The French language has a weight of 1.');
Chris@0 123 // Ensure that French language can now have a weight of 0.
Chris@0 124 $french->setWeight(0)->save();
Chris@0 125 $this->assertEqual($french->getWeight(), 0, 'The French language has a weight of 0.');
Chris@0 126 // Ensure that new languages created through the API get a weight of 0.
Chris@0 127 $afrikaans = ConfigurableLanguage::createFromLangcode('af');
Chris@0 128 $afrikaans->save();
Chris@0 129 $this->assertEqual($afrikaans->getWeight(), 0, 'The Afrikaans language has a weight of 0.');
Chris@0 130 // Ensure that a new language can be created with any weight.
Chris@0 131 $arabic = ConfigurableLanguage::createFromLangcode('ar');
Chris@0 132 $arabic->setWeight(4)->save();
Chris@0 133 $this->assertEqual($arabic->getWeight(), 4, 'The Arabic language has a weight of 0.');
Chris@0 134
Chris@0 135 $edit = [
Chris@0 136 'predefined_langcode' => 'de',
Chris@0 137 ];
Chris@0 138 $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
Chris@0 139 $language = $this->config('language.entity.de')->get();
Chris@0 140 $this->assertEqual($language['langcode'], 'fr');
Chris@0 141
Chris@0 142 // Ensure that German language has a weight of 5 after being created through
Chris@0 143 // the UI.
Chris@0 144 $french = ConfigurableLanguage::load('de');
Chris@0 145 $this->assertEqual($french->getWeight(), 5, 'The German language has a weight of 5.');
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Functional tests for setting system language weight on adding, editing and deleting languages.
Chris@0 150 */
Chris@0 151 public function testLanguageConfigurationWeight() {
Chris@0 152 // User to add and remove language.
Chris@0 153 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@0 154 $this->drupalLogin($admin_user);
Chris@0 155 $this->checkConfigurableLanguageWeight();
Chris@0 156
Chris@0 157 // Add predefined language.
Chris@0 158 $edit = [
Chris@0 159 'predefined_langcode' => 'fr',
Chris@0 160 ];
Chris@0 161 $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
Chris@0 162 $this->checkConfigurableLanguageWeight('after adding new language');
Chris@0 163
Chris@0 164 // Re-ordering languages.
Chris@0 165 $edit = [
Chris@0 166 'languages[en][weight]' => $this->getHighestConfigurableLanguageWeight() + 1,
Chris@0 167 ];
Chris@0 168 $this->drupalPostForm('admin/config/regional/language', $edit, 'Save configuration');
Chris@0 169 $this->checkConfigurableLanguageWeight('after re-ordering');
Chris@0 170
Chris@0 171 // Remove predefined language.
Chris@0 172 $this->drupalPostForm('admin/config/regional/language/delete/fr', [], 'Delete');
Chris@0 173 $this->checkConfigurableLanguageWeight('after deleting a language');
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Validates system languages are ordered after configurable languages.
Chris@0 178 *
Chris@0 179 * @param string $state
Chris@0 180 * (optional) A string for customizing assert messages, containing the
Chris@0 181 * description of the state of the check, for example: 'after re-ordering'.
Chris@0 182 * Defaults to 'by default'.
Chris@0 183 */
Chris@0 184 protected function checkConfigurableLanguageWeight($state = 'by default') {
Chris@0 185 // Reset language list.
Chris@0 186 \Drupal::languageManager()->reset();
Chris@0 187 $max_configurable_language_weight = $this->getHighestConfigurableLanguageWeight();
Chris@0 188 $replacements = ['@event' => $state];
Chris@0 189 foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $locked_language) {
Chris@0 190 $replacements['%language'] = $locked_language->getName();
Chris@0 191 $this->assertTrue($locked_language->getWeight() > $max_configurable_language_weight, format_string('System language %language has higher weight than configurable languages @event', $replacements));
Chris@0 192 }
Chris@0 193 }
Chris@0 194
Chris@0 195 /**
Chris@0 196 * Helper to get maximum weight of configurable (unlocked) languages.
Chris@0 197 *
Chris@0 198 * @return int
Chris@0 199 * Maximum weight of configurable languages.
Chris@0 200 */
Chris@12 201 protected function getHighestConfigurableLanguageWeight() {
Chris@0 202 $max_weight = 0;
Chris@0 203
Chris@0 204 $storage = $this->container->get('entity_type.manager')
Chris@0 205 ->getStorage('configurable_language');
Chris@0 206 $storage->resetCache();
Chris@0 207 /* @var $languages \Drupal\Core\Language\LanguageInterface[] */
Chris@0 208 $languages = $storage->loadMultiple();
Chris@0 209 foreach ($languages as $language) {
Chris@0 210 if (!$language->isLocked()) {
Chris@0 211 $max_weight = max($max_weight, $language->getWeight());
Chris@0 212 }
Chris@0 213 }
Chris@0 214
Chris@0 215 return $max_weight;
Chris@0 216 }
Chris@0 217
Chris@0 218 }