InstallerTranslationMultipleLanguageNonInteractiveTest.php in Drupal 10
File
core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageNonInteractiveTest.php
View source
<?php
namespace Drupal\FunctionalTests\Installer;
use Drupal\Tests\BrowserTestBase;
class InstallerTranslationMultipleLanguageNonInteractiveTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected $profile = 'testing_multilingual';
protected function prepareEnvironment() {
parent::prepareEnvironment();
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'));
}
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_en';
return $params;
}
public function testTranslationsLoaded() {
$this
->drupalLogin($this
->createUser([], NULL, TRUE));
$this
->assertEquals('SITE_NAME_en', \Drupal::config('system.site')
->get('name'));
$this
->drupalGet('admin/config/regional/language');
$this
->assertSession()
->pageTextContains('German');
$this
->assertSession()
->pageTextContains('Spanish');
$this
->assertSession()
->pageTextContains('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');
$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'));
$this
->assertEquals('Montag', t('Monday', [], [
'langcode' => 'de',
]));
}
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);
}
}
}
}