Chris@0: assertEqual(ConfigurableLanguage::load('en')->getWeight(), 0, 'The English language has a weight of 0.'); Chris@0: Chris@0: // User to add and remove language. Chris@0: $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Check if the Default English language has no path prefix. Chris@0: $this->drupalGet('admin/config/regional/language/detection/url'); Chris@0: $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.'); Chris@0: Chris@0: // Check that Add language is a primary button. Chris@0: $this->drupalGet('admin/config/regional/language/add'); Chris@0: $this->assertFieldByXPath('//input[contains(@class, "button--primary")]', 'Add language', 'Add language is a primary button'); Chris@0: Chris@0: // Add predefined language. Chris@0: $edit = [ Chris@0: 'predefined_langcode' => 'fr', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, 'Add language'); Chris@0: $this->assertText('French'); Chris@18: $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE])->toString(), [], 'Correct page redirection.'); Chris@0: // Langcode for Languages is always 'en'. Chris@0: $language = $this->config('language.entity.fr')->get(); Chris@0: $this->assertEqual($language['langcode'], 'en'); Chris@0: Chris@0: // Check if the Default English language has no path prefix. Chris@0: $this->drupalGet('admin/config/regional/language/detection/url'); Chris@0: $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.'); Chris@0: // Check if French has a path prefix. Chris@0: $this->drupalGet('admin/config/regional/language/detection/url'); Chris@0: $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.'); Chris@0: Chris@0: // Check if we can change the default language. Chris@0: $this->drupalGet('admin/config/regional/language'); Chris@0: $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.'); Chris@0: Chris@0: // Change the default language. Chris@0: $edit = [ Chris@0: 'site_default_language' => 'fr', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save configuration')); Chris@0: $this->rebuildContainer(); Chris@0: $this->assertFieldChecked('edit-site-default-language-fr', 'Default language updated.'); Chris@18: $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE, 'langcode' => 'fr'])->toString(), [], 'Correct page redirection.'); Chris@0: Chris@0: // Check if a valid language prefix is added after changing the default Chris@0: // language. Chris@0: $this->drupalGet('admin/config/regional/language/detection/url'); Chris@0: $this->assertFieldByXPath('//input[@name="prefix[en]"]', 'en', 'A valid path prefix has been added to the previous default language.'); Chris@0: // Check if French still has a path prefix. Chris@0: $this->drupalGet('admin/config/regional/language/detection/url'); Chris@0: $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French still has a path prefix.'); Chris@0: Chris@0: // Check that prefix can be changed. Chris@0: $edit = [ Chris@0: 'prefix[fr]' => 'french', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save configuration')); Chris@0: $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'french', 'French path prefix has changed.'); Chris@0: Chris@0: // Check that the prefix can be removed. Chris@0: $edit = [ Chris@0: 'prefix[fr]' => '', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save configuration')); Chris@0: $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: Chris@0: // Change default negotiation language. Chris@0: $this->config('language.negotiation')->set('selected_langcode', 'fr')->save(); Chris@0: // Check that the prefix of a language that is not the negotiation one Chris@0: // cannot be changed to empty string. Chris@0: $edit = [ Chris@0: 'prefix[en]' => '', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save configuration')); Chris@0: $this->assertText(t('The prefix may only be left blank for the selected detection fallback language.')); Chris@0: Chris@0: // Check that prefix cannot be changed to contain a slash. Chris@0: $edit = [ Chris@0: 'prefix[en]' => 'foo/bar', Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save configuration')); Chris@0: $this->assertText(t('The prefix may not contain a slash.'), 'English prefix cannot be changed to contain a slash.'); Chris@0: Chris@0: // Remove English language and add a new Language to check if langcode of Chris@0: // Language entity is 'en'. Chris@0: $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete')); Chris@0: $this->rebuildContainer(); Chris@0: $this->assertRaw(t('The %language (%langcode) language has been removed.', ['%language' => 'English', '%langcode' => 'en'])); Chris@0: Chris@0: // Ensure that French language has a weight of 1 after being created through Chris@0: // the UI. Chris@0: $french = ConfigurableLanguage::load('fr'); Chris@0: $this->assertEqual($french->getWeight(), 1, 'The French language has a weight of 1.'); Chris@0: // Ensure that French language can now have a weight of 0. Chris@0: $french->setWeight(0)->save(); Chris@0: $this->assertEqual($french->getWeight(), 0, 'The French language has a weight of 0.'); Chris@0: // Ensure that new languages created through the API get a weight of 0. Chris@0: $afrikaans = ConfigurableLanguage::createFromLangcode('af'); Chris@0: $afrikaans->save(); Chris@0: $this->assertEqual($afrikaans->getWeight(), 0, 'The Afrikaans language has a weight of 0.'); Chris@0: // Ensure that a new language can be created with any weight. Chris@0: $arabic = ConfigurableLanguage::createFromLangcode('ar'); Chris@0: $arabic->setWeight(4)->save(); Chris@0: $this->assertEqual($arabic->getWeight(), 4, 'The Arabic language has a weight of 0.'); Chris@0: Chris@0: $edit = [ Chris@0: 'predefined_langcode' => 'de', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language'); Chris@0: $language = $this->config('language.entity.de')->get(); Chris@0: $this->assertEqual($language['langcode'], 'fr'); Chris@0: Chris@0: // Ensure that German language has a weight of 5 after being created through Chris@0: // the UI. Chris@0: $french = ConfigurableLanguage::load('de'); Chris@0: $this->assertEqual($french->getWeight(), 5, 'The German language has a weight of 5.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Functional tests for setting system language weight on adding, editing and deleting languages. Chris@0: */ Chris@0: public function testLanguageConfigurationWeight() { Chris@0: // User to add and remove language. Chris@0: $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->checkConfigurableLanguageWeight(); Chris@0: Chris@0: // Add predefined language. Chris@0: $edit = [ Chris@0: 'predefined_langcode' => 'fr', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language'); Chris@0: $this->checkConfigurableLanguageWeight('after adding new language'); Chris@0: Chris@0: // Re-ordering languages. Chris@0: $edit = [ Chris@0: 'languages[en][weight]' => $this->getHighestConfigurableLanguageWeight() + 1, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language', $edit, 'Save configuration'); Chris@0: $this->checkConfigurableLanguageWeight('after re-ordering'); Chris@0: Chris@0: // Remove predefined language. Chris@0: $this->drupalPostForm('admin/config/regional/language/delete/fr', [], 'Delete'); Chris@0: $this->checkConfigurableLanguageWeight('after deleting a language'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validates system languages are ordered after configurable languages. Chris@0: * Chris@0: * @param string $state Chris@0: * (optional) A string for customizing assert messages, containing the Chris@0: * description of the state of the check, for example: 'after re-ordering'. Chris@0: * Defaults to 'by default'. Chris@0: */ Chris@0: protected function checkConfigurableLanguageWeight($state = 'by default') { Chris@0: // Reset language list. Chris@0: \Drupal::languageManager()->reset(); Chris@0: $max_configurable_language_weight = $this->getHighestConfigurableLanguageWeight(); Chris@0: $replacements = ['@event' => $state]; Chris@0: foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $locked_language) { Chris@0: $replacements['%language'] = $locked_language->getName(); Chris@0: $this->assertTrue($locked_language->getWeight() > $max_configurable_language_weight, format_string('System language %language has higher weight than configurable languages @event', $replacements)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper to get maximum weight of configurable (unlocked) languages. Chris@0: * Chris@0: * @return int Chris@0: * Maximum weight of configurable languages. Chris@0: */ Chris@12: protected function getHighestConfigurableLanguageWeight() { Chris@0: $max_weight = 0; Chris@0: Chris@0: $storage = $this->container->get('entity_type.manager') Chris@0: ->getStorage('configurable_language'); Chris@0: $storage->resetCache(); Chris@0: /* @var $languages \Drupal\Core\Language\LanguageInterface[] */ Chris@0: $languages = $storage->loadMultiple(); Chris@0: foreach ($languages as $language) { Chris@0: if (!$language->isLocked()) { Chris@0: $max_weight = max($max_weight, $language->getWeight()); Chris@0: } Chris@0: } Chris@0: Chris@0: return $max_weight; Chris@0: } Chris@0: Chris@0: }