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@0
|
54 ENDPO;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Tests that translations ended up at the expected places.
|
Chris@0
|
59 */
|
Chris@0
|
60 public function testTranslationsLoaded() {
|
Chris@0
|
61 // Verify German and Spanish were configured.
|
Chris@0
|
62 $this->drupalGet('admin/config/regional/language');
|
Chris@0
|
63 $this->assertText('German');
|
Chris@0
|
64 $this->assertText('Spanish');
|
Chris@0
|
65 // If the installer was English or we used a profile that keeps English, we
|
Chris@0
|
66 // expect that configured also. Otherwise English should not be configured
|
Chris@0
|
67 // on the site.
|
Chris@0
|
68 if ($this->langcode == 'en' || $this->profile == 'testing_multilingual_with_english') {
|
Chris@0
|
69 $this->assertText('English');
|
Chris@0
|
70 }
|
Chris@0
|
71 else {
|
Chris@0
|
72 $this->assertNoText('English');
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 // Verify the strings from the translation files were imported.
|
Chris@0
|
76 $this->verifyImportedStringsTranslated();
|
Chris@0
|
77
|
Chris@0
|
78 /** @var \Drupal\language\ConfigurableLanguageManager $language_manager */
|
Chris@0
|
79 $language_manager = \Drupal::languageManager();
|
Chris@0
|
80
|
Chris@0
|
81 // If the site was installed in a foreign language (only tested with German
|
Chris@0
|
82 // in subclasses), then the active configuration should be updated and no
|
Chris@0
|
83 // override should exist in German. Otherwise the German translation should
|
Chris@0
|
84 // end up in overrides the same way as Spanish (which is not used as a site
|
Chris@0
|
85 // installation language). English should be available based on profile
|
Chris@0
|
86 // information and should be possible to add if not yet added, making
|
Chris@0
|
87 // English overrides available.
|
Chris@0
|
88
|
Chris@0
|
89 $config = \Drupal::config('user.settings');
|
Chris@0
|
90 $override_de = $language_manager->getLanguageConfigOverride('de', 'user.settings');
|
Chris@0
|
91 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
|
Chris@0
|
92 $override_es = $language_manager->getLanguageConfigOverride('es', 'user.settings');
|
Chris@0
|
93
|
Chris@0
|
94 if ($this->langcode == 'de') {
|
Chris@0
|
95 // Active configuration should be in German and no German override should
|
Chris@0
|
96 // exist.
|
Chris@0
|
97 $this->assertEqual($config->get('anonymous'), 'Anonymous de');
|
Chris@0
|
98 $this->assertEqual($config->get('langcode'), 'de');
|
Chris@0
|
99 $this->assertTrue($override_de->isNew());
|
Chris@0
|
100
|
Chris@0
|
101 if ($this->profile == 'testing_multilingual_with_english') {
|
Chris@0
|
102 // English is already added in this profile. Should make the override
|
Chris@0
|
103 // available.
|
Chris@0
|
104 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
|
Chris@0
|
105 }
|
Chris@0
|
106 else {
|
Chris@0
|
107 // English is not yet available.
|
Chris@0
|
108 $this->assertTrue($override_en->isNew());
|
Chris@0
|
109
|
Chris@0
|
110 // Adding English should make the English override available.
|
Chris@0
|
111 $edit = ['predefined_langcode' => 'en'];
|
Chris@0
|
112 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
Chris@0
|
113 $override_en = $language_manager->getLanguageConfigOverride('en', 'user.settings');
|
Chris@0
|
114 $this->assertEqual($override_en->get('anonymous'), 'Anonymous');
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 // Activate a module, to make sure that config is not overridden by module
|
Chris@0
|
118 // installation.
|
Chris@0
|
119 $edit = [
|
Chris@0
|
120 'modules[views][enable]' => TRUE,
|
Chris@0
|
121 'modules[filter][enable]' => TRUE,
|
Chris@0
|
122 ];
|
Chris@0
|
123 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
124
|
Chris@0
|
125 // Verify the strings from the translation are still as expected.
|
Chris@0
|
126 $this->verifyImportedStringsTranslated();
|
Chris@0
|
127 }
|
Chris@0
|
128 else {
|
Chris@0
|
129 // Active configuration should be English.
|
Chris@0
|
130 $this->assertEqual($config->get('anonymous'), 'Anonymous');
|
Chris@0
|
131 $this->assertEqual($config->get('langcode'), 'en');
|
Chris@0
|
132 // There should not be an English override.
|
Chris@0
|
133 $this->assertTrue($override_en->isNew());
|
Chris@0
|
134 // German should be an override.
|
Chris@0
|
135 $this->assertEqual($override_de->get('anonymous'), 'Anonymous de');
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 // Spanish is always an override (never used as installation language).
|
Chris@0
|
139 $this->assertEqual($override_es->get('anonymous'), 'Anonymous es');
|
Chris@0
|
140
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * Helper function to verify that the expected strings are translated.
|
Chris@0
|
145 */
|
Chris@0
|
146 protected function verifyImportedStringsTranslated() {
|
Chris@0
|
147 $test_samples = ['Save and continue', 'Anonymous', 'Language'];
|
Chris@0
|
148 $langcodes = ['de', 'es'];
|
Chris@0
|
149
|
Chris@0
|
150 foreach ($test_samples as $sample) {
|
Chris@0
|
151 foreach ($langcodes as $langcode) {
|
Chris@0
|
152 $edit = [];
|
Chris@0
|
153 $edit['langcode'] = $langcode;
|
Chris@0
|
154 $edit['translation'] = 'translated';
|
Chris@0
|
155 $edit['string'] = $sample;
|
Chris@0
|
156 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Filter'));
|
Chris@0
|
157 $this->assertText($sample . ' ' . $langcode);
|
Chris@0
|
158 }
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 }
|