3 namespace Drupal\system\Tests\Installer;
5 use Drupal\simpletest\InstallerTestBase;
8 * Tests translation files for multiple languages get imported during install.
12 class InstallerTranslationMultipleLanguageTest extends InstallerTestBase {
15 * Switch to the multilingual testing profile.
19 protected $profile = 'testing_multilingual';
24 protected function setUpLanguage() {
25 // Place custom local translations in the translations directory.
26 mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
27 file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de'));
28 file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.es.po', $this->getPo('es'));
30 parent::setUpLanguage();
34 * Returns the string for the test .po file.
36 * @param string $langcode
39 * Contents for the test .po file.
41 protected function getPo($langcode) {
46 msgid "Save and continue"
47 msgstr "Save and continue $langcode"
50 msgstr "Anonymous $langcode"
53 msgstr "Language $langcode"
55 #: Testing site name configuration during the installer.
64 protected function installParameters() {
65 $params = parent::installParameters();
66 $params['forms']['install_configure_form']['site_name'] = 'SITE_NAME_' . $this->langcode;
71 * Tests that translations ended up at the expected places.
73 public function testTranslationsLoaded() {
74 // Ensure the title is correct.
75 $this->assertEqual('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name'));
77 // Verify German and Spanish were configured.
78 $this->drupalGet('admin/config/regional/language');
79 $this->assertText('German');
80 $this->assertText('Spanish');
81 // If the installer was English or we used a profile that keeps English, we
82 // expect that configured also. Otherwise English should not be configured
84 if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
85 $this->assertText('English');
88 $this->assertNoText('English');
91 // Verify the strings from the translation files were imported.
92 $this->verifyImportedStringsTranslated();
94 /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
95 $language_manager = \Drupal::languageManager();
97 // If the site was installed in a foreign language (only tested with German
98 // in subclasses), then the active configuration should be updated and no
99 // override should exist in German. Otherwise the German translation should
100 // end up in overrides the same way as Spanish (which is not used as a site
101 // installation language). English should be available based on profile
102 // information and should be possible to add if not yet added, making
103 // English overrides available.
105 $config = \Drupal::config('user.settings');
106 $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
107 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
108 $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings');
110 if ($this->langcode == 'de') {
111 // Active configuration should be in German and no German override should
113 $this->assertEqual($config->get('anonymous'), 'Anonymous de');
114 $this->assertEqual($config->get('langcode'), 'de');
115 $this->assertTrue($override_de->isNew());
117 if ($this->profile == 'testing_multilingual_with_english') {
118 // English is already added in this profile. Should make the override
120 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
123 // English is not yet available.
124 $this->assertTrue($override_en->isNew());
126 // Adding English should make the English override available.
127 $edit = ['predefined_langcode' => 'en'];
128 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
129 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
130 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
133 // Activate a module, to make sure that config is not overridden by module
136 'modules[views][enable]' => TRUE,
137 'modules[filter][enable]' => TRUE,
139 $this->drupalPostForm('admin/modules', $edit, t('Install'));
141 // Verify the strings from the translation are still as expected.
142 $this->verifyImportedStringsTranslated();
145 // Active configuration should be English.
146 $this->assertEqual($config->get('anonymous'), 'Anonymous');
147 $this->assertEqual($config->get('langcode'), 'en');
148 // There should not be an English override.
149 $this->assertTrue($override_en->isNew());
150 // German should be an override.
151 $this->assertEqual($override_de->get('anonymous'), 'Anonymous de');
154 // Spanish is always an override (never used as installation language).
155 $this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
160 * Helper function to verify that the expected strings are translated.
162 protected function verifyImportedStringsTranslated() {
163 $test_samples = ['Save and continue', 'Anonymous', 'Language'];
164 $langcodes = ['de', 'es'];
166 foreach ($test_samples as $sample) {
167 foreach ($langcodes as $langcode) {
169 $edit['langcode'] = $langcode;
170 $edit['translation'] = 'translated';
171 $edit['string'] = $sample;
172 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
173 $this->assertText($sample . ' ' . $langcode);