3 namespace Drupal\Tests\locale\Functional;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Component\Utility\SafeMarkup;
11 * Adds a new locale and translates its name. Checks the validation of
12 * translation strings and search results.
16 class LocaleTranslationUiTest extends BrowserTestBase {
23 public static $modules = ['locale'];
26 * Enable interface translation to English.
28 public function testEnglishTranslation() {
29 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
30 $this->drupalLogin($admin_user);
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.');
37 * Adds a language and tests string translation by users with the appropriate permissions.
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.
46 // The English name for the language. This will be translated.
47 $name = 'cucurbitaceae';
48 // This will be the translation of $name.
49 $translation = $this->randomMachineName(16);
50 $translation_to_en = $this->randomMachineName(16);
52 // Add custom language.
53 $this->drupalLogin($admin_user);
55 'predefined_langcode' => 'custom',
56 'langcode' => $langcode,
58 'direction' => LanguageInterface::DIRECTION_LTR,
60 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
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();
69 // Search for the name and translate it.
70 $this->drupalLogin($translate_user);
73 'langcode' => $langcode,
74 'translation' => 'untranslated',
76 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
77 $this->assertText($name, 'Search found the string as untranslated.');
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.');
90 // Assume this is the only result, given the random name.
91 $textarea = current($this->xpath('//textarea'));
92 $lid = $textarea->getAttribute('name');
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.');
102 'langcode' => $langcode,
103 'translation' => 'translated',
105 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
106 $this->assertRaw($translation, 'Non-English translation properly saved.');
111 'translation' => 'untranslated',
113 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
114 $textarea = current($this->xpath('//textarea'));
115 $lid = $textarea->getAttribute('name');
117 $lid => $translation_to_en,
119 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
123 'translation' => 'translated',
125 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
126 $this->assertRaw($translation_to_en, 'English translation properly saved.');
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.');
140 'translation' => 'untranslated',
142 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
143 $this->assertText(t('No strings available.'), 'String is translated.');
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.');
150 $this->drupalLogin($translate_user);
152 'string' => 'accompanies your username',
153 'langcode' => $langcode,
154 'translation' => 'untranslated',
156 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
157 $textarea = current($this->xpath('//textarea'));
158 $lid = $textarea->getAttribute('name');
160 $lid => 'Please enter your Llama username.',
162 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
164 $this->drupalLogout();
165 $this->drupalGet('xx/user/login');
166 $this->assertText('Please enter your Llama username.');
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();
182 // Delete the string.
183 $this->drupalLogin($translate_user);
187 'translation' => 'translated',
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 = $textarea->getAttribute('name');
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);
202 'translation' => 'untranslated',
204 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
205 $this->assertNoText(t('No strings available.'), 'The translation has been removed');
209 * Adds a language and checks that the JavaScript translation files are
210 * properly created and rebuilt on deletion.
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');
218 // The English name for the language. This will be translated.
219 $name = $this->randomMachineName(16);
221 // Add custom language.
223 'predefined_langcode' => 'custom',
224 'langcode' => $langcode,
226 'direction' => LanguageInterface::DIRECTION_LTR,
228 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
229 $this->container->get('language_manager')->reset();
231 // Build the JavaScript translation file.
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')
245 'langcode' => $langcode,
246 'translation' => 'all',
248 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
250 $textarea = current($this->xpath('//textarea'));
251 $lid = $textarea->getAttribute('name');
253 $lid => $this->randomMachineName(),
255 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
257 // Trigger JavaScript translation parsing and building.
258 _locale_rebuild_js($langcode);
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']));
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']));
272 * Tests the validation of the translation input.
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);
279 // The English name for the language. This will be translated.
280 $name = $this->randomMachineName(16);
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;
292 // Add custom language.
294 'predefined_langcode' => 'custom',
295 'langcode' => $langcode,
297 'direction' => LanguageInterface::DIRECTION_LTR,
299 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
301 t($name, [], ['langcode' => $langcode])->render();
302 // Reset locale cache.
305 'langcode' => $langcode,
306 'translation' => 'all',
308 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
309 // Find the edit path.
311 $textarea = current($this->xpath('//textarea'));
312 $lid = $textarea->getAttribute('name');
313 foreach ($bad_translations as $translation) {
315 $lid => $translation,
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->assertContains('error', $form_class[0]->getText(), 'The string was rejected as unsafe.');
321 $this->assertNoText(t('The string has been saved.'), 'The string was not saved.');
326 * Tests translation search form.
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']);
334 // Code for the language.
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);
341 // Add custom language.
342 $this->drupalLogin($admin_user);
344 'predefined_langcode' => 'custom',
345 'langcode' => $langcode,
347 'direction' => LanguageInterface::DIRECTION_LTR,
349 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
352 'predefined_langcode' => 'custom',
354 'label' => $this->randomMachineName(16),
355 'direction' => LanguageInterface::DIRECTION_LTR,
357 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
360 t($name, [], ['langcode' => $langcode])->render();
361 // Reset locale cache.
362 $this->container->get('string_translation')->reset();
363 $this->drupalLogout();
365 // Search for the name.
366 $this->drupalLogin($translate_user);
369 'langcode' => $langcode,
370 'translation' => 'all',
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
376 $this->assertText($name, 'Search found the string.');
378 // Ensure untranslated string doesn't appear if searching on 'only
379 // translated strings'.
382 'langcode' => $langcode,
383 'translation' => 'translated',
385 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
386 $this->assertText(t('No strings available.'), "Search didn't find the string.");
388 // Ensure untranslated string appears if searching on 'only untranslated
392 'langcode' => $langcode,
393 'translation' => 'untranslated',
395 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
396 $this->assertNoText(t('No strings available.'), 'Search found the string.');
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 = $textarea->getAttribute('name');
404 $lid => $translation,
406 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
408 // Ensure translated string does appear if searching on 'only
409 // translated strings'.
411 'string' => $translation,
412 'langcode' => $langcode,
413 'translation' => 'translated',
415 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
416 $this->assertNoText(t('No strings available.'), 'Search found the translation.');
418 // Ensure translated source string doesn't appear if searching on 'only
419 // untranslated strings'.
422 'langcode' => $langcode,
423 'translation' => 'untranslated',
425 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
426 $this->assertText(t('No strings available.'), "Search didn't find the source string.");
428 // Ensure translated string doesn't appear if searching on 'only
429 // untranslated strings'.
431 'string' => $translation,
432 'langcode' => $langcode,
433 'translation' => 'untranslated',
435 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
436 $this->assertText(t('No strings available.'), "Search didn't find the translation.");
438 // Ensure translated string does appear if searching on the custom language.
440 'string' => $translation,
441 'langcode' => $langcode,
442 'translation' => 'all',
444 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
445 $this->assertNoText(t('No strings available.'), 'Search found the translation.');
447 // Ensure translated string doesn't appear if searching in System (English).
449 'string' => $translation,
451 'translation' => 'all',
453 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
454 $this->assertText(t('No strings available.'), "Search didn't find the translation.");
456 // Search for a string that isn't in the system.
457 $unavailable_string = $this->randomMachineName(16);
459 'string' => $unavailable_string,
460 'langcode' => $langcode,
461 'translation' => 'all',
463 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
464 $this->assertText(t('No strings available.'), "Search didn't find the invalid string.");
468 * Tests that only changed strings are saved customized when edited.
470 public function testUICustomizedStrings() {
471 $user = $this->drupalCreateUser(['translate interface', 'administer languages', 'access administration pages']);
472 $this->drupalLogin($user);
473 ConfigurableLanguage::createFromLangcode('de')->save();
475 // Create test source string.
476 $string = $this->container->get('locale.storage')->createString([
477 'source' => $this->randomMachineName(100),
478 'context' => $this->randomMachineName(20),
481 // Create translation for new string and save it as non-customized.
482 $translation = $this->container->get('locale.storage')->createTranslation([
483 'lid' => $string->lid,
485 'translation' => $this->randomMachineName(100),
489 // Reset locale cache.
490 $this->container->get('string_translation')->reset();
492 // Ensure non-customized translation string does appear if searching
493 // non-customized translation.
495 'string' => $string->getString(),
497 'translation' => 'translated',
500 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
502 $this->assertText($translation->getString(), 'Translation is found in search result.');
504 // Submit the translations without changing the translation.
505 $textarea = current($this->xpath('//textarea'));
506 $lid = $textarea->getAttribute('name');
508 $lid => $translation->getString(),
510 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
512 // Ensure unchanged translation string does appear if searching
513 // non-customized translation.
515 'string' => $string->getString(),
517 'translation' => 'translated',
520 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
521 $this->assertText($string->getString(), 'Translation is not marked as customized.');
523 // Submit the translations with a new translation.
524 $textarea = current($this->xpath('//textarea'));
525 $lid = $textarea->getAttribute('name');
527 $lid => $this->randomMachineName(100),
529 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
531 // Ensure changed translation string does appear if searching customized
534 'string' => $string->getString(),
536 'translation' => 'translated',
539 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
540 $this->assertText($string->getString(), "Translation is marked as customized.");