comparison core/modules/locale/tests/src/Functional/LocaleUpdateTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\locale\Functional;
4
5 use Drupal\Core\Language\LanguageInterface;
6
7 /**
8 * Tests for updating the interface translations of projects.
9 *
10 * @group locale
11 */
12 class LocaleUpdateTest extends LocaleUpdateBase {
13
14 /**
15 * {@inheritdoc}
16 */
17 protected function setUp() {
18 parent::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');
27 }
28
29 /**
30 * Checks if a list of translatable projects gets build.
31 */
32 public function testUpdateProjects() {
33 module_load_include('compare.inc', 'locale');
34
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);
39 $this->resetAll();
40
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']));
46 }
47
48 /**
49 * Checks if local or remote translation sources are detected.
50 *
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.
56 *
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.
61 */
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'));
66
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);
71
72 // Create local and remote translations files.
73 $this->setTranslationFiles();
74 $config->set('translation.default_filename', '%project-%version.%language._po')->save();
75
76 // Set the test conditions.
77 $edit = [
78 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
79 ];
80 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
81
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');
91
92 // Set the test conditions.
93 $edit = [
94 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
95 ];
96 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
97
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');
109 }
110
111 /**
112 * Tests translation import from remote sources.
113 *
114 * Test conditions:
115 * - Source: remote and local files
116 * - Import overwrite: all existing translations
117 */
118 public function testUpdateImportSourceRemote() {
119 $config = $this->config('locale.settings');
120
121 // Build the test environment.
122 $this->setTranslationFiles();
123 $this->setCurrentTranslations();
124 $config->set('translation.default_filename', '%project-%version.%language._po');
125
126 // Set the update conditions for this test.
127 $edit = [
128 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
129 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
130 ];
131 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
132
133 // Get the translation status.
134 $this->drupalGet('admin/reports/translations/check');
135
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');
141
142 // Execute the translation update.
143 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
144
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');
150
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');
162
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');
171 }
172
173 /**
174 * Tests translation import from local sources.
175 *
176 * Test conditions:
177 * - Source: local files only
178 * - Import overwrite: all existing translations
179 */
180 public function testUpdateImportSourceLocal() {
181 $config = $this->config('locale.settings');
182
183 // Build the test environment.
184 $this->setTranslationFiles();
185 $this->setCurrentTranslations();
186 $config->set('translation.default_filename', '%project-%version.%language._po');
187
188 // Set the update conditions for this test.
189 $edit = [
190 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_LOCAL,
191 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_ALL,
192 ];
193 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
194
195 // Execute the translation update.
196 $this->drupalGet('admin/reports/translations/check');
197 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
198
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');
204
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');
216
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');
225 }
226
227 /**
228 * Tests translation import and only overwrite non-customized translations.
229 *
230 * Test conditions:
231 * - Source: remote and local files
232 * - Import overwrite: only overwrite non-customized translations
233 */
234 public function testUpdateImportModeNonCustomized() {
235 $config = $this->config('locale.settings');
236
237 // Build the test environment.
238 $this->setTranslationFiles();
239 $this->setCurrentTranslations();
240 $config->set('translation.default_filename', '%project-%version.%language._po');
241
242 // Set the test conditions.
243 $edit = [
244 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
245 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NON_CUSTOMIZED,
246 ];
247 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
248
249 // Execute translation update.
250 $this->drupalGet('admin/reports/translations/check');
251 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
252
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');
261 }
262
263 /**
264 * Tests translation import and don't overwrite any translation.
265 *
266 * Test conditions:
267 * - Source: remote and local files
268 * - Import overwrite: don't overwrite any existing translation
269 */
270 public function testUpdateImportModeNone() {
271 $config = $this->config('locale.settings');
272
273 // Build the test environment.
274 $this->setTranslationFiles();
275 $this->setCurrentTranslations();
276 $config->set('translation.default_filename', '%project-%version.%language._po');
277
278 // Set the test conditions.
279 $edit = [
280 'use_source' => LOCALE_TRANSLATION_USE_SOURCE_REMOTE_AND_LOCAL,
281 'overwrite' => LOCALE_TRANSLATION_OVERWRITE_NONE,
282 ];
283 $this->drupalPostForm('admin/config/regional/translate/settings', $edit, t('Save configuration'));
284
285 // Execute translation update.
286 $this->drupalGet('admin/reports/translations/check');
287 $this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
288
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');
297 }
298
299 /**
300 * Tests automatic translation import when a module is enabled.
301 */
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);
305
306 // Check if there is no translation yet.
307 $this->assertTranslation('Tuesday', '', 'de');
308
309 // Enable a module.
310 $edit = [
311 'modules[locale_test_translate][enable]' => 'locale_test_translate',
312 ];
313 $this->drupalPostForm('admin/modules', $edit, t('Install'));
314
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');
319
320 $edit = [
321 'uninstall[locale_test_translate]' => 1,
322 ];
323 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
324 $this->drupalPostForm(NULL, [], t('Uninstall'));
325
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');
331 }
332
333 /**
334 * Tests automatic translation import when a language is added.
335 *
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.
339 */
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);
343
344 // Enable a module.
345 $edit = [
346 'modules[locale_test_translate][enable]' => 'locale_test_translate',
347 ];
348 $this->drupalPostForm('admin/modules', $edit, t('Install'));
349
350 // Check if there is no Dutch translation yet.
351 $this->assertTranslation('Extraday', '', 'nl');
352 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
353
354 // Add a language.
355 $edit = [
356 'predefined_langcode' => 'nl',
357 ];
358 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
359
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');
364
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');
368
369 // Remove a language.
370 $this->drupalPostForm('admin/config/regional/language/delete/nl', [], t('Delete'));
371
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');
375
376 // Check that the Dutch translation is gone.
377 $this->assertTranslation('Extraday', '', 'nl');
378 $this->assertTranslation('Tuesday', 'Dienstag', 'de');
379 }
380
381 /**
382 * Tests automatic translation import when a custom language is added.
383 */
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);
387
388 // Enable a module.
389 $edit = [
390 'modules[locale_test_translate][enable]' => 'locale_test_translate',
391 ];
392 $this->drupalPostForm('admin/modules', $edit, t('Install'));
393
394 // Create a custom language with language code 'xx' and a random
395 // name.
396 $langcode = 'xx';
397 $name = $this->randomMachineName(16);
398 $edit = [
399 'predefined_langcode' => 'custom',
400 'langcode' => $langcode,
401 'label' => $name,
402 'direction' => LanguageInterface::DIRECTION_LTR,
403 ];
404 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
405
406 // Ensure the translation file is automatically imported when the language
407 // was added.
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.');
410
411 // Ensure the strings were successfully imported.
412 $search = [
413 'string' => 'lundi',
414 'langcode' => $langcode,
415 'translation' => 'translated',
416 ];
417 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
418 $this->assertNoText(t('No strings available.'), 'String successfully imported.');
419
420 // Ensure the multiline string was imported.
421 $search = [
422 'string' => 'Source string for multiline translation',
423 'langcode' => $langcode,
424 'translation' => 'all',
425 ];
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.');
428
429 // Ensure 'Allowed HTML source string' was imported but the translation for
430 // 'Another allowed HTML source string' was not because it contains invalid
431 // HTML.
432 $search = [
433 'string' => 'HTML source string',
434 'langcode' => $langcode,
435 'translation' => 'all',
436 ];
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.');
440 }
441
442 }