3 namespace Drupal\Tests\config_translation\Functional;
5 use Behat\Mink\Element\NodeElement;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Render\FormattableMarkup;
8 use Drupal\Core\Language\Language;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Test\AssertMailTrait;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\field\Entity\FieldStorageConfig;
13 use Drupal\filter\Entity\FilterFormat;
14 use Drupal\language\Entity\ConfigurableLanguage;
15 use Drupal\node\Entity\NodeType;
16 use Drupal\Tests\BrowserTestBase;
19 * Translate settings and entities to various languages.
21 * @group config_translation
23 class ConfigTranslationUiTest extends BrowserTestBase {
32 public static $modules = [
35 'config_translation_test',
50 * Languages to enable.
54 protected $langcodes = ['fr', 'ta'];
57 * Administrator user for tests.
59 * @var \Drupal\user\UserInterface
64 * Translator user for tests.
66 * @var \Drupal\user\UserInterface
68 protected $translatorUser;
71 * String translation storage object.
73 * @var \Drupal\locale\StringStorageInterface
75 protected $localeStorage;
77 protected function setUp() {
79 $translator_permissions = [
80 'translate configuration',
83 /** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
84 $filter_test_format = FilterFormat::load('filter_test');
85 /** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
86 $filtered_html_format = FilterFormat::load('filtered_html');
87 /** @var \Drupal\filter\FilterFormatInterface $full_html_format */
88 $full_html_format = FilterFormat::load('full_html');
90 $admin_permissions = array_merge(
91 $translator_permissions,
93 'administer languages',
94 'administer site configuration',
96 'administer contact forms',
98 $filtered_html_format->getPermissionName(),
99 $full_html_format->getPermissionName(),
100 $filter_test_format->getPermissionName(),
101 'access site-wide contact form',
102 'access contextual links',
104 'administer account settings',
106 'bypass node access',
107 'administer content types',
108 'translate interface',
111 // Create and log in user.
112 $this->translatorUser = $this->drupalCreateUser($translator_permissions);
113 $this->adminUser = $this->drupalCreateUser($admin_permissions);
116 foreach ($this->langcodes as $langcode) {
117 ConfigurableLanguage::createFromLangcode($langcode)->save();
119 $this->localeStorage = $this->container->get('locale.storage');
120 $this->drupalPlaceBlock('local_tasks_block');
121 $this->drupalPlaceBlock('page_title_block');
125 * Tests the site information translation interface.
127 public function testSiteInformationTranslationUi() {
128 $this->drupalLogin($this->adminUser);
130 $site_name = 'Name of the site for testing configuration translation';
131 $site_slogan = 'Site slogan for testing configuration translation';
132 $site_name_label = 'Site name';
133 $fr_site_name = 'Nom du site pour tester la configuration traduction';
134 $fr_site_slogan = 'Slogan du site pour tester la traduction de configuration';
135 $fr_site_name_label = 'Libellé du champ "Nom du site"';
136 $translation_base_url = 'admin/config/system/site-information/translate';
138 // Set site name and slogan for default language.
139 $this->setSiteInformation($site_name, $site_slogan);
141 $this->drupalGet('admin/config/system/site-information');
142 // Check translation tab exist.
143 $this->assertLinkByHref($translation_base_url);
145 $this->drupalGet($translation_base_url);
147 // Check that the 'Edit' link in the source language links back to the
149 $this->clickLink(t('Edit'));
150 // Also check that saving the form leads back to the translation overview.
151 $this->drupalPostForm(NULL, [], t('Save configuration'));
152 $this->assertUrl($translation_base_url);
154 // Check 'Add' link of French to visit add page.
155 $this->assertLinkByHref("$translation_base_url/fr/add");
156 $this->clickLink(t('Add'));
158 // Make sure original text is present on this page.
159 $this->assertRaw($site_name);
160 $this->assertRaw($site_slogan);
162 // Update site name and slogan for French.
164 'translation[config_names][system.site][name]' => $fr_site_name,
165 'translation[config_names][system.site][slogan]' => $fr_site_slogan,
168 $this->drupalPostForm("$translation_base_url/fr/add", $edit, t('Save translation'));
169 $this->assertRaw(t('Successfully saved @language translation.', ['@language' => 'French']));
171 // Check for edit, delete links (and no 'add' link) for French language.
172 $this->assertNoLinkByHref("$translation_base_url/fr/add");
173 $this->assertLinkByHref("$translation_base_url/fr/edit");
174 $this->assertLinkByHref("$translation_base_url/fr/delete");
176 // Check translation saved proper.
177 $this->drupalGet("$translation_base_url/fr/edit");
178 $this->assertFieldByName('translation[config_names][system.site][name]', $fr_site_name);
179 $this->assertFieldByName('translation[config_names][system.site][slogan]', $fr_site_slogan);
181 // Place branding block with site name and slogan into header region.
182 $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
184 // Check French translation of site name and slogan are in place.
185 $this->drupalGet('fr');
186 $this->assertRaw($fr_site_name);
187 $this->assertRaw($fr_site_slogan);
189 // Visit French site to ensure base language string present as source.
190 $this->drupalGet("fr/$translation_base_url/fr/edit");
191 $this->assertText($site_name);
192 $this->assertText($site_slogan);
194 // Translate 'Site name' label in French.
196 'string' => $site_name_label,
198 'translation' => 'untranslated',
200 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
202 $textarea = current($this->xpath('//textarea'));
203 $lid = $textarea->getAttribute('name');
205 $lid => $fr_site_name_label,
207 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
209 // Ensure that the label is in French (and not in English).
210 $this->drupalGet("fr/$translation_base_url/fr/edit");
211 $this->assertText($fr_site_name_label);
212 $this->assertNoText($site_name_label);
214 // Ensure that the label is also in French (and not in English)
215 // when editing another language with the interface in French.
216 $this->drupalGet("fr/$translation_base_url/ta/edit");
217 $this->assertText($fr_site_name_label);
218 $this->assertNoText($site_name_label);
220 // Ensure that the label is not translated when the interface is in English.
221 $this->drupalGet("$translation_base_url/fr/edit");
222 $this->assertText($site_name_label);
223 $this->assertNoText($fr_site_name_label);
227 * Tests the site information translation interface.
229 public function testSourceValueDuplicateSave() {
230 $this->drupalLogin($this->adminUser);
232 $site_name = 'Site name for testing configuration translation';
233 $site_slogan = 'Site slogan for testing configuration translation';
234 $translation_base_url = 'admin/config/system/site-information/translate';
235 $this->setSiteInformation($site_name, $site_slogan);
237 $this->drupalGet($translation_base_url);
239 // Case 1: Update new value for site slogan and site name.
241 'translation[config_names][system.site][name]' => 'FR ' . $site_name,
242 'translation[config_names][system.site][slogan]' => 'FR ' . $site_slogan,
244 // First time, no overrides, so just Add link.
245 $this->drupalPostForm("$translation_base_url/fr/add", $edit, t('Save translation'));
247 // Read overridden file from active config.
248 $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
250 // Expect both name and slogan in language specific file.
252 'name' => 'FR ' . $site_name,
253 'slogan' => 'FR ' . $site_slogan,
255 $this->assertEqual($expected, $override->get());
257 // Case 2: Update new value for site slogan and default value for site name.
258 $this->drupalGet("$translation_base_url/fr/edit");
259 // Assert that the language configuration does not leak outside of the
260 // translation form into the actual site name and slogan.
261 $this->assertNoText('FR ' . $site_name);
262 $this->assertNoText('FR ' . $site_slogan);
264 'translation[config_names][system.site][name]' => $site_name,
265 'translation[config_names][system.site][slogan]' => 'FR ' . $site_slogan,
267 $this->drupalPostForm(NULL, $edit, t('Save translation'));
268 $this->assertRaw(t('Successfully updated @language translation.', ['@language' => 'French']));
269 $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
271 // Expect only slogan in language specific file.
272 $expected = 'FR ' . $site_slogan;
273 $this->assertEqual($expected, $override->get('slogan'));
275 // Case 3: Keep default value for site name and slogan.
276 $this->drupalGet("$translation_base_url/fr/edit");
277 $this->assertNoText('FR ' . $site_slogan);
279 'translation[config_names][system.site][name]' => $site_name,
280 'translation[config_names][system.site][slogan]' => $site_slogan,
282 $this->drupalPostForm(NULL, $edit, t('Save translation'));
283 $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
285 // Expect no language specific file.
286 $this->assertTrue($override->isNew());
288 // Check configuration page with translator user. Should have no access.
289 $this->drupalLogout();
290 $this->drupalLogin($this->translatorUser);
291 $this->drupalGet('admin/config/system/site-information');
292 $this->assertResponse(403);
294 // While translator can access the translation page, the edit link is not
295 // present due to lack of permissions.
296 $this->drupalGet($translation_base_url);
297 $this->assertNoLink(t('Edit'));
299 // Check 'Add' link for French.
300 $this->assertLinkByHref("$translation_base_url/fr/add");
304 * Tests the contact form translation.
306 public function testContactConfigEntityTranslation() {
307 $this->drupalLogin($this->adminUser);
309 $this->drupalGet('admin/structure/contact');
311 // Check for default contact form configuration entity from Contact module.
312 $this->assertLinkByHref('admin/structure/contact/manage/feedback');
314 // Save default language configuration.
315 $label = 'Send your feedback';
318 'recipients' => 'sales@example.com,support@example.com',
319 'reply' => 'Thank you for your mail',
321 $this->drupalPostForm('admin/structure/contact/manage/feedback', $edit, t('Save'));
323 // Ensure translation link is present.
324 $translation_base_url = 'admin/structure/contact/manage/feedback/translate';
325 $this->assertLinkByHref($translation_base_url);
327 // Make sure translate tab is present.
328 $this->drupalGet('admin/structure/contact/manage/feedback');
329 $this->assertLink(t('Translate @type', ['@type' => 'contact form']));
331 // Visit the form to confirm the changes.
332 $this->drupalGet('contact/feedback');
333 $this->assertText($label);
335 foreach ($this->langcodes as $langcode) {
336 $this->drupalGet($translation_base_url);
337 $this->assertLink(t('Translate @type', ['@type' => 'contact form']));
339 // 'Add' link should be present for $langcode translation.
340 $translation_page_url = "$translation_base_url/$langcode/add";
341 $this->assertLinkByHref($translation_page_url);
343 // Make sure original text is present on this page.
344 $this->drupalGet($translation_page_url);
345 $this->assertText($label);
347 // Update translatable fields.
349 'translation[config_names][contact.form.feedback][label]' => 'Website feedback - ' . $langcode,
350 'translation[config_names][contact.form.feedback][reply]' => 'Thank you for your mail - ' . $langcode,
353 // Save language specific version of form.
354 $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));
356 // Expect translated values in language specific file.
357 $override = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'contact.form.feedback');
359 'label' => 'Website feedback - ' . $langcode,
360 'reply' => 'Thank you for your mail - ' . $langcode,
362 $this->assertEqual($expected, $override->get());
364 // Check for edit, delete links (and no 'add' link) for $langcode.
365 $this->assertNoLinkByHref("$translation_base_url/$langcode/add");
366 $this->assertLinkByHref("$translation_base_url/$langcode/edit");
367 $this->assertLinkByHref("$translation_base_url/$langcode/delete");
369 // Visit language specific version of form to check label.
370 $this->drupalGet($langcode . '/contact/feedback');
371 $this->assertText('Website feedback - ' . $langcode);
375 'subject[0][value]' => 'Test subject',
376 'message[0][value]' => 'Test message',
378 $this->drupalPostForm(NULL, $edit, t('Send message'));
381 // Now that all language translations are present, check translation and
382 // original text all appear in any translated page on the translation
384 foreach ($this->langcodes as $langcode) {
385 $langcode_prefixes = array_merge([''], $this->langcodes);
386 foreach ($langcode_prefixes as $langcode_prefix) {
387 $this->drupalGet(ltrim("$langcode_prefix/$translation_base_url/$langcode/edit", '/'));
388 $this->assertFieldByName('translation[config_names][contact.form.feedback][label]', 'Website feedback - ' . $langcode);
389 $this->assertText($label);
393 // We get all emails so no need to check inside the loop.
394 $captured_emails = $this->getMails();
396 // Check language specific auto reply text in email body.
397 foreach ($captured_emails as $email) {
398 if ($email['id'] == 'contact_page_autoreply') {
399 // Trim because we get an added newline for the body.
400 $this->assertEqual(trim($email['body']), 'Thank you for your mail - ' . $email['langcode']);
404 // Test that delete links work and operations perform properly.
405 foreach ($this->langcodes as $langcode) {
406 $replacements = ['%label' => t('@label @entity_type', ['@label' => $label, '@entity_type' => mb_strtolower(t('Contact form'))]), '@language' => \Drupal::languageManager()->getLanguage($langcode)->getName()];
408 $this->drupalGet("$translation_base_url/$langcode/delete");
409 $this->assertRaw(t('Are you sure you want to delete the @language translation of %label?', $replacements));
410 // Assert link back to list page to cancel delete is present.
411 $this->assertLinkByHref($translation_base_url);
413 $this->drupalPostForm(NULL, [], t('Delete'));
414 $this->assertRaw(t('@language translation of %label was deleted', $replacements));
415 $this->assertLinkByHref("$translation_base_url/$langcode/add");
416 $this->assertNoLinkByHref("translation_base_url/$langcode/edit");
417 $this->assertNoLinkByHref("$translation_base_url/$langcode/delete");
419 // Expect no language specific file present anymore.
420 $override = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'contact.form.feedback');
421 $this->assertTrue($override->isNew());
424 // Check configuration page with translator user. Should have no access.
425 $this->drupalLogout();
426 $this->drupalLogin($this->translatorUser);
427 $this->drupalGet('admin/structure/contact/manage/feedback');
428 $this->assertResponse(403);
430 // While translator can access the translation page, the edit link is not
431 // present due to lack of permissions.
432 $this->drupalGet($translation_base_url);
433 $this->assertNoLink(t('Edit'));
435 // Check 'Add' link for French.
436 $this->assertLinkByHref("$translation_base_url/fr/add");
440 * Tests date format translation.
442 public function testDateFormatTranslation() {
443 $this->drupalLogin($this->adminUser);
445 $this->drupalGet('admin/config/regional/date-time');
447 // Check for medium format.
448 $this->assertLinkByHref('admin/config/regional/date-time/formats/manage/medium');
450 // Save default language configuration for a new format.
452 'label' => 'Custom medium date',
453 'id' => 'custom_medium',
454 'date_format_pattern' => 'Y. m. d. H:i',
456 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
458 // Test translating a default shipped format and our custom format.
460 'medium' => 'Default medium date',
461 'custom_medium' => 'Custom medium date',
463 foreach ($formats as $id => $label) {
464 $translation_base_url = 'admin/config/regional/date-time/formats/manage/' . $id . '/translate';
466 $this->drupalGet($translation_base_url);
468 // 'Add' link should be present for French translation.
469 $translation_page_url = "$translation_base_url/fr/add";
470 $this->assertLinkByHref($translation_page_url);
472 // Make sure original text is present on this page.
473 $this->drupalGet($translation_page_url);
474 $this->assertText($label);
476 // Make sure that the date library is added.
477 $this->assertRaw('core/modules/system/js/system.date.js');
479 // Update translatable fields.
481 'translation[config_names][core.date_format.' . $id . '][label]' => $id . ' - FR',
482 'translation[config_names][core.date_format.' . $id . '][pattern]' => 'D',
485 // Save language specific version of form.
486 $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));
488 // Get translation and check we've got the right value.
489 $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'core.date_format.' . $id);
491 'label' => $id . ' - FR',
494 $this->assertEqual($expected, $override->get());
496 // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
498 $formatted_date = format_date(494015820, $id, NULL, 'America/New_York', 'fr');
499 $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
504 * Tests the account settings translation interface.
506 * This is the only special case so far where we have multiple configuration
507 * names involved building up one configuration translation form. Test that
508 * the translations are saved for all configuration names properly.
510 public function testAccountSettingsConfigurationTranslation() {
511 $this->drupalLogin($this->adminUser);
513 $this->drupalGet('admin/config/people/accounts');
514 $this->assertLink(t('Translate @type', ['@type' => 'account settings']));
516 $this->drupalGet('admin/config/people/accounts/translate');
517 $this->assertLink(t('Translate @type', ['@type' => 'account settings']));
518 $this->assertLinkByHref('admin/config/people/accounts/translate/fr/add');
520 // Update account settings fields for French.
522 'translation[config_names][user.settings][anonymous]' => 'Anonyme',
523 'translation[config_names][user.mail][status_blocked][subject]' => 'Testing, your account is blocked.',
524 'translation[config_names][user.mail][status_blocked][body]' => 'Testing account blocked body.',
527 $this->drupalPostForm('admin/config/people/accounts/translate/fr/add', $edit, t('Save translation'));
529 // Make sure the changes are saved and loaded back properly.
530 $this->drupalGet('admin/config/people/accounts/translate/fr/edit');
531 foreach ($edit as $key => $value) {
532 // Check the translations appear in the right field type as well.
533 $xpath = '//' . (strpos($key, '[body]') ? 'textarea' : 'input') . '[@name="' . $key . '"]';
534 $this->assertFieldByXPath($xpath, $value);
536 // Check that labels for email settings appear.
537 $this->assertText(t('Account cancellation confirmation'));
538 $this->assertText(t('Password recovery'));
542 * Tests source and target language edge cases.
544 public function testSourceAndTargetLanguage() {
545 $this->drupalLogin($this->adminUser);
547 // Loading translation page for not-specified language (und)
548 // should return 403.
549 $this->drupalGet('admin/config/system/site-information/translate/und/add');
550 $this->assertResponse(403);
552 // Check the source language doesn't have 'Add' or 'Delete' link and
553 // make sure source language edit goes to original configuration page
554 // not the translation specific edit page.
555 $this->drupalGet('admin/config/system/site-information/translate');
556 $this->assertNoLinkByHref('admin/config/system/site-information/translate/en/edit');
557 $this->assertNoLinkByHref('admin/config/system/site-information/translate/en/add');
558 $this->assertNoLinkByHref('admin/config/system/site-information/translate/en/delete');
559 $this->assertLinkByHref('admin/config/system/site-information');
561 // Translation addition to source language should return 403.
562 $this->drupalGet('admin/config/system/site-information/translate/en/add');
563 $this->assertResponse(403);
565 // Translation editing in source language should return 403.
566 $this->drupalGet('admin/config/system/site-information/translate/en/edit');
567 $this->assertResponse(403);
569 // Translation deletion in source language should return 403.
570 $this->drupalGet('admin/config/system/site-information/translate/en/delete');
571 $this->assertResponse(403);
573 // Set default language of site information to not-specified language (und).
574 $this->config('system.site')
575 ->set('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED)
578 // Make sure translation tab does not exist on the configuration page.
579 $this->drupalGet('admin/config/system/site-information');
580 $this->assertNoLinkByHref('admin/config/system/site-information/translate');
582 // If source language is not specified, translation page should be 403.
583 $this->drupalGet('admin/config/system/site-information/translate');
584 $this->assertResponse(403);
588 * Tests the views translation interface.
590 public function testViewsTranslationUI() {
591 $this->drupalLogin($this->adminUser);
593 $description = 'All content promoted to the front page.';
594 $human_readable_name = 'Frontpage';
595 $display_settings_master = 'Master';
596 $display_options_master = '(Empty)';
597 $translation_base_url = 'admin/structure/views/view/frontpage/translate';
599 $this->drupalGet($translation_base_url);
601 // Check 'Add' link of French to visit add page.
602 $this->assertLinkByHref("$translation_base_url/fr/add");
603 $this->clickLink(t('Add'));
605 // Make sure original text is present on this page.
606 $this->assertRaw($description);
607 $this->assertRaw($human_readable_name);
609 // Update Views Fields for French.
611 'translation[config_names][views.view.frontpage][description]' => $description . " FR",
612 'translation[config_names][views.view.frontpage][label]' => $human_readable_name . " FR",
613 'translation[config_names][views.view.frontpage][display][default][display_title]' => $display_settings_master . " FR",
614 'translation[config_names][views.view.frontpage][display][default][display_options][title]' => $display_options_master . " FR",
616 $this->drupalPostForm("$translation_base_url/fr/add", $edit, t('Save translation'));
617 $this->assertRaw(t('Successfully saved @language translation.', ['@language' => 'French']));
619 // Check for edit, delete links (and no 'add' link) for French language.
620 $this->assertNoLinkByHref("$translation_base_url/fr/add");
621 $this->assertLinkByHref("$translation_base_url/fr/edit");
622 $this->assertLinkByHref("$translation_base_url/fr/delete");
624 // Check translation saved proper.
625 $this->drupalGet("$translation_base_url/fr/edit");
626 $this->assertFieldByName('translation[config_names][views.view.frontpage][description]', $description . " FR");
627 $this->assertFieldByName('translation[config_names][views.view.frontpage][label]', $human_readable_name . " FR");
628 $this->assertFieldByName('translation[config_names][views.view.frontpage][display][default][display_title]', $display_settings_master . " FR");
629 $this->assertFieldByName('translation[config_names][views.view.frontpage][display][default][display_options][title]', $display_options_master . " FR");
633 * Test the number of source elements for plural strings in config translation forms.
635 public function testPluralConfigStringsSourceElements() {
636 $this->drupalLogin($this->adminUser);
638 // Languages to test, with various number of plural forms.
640 'vi' => ['plurals' => 1, 'expected' => [TRUE, FALSE, FALSE, FALSE]],
641 'fr' => ['plurals' => 2, 'expected' => [TRUE, TRUE, FALSE, FALSE]],
642 'sl' => ['plurals' => 4, 'expected' => [TRUE, TRUE, TRUE, TRUE]],
645 foreach ($languages as $langcode => $data) {
646 // Import a .po file to add a new language with a given number of plural forms
647 $name = \Drupal::service('file_system')->tempnam('temporary://', $langcode . '_') . '.po';
648 file_put_contents($name, $this->getPoFile($data['plurals']));
649 $this->drupalPostForm('admin/config/regional/translate/import', [
650 'langcode' => $langcode,
651 'files[file]' => $name,
654 // Change the config langcode of the 'files' view.
655 $config = \Drupal::service('config.factory')->getEditable('views.view.files');
656 $config->set('langcode', $langcode);
659 // Go to the translation page of the 'files' view.
660 $translation_url = 'admin/structure/views/view/files/translate/en/add';
661 $this->drupalGet($translation_url);
663 // Check if the expected number of source elements are present.
664 foreach ($data['expected'] as $index => $expected) {
666 $this->assertRaw('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index);
669 $this->assertNoRaw('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index);
676 * Test translation of plural strings with multiple plural forms in config.
678 public function testPluralConfigStrings() {
679 $this->drupalLogin($this->adminUser);
681 // First import a .po file with multiple plural forms.
682 // This will also automatically add the 'sl' language.
683 $name = \Drupal::service('file_system')->tempnam('temporary://', "sl_") . '.po';
684 file_put_contents($name, $this->getPoFile(4));
685 $this->drupalPostForm('admin/config/regional/translate/import', [
687 'files[file]' => $name,
690 // Translate the files view, as this one uses numeric formatters.
691 $description = 'Singular form';
692 $field_value = '1 place';
693 $field_value_plural = '@count places';
694 $translation_url = 'admin/structure/views/view/files/translate/sl/add';
695 $this->drupalGet($translation_url);
697 // Make sure original text is present on this page, in addition to 2 new
699 $this->assertRaw($description);
700 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', $field_value);
701 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', $field_value_plural);
702 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', '');
703 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', '');
705 // Then make sure it also works.
707 'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]' => $field_value . ' SL',
708 'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]' => $field_value_plural . ' 1 SL',
709 'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]' => $field_value_plural . ' 2 SL',
710 'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]' => $field_value_plural . ' 3 SL',
712 $this->drupalPostForm($translation_url, $edit, t('Save translation'));
714 // Make sure the values have changed.
715 $this->drupalGet($translation_url);
716 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', "$field_value SL");
717 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', "$field_value_plural 1 SL");
718 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', "$field_value_plural 2 SL");
719 $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', "$field_value_plural 3 SL");
723 * Tests the translation of field and field storage configuration.
725 public function testFieldConfigTranslation() {
726 // Add a test field which has a translatable field setting and a
727 // translatable field storage setting.
728 $field_name = strtolower($this->randomMachineName());
729 $field_storage = FieldStorageConfig::create([
730 'field_name' => $field_name,
731 'entity_type' => 'entity_test',
732 'type' => 'test_field',
735 $translatable_storage_setting = $this->randomString();
736 $field_storage->setSetting('translatable_storage_setting', $translatable_storage_setting);
737 $field_storage->save();
739 $bundle = strtolower($this->randomMachineName());
740 entity_test_create_bundle($bundle);
741 $field = FieldConfig::create([
742 'field_name' => $field_name,
743 'entity_type' => 'entity_test',
747 $translatable_field_setting = $this->randomString();
748 $field->setSetting('translatable_field_setting', $translatable_field_setting);
751 $this->drupalLogin($this->translatorUser);
753 $this->drupalGet("/entity_test/structure/$bundle/fields/entity_test.$bundle.$field_name/translate");
754 $this->clickLink('Add');
756 $this->assertText('Translatable field setting');
757 $this->assertEscaped($translatable_field_setting);
758 $this->assertText('Translatable storage setting');
759 $this->assertEscaped($translatable_storage_setting);
763 * Tests the translation of a boolean field settings.
765 public function testBooleanFieldConfigTranslation() {
766 // Add a test boolean field.
767 $field_name = strtolower($this->randomMachineName());
768 FieldStorageConfig::create([
769 'field_name' => $field_name,
770 'entity_type' => 'entity_test',
774 $bundle = strtolower($this->randomMachineName());
775 entity_test_create_bundle($bundle);
776 $field = FieldConfig::create([
777 'field_name' => $field_name,
778 'entity_type' => 'entity_test',
782 $on_label = 'On label (with <em>HTML</em> & things)';
783 $field->setSetting('on_label', $on_label);
784 $off_label = 'Off label (with <em>HTML</em> & things)';
785 $field->setSetting('off_label', $off_label);
788 $this->drupalLogin($this->translatorUser);
790 $this->drupalGet("/entity_test/structure/$bundle/fields/entity_test.$bundle.$field_name/translate");
791 $this->clickLink('Add');
793 // Checks the text of details summary element that surrounds the translation
795 $this->assertText(Html::escape(strip_tags($on_label)) . ' Boolean settings');
797 // Checks that the correct on and off labels appear on the form.
798 $this->assertEscaped($on_label);
799 $this->assertEscaped($off_label);
803 * Test translation storage in locale storage.
805 public function testLocaleDBStorage() {
806 // Enable import of translations. By default this is disabled for automated
808 $this->config('locale.settings')
809 ->set('translation.import_enabled', TRUE)
810 ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
813 $this->drupalLogin($this->adminUser);
816 $name = $this->randomMachineName(16);
818 'predefined_langcode' => 'custom',
819 'langcode' => $langcode,
821 'direction' => Language::DIRECTION_LTR,
823 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
825 // Make sure there is no translation stored in locale storage before edit.
826 $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
827 $this->assertTrue(empty($translation));
829 // Add custom translation.
831 'translation[config_names][user.settings][anonymous]' => 'Anonyme',
833 $this->drupalPostForm('admin/config/people/accounts/translate/fr/add', $edit, t('Save translation'));
835 // Make sure translation stored in locale storage after saved language
836 // specific configuration translation.
837 $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
838 $this->assertEqual('Anonyme', $translation->getString());
840 // revert custom translations to base translation.
842 'translation[config_names][user.settings][anonymous]' => 'Anonymous',
844 $this->drupalPostForm('admin/config/people/accounts/translate/fr/edit', $edit, t('Save translation'));
846 // Make sure there is no translation stored in locale storage after revert.
847 $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
848 $this->assertEqual('Anonymous', $translation->getString());
852 * Tests the single language existing.
854 public function testSingleLanguageUI() {
855 $this->drupalLogin($this->adminUser);
857 // Delete French language
858 $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
859 $this->assertRaw(t('The %language (%langcode) language has been removed.', ['%language' => 'French', '%langcode' => 'fr']));
861 // Change default language to Tamil.
863 'site_default_language' => 'ta',
865 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
866 $this->assertRaw(t('Configuration saved.'));
868 // Delete English language
869 $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
870 $this->assertRaw(t('The %language (%langcode) language has been removed.', ['%language' => 'English', '%langcode' => 'en']));
872 // Visit account setting translation page, this should not
873 // throw any notices.
874 $this->drupalGet('admin/config/people/accounts/translate');
875 $this->assertResponse(200);
879 * Tests the config_translation_info_alter() hook.
881 public function testAlterInfo() {
882 $this->drupalLogin($this->adminUser);
884 $this->container->get('state')->set('config_translation_test_config_translation_info_alter', TRUE);
885 $this->container->get('plugin.manager.config_translation.mapper')->clearCachedDefinitions();
887 // Check out if the translation page has the altered in settings.
888 $this->drupalGet('admin/config/system/site-information/translate/fr/add');
889 $this->assertText(t('Feed channel'));
890 $this->assertText(t('Feed description'));
892 // Check if the translation page does not have the altered out settings.
893 $this->drupalGet('admin/config/people/accounts/translate/fr/add');
894 $this->assertText(t('Name'));
895 $this->assertNoText(t('Account cancellation confirmation'));
896 $this->assertNoText(t('Password recovery'));
900 * Tests the sequence data type translation.
902 public function testSequenceTranslation() {
903 $this->drupalLogin($this->adminUser);
904 /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
905 $config_factory = $this->container->get('config.factory');
912 $actual = $config_factory
913 ->getEditable('config_translation_test.content')
915 $this->assertEqual($expected, $actual);
918 'translation[config_names][config_translation_test.content][content][value]' => '<p><strong>Hello World</strong> - FR</p>',
919 'translation[config_names][config_translation_test.content][animals][0]' => 'kitten - FR',
920 'translation[config_names][config_translation_test.content][animals][1]' => 'llama - FR',
921 'translation[config_names][config_translation_test.content][animals][2]' => 'elephant - FR',
923 $this->drupalPostForm('admin/config/media/file-system/translate/fr/add', $edit, t('Save translation'));
925 $this->container->get('language.config_factory_override')
926 ->setLanguage(new Language(['id' => 'fr']));
933 $actual = $config_factory
934 ->get('config_translation_test.content')
936 $this->assertEqual($expected, $actual);
940 * Test text_format translation.
942 public function testTextFormatTranslation() {
943 $this->drupalLogin($this->adminUser);
944 /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
945 $config_factory = $this->container->get('config.factory');
948 'value' => '<p><strong>Hello World</strong></p>',
949 'format' => 'plain_text',
951 $actual = $config_factory
952 ->get('config_translation_test.content')
953 ->getOriginal('content', FALSE);
954 $this->assertEqual($expected, $actual);
956 $translation_base_url = 'admin/config/media/file-system/translate';
957 $this->drupalGet($translation_base_url);
959 // 'Add' link should be present for French translation.
960 $translation_page_url = "$translation_base_url/fr/add";
961 $this->assertLinkByHref($translation_page_url);
963 $this->drupalGet($translation_page_url);
965 // Assert that changing the text format is not possible, even for an
967 $this->assertNoFieldByName('translation[config_names][config_translation_test.content][content][format]');
969 // Update translatable fields.
971 'translation[config_names][config_translation_test.content][content][value]' => '<p><strong>Hello World</strong> - FR</p>',
974 // Save language specific version of form.
975 $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));
977 // Get translation and check we've got the right value.
979 'value' => '<p><strong>Hello World</strong> - FR</p>',
980 'format' => 'plain_text',
982 $this->container->get('language.config_factory_override')
983 ->setLanguage(new Language(['id' => 'fr']));
984 $actual = $config_factory
985 ->get('config_translation_test.content')
987 $this->assertEqual($expected, $actual);
989 // Change the text format of the source configuration and verify that the
990 // text format of the translation does not change because that could lead to
991 // security vulnerabilities.
993 ->getEditable('config_translation_test.content')
994 ->set('content.format', 'full_html')
997 $actual = $config_factory
998 ->get('config_translation_test.content')
1000 // The translation should not have changed, so re-use $expected.
1001 $this->assertEqual($expected, $actual);
1003 // Because the text is now in a text format that the translator does not
1004 // have access to, the translator should not be able to translate it.
1005 $translation_page_url = "$translation_base_url/fr/edit";
1006 $this->drupalLogin($this->translatorUser);
1007 $this->drupalGet($translation_page_url);
1008 $this->assertDisabledTextarea('edit-translation-config-names-config-translation-testcontent-content-value');
1009 $this->drupalPostForm(NULL, [], t('Save translation'));
1010 // Check that submitting the form did not update the text format of the
1012 $actual = $config_factory
1013 ->get('config_translation_test.content')
1015 $this->assertEqual($expected, $actual);
1017 // The administrator must explicitly change the text format.
1018 $this->drupalLogin($this->adminUser);
1020 'translation[config_names][config_translation_test.content][content][format]' => 'full_html',
1022 $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));
1024 'value' => '<p><strong>Hello World</strong> - FR</p>',
1025 'format' => 'full_html',
1027 $actual = $config_factory
1028 ->get('config_translation_test.content')
1030 $this->assertEqual($expected, $actual);
1034 * Tests field translation for node fields.
1036 public function testNodeFieldTranslation() {
1037 NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
1039 $field_name = 'translatable_field';
1040 $field_storage = FieldStorageConfig::create([
1041 'field_name' => $field_name,
1042 'entity_type' => 'node',
1046 $field_storage->setSetting('translatable_storage_setting', 'translatable_storage_setting');
1047 $field_storage->save();
1048 $field = FieldConfig::create([
1049 'field_name' => $field_name,
1050 'entity_type' => 'node',
1051 'bundle' => 'article',
1055 $this->drupalLogin($this->translatorUser);
1057 $this->drupalGet("/entity_test/structure/article/fields/node.article.$field_name/translate");
1058 $this->clickLink('Add');
1061 'translation[config_names][field.field.node.article.translatable_field][description]' => 'FR Help text.',
1062 'translation[config_names][field.field.node.article.translatable_field][label]' => 'FR label',
1064 $this->drupalPostForm(NULL, $form_values, 'Save translation');
1065 $this->assertText('Successfully saved French translation.');
1067 // Check that the translations are saved.
1068 $this->clickLink('Add');
1069 $this->assertRaw('FR label');
1073 * Gets translation from locale storage.
1075 * @param $config_name
1076 * Configuration object.
1078 * Translation configuration field key.
1080 * String language code to load translation.
1082 * @return bool|mixed
1083 * Returns translation if exists, FALSE otherwise.
1085 protected function getTranslation($config_name, $key, $langcode) {
1086 $settings_locations = $this->localeStorage->getLocations(['type' => 'configuration', 'name' => $config_name]);
1087 $this->assertTrue(!empty($settings_locations), format_string('Configuration locations found for %config_name.', ['%config_name' => $config_name]));
1089 if (!empty($settings_locations)) {
1090 $source = $this->container->get('config.factory')->get($config_name)->get($key);
1091 $source_string = $this->localeStorage->findString(['source' => $source, 'type' => 'configuration']);
1092 $this->assertTrue(!empty($source_string), format_string('Found string for %config_name.%key.', ['%config_name' => $config_name, '%key' => $key]));
1094 if (!empty($source_string)) {
1096 'lid' => $source_string->lid,
1097 'language' => $langcode,
1099 $translations = $this->localeStorage->getTranslations($conditions + ['translated' => TRUE]);
1100 return reset($translations);
1107 * Sets site name and slogan for default language, helps in tests.
1109 * @param string $site_name
1110 * @param string $site_slogan
1112 protected function setSiteInformation($site_name, $site_slogan) {
1114 'site_name' => $site_name,
1115 'site_slogan' => $site_slogan,
1117 $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
1118 $this->assertRaw(t('The configuration options have been saved.'));
1122 * Get server-rendered contextual links for the given contextual link ids.
1125 * An array of contextual link ids.
1126 * @param string $current_path
1127 * The Drupal path for the page for which the contextual links are rendered.
1130 * The response body.
1132 protected function renderContextualLinks($ids, $current_path) {
1134 for ($i = 0; $i < count($ids); $i++) {
1135 $post['ids[' . $i . ']'] = $ids[$i];
1137 return $this->drupalPostWithFormat('contextual/render', 'json', $post, ['query' => ['destination' => $current_path]]);
1141 * Asserts that a textarea with a given ID has been disabled from editing.
1144 * The HTML ID of the textarea.
1147 * TRUE if the assertion passed; FALSE otherwise.
1149 protected function assertDisabledTextarea($id) {
1150 $textarea = $this->xpath('//textarea[@id=:id and contains(@disabled, "disabled")]', [
1153 $textarea = reset($textarea);
1154 $this->assertTrue($textarea instanceof NodeElement, new FormattableMarkup('Disabled field @id exists.', [
1157 $expected = 'This field has been disabled because you do not have sufficient permissions to edit it.';
1158 $this->assertEqual($textarea->getText(), $expected, new FormattableMarkup('Disabled textarea @id hides text in an inaccessible text format.', [
1161 // Make sure the text format select is not shown.
1162 $select_id = str_replace('value', 'format--2', $id);
1163 $select = $this->xpath('//select[@id=:id]', [':id' => $select_id]);
1164 return $this->assertFalse($select, new FormattableMarkup('Field @id does not exist.', [
1170 * Helper function that returns a .po file with a given number of plural forms.
1172 public function getPoFile($plurals) {
1175 $po_file[1] = <<< EOF
1178 "Project-Id-Version: Drupal 8\\n"
1179 "MIME-Version: 1.0\\n"
1180 "Content-Type: text/plain; charset=UTF-8\\n"
1181 "Content-Transfer-Encoding: 8bit\\n"
1182 "Plural-Forms: nplurals=1; plural=0;\\n"
1185 $po_file[2] = <<< EOF
1188 "Project-Id-Version: Drupal 8\\n"
1189 "MIME-Version: 1.0\\n"
1190 "Content-Type: text/plain; charset=UTF-8\\n"
1191 "Content-Transfer-Encoding: 8bit\\n"
1192 "Plural-Forms: nplurals=2; plural=(n>1);\\n"
1195 $po_file[4] = <<< EOF
1198 "Project-Id-Version: Drupal 8\\n"
1199 "MIME-Version: 1.0\\n"
1200 "Content-Type: text/plain; charset=UTF-8\\n"
1201 "Content-Transfer-Encoding: 8bit\\n"
1202 "Plural-Forms: nplurals=4; plural=(((n%100)==1)?(0):(((n%100)==2)?(1):((((n%100)==3)||((n%100)==4))?(2):3)));\\n"
1205 return $po_file[$plurals];