3 namespace Drupal\FunctionalTests\Installer;
6 * Tests translation files for multiple languages get imported during install.
10 class InstallerTranslationMultipleLanguageTest extends InstallerTestBase {
13 * Switch to the multilingual testing profile.
17 protected $profile = 'testing_multilingual';
22 protected function setUpLanguage() {
23 // Place custom local translations in the translations directory.
24 mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
25 file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de'));
26 file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.es.po', $this->getPo('es'));
28 parent::setUpLanguage();
32 * Returns the string for the test .po file.
34 * @param string $langcode
37 * Contents for the test .po file.
39 protected function getPo($langcode) {
44 msgid "Save and continue"
45 msgstr "Save and continue $langcode"
48 msgstr "Anonymous $langcode"
51 msgstr "Language $langcode"
53 #: Testing site name configuration during the installer.
62 protected function installParameters() {
63 $params = parent::installParameters();
64 $params['forms']['install_configure_form']['site_name'] = 'SITE_NAME_' . $this->langcode;
69 * Tests that translations ended up at the expected places.
71 public function testTranslationsLoaded() {
72 // Ensure the title is correct.
73 $this->assertEqual('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name'));
75 // Verify German and Spanish were configured.
76 $this->drupalGet('admin/config/regional/language');
77 $this->assertText('German');
78 $this->assertText('Spanish');
79 // If the installer was English or we used a profile that keeps English, we
80 // expect that configured also. Otherwise English should not be configured
82 if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
83 $this->assertText('English');
86 $this->assertNoText('English');
89 // Verify the strings from the translation files were imported.
90 $this->verifyImportedStringsTranslated();
92 /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
93 $language_manager = \Drupal::languageManager();
95 // If the site was installed in a foreign language (only tested with German
96 // in subclasses), then the active configuration should be updated and no
97 // override should exist in German. Otherwise the German translation should
98 // end up in overrides the same way as Spanish (which is not used as a site
99 // installation language). English should be available based on profile
100 // information and should be possible to add if not yet added, making
101 // English overrides available.
103 $config = \Drupal::config('user.settings');
104 $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
105 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
106 $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings');
108 if ($this->langcode == 'de') {
109 // Active configuration should be in German and no German override should
111 $this->assertEqual($config->get('anonymous'), 'Anonymous de');
112 $this->assertEqual($config->get('langcode'), 'de');
113 $this->assertTrue($override_de->isNew());
115 if ($this->profile == 'testing_multilingual_with_english') {
116 // English is already added in this profile. Should make the override
118 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
121 // English is not yet available.
122 $this->assertTrue($override_en->isNew());
124 // Adding English should make the English override available.
125 $edit = ['predefined_langcode' => 'en'];
126 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
127 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
128 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
131 // Activate a module, to make sure that config is not overridden by module
134 'modules[views][enable]' => TRUE,
135 'modules[filter][enable]' => TRUE,
137 $this->drupalPostForm('admin/modules', $edit, t('Install'));
139 // Verify the strings from the translation are still as expected.
140 $this->verifyImportedStringsTranslated();
143 // Active configuration should be English.
144 $this->assertEqual($config->get('anonymous'), 'Anonymous');
145 $this->assertEqual($config->get('langcode'), 'en');
146 // There should not be an English override.
147 $this->assertTrue($override_en->isNew());
148 // German should be an override.
149 $this->assertEqual($override_de->get('anonymous'), 'Anonymous de');
152 // Spanish is always an override (never used as installation language).
153 $this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
158 * Helper function to verify that the expected strings are translated.
160 protected function verifyImportedStringsTranslated() {
161 $test_samples = ['Save and continue', 'Anonymous', 'Language'];
162 $langcodes = ['de', 'es'];
164 foreach ($test_samples as $sample) {
165 foreach ($langcodes as $langcode) {
167 $edit['langcode'] = $langcode;
168 $edit['translation'] = 'translated';
169 $edit['string'] = $sample;
170 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
171 $this->assertText($sample . ' ' . $langcode);