comparison core/modules/system/src/Tests/Installer/InstallerTranslationMultipleLanguageTest.php @ 0:4c8ae668cc8c

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