View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class LanguageConfigurationTest extends BrowserTestBase {
protected static $modules = [
'language',
];
protected $defaultTheme = 'stark';
public function testLanguageConfiguration() {
$this
->assertEquals(0, ConfigurableLanguage::load('en')
->getWeight(), 'The English language has a weight of 0.');
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertSession()
->fieldValueEquals("prefix[en]", '');
$this
->drupalGet('admin/config/regional/language/add');
$button = $this
->assertSession()
->buttonExists('Add language');
$this
->assertTrue($button
->hasClass("button--primary"));
$edit = [
'predefined_langcode' => 'fr',
];
$this
->submitForm($edit, 'Add language');
$this
->assertSession()
->pageTextContains('French');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
$language = $this
->config('language.entity.fr')
->get();
$this
->assertEquals('en', $language['langcode']);
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertSession()
->fieldValueEquals("prefix[en]", '');
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertSession()
->fieldValueEquals("prefix[fr]", 'fr');
$this
->drupalGet('admin/config/regional/language');
$this
->assertSession()
->checkboxChecked('edit-site-default-language-en');
$edit = [
'site_default_language' => 'fr',
];
$this
->submitForm($edit, 'Save configuration');
$this
->rebuildContainer();
$this
->assertSession()
->checkboxChecked('edit-site-default-language-fr');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
'langcode' => 'fr',
]));
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertSession()
->fieldValueEquals("prefix[en]", 'en');
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertSession()
->fieldValueEquals("prefix[fr]", 'fr');
$edit = [
'prefix[fr]' => 'french',
];
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->fieldValueEquals("prefix[fr]", 'french');
$edit = [
'prefix[fr]' => '',
];
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextNotContains('The prefix may only be left blank for the selected detection fallback language.');
$this
->config('language.negotiation')
->set('selected_langcode', 'fr')
->save();
$edit = [
'prefix[en]' => '',
];
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('The prefix may only be left blank for the selected detection fallback language.');
$edit = [
'prefix[en]' => 'foo/bar',
];
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('The prefix may not contain a slash.');
$this
->drupalGet('admin/config/regional/language/delete/en');
$this
->submitForm([], 'Delete');
$this
->rebuildContainer();
$this
->assertSession()
->pageTextContains("The English (en) language has been removed.");
$french = ConfigurableLanguage::load('fr');
$this
->assertEquals(1, $french
->getWeight(), 'The French language has a weight of 1.');
$french
->setWeight(0)
->save();
$this
->assertEquals(0, $french
->getWeight(), 'The French language has a weight of 0.');
$afrikaans = ConfigurableLanguage::createFromLangcode('af');
$afrikaans
->save();
$this
->assertEquals(0, $afrikaans
->getWeight(), 'The Afrikaans language has a weight of 0.');
$arabic = ConfigurableLanguage::createFromLangcode('ar');
$arabic
->setWeight(4)
->save();
$this
->assertEquals(4, $arabic
->getWeight(), 'The Arabic language has a weight of 0.');
$edit = [
'predefined_langcode' => 'de',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$language = $this
->config('language.entity.de')
->get();
$this
->assertEquals('fr', $language['langcode']);
$french = ConfigurableLanguage::load('de');
$this
->assertEquals(5, $french
->getWeight(), 'The German language has a weight of 5.');
}
public function testLanguageConfigurationWeight() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$this
->checkConfigurableLanguageWeight();
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->checkConfigurableLanguageWeight('after adding new language');
$edit = [
'languages[en][weight]' => $this
->getHighestConfigurableLanguageWeight() + 1,
];
$this
->drupalGet('admin/config/regional/language');
$this
->submitForm($edit, 'Save configuration');
$this
->checkConfigurableLanguageWeight('after re-ordering');
$this
->drupalGet('admin/config/regional/language/delete/fr');
$this
->submitForm([], 'Delete');
$this
->checkConfigurableLanguageWeight('after deleting a language');
}
protected function checkConfigurableLanguageWeight($state = 'by default') {
\Drupal::languageManager()
->reset();
$max_configurable_language_weight = $this
->getHighestConfigurableLanguageWeight();
foreach (\Drupal::languageManager()
->getLanguages(LanguageInterface::STATE_LOCKED) as $locked_language) {
$this
->assertGreaterThan($max_configurable_language_weight, $locked_language
->getWeight(), sprintf('System language %s does not have higher weight than configurable languages %s', $locked_language
->getName(), $state));
}
}
protected function getHighestConfigurableLanguageWeight() {
$max_weight = 0;
$storage = $this->container
->get('entity_type.manager')
->getStorage('configurable_language');
$storage
->resetCache();
$languages = $storage
->loadMultiple();
foreach ($languages as $language) {
if (!$language
->isLocked()) {
$max_weight = max($max_weight, $language
->getWeight());
}
}
return $max_weight;
}
}