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