53f77c19fd2c005fd2cf1fa7146e6b2b51d64141
[yaffs-website] / web / core / modules / locale / src / Tests / LocaleTranslationUiTest.php
1 <?php
2
3 namespace Drupal\locale\Tests;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Component\Utility\SafeMarkup;
9
10 /**
11  * Adds a new locale and translates its name. Checks the validation of
12  * translation strings and search results.
13  *
14  * @group locale
15  */
16 class LocaleTranslationUiTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['locale'];
24
25   /**
26    * Enable interface translation to English.
27    */
28   public function testEnglishTranslation() {
29     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
30     $this->drupalLogin($admin_user);
31
32     $this->drupalPostForm('admin/config/regional/language/edit/en', ['locale_translate_english' => TRUE], t('Save language'));
33     $this->assertLinkByHref('/admin/config/regional/translate?langcode=en', 0, 'Enabled interface translation to English.');
34   }
35
36   /**
37    * Adds a language and tests string translation by users with the appropriate permissions.
38    */
39   public function testStringTranslation() {
40     // User to add and remove language.
41     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
42     // User to translate and delete string.
43     $translate_user = $this->drupalCreateUser(['translate interface', 'access administration pages']);
44     // Code for the language.
45     $langcode = 'xx';
46     // The English name for the language. This will be translated.
47     $name = $this->randomMachineName(16);
48     // This will be the translation of $name.
49     $translation = $this->randomMachineName(16);
50     $translation_to_en = $this->randomMachineName(16);
51
52     // Add custom language.
53     $this->drupalLogin($admin_user);
54     $edit = [
55       'predefined_langcode' => 'custom',
56       'langcode' => $langcode,
57       'label' => $name,
58       'direction' => LanguageInterface::DIRECTION_LTR,
59     ];
60     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
61     // Add string.
62     t($name, [], ['langcode' => $langcode])->render();
63     // Reset locale cache.
64     $this->container->get('string_translation')->reset();
65     $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
66     $this->assertText(t($name), 'Test language added.');
67     $this->drupalLogout();
68
69     // Search for the name and translate it.
70     $this->drupalLogin($translate_user);
71     $search = [
72       'string' => $name,
73       'langcode' => $langcode,
74       'translation' => 'untranslated',
75     ];
76     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
77     $this->assertText($name, 'Search found the string as untranslated.');
78
79     // No t() here, it's surely not translated yet.
80     $this->assertText($name, 'name found on edit screen.');
81     $this->assertNoOption('edit-langcode', 'en', 'No way to translate the string to English.');
82     $this->drupalLogout();
83     $this->drupalLogin($admin_user);
84     $this->drupalPostForm('admin/config/regional/language/edit/en', ['locale_translate_english' => TRUE], t('Save language'));
85     $this->drupalLogout();
86     $this->drupalLogin($translate_user);
87     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
88     $this->assertText($name, 'Search found the string as untranslated.');
89
90     // Assume this is the only result, given the random name.
91     $textarea = current($this->xpath('//textarea'));
92     $lid = (string) $textarea[0]['name'];
93     $edit = [
94       $lid => $translation,
95     ];
96     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
97     $this->assertText(t('The strings have been saved.'), 'The strings have been saved.');
98     $url_bits = explode('?', $this->getUrl());
99     $this->assertEqual($url_bits[0], \Drupal::url('locale.translate_page', [], ['absolute' => TRUE]), 'Correct page redirection.');
100     $search = [
101       'string' => $name,
102       'langcode' => $langcode,
103       'translation' => 'translated',
104     ];
105     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
106     $this->assertRaw($translation, 'Non-English translation properly saved.');
107
108     $search = [
109       'string' => $name,
110       'langcode' => 'en',
111       'translation' => 'untranslated',
112     ];
113     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
114     $textarea = current($this->xpath('//textarea'));
115     $lid = (string) $textarea[0]['name'];
116     $edit = [
117       $lid => $translation_to_en,
118     ];
119     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
120     $search = [
121       'string' => $name,
122       'langcode' => 'en',
123       'translation' => 'translated',
124     ];
125     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
126     $this->assertRaw($translation_to_en, 'English translation properly saved.');
127
128     $this->assertTrue($name != $translation && t($name, [], ['langcode' => $langcode]) == $translation, 't() works for non-English.');
129     // Refresh the locale() cache to get fresh data from t() below. We are in
130     // the same HTTP request and therefore t() is not refreshed by saving the
131     // translation above.
132     $this->container->get('string_translation')->reset();
133     // Now we should get the proper fresh translation from t().
134     $this->assertTrue($name != $translation_to_en && t($name, [], ['langcode' => 'en']) == $translation_to_en, 't() works for English.');
135     $this->assertTrue(t($name, [], ['langcode' => LanguageInterface::LANGCODE_SYSTEM]) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
136
137     $search = [
138       'string' => $name,
139       'langcode' => 'en',
140       'translation' => 'untranslated',
141     ];
142     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
143     $this->assertText(t('No strings available.'), 'String is translated.');
144
145     // Test invalidation of 'rendered' cache tag after string translation.
146     $this->drupalLogout();
147     $this->drupalGet('xx/user/login');
148     $this->assertText('Enter the password that accompanies your username.');
149
150     $this->drupalLogin($translate_user);
151     $search = [
152       'string' => 'accompanies your username',
153       'langcode' => $langcode,
154       'translation' => 'untranslated',
155     ];
156     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
157     $textarea = current($this->xpath('//textarea'));
158     $lid = (string) $textarea[0]['name'];
159     $edit = [
160       $lid => 'Please enter your Llama username.',
161     ];
162     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
163
164     $this->drupalLogout();
165     $this->drupalGet('xx/user/login');
166     $this->assertText('Please enter your Llama username.');
167
168     // Delete the language.
169     $this->drupalLogin($admin_user);
170     $path = 'admin/config/regional/language/delete/' . $langcode;
171     // This a confirm form, we do not need any fields changed.
172     $this->drupalPostForm($path, [], t('Delete'));
173     // We need raw here because %language and %langcode will add HTML.
174     $t_args = ['%language' => $name, '%langcode' => $langcode];
175     $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
176     // Reload to remove $name.
177     $this->drupalGet($path);
178     // Verify that language is no longer found.
179     $this->assertResponse(404, 'Language no longer found.');
180     $this->drupalLogout();
181
182     // Delete the string.
183     $this->drupalLogin($translate_user);
184     $search = [
185       'string' => $name,
186       'langcode' => 'en',
187       'translation' => 'translated',
188     ];
189     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
190     // Assume this is the only result, given the random name.
191     $textarea = current($this->xpath('//textarea'));
192     $lid = (string) $textarea[0]['name'];
193     $edit = [
194       $lid => '',
195     ];
196     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
197     $this->assertRaw($name, 'The strings have been saved.');
198     $this->drupalLogin($translate_user);
199     $search = [
200       'string' => $name,
201       'langcode' => 'en',
202       'translation' => 'untranslated',
203     ];
204     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
205     $this->assertNoText(t('No strings available.'), 'The translation has been removed');
206   }
207
208   /**
209    * Adds a language and checks that the JavaScript translation files are
210    * properly created and rebuilt on deletion.
211    */
212   public function testJavaScriptTranslation() {
213     $user = $this->drupalCreateUser(['translate interface', 'administer languages', 'access administration pages']);
214     $this->drupalLogin($user);
215     $config = $this->config('locale.settings');
216
217     $langcode = 'xx';
218     // The English name for the language. This will be translated.
219     $name = $this->randomMachineName(16);
220
221     // Add custom language.
222     $edit = [
223       'predefined_langcode' => 'custom',
224       'langcode' => $langcode,
225       'label' => $name,
226       'direction' => LanguageInterface::DIRECTION_LTR,
227     ];
228     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
229     $this->container->get('language_manager')->reset();
230
231     // Build the JavaScript translation file.
232
233     // Retrieve the source string of the first string available in the
234     // {locales_source} table and translate it.
235     $query = db_select('locales_source', 's');
236     $query->addJoin('INNER', 'locales_location', 'l', 's.lid = l.lid');
237     $source = $query->fields('s', ['source'])
238       ->condition('l.type', 'javascript')
239       ->range(0, 1)
240       ->execute()
241       ->fetchField();
242
243     $search = [
244       'string' => $source,
245       'langcode' => $langcode,
246       'translation' => 'all',
247     ];
248     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
249
250     $textarea = current($this->xpath('//textarea'));
251     $lid = (string) $textarea[0]['name'];
252     $edit = [
253       $lid => $this->randomMachineName(),
254     ];
255     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
256
257     // Trigger JavaScript translation parsing and building.
258     _locale_rebuild_js($langcode);
259
260     $locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: [];
261     $js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
262     $this->assertTrue($result = file_exists($js_file), SafeMarkup::format('JavaScript file created: %file', ['%file' => $result ? $js_file : 'not found']));
263
264     // Test JavaScript translation rebuilding.
265     file_unmanaged_delete($js_file);
266     $this->assertTrue($result = !file_exists($js_file), SafeMarkup::format('JavaScript file deleted: %file', ['%file' => $result ? $js_file : 'found']));
267     _locale_rebuild_js($langcode);
268     $this->assertTrue($result = file_exists($js_file), SafeMarkup::format('JavaScript file rebuilt: %file', ['%file' => $result ? $js_file : 'not found']));
269   }
270
271   /**
272    * Tests the validation of the translation input.
273    */
274   public function testStringValidation() {
275     // User to add language and strings.
276     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']);
277     $this->drupalLogin($admin_user);
278     $langcode = 'xx';
279     // The English name for the language. This will be translated.
280     $name = $this->randomMachineName(16);
281
282     // These will be the invalid translations of $name.
283     $key = $this->randomMachineName(16);
284     $bad_translations[$key] = "<script>alert('xss');</script>" . $key;
285     $key = $this->randomMachineName(16);
286     $bad_translations[$key] = '<img SRC="javascript:alert(\'xss\');">' . $key;
287     $key = $this->randomMachineName(16);
288     $bad_translations[$key] = '<<SCRIPT>alert("xss");//<</SCRIPT>' . $key;
289     $key = $this->randomMachineName(16);
290     $bad_translations[$key] = "<BODY ONLOAD=alert('xss')>" . $key;
291
292     // Add custom language.
293     $edit = [
294       'predefined_langcode' => 'custom',
295       'langcode' => $langcode,
296       'label' => $name,
297       'direction' => LanguageInterface::DIRECTION_LTR,
298     ];
299     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
300     // Add string.
301     t($name, [], ['langcode' => $langcode])->render();
302     // Reset locale cache.
303     $search = [
304       'string' => $name,
305       'langcode' => $langcode,
306       'translation' => 'all',
307     ];
308     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
309     // Find the edit path.
310
311     $textarea = current($this->xpath('//textarea'));
312     $lid = (string) $textarea[0]['name'];
313     foreach ($bad_translations as $translation) {
314       $edit = [
315         $lid => $translation,
316       ];
317       $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
318       // Check for a form error on the textarea.
319       $form_class = $this->xpath('//form[@id="locale-translate-edit-form"]//textarea/@class');
320       $this->assertNotIdentical(FALSE, strpos($form_class[0], 'error'), 'The string was rejected as unsafe.');
321       $this->assertNoText(t('The string has been saved.'), 'The string was not saved.');
322     }
323   }
324
325   /**
326    * Tests translation search form.
327    */
328   public function testStringSearch() {
329     // User to add and remove language.
330     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
331     // User to translate and delete string.
332     $translate_user = $this->drupalCreateUser(['translate interface', 'access administration pages']);
333
334     // Code for the language.
335     $langcode = 'xx';
336     // The English name for the language. This will be translated.
337     $name = $this->randomMachineName(16);
338     // This will be the translation of $name.
339     $translation = $this->randomMachineName(16);
340
341     // Add custom language.
342     $this->drupalLogin($admin_user);
343     $edit = [
344       'predefined_langcode' => 'custom',
345       'langcode' => $langcode,
346       'label' => $name,
347       'direction' => LanguageInterface::DIRECTION_LTR,
348     ];
349     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
350
351     $edit = [
352       'predefined_langcode' => 'custom',
353       'langcode' => 'yy',
354       'label' => $this->randomMachineName(16),
355       'direction' => LanguageInterface::DIRECTION_LTR,
356     ];
357     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
358
359     // Add string.
360     t($name, [], ['langcode' => $langcode])->render();
361     // Reset locale cache.
362     $this->container->get('string_translation')->reset();
363     $this->drupalLogout();
364
365     // Search for the name.
366     $this->drupalLogin($translate_user);
367     $search = [
368       'string' => $name,
369       'langcode' => $langcode,
370       'translation' => 'all',
371     ];
372     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
373     // assertText() seems to remove the input field where $name always could be
374     // found, so this is not a false assert. See how assertNoText succeeds
375     // later.
376     $this->assertText($name, 'Search found the string.');
377
378     // Ensure untranslated string doesn't appear if searching on 'only
379     // translated strings'.
380     $search = [
381       'string' => $name,
382       'langcode' => $langcode,
383       'translation' => 'translated',
384     ];
385     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
386     $this->assertText(t('No strings available.'), "Search didn't find the string.");
387
388     // Ensure untranslated string appears if searching on 'only untranslated
389     // strings'.
390     $search = [
391       'string' => $name,
392       'langcode' => $langcode,
393       'translation' => 'untranslated',
394     ];
395     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
396     $this->assertNoText(t('No strings available.'), 'Search found the string.');
397
398     // Add translation.
399     // Assume this is the only result, given the random name.
400     // We save the lid from the path.
401     $textarea = current($this->xpath('//textarea'));
402     $lid = (string) $textarea[0]['name'];
403     $edit = [
404       $lid => $translation,
405     ];
406     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
407
408     // Ensure translated string does appear if searching on 'only
409     // translated strings'.
410     $search = [
411       'string' => $translation,
412       'langcode' => $langcode,
413       'translation' => 'translated',
414     ];
415     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
416     $this->assertNoText(t('No strings available.'), 'Search found the translation.');
417
418     // Ensure translated source string doesn't appear if searching on 'only
419     // untranslated strings'.
420     $search = [
421       'string' => $name,
422       'langcode' => $langcode,
423       'translation' => 'untranslated',
424     ];
425     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
426     $this->assertText(t('No strings available.'), "Search didn't find the source string.");
427
428     // Ensure translated string doesn't appear if searching on 'only
429     // untranslated strings'.
430     $search = [
431       'string' => $translation,
432       'langcode' => $langcode,
433       'translation' => 'untranslated',
434     ];
435     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
436     $this->assertText(t('No strings available.'), "Search didn't find the translation.");
437
438     // Ensure translated string does appear if searching on the custom language.
439     $search = [
440       'string' => $translation,
441       'langcode' => $langcode,
442       'translation' => 'all',
443     ];
444     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
445     $this->assertNoText(t('No strings available.'), 'Search found the translation.');
446
447     // Ensure translated string doesn't appear if searching in System (English).
448     $search = [
449       'string' => $translation,
450       'langcode' => 'yy',
451       'translation' => 'all',
452     ];
453     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
454     $this->assertText(t('No strings available.'), "Search didn't find the translation.");
455
456     // Search for a string that isn't in the system.
457     $unavailable_string = $this->randomMachineName(16);
458     $search = [
459       'string' => $unavailable_string,
460       'langcode' => $langcode,
461       'translation' => 'all',
462     ];
463     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
464     $this->assertText(t('No strings available.'), "Search didn't find the invalid string.");
465   }
466
467   /**
468    * Tests that only changed strings are saved customized when edited.
469    */
470   public function testUICustomizedStrings() {
471     $user = $this->drupalCreateUser(['translate interface', 'administer languages', 'access administration pages']);
472     $this->drupalLogin($user);
473     ConfigurableLanguage::createFromLangcode('de')->save();
474
475     // Create test source string.
476     $string = $this->container->get('locale.storage')->createString([
477       'source' => $this->randomMachineName(100),
478       'context' => $this->randomMachineName(20),
479     ])->save();
480
481     // Create translation for new string and save it as non-customized.
482     $translation = $this->container->get('locale.storage')->createTranslation([
483       'lid' => $string->lid,
484       'language' => 'de',
485       'translation' => $this->randomMachineName(100),
486       'customized' => 0,
487     ])->save();
488
489     // Reset locale cache.
490     $this->container->get('string_translation')->reset();
491
492     // Ensure non-customized translation string does appear if searching
493     // non-customized translation.
494     $search = [
495       'string' => $string->getString(),
496       'langcode' => 'de',
497       'translation' => 'translated',
498       'customized' => '0',
499     ];
500     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
501
502     $this->assertText($translation->getString(), 'Translation is found in search result.');
503
504     // Submit the translations without changing the translation.
505     $textarea = current($this->xpath('//textarea'));
506     $lid = (string) $textarea[0]['name'];
507     $edit = [
508       $lid => $translation->getString(),
509     ];
510     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
511
512     // Ensure unchanged translation string does appear if searching
513     // non-customized translation.
514     $search = [
515       'string' => $string->getString(),
516       'langcode' => 'de',
517       'translation' => 'translated',
518       'customized' => '0',
519     ];
520     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
521     $this->assertText($string->getString(), 'Translation is not marked as customized.');
522
523     // Submit the translations with a new translation.
524     $textarea = current($this->xpath('//textarea'));
525     $lid = (string) $textarea[0]['name'];
526     $edit = [
527       $lid => $this->randomMachineName(100),
528     ];
529     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
530
531     // Ensure changed translation string does appear if searching customized
532     // translation.
533     $search = [
534       'string' => $string->getString(),
535       'langcode' => 'de',
536       'translation' => 'translated',
537       'customized' => '1',
538     ];
539     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
540     $this->assertText($string->getString(), "Translation is marked as customized.");
541   }
542
543 }