View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
class LanguageListTest extends BrowserTestBase {
protected static $modules = [
'language',
];
protected $defaultTheme = 'stark';
public function testLanguageList() {
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
$languages = \Drupal::service('language_manager')
->getLanguages();
$last_language_weight = end($languages)
->getWeight();
$edit = [
'predefined_langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->assertSession()
->pageTextContains('French');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
$this
->rebuildContainer();
$languages = \Drupal::service('language_manager')
->getLanguages();
$last_language = end($languages);
$this
->assertEquals($last_language_weight + 1, $last_language
->getWeight());
$this
->assertEquals($edit['predefined_langcode'], $last_language
->getId());
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => Language::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
$this
->assertSession()
->responseContains('"edit-languages-' . $langcode . '-weight"');
$this
->assertSession()
->pageTextContains($name);
$language = \Drupal::service('language_manager')
->getLanguage($langcode);
$english = \Drupal::service('language_manager')
->getLanguage('en');
$path = 'admin/config/regional/language';
$this
->drupalGet($path);
$this
->assertSession()
->checkboxChecked('edit-site-default-language-en');
$edit = [
'site_default_language' => $langcode,
];
$this
->submitForm($edit, 'Save configuration');
$this
->rebuildContainer();
$this
->assertSession()
->checkboxNotChecked('edit-site-default-language-en');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
'language' => $language,
]));
$this
->drupalGet('admin/config/regional/language/delete/' . $langcode);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/config/regional/language');
$this
->clickLink('Edit');
$this
->assertSession()
->titleEquals('Edit language | Drupal');
$name = $this
->randomMachineName(16);
$edit = [
'label' => $name,
];
$this
->drupalGet('admin/config/regional/language/edit/' . $langcode);
$this
->submitForm($edit, 'Save language');
$this
->assertSession()
->pageTextContains($name);
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
'language' => $language,
]));
$edit = [
'site_default_language' => 'en',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save configuration');
$this
->rebuildContainer();
$this
->drupalGet('admin/config/regional/language');
$this
->clickLink('Delete');
$this
->assertSession()
->pageTextContains('Are you sure you want to delete the language');
$this
->drupalGet('admin/config/regional/language/delete/' . $langcode);
$this
->clickLink('Cancel');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
'language' => $english,
]));
$this
->assertSession()
->pageTextContains($name);
$this
->drupalGet('admin/config/regional/language/delete/' . $langcode);
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains("The {$name} ({$langcode}) language has been removed.");
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
'language' => $english,
]));
$this
->drupalGet('admin/config/regional/language/delete/' . $langcode);
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet('admin/config/regional/language/delete/fr');
$this
->submitForm([], 'Delete');
$this
->rebuildContainer();
$this
->assertSession()
->pageTextContains("The French (fr) language has been removed.");
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
$this
->drupalGet('admin/config/regional/language/delete/fr');
$this
->assertSession()
->statusCodeEquals(404);
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => Language::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
$this
->assertSession()
->pageTextContains($name);
$path = 'admin/config/regional/language';
$this
->drupalGet($path);
$this
->assertSession()
->checkboxChecked('edit-site-default-language-en');
$edit = [
'site_default_language' => $langcode,
];
$this
->submitForm($edit, 'Save configuration');
$this
->rebuildContainer();
$this
->assertSession()
->checkboxNotChecked('edit-site-default-language-en');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
'language' => $language,
]));
$this
->drupalGet('admin/config/regional/language/delete/en');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains("The English (en) language has been removed.");
$this
->rebuildContainer();
$this
->drupalGet('admin/config/regional/language/delete/und');
$this
->assertSession()
->statusCodeEquals(403);
$edit = [
'predefined_langcode' => 'nl',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$this
->drupalGet('admin/config/regional/language');
$language_storage = $this->container
->get('entity_type.manager')
->getStorage('configurable_language');
$language_storage
->load('nl')
->delete();
$this
->submitForm([
'site_default_language' => 'nl',
], 'Save configuration');
$this
->assertSession()
->pageTextContains('Selected default language no longer exists.');
$this
->assertSession()
->checkboxNotChecked('edit-site-default-language-xx');
}
public function testLanguageStates() {
ConfigurableLanguage::create([
'label' => $this
->randomMachineName(),
'id' => 'l1',
])
->save();
ConfigurableLanguage::create([
'label' => $this
->randomMachineName(),
'id' => 'l2',
'locked' => TRUE,
])
->save();
ConfigurableLanguage::create([
'label' => $this
->randomMachineName(),
'id' => 'l3',
])
->save();
ConfigurableLanguage::create([
'label' => $this
->randomMachineName(),
'id' => 'l4',
'locked' => TRUE,
])
->save();
$expected_locked_languages = [
'l4' => 'l4',
'l2' => 'l2',
'und' => 'und',
'zxx' => 'zxx',
];
$expected_all_languages = [
'l4' => 'l4',
'l3' => 'l3',
'l2' => 'l2',
'l1' => 'l1',
'en' => 'en',
'und' => 'und',
'zxx' => 'zxx',
];
$expected_conf_languages = [
'l3' => 'l3',
'l1' => 'l1',
'en' => 'en',
];
$locked_languages = $this->container
->get('language_manager')
->getLanguages(LanguageInterface::STATE_LOCKED);
$this
->assertEquals([], array_diff_key($expected_locked_languages, $locked_languages), 'Locked languages loaded correctly.');
$all_languages = $this->container
->get('language_manager')
->getLanguages(LanguageInterface::STATE_ALL);
$this
->assertEquals([], array_diff_key($expected_all_languages, $all_languages), 'All languages loaded correctly.');
$conf_languages = $this->container
->get('language_manager')
->getLanguages();
$this
->assertEquals([], array_diff_key($expected_conf_languages, $conf_languages), 'Configurable languages loaded correctly.');
}
}