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