View source
<?php
namespace Drupal\FunctionalTests\Installer;
class InstallerTranslationMultipleLanguageTest extends InstallerTestBase {
protected $defaultTheme = 'stark';
protected $profile = 'testing_multilingual';
protected function setUpLanguage() {
mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this
->getPo('de'));
file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.es.po', $this
->getPo('es'));
parent::setUpLanguage();
}
protected function getPo($langcode) {
return <<<ENDPO
msgid ""
msgstr ""
msgid "Save and continue"
msgstr "Save and continue {<span class="php-variable">$langcode</span>}"
msgid "Anonymous"
msgstr "Anonymous {<span class="php-variable">$langcode</span>}"
msgid "Language"
msgstr "Language {<span class="php-variable">$langcode</span>}"
#: Testing site name configuration during the installer.
msgid "Drupal"
msgstr "Drupal"
ENDPO;
}
protected function installParameters() {
$params = parent::installParameters();
$params['forms']['install_configure_form']['site_name'] = 'SITE_NAME_' . $this->langcode;
return $params;
}
public function testTranslationsLoaded() {
$this
->assertEquals('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')
->get('name'));
$this
->drupalGet('admin/config/regional/language');
$this
->assertSession()
->pageTextContains('German');
$this
->assertSession()
->pageTextContains('Spanish');
if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
$this
->assertSession()
->pageTextContains('English');
}
else {
$this
->assertSession()
->pageTextNotContains('English');
}
$this
->verifyImportedStringsTranslated();
$language_manager = \Drupal::languageManager();
$config = \Drupal::config('user.settings');
$override_de = $language_manager
->getLanguageConfigOverride('de', 'user.settings');
$override_en = $language_manager
->getLanguageConfigOverride('en', 'user.settings');
$override_es = $language_manager
->getLanguageConfigOverride('es', 'user.settings');
if ($this->langcode == 'de') {
$this
->assertEquals('Anonymous de', $config
->get('anonymous'));
$this
->assertEquals('de', $config
->get('langcode'));
$this
->assertTrue($override_de
->isNew());
if ($this->profile == 'testing_multilingual_with_english') {
$this
->assertEquals('Anonymous', $override_en
->get('anonymous'));
}
else {
$this
->assertTrue($override_en
->isNew());
$edit = [
'predefined_langcode' => 'en',
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add language');
$override_en = $language_manager
->getLanguageConfigOverride('en', 'user.settings');
$this
->assertEquals('Anonymous', $override_en
->get('anonymous'));
}
$edit = [
'modules[views][enable]' => TRUE,
'modules[filter][enable]' => TRUE,
];
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$this
->verifyImportedStringsTranslated();
}
else {
$this
->assertEquals('Anonymous', $config
->get('anonymous'));
$this
->assertEquals('en', $config
->get('langcode'));
$this
->assertTrue($override_en
->isNew());
$this
->assertEquals('Anonymous de', $override_de
->get('anonymous'));
}
$this
->assertEquals('Anonymous es', $override_es
->get('anonymous'));
}
protected function verifyImportedStringsTranslated() {
$test_samples = [
'Save and continue',
'Anonymous',
'Language',
];
$langcodes = [
'de',
'es',
];
foreach ($test_samples as $sample) {
foreach ($langcodes as $langcode) {
$edit = [];
$edit['langcode'] = $langcode;
$edit['translation'] = 'translated';
$edit['string'] = $sample;
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Filter');
$this
->assertSession()
->pageTextContains($sample . ' ' . $langcode);
}
}
}
}