dcde753d767d61acd15b10b6c168f6d3055a8bc1
[yaffs-website] / web / core / modules / system / src / Tests / Installer / InstallerTranslationMultipleLanguageTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Installer;
4
5 use Drupal\simpletest\InstallerTestBase;
6
7 /**
8  * Tests translation files for multiple languages get imported during install.
9  *
10  * @group Installer
11  */
12 class InstallerTranslationMultipleLanguageTest extends InstallerTestBase {
13
14   /**
15    * Switch to the multilingual testing profile.
16    *
17    * @var string
18    */
19   protected $profile = 'testing_multilingual';
20
21   /**
22    * {@inheritdoc}
23    */
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'));
29
30     parent::setUpLanguage();
31   }
32
33   /**
34    * Returns the string for the test .po file.
35    *
36    * @param string $langcode
37    *   The language code.
38    * @return string
39    *   Contents for the test .po file.
40    */
41   protected function getPo($langcode) {
42     return <<<ENDPO
43 msgid ""
44 msgstr ""
45
46 msgid "Save and continue"
47 msgstr "Save and continue $langcode"
48
49 msgid "Anonymous"
50 msgstr "Anonymous $langcode"
51
52 msgid "Language"
53 msgstr "Language $langcode"
54
55 #: Testing site name configuration during the installer.
56 msgid "Drupal"
57 msgstr "Drupal"
58 ENDPO;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   protected function installParameters() {
65     $params = parent::installParameters();
66     $params['forms']['install_configure_form']['site_name'] = 'SITE_NAME_' . $this->langcode;
67     return $params;
68   }
69
70   /**
71    * Tests that translations ended up at the expected places.
72    */
73   public function testTranslationsLoaded() {
74     // Ensure the title is correct.
75     $this->assertEqual('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name'));
76
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
83     // on the site.
84     if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
85       $this->assertText('English');
86     }
87     else {
88       $this->assertNoText('English');
89     }
90
91     // Verify the strings from the translation files were imported.
92     $this->verifyImportedStringsTranslated();
93
94     /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
95     $language_manager = \Drupal::languageManager();
96
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.
104
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');
109
110     if ($this->langcode == 'de') {
111       // Active configuration should be in German and no German override should
112       // exist.
113       $this->assertEqual($config->get('anonymous'), 'Anonymous de');
114       $this->assertEqual($config->get('langcode'), 'de');
115       $this->assertTrue($override_de->isNew());
116
117       if ($this->profile == 'testing_multilingual_with_english') {
118         // English is already added in this profile. Should make the override
119         // available.
120         $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
121       }
122       else {
123         // English is not yet available.
124         $this->assertTrue($override_en->isNew());
125
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');
131       }
132
133       // Activate a module, to make sure that config is not overridden by module
134       // installation.
135       $edit = [
136         'modules[views][enable]' => TRUE,
137         'modules[filter][enable]' => TRUE,
138       ];
139       $this->drupalPostForm('admin/modules', $edit, t('Install'));
140
141       // Verify the strings from the translation are still as expected.
142       $this->verifyImportedStringsTranslated();
143     }
144     else {
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');
152     }
153
154     // Spanish is always an override (never used as installation language).
155     $this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
156
157   }
158
159   /**
160    * Helper function to verify that the expected strings are translated.
161    */
162   protected function verifyImportedStringsTranslated() {
163     $test_samples = ['Save and continue', 'Anonymous', 'Language'];
164     $langcodes = ['de', 'es'];
165
166     foreach ($test_samples as $sample) {
167       foreach ($langcodes as $langcode) {
168         $edit = [];
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);
174       }
175     }
176   }
177
178 }