View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class LanguageConfigurationTest extends BrowserTestBase {
public static $modules = [
'language',
];
protected $defaultTheme = 'stark';
public function testLanguageConfiguration() {
$this
->assertEqual(ConfigurableLanguage::load('en')
->getWeight(), 0, '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
->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
$this
->drupalGet('admin/config/regional/language/add');
$this
->assertFieldByXPath('//input[contains(@class, "button--primary")]', 'Add language', 'Add language is a primary button');
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalPostForm(NULL, $edit, 'Add language');
$this
->assertText('French');
$this
->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], [
'absolute' => TRUE,
])
->toString(), [], 'Correct page redirection.');
$language = $this
->config('language.entity.fr')
->get();
$this
->assertEqual($language['langcode'], 'en');
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.');
$this
->drupalGet('admin/config/regional/language');
$this
->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
$edit = [
'site_default_language' => 'fr',
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this
->rebuildContainer();
$this
->assertFieldChecked('edit-site-default-language-fr', 'Default language updated.');
$this
->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], [
'absolute' => TRUE,
'langcode' => 'fr',
])
->toString(), [], 'Correct page redirection.');
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertFieldByXPath('//input[@name="prefix[en]"]', 'en', 'A valid path prefix has been added to the previous default language.');
$this
->drupalGet('admin/config/regional/language/detection/url');
$this
->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French still has a path prefix.');
$edit = [
'prefix[fr]' => 'french',
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this
->assertFieldByXPath('//input[@name="prefix[fr]"]', 'french', 'French path prefix has changed.');
$edit = [
'prefix[fr]' => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$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');
$this
->config('language.negotiation')
->set('selected_langcode', 'fr')
->save();
$edit = [
'prefix[en]' => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this
->assertText(t('The prefix may only be left blank for the selected detection fallback language.'));
$edit = [
'prefix[en]' => 'foo/bar',
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this
->assertText(t('The prefix may not contain a slash.'), 'English prefix cannot be changed to contain a slash.');
$this
->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
$this
->rebuildContainer();
$this
->assertRaw(t('The %language (%langcode) language has been removed.', [
'%language' => 'English',
'%langcode' => 'en',
]));
$french = ConfigurableLanguage::load('fr');
$this
->assertEqual($french
->getWeight(), 1, 'The French language has a weight of 1.');
$french
->setWeight(0)
->save();
$this
->assertEqual($french
->getWeight(), 0, 'The French language has a weight of 0.');
$afrikaans = ConfigurableLanguage::createFromLangcode('af');
$afrikaans
->save();
$this
->assertEqual($afrikaans
->getWeight(), 0, 'The Afrikaans language has a weight of 0.');
$arabic = ConfigurableLanguage::createFromLangcode('ar');
$arabic
->setWeight(4)
->save();
$this
->assertEqual($arabic
->getWeight(), 4, 'The Arabic language has a weight of 0.');
$edit = [
'predefined_langcode' => 'de',
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
$language = $this
->config('language.entity.de')
->get();
$this
->assertEqual($language['langcode'], 'fr');
$french = ConfigurableLanguage::load('de');
$this
->assertEqual($french
->getWeight(), 5, '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
->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
$this
->checkConfigurableLanguageWeight('after adding new language');
$edit = [
'languages[en][weight]' => $this
->getHighestConfigurableLanguageWeight() + 1,
];
$this
->drupalPostForm('admin/config/regional/language', $edit, 'Save configuration');
$this
->checkConfigurableLanguageWeight('after re-ordering');
$this
->drupalPostForm('admin/config/regional/language/delete/fr', [], 'Delete');
$this
->checkConfigurableLanguageWeight('after deleting a language');
}
protected function checkConfigurableLanguageWeight($state = 'by default') {
\Drupal::languageManager()
->reset();
$max_configurable_language_weight = $this
->getHighestConfigurableLanguageWeight();
$replacements = [
'@event' => $state,
];
foreach (\Drupal::languageManager()
->getLanguages(LanguageInterface::STATE_LOCKED) as $locked_language) {
$replacements['%language'] = $locked_language
->getName();
$this
->assertTrue($locked_language
->getWeight() > $max_configurable_language_weight, new FormattableMarkup('System language %language has higher weight than configurable languages @event', $replacements));
}
}
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;
}
}