479a60046bf55fb50a6bbee19c7a1ea8d2dff154
[yaffs-website] / web / core / modules / locale / src / Tests / LocaleConfigTranslationImportTest.php
1 <?php
2
3 namespace Drupal\locale\Tests;
4
5 use Drupal\locale\Locale;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
8
9 /**
10  * Tests translation update's effects on configuration translations.
11  *
12  * @group locale
13  */
14 class LocaleConfigTranslationImportTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['language', 'locale_test_translate'];
22
23   /**
24    * Test update changes configuration translations if enabled after language.
25    */
26   public function testConfigTranslationImport() {
27     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions']);
28     $this->drupalLogin($admin_user);
29
30     // Add a language. The Afrikaans translation file of locale_test_translate
31     // (test.af.po) has been prepared with a configuration translation.
32     ConfigurableLanguage::createFromLangcode('af')->save();
33
34     // Enable locale module.
35     $this->container->get('module_installer')->install(['locale']);
36     $this->resetAll();
37
38     // Enable import of translations. By default this is disabled for automated
39     // tests.
40     $this->config('locale.settings')
41       ->set('translation.import_enabled', TRUE)
42       ->save();
43
44     // Add translation permissions now that the locale module has been enabled.
45     $edit = [
46       'authenticated[translate interface]' => 'translate interface',
47     ];
48     $this->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
49
50     // Check and update the translation status. This will import the Afrikaans
51     // translations of locale_test_translate module.
52     $this->drupalGet('admin/reports/translations/check');
53
54     // Override the Drupal core translation status to be up to date.
55     // Drupal core should not be a subject in this test.
56     $status = locale_translation_get_status();
57     $status['drupal']['af']->type = 'current';
58     \Drupal::state()->set('locale.translation_status', $status);
59
60     $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
61
62     // Check if configuration translations have been imported.
63     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'system.maintenance');
64     $this->assertEqual($override->get('message'), 'Ons is tans besig met onderhoud op @site. Wees asseblief geduldig, ons sal binnekort weer terug wees.');
65   }
66
67   /**
68    * Test update changes configuration translations if enabled after language.
69    */
70   public function testConfigTranslationModuleInstall() {
71
72     // Enable locale, block and config_translation modules.
73     $this->container->get('module_installer')->install(['block', 'config_translation']);
74     $this->resetAll();
75
76     // The testing profile overrides locale.settings to disable translation
77     // import. Test that this override is in place.
78     $this->assertFalse($this->config('locale.settings')->get('translation.import_enabled'), 'Translations imports are disabled by default in the Testing profile.');
79
80     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate configuration']);
81     $this->drupalLogin($admin_user);
82
83     // Enable import of translations. By default this is disabled for automated
84     // tests.
85     $this->config('locale.settings')
86       ->set('translation.import_enabled', TRUE)
87       ->save();
88
89     // Add predefined language.
90     $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
91
92     // Add the system branding block to the page.
93     $this->drupalPlaceBlock('system_branding_block', ['region' => 'header', 'id' => 'site-branding']);
94     $this->drupalPostForm('admin/config/system/site-information', ['site_slogan' => 'Test site slogan'], 'Save configuration');
95     $this->drupalPostForm('admin/config/system/site-information/translate/af/edit', ['translation[config_names][system.site][slogan]' => 'Test site slogan in Afrikaans'], 'Save translation');
96
97     // Get the front page and ensure that the translated configuration appears.
98     $this->drupalGet('af');
99     $this->assertText('Test site slogan in Afrikaans');
100
101     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
102     $this->assertEqual('Locale can translate Afrikaans', $override->get('translatable_default_with_translation'));
103
104     // Update test configuration.
105     $override
106       ->set('translatable_no_default', 'This translation is preserved')
107       ->set('translatable_default_with_translation', 'This translation is preserved')
108       ->set('translatable_default_with_no_translation', 'This translation is preserved')
109       ->save();
110
111     // Install any module.
112     $this->drupalPostForm('admin/modules', ['modules[dblog][enable]' => 'dblog'], t('Install'));
113     $this->assertText('Module Database Logging has been enabled.');
114
115     // Get the front page and ensure that the translated configuration still
116     // appears.
117     $this->drupalGet('af');
118     $this->assertText('Test site slogan in Afrikaans');
119
120     $this->rebuildContainer();
121     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
122     $expected = [
123       'translatable_no_default' => 'This translation is preserved',
124       'translatable_default_with_translation' => 'This translation is preserved',
125       'translatable_default_with_no_translation' => 'This translation is preserved'
126     ];
127     $this->assertEqual($expected, $override->get());
128   }
129
130   /**
131    * Test removing a string from Locale deletes configuration translations.
132    */
133   public function testLocaleRemovalAndConfigOverrideDelete() {
134     // Enable the locale module.
135     $this->container->get('module_installer')->install(['locale']);
136     $this->resetAll();
137
138     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
139     $this->drupalLogin($admin_user);
140
141     // Enable import of translations. By default this is disabled for automated
142     // tests.
143     $this->config('locale.settings')
144       ->set('translation.import_enabled', TRUE)
145       ->save();
146
147     // Add predefined language.
148     $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
149
150     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
151     $this->assertEqual(['translatable_default_with_translation' => 'Locale can translate Afrikaans'], $override->get());
152
153     // Remove the string from translation to simulate a Locale removal. Note
154     // that is no current way of doing this in the UI.
155     $locale_storage = \Drupal::service('locale.storage');
156     $string = $locale_storage->findString(['source' => 'Locale can translate']);
157     \Drupal::service('locale.storage')->delete($string);
158     // Force a rebuild of config translations.
159     $count = Locale::config()->updateConfigTranslations(['locale_test_translate.settings'], ['af']);
160     $this->assertEqual($count, 1, 'Correct count of updated translations');
161
162     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
163     $this->assertEqual([], $override->get());
164     $this->assertTrue($override->isNew(), 'The configuration override was deleted when the Locale string was deleted.');
165   }
166
167   /**
168    * Test removing a string from Locale changes configuration translations.
169    */
170   public function testLocaleRemovalAndConfigOverridePreserve() {
171     // Enable the locale module.
172     $this->container->get('module_installer')->install(['locale']);
173     $this->resetAll();
174
175     $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'administer permissions', 'translate interface']);
176     $this->drupalLogin($admin_user);
177
178     // Enable import of translations. By default this is disabled for automated
179     // tests.
180     $this->config('locale.settings')
181       ->set('translation.import_enabled', TRUE)
182       ->save();
183
184     // Add predefined language.
185     $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'af'], t('Add language'));
186
187     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
188     // Update test configuration.
189     $override
190       ->set('translatable_no_default', 'This translation is preserved')
191       ->set('translatable_default_with_no_translation', 'This translation is preserved')
192       ->save();
193     $expected = [
194       'translatable_default_with_translation' => 'Locale can translate Afrikaans',
195       'translatable_no_default' => 'This translation is preserved',
196       'translatable_default_with_no_translation' => 'This translation is preserved'
197     ];
198     $this->assertEqual($expected, $override->get());
199
200     // Set the translated string to empty.
201     $search = [
202       'string' => 'Locale can translate',
203       'langcode' => 'af',
204       'translation' => 'all',
205     ];
206     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
207     $textareas = $this->xpath('//textarea');
208     $textarea = current($textareas);
209     $lid = (string) $textarea[0]['name'];
210     $edit = [
211       $lid => '',
212     ];
213     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
214
215     $override = \Drupal::languageManager()->getLanguageConfigOverride('af', 'locale_test_translate.settings');
216     $expected = [
217       'translatable_no_default' => 'This translation is preserved',
218       'translatable_default_with_no_translation' => 'This translation is preserved'
219     ];
220     $this->assertEqual($expected, $override->get());
221   }
222
223 }