Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Installer;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\simpletest\InstallerTestBase;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests translation files for multiple languages get imported during install.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group Installer
|
Chris@0
|
11 */
|
Chris@0
|
12 class InstallerTranslationMultipleLanguageTest extends InstallerTestBase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Switch to the multilingual testing profile.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var string
|
Chris@0
|
18 */
|
Chris@0
|
19 protected $profile = 'testing_multilingual';
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * {@inheritdoc}
|
Chris@0
|
23 */
|
Chris@0
|
24 protected function setUpLanguage() {
|
Chris@0
|
25 // Place custom local translations in the translations directory.
|
Chris@0
|
26 mkdir(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE);
|
Chris@0
|
27 file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de'));
|
Chris@0
|
28 file_put_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.es.po', $this->getPo('es'));
|
Chris@0
|
29
|
Chris@0
|
30 parent::setUpLanguage();
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Returns the string for the test .po file.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @param string $langcode
|
Chris@0
|
37 * The language code.
|
Chris@0
|
38 * @return string
|
Chris@0
|
39 * Contents for the test .po file.
|
Chris@0
|
40 */
|
Chris@0
|
41 protected function getPo($langcode) {
|
Chris@0
|
42 return <<<ENDPO
|
Chris@0
|
43 msgid ""
|
Chris@0
|
44 msgstr ""
|
Chris@0
|
45
|
Chris@0
|
46 msgid "Save and continue"
|
Chris@0
|
47 msgstr "Save and continue $langcode"
|
Chris@0
|
48
|
Chris@0
|
49 msgid "Anonymous"
|
Chris@0
|
50 msgstr "Anonymous $langcode"
|
Chris@0
|
51
|
Chris@0
|
52 msgid "Language"
|
Chris@0
|
53 msgstr "Language $langcode"
|
Chris@12
|
54
|
Chris@12
|
55 #: Testing site name configuration during the installer.
|
Chris@12
|
56 msgid "Drupal"
|
Chris@12
|
57 msgstr "Drupal"
|
Chris@0
|
58 ENDPO;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@12
|
62 * {@inheritdoc}
|
Chris@12
|
63 */
|
Chris@12
|
64 protected function installParameters() {
|
Chris@12
|
65 $params = parent::installParameters();
|
Chris@12
|
66 $params['forms']['install_configure_form']['site_name'] = 'SITE_NAME_' . $this->langcode;
|
Chris@12
|
67 return $params;
|
Chris@12
|
68 }
|
Chris@12
|
69
|
Chris@12
|
70 /**
|
Chris@0
|
71 * Tests that translations ended up at the expected places.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function testTranslationsLoaded() {
|
Chris@12
|
74 // Ensure the title is correct.
|
Chris@12
|
75 $this->assertEqual('SITE_NAME_' . $this->langcode, \Drupal::config('system.site')->get('name'));
|
Chris@12
|
76
|
Chris@0
|
77 // Verify German and Spanish were configured.
|
Chris@0
|
78 $this->drupalGet('admin/config/regional/language');
|
Chris@0
|
79 $this->assertText('German');
|
Chris@0
|
80 $this->assertText('Spanish');
|
Chris@0
|
81 // If the installer was English or we used a profile that keeps English, we
|
Chris@0
|
82 // expect that configured also. Otherwise English should not be configured
|
Chris@0
|
83 // on the site.
|
Chris@0
|
84 if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
|
Chris@0
|
85 $this->assertText('English');
|
Chris@0
|
86 }
|
Chris@0
|
87 else {
|
Chris@0
|
88 $this->assertNoText('English');
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 // Verify the strings from the translation files were imported.
|
Chris@0
|
92 $this->verifyImportedStringsTranslated();
|
Chris@0
|
93
|
Chris@0
|
94 /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
|
Chris@0
|
95 $language_manager = \Drupal::languageManager();
|
Chris@0
|
96
|
Chris@0
|
97 // If the site was installed in a foreign language (only tested with German
|
Chris@0
|
98 // in subclasses), then the active configuration should be updated and no
|
Chris@0
|
99 // override should exist in German. Otherwise the German translation should
|
Chris@0
|
100 // end up in overrides the same way as Spanish (which is not used as a site
|
Chris@0
|
101 // installation language). English should be available based on profile
|
Chris@0
|
102 // information and should be possible to add if not yet added, making
|
Chris@0
|
103 // English overrides available.
|
Chris@0
|
104
|
Chris@0
|
105 $config = \Drupal::config('user.settings');
|
Chris@0
|
106 $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
|
Chris@0
|
107 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
|
Chris@0
|
108 $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings');
|
Chris@0
|
109
|
Chris@0
|
110 if ($this->langcode == 'de') {
|
Chris@0
|
111 // Active configuration should be in German and no German override should
|
Chris@0
|
112 // exist.
|
Chris@0
|
113 $this->assertEqual($config->get('anonymous'), 'Anonymous de');
|
Chris@0
|
114 $this->assertEqual($config->get('langcode'), 'de');
|
Chris@0
|
115 $this->assertTrue($override_de->isNew());
|
Chris@0
|
116
|
Chris@0
|
117 if ($this->profile == 'testing_multilingual_with_english') {
|
Chris@0
|
118 // English is already added in this profile. Should make the override
|
Chris@0
|
119 // available.
|
Chris@0
|
120 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
|
Chris@0
|
121 }
|
Chris@0
|
122 else {
|
Chris@0
|
123 // English is not yet available.
|
Chris@0
|
124 $this->assertTrue($override_en->isNew());
|
Chris@0
|
125
|
Chris@0
|
126 // Adding English should make the English override available.
|
Chris@0
|
127 $edit = ['predefined_langcode' => 'en'];
|
Chris@0
|
128 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
Chris@0
|
129 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
|
Chris@0
|
130 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 // Activate a module, to make sure that config is not overridden by module
|
Chris@0
|
134 // installation.
|
Chris@0
|
135 $edit = [
|
Chris@0
|
136 'modules[views][enable]' => TRUE,
|
Chris@0
|
137 'modules[filter][enable]' => TRUE,
|
Chris@0
|
138 ];
|
Chris@0
|
139 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
140
|
Chris@0
|
141 // Verify the strings from the translation are still as expected.
|
Chris@0
|
142 $this->verifyImportedStringsTranslated();
|
Chris@0
|
143 }
|
Chris@0
|
144 else {
|
Chris@0
|
145 // Active configuration should be English.
|
Chris@0
|
146 $this->assertEqual($config->get('anonymous'), 'Anonymous');
|
Chris@0
|
147 $this->assertEqual($config->get('langcode'), 'en');
|
Chris@0
|
148 // There should not be an English override.
|
Chris@0
|
149 $this->assertTrue($override_en->isNew());
|
Chris@0
|
150 // German should be an override.
|
Chris@0
|
151 $this->assertEqual($override_de->get('anonymous'), 'Anonymous de');
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 // Spanish is always an override (never used as installation language).
|
Chris@0
|
155 $this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
|
Chris@0
|
156
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Helper function to verify that the expected strings are translated.
|
Chris@0
|
161 */
|
Chris@0
|
162 protected function verifyImportedStringsTranslated() {
|
Chris@0
|
163 $test_samples = ['Save and continue', 'Anonymous', 'Language'];
|
Chris@0
|
164 $langcodes = ['de', 'es'];
|
Chris@0
|
165
|
Chris@0
|
166 foreach ($test_samples as $sample) {
|
Chris@0
|
167 foreach ($langcodes as $langcode) {
|
Chris@0
|
168 $edit = [];
|
Chris@0
|
169 $edit['langcode'] = $langcode;
|
Chris@0
|
170 $edit['translation'] = 'translated';
|
Chris@0
|
171 $edit['string'] = $sample;
|
Chris@0
|
172 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
|
Chris@0
|
173 $this->assertText($sample . ' ' . $langcode);
|
Chris@0
|
174 }
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 }
|