3 namespace Drupal\locale\Tests;
5 use Drupal\Core\Language\LanguageInterface;
8 * Tests for updating the interface translations of projects.
12 class LocaleUpdateTest extends LocaleUpdateBase {
17 protected function setUp() {
19 module_load_include('compare.inc', 'locale');
20 module_load_include('fetch.inc', 'locale');
21 $admin_user = $this->drupalCreateUser(['administer modules', 'administer site configuration', 'administer languages', 'access administration pages', 'translate interface']);
22 $this->drupalLogin($admin_user);
23 // We use German as test language. This language must match the translation
24 // file that come with the locale_test module (test.de.po) and can therefore
25 // not be chosen randomly.
26 $this->addLanguage('de');
30 * Checks if a list of translatable projects gets build.
32 public function testUpdateProjects() {
33 module_load_include('compare.inc', 'locale');
35 // Make the test modules look like a normal custom module. i.e. make the
36 // modules not hidden. locale_test_system_info_alter() modifies the project
37 // info of the locale_test and locale_test_translate modules.
38 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
41 // Check if interface translation data is collected from hook_info.
42 $projects = locale_translation_project_list();
43 $this->assertFalse(isset($projects['locale_test_translate']), 'Hidden module not found');
44 $this->assertEqual($projects['locale_test']['info']['interface translation server pattern'], 'core/modules/locale/test/test.%language.po', 'Interface translation parameter found in project info.');
45 $this->assertEqual($projects['locale_test']['name'], 'locale_test', format_string('%key found in project info.', ['%key' => 'interface translation project']));
49 * Checks if local or remote translation sources are detected.
51 * The translation status process by default checks the status of the
52 * installed projects. For testing purpose a predefined set of modules with
53 * fixed file names and release versions is used. This custom project
54 * definition is applied using a hook_locale_translation_projects_alter
55 * implementation in the locale_test module.
57 * This test generates a set of local and remote translation files in their
58 * respective local and remote translation directory. The test checks whether
59 * the most recent files are selected in the different check scenarios: check
60 * for local files only, check for both local and remote files.
62 public function testUpdateCheckStatus() {
63 // Case when contributed modules are absent.
64 $this->drupalGet('admin/reports/translations');
65 $this->assertText(t('Missing translations for one project'));
67 $config = $this->config('locale.settings');
68 // Set a flag to let the locale_test module replace the project data with a
69 // set of test projects.
70 \Drupal::state()->set('locale.test_projects_alter', TRUE);
72 // Create local and remote translations files.
73 $this->setTranslationFiles();
74 $config->set('translation.default_filename', '%project-%version.%language._po')->save();
76 // Set the test conditions.
78 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
80 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
82 // Get status of translation sources at local file system.
83 $this->drupalGet('admin/reports/translations/check');
84 $result = locale_translation_get_status();
85 $this->assertEqual($result['contrib_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_one found');
86 $this->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestampOld, 'Translation timestamp found');
87 $this->assertEqual($result['contrib_module_two']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_two found');
88 $this->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
89 $this->assertEqual($result['locale_test']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of locale_test found');
90 $this->assertEqual($result['custom_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of custom_module_one found');
92 // Set the test conditions.
94 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
96 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
98 // Get status of translation sources at both local and remote locations.
99 $this->drupalGet('admin/reports/translations/check');
100 $result = locale_translation_get_status();
101 $this->assertEqual($result['contrib_module_one']['de']->type, LOCALE_TRANSLATION_REMOTE, 'Translation of contrib_module_one found');
102 $this->assertEqual($result['contrib_module_one']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
103 $this->assertEqual($result['contrib_module_two']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_two found');
104 $this->assertEqual($result['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation timestamp found');
105 $this->assertEqual($result['contrib_module_three']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of contrib_module_three found');
106 $this->assertEqual($result['contrib_module_three']['de']->timestamp, $this->timestampOld, 'Translation timestamp found');
107 $this->assertEqual($result['locale_test']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of locale_test found');
108 $this->assertEqual($result['custom_module_one']['de']->type, LOCALE_TRANSLATION_LOCAL, 'Translation of custom_module_one found');
112 * Tests translation import from remote sources.
115 * - Source: remote and local files
116 * - Import overwrite: all existing translations
118 public function testUpdateImportSourceRemote() {
119 $config = $this->config('locale.settings');
121 // Build the test environment.
122 $this->setTranslationFiles();
123 $this->setCurrentTranslations();
124 $config->set('translation.default_filename', '%project-%version.%language._po');
126 // Set the update conditions for this test.
128 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
129 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
131 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
133 // Get the translation status.
134 $this->drupalGet('admin/reports/translations/check');
136 // Check the status on the Available translation status page.
137 $this->assertRaw('<label for="edit-langcodes-de" class="visually-hidden">Update German</label>', 'German language found');
138 $this->assertText('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test', 'Updates found');
139 $this->assertText('Contributed module one (' . format_date($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module one');
140 $this->assertText('Contributed module two (' . format_date($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module two');
142 // Execute the translation update.
143 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
145 // Check if the translation has been updated, using the status cache.
146 $status = locale_translation_get_status();
147 $this->assertEqual($status['contrib_module_one']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_one found');
148 $this->assertEqual($status['contrib_module_two']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_two found');
149 $this->assertEqual($status['contrib_module_three']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_three found');
151 // Check the new translation status.
152 // The static cache needs to be flushed first to get the most recent data
153 // from the database. The function was called earlier during this test.
154 drupal_static_reset('locale_translation_get_file_history');
155 $history = locale_translation_get_file_history();
156 $this->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestampNow, 'Translation of contrib_module_one is imported');
157 $this->assertTrue($history['contrib_module_one']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_one is updated');
158 $this->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation of contrib_module_two is imported');
159 $this->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_two is updated');
160 $this->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestampMedium, 'Translation of contrib_module_three is not imported');
161 $this->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_three is not updated');
163 // Check whether existing translations have (not) been overwritten.
164 $this->assertEqual(t('January', [], ['langcode' => 'de']), 'Januar_1', 'Translation of January');
165 $this->assertEqual(t('February', [], ['langcode' => 'de']), 'Februar_2', 'Translation of February');
166 $this->assertEqual(t('March', [], ['langcode' => 'de']), 'Marz_2', 'Translation of March');
167 $this->assertEqual(t('April', [], ['langcode' => 'de']), 'April_2', 'Translation of April');
168 $this->assertEqual(t('May', [], ['langcode' => 'de']), 'Mai_customized', 'Translation of May');
169 $this->assertEqual(t('June', [], ['langcode' => 'de']), 'Juni', 'Translation of June');
170 $this->assertEqual(t('Monday', [], ['langcode' => 'de']), 'Montag', 'Translation of Monday');
174 * Tests translation import from local sources.
177 * - Source: local files only
178 * - Import overwrite: all existing translations
180 public function testUpdateImportSourceLocal() {
181 $config = $this->config('locale.settings');
183 // Build the test environment.
184 $this->setTranslationFiles();
185 $this->setCurrentTranslations();
186 $config->set('translation.default_filename', '%project-%version.%language._po');
188 // Set the update conditions for this test.
190 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
191 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
193 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
195 // Execute the translation update.
196 $this->drupalGet('admin/reports/translations/check');
197 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
199 // Check if the translation has been updated, using the status cache.
200 $status = locale_translation_get_status();
201 $this->assertEqual($status['contrib_module_one']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_one found');
202 $this->assertEqual($status['contrib_module_two']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_two found');
203 $this->assertEqual($status['contrib_module_three']['de']->type, LOCALE_TRANSLATION_CURRENT, 'Translation of contrib_module_three found');
205 // Check the new translation status.
206 // The static cache needs to be flushed first to get the most recent data
207 // from the database. The function was called earlier during this test.
208 drupal_static_reset('locale_translation_get_file_history');
209 $history = locale_translation_get_file_history();
210 $this->assertTrue($history['contrib_module_one']['de']->timestamp >= $this->timestampMedium, 'Translation of contrib_module_one is imported');
211 $this->assertEqual($history['contrib_module_one']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_one is updated');
212 $this->assertEqual($history['contrib_module_two']['de']->timestamp, $this->timestampNew, 'Translation of contrib_module_two is imported');
213 $this->assertTrue($history['contrib_module_two']['de']->last_checked >= $this->timestampNow, 'Translation of contrib_module_two is updated');
214 $this->assertEqual($history['contrib_module_three']['de']->timestamp, $this->timestampMedium, 'Translation of contrib_module_three is not imported');
215 $this->assertEqual($history['contrib_module_three']['de']->last_checked, $this->timestampMedium, 'Translation of contrib_module_three is not updated');
217 // Check whether existing translations have (not) been overwritten.
218 $this->assertEqual(t('January', [], ['langcode' => 'de']), 'Januar_customized', 'Translation of January');
219 $this->assertEqual(t('February', [], ['langcode' => 'de']), 'Februar_2', 'Translation of February');
220 $this->assertEqual(t('March', [], ['langcode' => 'de']), 'Marz_2', 'Translation of March');
221 $this->assertEqual(t('April', [], ['langcode' => 'de']), 'April_2', 'Translation of April');
222 $this->assertEqual(t('May', [], ['langcode' => 'de']), 'Mai_customized', 'Translation of May');
223 $this->assertEqual(t('June', [], ['langcode' => 'de']), 'Juni', 'Translation of June');
224 $this->assertEqual(t('Monday', [], ['langcode' => 'de']), 'Montag', 'Translation of Monday');
228 * Tests translation import and only overwrite non-customized translations.
231 * - Source: remote and local files
232 * - Import overwrite: only overwrite non-customized translations
234 public function testUpdateImportModeNonCustomized() {
235 $config = $this->config('locale.settings');
237 // Build the test environment.
238 $this->setTranslationFiles();
239 $this->setCurrentTranslations();
240 $config->set('translation.default_filename', '%project-%version.%language._po');
242 // Set the test conditions.
244 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
245 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED,
247 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
249 // Execute translation update.
250 $this->drupalGet('admin/reports/translations/check');
251 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
253 // Check whether existing translations have (not) been overwritten.
254 $this->assertEqual(t('January', [], ['langcode' => 'de']), 'Januar_customized', 'Translation of January');
255 $this->assertEqual(t('February', [], ['langcode' => 'de']), 'Februar_customized', 'Translation of February');
256 $this->assertEqual(t('March', [], ['langcode' => 'de']), 'Marz_2', 'Translation of March');
257 $this->assertEqual(t('April', [], ['langcode' => 'de']), 'April_2', 'Translation of April');
258 $this->assertEqual(t('May', [], ['langcode' => 'de']), 'Mai_customized', 'Translation of May');
259 $this->assertEqual(t('June', [], ['langcode' => 'de']), 'Juni', 'Translation of June');
260 $this->assertEqual(t('Monday', [], ['langcode' => 'de']), 'Montag', 'Translation of Monday');
264 * Tests translation import and don't overwrite any translation.
267 * - Source: remote and local files
268 * - Import overwrite: don't overwrite any existing translation
270 public function testUpdateImportModeNone() {
271 $config = $this->config('locale.settings');
273 // Build the test environment.
274 $this->setTranslationFiles();
275 $this->setCurrentTranslations();
276 $config->set('translation.default_filename', '%project-%version.%language._po');
278 // Set the test conditions.
280 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
281 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NONE,
283 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
285 // Execute translation update.
286 $this->drupalGet('admin/reports/translations/check');
287 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
289 // Check whether existing translations have (not) been overwritten.
290 $this->assertTranslation('January', 'Januar_customized', 'de');
291 $this->assertTranslation('February', 'Februar_customized', 'de');
292 $this->assertTranslation('March', 'Marz', 'de');
293 $this->assertTranslation('April', 'April_2', 'de');
294 $this->assertTranslation('May', 'Mai_customized', 'de');
295 $this->assertTranslation('June', 'Juni', 'de');
296 $this->assertTranslation('Monday', 'Montag', 'de');
300 * Tests automatic translation import when a module is enabled.
302 public function testEnableUninstallModule() {
303 // Make the hidden test modules look like a normal custom module.
304 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
306 // Check if there is no translation yet.
307 $this->assertTranslation('Tuesday', '', 'de');
311 'modules[locale_test_translate][enable]' => 'locale_test_translate',
313 $this->drupalPostForm('admin/modules', $edit, t('Install'));
315 // Check if translations have been imported.
316 $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.',
317 ['%number' => 7, '%update' => 0, '%delete' => 0]), 'One translation file imported.');
318 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
321 'uninstall[locale_test_translate]' => 1,
323 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
324 $this->drupalPostForm(NULL, [], t('Uninstall'));
326 // Check if the file data is removed from the database.
327 $history = locale_translation_get_file_history();
328 $this->assertFalse(isset($history['locale_test_translate']), 'Project removed from the file history');
329 $projects = locale_translation_get_projects();
330 $this->assertFalse(isset($projects['locale_test_translate']), 'Project removed from the project list');
334 * Tests automatic translation import when a language is added.
336 * When a language is added, the system will check for translations files of
337 * enabled modules and will import them. When a language is removed the system
338 * will remove all translations of that language from the database.
340 public function testEnableLanguage() {
341 // Make the hidden test modules look like a normal custom module.
342 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
346 'modules[locale_test_translate][enable]' => 'locale_test_translate',
348 $this->drupalPostForm('admin/modules', $edit, t('Install'));
350 // Check if there is no Dutch translation yet.
351 $this->assertTranslation('Extraday', '', 'nl');
352 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
356 'predefined_langcode' => 'nl',
358 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
360 // Check if the right number of translations are added.
361 $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.',
362 ['%number' => 8, '%update' => 0, '%delete' => 0]), 'One language added.');
363 $this->assertTranslation('Extraday', 'extra dag', 'nl');
365 // Check if the language data is added to the database.
366 $result = db_query("SELECT project FROM {locale_file} WHERE langcode='nl'")->fetchField();
367 $this->assertTrue($result, 'Files added to file history');
369 // Remove a language.
370 $this->drupalPostForm('admin/config/regional/language/delete/nl', [], t('Delete'));
372 // Check if the language data is removed from the database.
373 $result = db_query("SELECT project FROM {locale_file} WHERE langcode='nl'")->fetchField();
374 $this->assertFalse($result, 'Files removed from file history');
376 // Check that the Dutch translation is gone.
377 $this->assertTranslation('Extraday', '', 'nl');
378 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
382 * Tests automatic translation import when a custom language is added.
384 public function testEnableCustomLanguage() {
385 // Make the hidden test modules look like a normal custom module.
386 \Drupal::state()->set('locale.test_system_info_alter', TRUE);
390 'modules[locale_test_translate][enable]' => 'locale_test_translate',
392 $this->drupalPostForm('admin/modules', $edit, t('Install'));
394 // Create a custom language with language code 'xx' and a random
397 $name = $this->randomMachineName(16);
399 'predefined_langcode' => 'custom',
400 'langcode' => $langcode,
402 'direction' => LanguageInterface::DIRECTION_LTR,
404 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
406 // Ensure the translation file is automatically imported when the language
408 $this->assertText(t('One translation file imported.'), 'Language file automatically imported.');
409 $this->assertText(t('One translation string was skipped because of disallowed or malformed HTML'), 'Language file automatically imported.');
411 // Ensure the strings were successfully imported.
414 'langcode' => $langcode,
415 'translation' => 'translated',
417 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
418 $this->assertNoText(t('No strings available.'), 'String successfully imported.');
420 // Ensure the multiline string was imported.
422 'string' => 'Source string for multiline translation',
423 'langcode' => $langcode,
424 'translation' => 'all',
426 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
427 $this->assertText('Multiline translation string to make sure that import works with it.', 'String successfully imported.');
429 // Ensure 'Allowed HTML source string' was imported but the translation for
430 // 'Another allowed HTML source string' was not because it contains invalid
433 'string' => 'HTML source string',
434 'langcode' => $langcode,
435 'translation' => 'all',
437 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
438 $this->assertText('Allowed HTML source string', 'String successfully imported.');
439 $this->assertNoText('Another allowed HTML source string', 'String with disallowed translation not imported.');