ConfigTranslationUiTest.php 47.1 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\config_translation\Functional;
4

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 12
use Drupal\field\Entity\FieldConfig;
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;
17 18

/**
19 20 21
 * Translate settings and entities to various languages.
 *
 * @group config_translation
22
 */
23 24 25
class ConfigTranslationUiTest extends BrowserTestBase {

  use AssertMailTrait;
26 27 28 29 30 31

  /**
   * Modules to enable.
   *
   * @var array
   */
32
  protected static $modules = [
33
    'block',
34 35 36 37 38 39 40 41 42 43 44 45 46 47
    'config_translation',
    'config_translation_test',
    'contact',
    'contact_test',
    'contextual',
    'entity_test',
    'field_test',
    'field_ui',
    'filter',
    'filter_test',
    'node',
    'views',
    'views_ui',
  ];
48

49 50 51 52 53
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

54 55 56 57 58
  /**
   * Languages to enable.
   *
   * @var array
   */
59
  protected $langcodes = ['fr', 'ta'];
60 61 62 63 64 65

  /**
   * Administrator user for tests.
   *
   * @var \Drupal\user\UserInterface
   */
66
  protected $adminUser;
67 68 69 70 71 72

  /**
   * Translator user for tests.
   *
   * @var \Drupal\user\UserInterface
   */
73
  protected $translatorUser;
74 75 76 77 78 79 80 81

  /**
   * String translation storage object.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected $localeStorage;

82
  protected function setUp(): void {
83
    parent::setUp();
84
    $translator_permissions = [
85
      'translate configuration',
86
    ];
87 88

    /** @var \Drupal\filter\FilterFormatInterface $filter_test_format */
89
    $filter_test_format = FilterFormat::load('filter_test');
90
    /** @var \Drupal\filter\FilterFormatInterface $filtered_html_format */
91
    $filtered_html_format = FilterFormat::load('filtered_html');
92
    /** @var \Drupal\filter\FilterFormatInterface $full_html_format */
93
    $full_html_format = FilterFormat::load('full_html');
94

95 96
    $admin_permissions = array_merge(
      $translator_permissions,
97
      [
98 99
        'administer languages',
        'administer site configuration',
100
        'link to any page',
101
        'administer contact forms',
102 103 104 105
        'administer filters',
        $filtered_html_format->getPermissionName(),
        $full_html_format->getPermissionName(),
        $filter_test_format->getPermissionName(),
106 107 108
        'access site-wide contact form',
        'access contextual links',
        'administer views',
109
        'administer account settings',
110 111 112
        'administer themes',
        'bypass node access',
        'administer content types',
113
        'translate interface',
114
      ]
115
    );
116
    // Create and log in user.
117 118
    $this->translatorUser = $this->drupalCreateUser($translator_permissions);
    $this->adminUser = $this->drupalCreateUser($admin_permissions);
119 120 121

    // Add languages.
    foreach ($this->langcodes as $langcode) {
122
      ConfigurableLanguage::createFromLangcode($langcode)->save();
123 124
    }
    $this->localeStorage = $this->container->get('locale.storage');
125
    $this->drupalPlaceBlock('local_tasks_block');
126
    $this->drupalPlaceBlock('page_title_block');
127 128 129 130 131 132
  }

  /**
   * Tests the site information translation interface.
   */
  public function testSiteInformationTranslationUi() {
133
    $this->drupalLogin($this->adminUser);
134

135
    $site_name = 'Name of the site for testing configuration translation';
136
    $site_slogan = 'Site slogan for testing configuration translation';
137
    $site_name_label = 'Site name';
138 139
    $fr_site_name = 'Nom du site pour tester la configuration traduction';
    $fr_site_slogan = 'Slogan du site pour tester la traduction de configuration';
140
    $fr_site_name_label = 'Libellé du champ "Nom du site"';
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    $translation_base_url = 'admin/config/system/site-information/translate';

    // Set site name and slogan for default language.
    $this->setSiteInformation($site_name, $site_slogan);

    $this->drupalGet('admin/config/system/site-information');
    // Check translation tab exist.
    $this->assertLinkByHref($translation_base_url);

    $this->drupalGet($translation_base_url);

    // Check that the 'Edit' link in the source language links back to the
    // original form.
    $this->clickLink(t('Edit'));
    // Also check that saving the form leads back to the translation overview.
156
    $this->drupalPostForm(NULL, [], t('Save configuration'));
157 158 159 160 161 162 163 164 165 166 167
    $this->assertUrl($translation_base_url);

    // Check 'Add' link of French to visit add page.
    $this->assertLinkByHref("$translation_base_url/fr/add");
    $this->clickLink(t('Add'));

    // Make sure original text is present on this page.
    $this->assertRaw($site_name);
    $this->assertRaw($site_slogan);

    // Update site name and slogan for French.
168
    $edit = [
169 170
      'translation[config_names][system.site][name]' => $fr_site_name,
      'translation[config_names][system.site][slogan]' => $fr_site_slogan,
171
    ];
172 173

    $this->drupalPostForm("$translation_base_url/fr/add", $edit, t('Save translation'));
174
    $this->assertRaw(t('Successfully saved @language translation.', ['@language' => 'French']));
175 176 177 178 179 180 181 182

    // Check for edit, delete links (and no 'add' link) for French language.
    $this->assertNoLinkByHref("$translation_base_url/fr/add");
    $this->assertLinkByHref("$translation_base_url/fr/edit");
    $this->assertLinkByHref("$translation_base_url/fr/delete");

    // Check translation saved proper.
    $this->drupalGet("$translation_base_url/fr/edit");
183 184
    $this->assertFieldByName('translation[config_names][system.site][name]', $fr_site_name);
    $this->assertFieldByName('translation[config_names][system.site][slogan]', $fr_site_slogan);
185

186 187 188
    // Place branding block with site name and slogan into header region.
    $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);

189 190 191 192 193 194 195 196 197
    // Check French translation of site name and slogan are in place.
    $this->drupalGet('fr');
    $this->assertRaw($fr_site_name);
    $this->assertRaw($fr_site_slogan);

    // Visit French site to ensure base language string present as source.
    $this->drupalGet("fr/$translation_base_url/fr/edit");
    $this->assertText($site_name);
    $this->assertText($site_slogan);
198 199

    // Translate 'Site name' label in French.
200
    $search = [
201 202 203
      'string' => $site_name_label,
      'langcode' => 'fr',
      'translation' => 'untranslated',
204
    ];
205 206 207
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));

    $textarea = current($this->xpath('//textarea'));
208
    $lid = $textarea->getAttribute('name');
209
    $edit = [
210
      $lid => $fr_site_name_label,
211
    ];
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));

    // Ensure that the label is in French (and not in English).
    $this->drupalGet("fr/$translation_base_url/fr/edit");
    $this->assertText($fr_site_name_label);
    $this->assertNoText($site_name_label);

    // Ensure that the label is also in French (and not in English)
    // when editing another language with the interface in French.
    $this->drupalGet("fr/$translation_base_url/ta/edit");
    $this->assertText($fr_site_name_label);
    $this->assertNoText($site_name_label);

    // Ensure that the label is not translated when the interface is in English.
    $this->drupalGet("$translation_base_url/fr/edit");
    $this->assertText($site_name_label);
    $this->assertNoText($fr_site_name_label);
229 230 231 232 233 234
  }

  /**
   * Tests the site information translation interface.
   */
  public function testSourceValueDuplicateSave() {
235
    $this->drupalLogin($this->adminUser);
236 237 238 239 240 241 242 243 244

    $site_name = 'Site name for testing configuration translation';
    $site_slogan = 'Site slogan for testing configuration translation';
    $translation_base_url = 'admin/config/system/site-information/translate';
    $this->setSiteInformation($site_name, $site_slogan);

    $this->drupalGet($translation_base_url);

    // Case 1: Update new value for site slogan and site name.
245
    $edit = [
246 247
      'translation[config_names][system.site][name]' => 'FR ' . $site_name,
      'translation[config_names][system.site][slogan]' => 'FR ' . $site_slogan,
248
    ];
249 250 251 252
    // First time, no overrides, so just Add link.
    $this->drupalPostForm("$translation_base_url/fr/add", $edit, t('Save translation'));

    // Read overridden file from active config.
253
    $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
254 255

    // Expect both name and slogan in language specific file.
256
    $expected = [
257 258
      'name' => 'FR ' . $site_name,
      'slogan' => 'FR ' . $site_slogan,
259
    ];
260
    $this->assertEqual($expected, $override->get());
261 262 263 264 265 266 267

    // Case 2: Update new value for site slogan and default value for site name.
    $this->drupalGet("$translation_base_url/fr/edit");
    // Assert that the language configuration does not leak outside of the
    // translation form into the actual site name and slogan.
    $this->assertNoText('FR ' . $site_name);
    $this->assertNoText('FR ' . $site_slogan);
268
    $edit = [
269 270
      'translation[config_names][system.site][name]' => $site_name,
      'translation[config_names][system.site][slogan]' => 'FR ' . $site_slogan,
271
    ];
272
    $this->drupalPostForm(NULL, $edit, t('Save translation'));
273
    $this->assertRaw(t('Successfully updated @language translation.', ['@language' => 'French']));
274
    $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
275 276

    // Expect only slogan in language specific file.
277 278
    $expected = 'FR ' . $site_slogan;
    $this->assertEqual($expected, $override->get('slogan'));
279 280 281 282

    // Case 3: Keep default value for site name and slogan.
    $this->drupalGet("$translation_base_url/fr/edit");
    $this->assertNoText('FR ' . $site_slogan);
283
    $edit = [
284 285
      'translation[config_names][system.site][name]' => $site_name,
      'translation[config_names][system.site][slogan]' => $site_slogan,
286
    ];
287
    $this->drupalPostForm(NULL, $edit, t('Save translation'));
288
    $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
289 290

    // Expect no language specific file.
291
    $this->assertTrue($override->isNew());
292 293 294

    // Check configuration page with translator user. Should have no access.
    $this->drupalLogout();
295
    $this->drupalLogin($this->translatorUser);
296 297 298 299 300 301 302 303 304 305 306 307 308
    $this->drupalGet('admin/config/system/site-information');
    $this->assertResponse(403);

    // While translator can access the translation page, the edit link is not
    // present due to lack of permissions.
    $this->drupalGet($translation_base_url);
    $this->assertNoLink(t('Edit'));

    // Check 'Add' link for French.
    $this->assertLinkByHref("$translation_base_url/fr/add");
  }

  /**
309
   * Tests the contact form translation.
310 311
   */
  public function testContactConfigEntityTranslation() {
312
    $this->drupalLogin($this->adminUser);
313 314 315 316 317 318 319 320

    $this->drupalGet('admin/structure/contact');

    // Check for default contact form configuration entity from Contact module.
    $this->assertLinkByHref('admin/structure/contact/manage/feedback');

    // Save default language configuration.
    $label = 'Send your feedback';
321
    $edit = [
322 323 324
      'label' => $label,
      'recipients' => 'sales@example.com,support@example.com',
      'reply' => 'Thank you for your mail',
325
    ];
326 327 328 329 330 331
    $this->drupalPostForm('admin/structure/contact/manage/feedback', $edit, t('Save'));

    // Ensure translation link is present.
    $translation_base_url = 'admin/structure/contact/manage/feedback/translate';
    $this->assertLinkByHref($translation_base_url);

332 333
    // Make sure translate tab is present.
    $this->drupalGet('admin/structure/contact/manage/feedback');
334
    $this->assertLink(t('Translate @type', ['@type' => 'contact form']));
335

336 337 338 339 340 341
    // Visit the form to confirm the changes.
    $this->drupalGet('contact/feedback');
    $this->assertText($label);

    foreach ($this->langcodes as $langcode) {
      $this->drupalGet($translation_base_url);
342
      $this->assertLink(t('Translate @type', ['@type' => 'contact form']));
343 344 345 346 347 348 349 350 351 352

      // 'Add' link should be present for $langcode translation.
      $translation_page_url = "$translation_base_url/$langcode/add";
      $this->assertLinkByHref($translation_page_url);

      // Make sure original text is present on this page.
      $this->drupalGet($translation_page_url);
      $this->assertText($label);

      // Update translatable fields.
353
      $edit = [
354 355
        'translation[config_names][contact.form.feedback][label]' => 'Website feedback - ' . $langcode,
        'translation[config_names][contact.form.feedback][reply]' => 'Thank you for your mail - ' . $langcode,
356
      ];
357 358 359 360 361

      // Save language specific version of form.
      $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));

      // Expect translated values in language specific file.
362
      $override = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'contact.form.feedback');
363
      $expected = [
364 365
        'label' => 'Website feedback - ' . $langcode,
        'reply' => 'Thank you for your mail - ' . $langcode,
366
      ];
367
      $this->assertEqual($expected, $override->get());
368 369 370 371 372 373 374 375 376 377 378

      // Check for edit, delete links (and no 'add' link) for $langcode.
      $this->assertNoLinkByHref("$translation_base_url/$langcode/add");
      $this->assertLinkByHref("$translation_base_url/$langcode/edit");
      $this->assertLinkByHref("$translation_base_url/$langcode/delete");

      // Visit language specific version of form to check label.
      $this->drupalGet($langcode . '/contact/feedback');
      $this->assertText('Website feedback - ' . $langcode);

      // Submit feedback.
379
      $edit = [
380 381
        'subject[0][value]' => 'Test subject',
        'message[0][value]' => 'Test message',
382
      ];
383 384 385 386 387 388 389
      $this->drupalPostForm(NULL, $edit, t('Send message'));
    }

    // Now that all language translations are present, check translation and
    // original text all appear in any translated page on the translation
    // forms.
    foreach ($this->langcodes as $langcode) {
390
      $langcode_prefixes = array_merge([''], $this->langcodes);
391
      foreach ($langcode_prefixes as $langcode_prefix) {
392
        $this->drupalGet(ltrim("$langcode_prefix/$translation_base_url/$langcode/edit", '/'));
393
        $this->assertFieldByName('translation[config_names][contact.form.feedback][label]', 'Website feedback - ' . $langcode);
394 395 396 397 398
        $this->assertText($label);
      }
    }

    // We get all emails so no need to check inside the loop.
399
    $captured_emails = $this->getMails();
400 401 402 403 404 405 406 407 408 409 410

    // Check language specific auto reply text in email body.
    foreach ($captured_emails as $email) {
      if ($email['id'] == 'contact_page_autoreply') {
        // Trim because we get an added newline for the body.
        $this->assertEqual(trim($email['body']), 'Thank you for your mail - ' . $email['langcode']);
      }
    }

    // Test that delete links work and operations perform properly.
    foreach ($this->langcodes as $langcode) {
411
      $replacements = ['%label' => t('@label @entity_type', ['@label' => $label, '@entity_type' => mb_strtolower(t('Contact form'))]), '@language' => \Drupal::languageManager()->getLanguage($langcode)->getName()];
412 413 414 415 416 417

      $this->drupalGet("$translation_base_url/$langcode/delete");
      $this->assertRaw(t('Are you sure you want to delete the @language translation of %label?', $replacements));
      // Assert link back to list page to cancel delete is present.
      $this->assertLinkByHref($translation_base_url);

418
      $this->drupalPostForm(NULL, [], t('Delete'));
419 420 421 422 423 424
      $this->assertRaw(t('@language translation of %label was deleted', $replacements));
      $this->assertLinkByHref("$translation_base_url/$langcode/add");
      $this->assertNoLinkByHref("translation_base_url/$langcode/edit");
      $this->assertNoLinkByHref("$translation_base_url/$langcode/delete");

      // Expect no language specific file present anymore.
425
      $override = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'contact.form.feedback');
426
      $this->assertTrue($override->isNew());
427 428 429 430
    }

    // Check configuration page with translator user. Should have no access.
    $this->drupalLogout();
431
    $this->drupalLogin($this->translatorUser);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    $this->drupalGet('admin/structure/contact/manage/feedback');
    $this->assertResponse(403);

    // While translator can access the translation page, the edit link is not
    // present due to lack of permissions.
    $this->drupalGet($translation_base_url);
    $this->assertNoLink(t('Edit'));

    // Check 'Add' link for French.
    $this->assertLinkByHref("$translation_base_url/fr/add");
  }

  /**
   * Tests date format translation.
   */
  public function testDateFormatTranslation() {
448
    $this->drupalLogin($this->adminUser);
449 450 451 452 453 454 455

    $this->drupalGet('admin/config/regional/date-time');

    // Check for medium format.
    $this->assertLinkByHref('admin/config/regional/date-time/formats/manage/medium');

    // Save default language configuration for a new format.
456
    $edit = [
457 458 459
      'label' => 'Custom medium date',
      'id' => 'custom_medium',
      'date_format_pattern' => 'Y. m. d. H:i',
460
    ];
461 462 463
    $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));

    // Test translating a default shipped format and our custom format.
464
    $formats = [
465 466
      'medium' => 'Default medium date',
      'custom_medium' => 'Custom medium date',
467
    ];
468
    foreach ($formats as $id => $label) {
469 470 471 472 473 474 475 476 477 478 479 480
      $translation_base_url = 'admin/config/regional/date-time/formats/manage/' . $id . '/translate';

      $this->drupalGet($translation_base_url);

      // 'Add' link should be present for French translation.
      $translation_page_url = "$translation_base_url/fr/add";
      $this->assertLinkByHref($translation_page_url);

      // Make sure original text is present on this page.
      $this->drupalGet($translation_page_url);
      $this->assertText($label);

481 482 483
      // Make sure that the date library is added.
      $this->assertRaw('core/modules/system/js/system.date.js');

484
      // Update translatable fields.
485
      $edit = [
486 487
        'translation[config_names][core.date_format.' . $id . '][label]' => $id . ' - FR',
        'translation[config_names][core.date_format.' . $id . '][pattern]' => 'D',
488
      ];
489 490 491 492 493

      // Save language specific version of form.
      $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));

      // Get translation and check we've got the right value.
494
      $override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'core.date_format.' . $id);
495
      $expected = [
496
        'label' => $id . ' - FR',
497
        'pattern' => 'D',
498
      ];
499
      $this->assertEqual($expected, $override->get());
500 501 502

      // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
      // display "Tue".
503
      $formatted_date = $this->container->get('date.formatter')->format(494015820, $id, NULL, 'America/New_York', 'fr');
504 505 506 507 508 509 510 511 512 513 514 515
      $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
    }
  }

  /**
   * Tests the account settings translation interface.
   *
   * This is the only special case so far where we have multiple configuration
   * names involved building up one configuration translation form. Test that
   * the translations are saved for all configuration names properly.
   */
  public function testAccountSettingsConfigurationTranslation() {
516
    $this->drupalLogin($this->adminUser);
517

518
    $this->drupalGet('admin/config/people/accounts');
519
    $this->assertLink(t('Translate @type', ['@type' => 'account settings']));
520

521
    $this->drupalGet('admin/config/people/accounts/translate');
522
    $this->assertLink(t('Translate @type', ['@type' => 'account settings']));
523 524 525
    $this->assertLinkByHref('admin/config/people/accounts/translate/fr/add');

    // Update account settings fields for French.
526
    $edit = [
527 528 529
      'translation[config_names][user.settings][anonymous]' => 'Anonyme',
      'translation[config_names][user.mail][status_blocked][subject]' => 'Testing, your account is blocked.',
      'translation[config_names][user.mail][status_blocked][body]' => 'Testing account blocked body.',
530
    ];
531 532 533 534 535 536 537

    $this->drupalPostForm('admin/config/people/accounts/translate/fr/add', $edit, t('Save translation'));

    // Make sure the changes are saved and loaded back properly.
    $this->drupalGet('admin/config/people/accounts/translate/fr/edit');
    foreach ($edit as $key => $value) {
      // Check the translations appear in the right field type as well.
538
      $xpath = '//' . (strpos($key, '[body]') ? 'textarea' : 'input') . '[@name="' . $key . '"]';
539 540 541 542 543 544 545 546 547 548 549
      $this->assertFieldByXPath($xpath, $value);
    }
    // Check that labels for email settings appear.
    $this->assertText(t('Account cancellation confirmation'));
    $this->assertText(t('Password recovery'));
  }

  /**
   * Tests source and target language edge cases.
   */
  public function testSourceAndTargetLanguage() {
550
    $this->drupalLogin($this->adminUser);
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

    // Loading translation page for not-specified language (und)
    // should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/und/add');
    $this->assertResponse(403);

    // Check the source language doesn't have 'Add' or 'Delete' link and
    // make sure source language edit goes to original configuration page
    // not the translation specific edit page.
    $this->drupalGet('admin/config/system/site-information/translate');
    $this->assertNoLinkByHref('admin/config/system/site-information/translate/en/edit');
    $this->assertNoLinkByHref('admin/config/system/site-information/translate/en/add');
    $this->assertNoLinkByHref('admin/config/system/site-information/translate/en/delete');
    $this->assertLinkByHref('admin/config/system/site-information');

    // Translation addition to source language should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/en/add');
    $this->assertResponse(403);

    // Translation editing in source language should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/en/edit');
    $this->assertResponse(403);

    // Translation deletion in source language should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/en/delete');
    $this->assertResponse(403);

    // Set default language of site information to not-specified language (und).
579
    $this->config('system.site')
580
      ->set('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED)
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
      ->save();

    // Make sure translation tab does not exist on the configuration page.
    $this->drupalGet('admin/config/system/site-information');
    $this->assertNoLinkByHref('admin/config/system/site-information/translate');

    // If source language is not specified, translation page should be 403.
    $this->drupalGet('admin/config/system/site-information/translate');
    $this->assertResponse(403);
  }

  /**
   * Tests the views translation interface.
   */
  public function testViewsTranslationUI() {
596
    $this->drupalLogin($this->adminUser);
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

    $description = 'All content promoted to the front page.';
    $human_readable_name = 'Frontpage';
    $display_settings_master = 'Master';
    $display_options_master = '(Empty)';
    $translation_base_url = 'admin/structure/views/view/frontpage/translate';

    $this->drupalGet($translation_base_url);

    // Check 'Add' link of French to visit add page.
    $this->assertLinkByHref("$translation_base_url/fr/add");
    $this->clickLink(t('Add'));

    // Make sure original text is present on this page.
    $this->assertRaw($description);
    $this->assertRaw($human_readable_name);

    // Update Views Fields for French.
615
    $edit = [
616 617 618 619
      'translation[config_names][views.view.frontpage][description]' => $description . " FR",
      'translation[config_names][views.view.frontpage][label]' => $human_readable_name . " FR",
      'translation[config_names][views.view.frontpage][display][default][display_title]' => $display_settings_master . " FR",
      'translation[config_names][views.view.frontpage][display][default][display_options][title]' => $display_options_master . " FR",
620
    ];
621
    $this->drupalPostForm("$translation_base_url/fr/add", $edit, t('Save translation'));
622
    $this->assertRaw(t('Successfully saved @language translation.', ['@language' => 'French']));
623 624 625 626 627 628 629 630

    // Check for edit, delete links (and no 'add' link) for French language.
    $this->assertNoLinkByHref("$translation_base_url/fr/add");
    $this->assertLinkByHref("$translation_base_url/fr/edit");
    $this->assertLinkByHref("$translation_base_url/fr/delete");

    // Check translation saved proper.
    $this->drupalGet("$translation_base_url/fr/edit");
631 632 633 634
    $this->assertFieldByName('translation[config_names][views.view.frontpage][description]', $description . " FR");
    $this->assertFieldByName('translation[config_names][views.view.frontpage][label]', $human_readable_name . " FR");
    $this->assertFieldByName('translation[config_names][views.view.frontpage][display][default][display_title]', $display_settings_master . " FR");
    $this->assertFieldByName('translation[config_names][views.view.frontpage][display][default][display_options][title]', $display_options_master . " FR");
635 636
  }

637 638 639 640 641 642 643
  /**
   * Test the number of source elements for plural strings in config translation forms.
   */
  public function testPluralConfigStringsSourceElements() {
    $this->drupalLogin($this->adminUser);

    // Languages to test, with various number of plural forms.
644 645 646 647 648
    $languages = [
      'vi' => ['plurals' => 1, 'expected' => [TRUE, FALSE, FALSE, FALSE]],
      'fr' => ['plurals' => 2, 'expected' => [TRUE, TRUE, FALSE, FALSE]],
      'sl' => ['plurals' => 4, 'expected' => [TRUE, TRUE, TRUE, TRUE]],
    ];
649 650 651

    foreach ($languages as $langcode => $data) {
      // Import a .po file to add a new language with a given number of plural forms
652
      $name = \Drupal::service('file_system')->tempnam('temporary://', $langcode . '_') . '.po';
653
      file_put_contents($name, $this->getPoFile($data['plurals']));
654
      $this->drupalPostForm('admin/config/regional/translate/import', [
655 656
        'langcode' => $langcode,
        'files[file]' => $name,
657
      ], t('Import'));
658 659 660 661 662 663 664

      // Change the config langcode of the 'files' view.
      $config = \Drupal::service('config.factory')->getEditable('views.view.files');
      $config->set('langcode', $langcode);
      $config->save();

      // Go to the translation page of the 'files' view.
665
      $translation_url = 'admin/structure/views/view/files/translate/en/add';
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
      $this->drupalGet($translation_url);

      // Check if the expected number of source elements are present.
      foreach ($data['expected'] as $index => $expected) {
        if ($expected) {
          $this->assertRaw('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index);
        }
        else {
          $this->assertNoRaw('edit-source-config-names-viewsviewfiles-display-default-display-options-fields-count-format-plural-string-' . $index);
        }
      }
    }
  }

  /**
   * Test translation of plural strings with multiple plural forms in config.
   */
  public function testPluralConfigStrings() {
    $this->drupalLogin($this->adminUser);

    // First import a .po file with multiple plural forms.
    // This will also automatically add the 'sl' language.
688
    $name = \Drupal::service('file_system')->tempnam('temporary://', "sl_") . '.po';
689
    file_put_contents($name, $this->getPoFile(4));
690
    $this->drupalPostForm('admin/config/regional/translate/import', [
691 692
      'langcode' => 'sl',
      'files[file]' => $name,
693
    ], t('Import'));
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

    // Translate the files view, as this one uses numeric formatters.
    $description = 'Singular form';
    $field_value = '1 place';
    $field_value_plural = '@count places';
    $translation_url = 'admin/structure/views/view/files/translate/sl/add';
    $this->drupalGet($translation_url);

    // Make sure original text is present on this page, in addition to 2 new
    // empty fields.
    $this->assertRaw($description);
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', $field_value);
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', $field_value_plural);
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', '');
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', '');

    // Then make sure it also works.
    $edit = [
      'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]' => $field_value . ' SL',
      'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]' => $field_value_plural . ' 1 SL',
      'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]' => $field_value_plural . ' 2 SL',
      'translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]' => $field_value_plural . ' 3 SL',
    ];
    $this->drupalPostForm($translation_url, $edit, t('Save translation'));

    // Make sure the values have changed.
    $this->drupalGet($translation_url);
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', "$field_value SL");
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', "$field_value_plural 1 SL");
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', "$field_value_plural 2 SL");
    $this->assertFieldByName('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', "$field_value_plural 3 SL");
  }

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  /**
   * Tests the translation of field and field storage configuration.
   */
  public function testFieldConfigTranslation() {
    // Add a test field which has a translatable field setting and a
    // translatable field storage setting.
    $field_name = strtolower($this->randomMachineName());
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'test_field',
    ]);

    $translatable_storage_setting = $this->randomString();
    $field_storage->setSetting('translatable_storage_setting', $translatable_storage_setting);
    $field_storage->save();

    $bundle = strtolower($this->randomMachineName());
    entity_test_create_bundle($bundle);
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'bundle' => $bundle,
    ]);

    $translatable_field_setting = $this->randomString();
    $field->setSetting('translatable_field_setting', $translatable_field_setting);
    $field->save();

    $this->drupalLogin($this->translatorUser);

    $this->drupalGet("/entity_test/structure/$bundle/fields/entity_test.$bundle.$field_name/translate");
    $this->clickLink('Add');

    $this->assertText('Translatable field setting');
762
    $this->assertEscaped($translatable_field_setting);
763
    $this->assertText('Translatable storage setting');
764
    $this->assertEscaped($translatable_storage_setting);
765 766
  }

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  /**
   * Tests the translation of a boolean field settings.
   */
  public function testBooleanFieldConfigTranslation() {
    // Add a test boolean field.
    $field_name = strtolower($this->randomMachineName());
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'boolean',
    ])->save();

    $bundle = strtolower($this->randomMachineName());
    entity_test_create_bundle($bundle);
    $field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'bundle' => $bundle,
    ]);

    $on_label = 'On label (with <em>HTML</em> & things)';
    $field->setSetting('on_label', $on_label);
    $off_label = 'Off label (with <em>HTML</em> & things)';
    $field->setSetting('off_label', $off_label);
    $field->save();

    $this->drupalLogin($this->translatorUser);

    $this->drupalGet("/entity_test/structure/$bundle/fields/entity_test.$bundle.$field_name/translate");
    $this->clickLink('Add');

    // Checks the text of details summary element that surrounds the translation
    // options.
    $this->assertText(Html::escape(strip_tags($on_label)) . ' Boolean settings');

    // Checks that the correct on and off labels appear on the form.
    $this->assertEscaped($on_label);
    $this->assertEscaped($off_label);
  }

807 808 809 810
  /**
   * Test translation storage in locale storage.
   */
  public function testLocaleDBStorage() {
811 812
    // Enable import of translations. By default this is disabled for automated
    // tests.
813
    $this->config('locale.settings')
814
      ->set('translation.import_enabled', TRUE)
815
      ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
816 817
      ->save();

818
    $this->drupalLogin($this->adminUser);
819 820

    $langcode = 'xx';
821
    $name = $this->randomMachineName(16);
822
    $edit = [
823 824
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
825
      'label' => $name,
826
      'direction' => Language::DIRECTION_LTR,
827
    ];
828 829 830 831 832 833 834
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));

    // Make sure there is no translation stored in locale storage before edit.
    $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
    $this->assertTrue(empty($translation));

    // Add custom translation.
835
    $edit = [
836
      'translation[config_names][user.settings][anonymous]' => 'Anonyme',
837
    ];
838 839 840 841 842 843 844 845
    $this->drupalPostForm('admin/config/people/accounts/translate/fr/add', $edit, t('Save translation'));

    // Make sure translation stored in locale storage after saved language
    // specific configuration translation.
    $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
    $this->assertEqual('Anonyme', $translation->getString());

    // revert custom translations to base translation.
846
    $edit = [
847
      'translation[config_names][user.settings][anonymous]' => 'Anonymous',
848
    ];
849 850 851 852 853 854 855 856 857 858 859
    $this->drupalPostForm('admin/config/people/accounts/translate/fr/edit', $edit, t('Save translation'));

    // Make sure there is no translation stored in locale storage after revert.
    $translation = $this->getTranslation('user.settings', 'anonymous', 'fr');
    $this->assertEqual('Anonymous', $translation->getString());
  }

  /**
   * Tests the single language existing.
   */
  public function testSingleLanguageUI() {
860
    $this->drupalLogin($this->adminUser);
861 862

    // Delete French language
863 864
    $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
    $this->assertRaw(t('The %language (%langcode) language has been removed.', ['%language' => 'French', '%langcode' => 'fr']));
865 866

    // Change default language to Tamil.
867
    $edit = [
868
      'site_default_language' => 'ta',
869
    ];
870 871
    $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
    $this->assertRaw(t('Configuration saved.'));
872 873

    // Delete English language
874 875
    $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
    $this->assertRaw(t('The %language (%langcode) language has been removed.', ['%language' => 'English', '%langcode' => 'en']));
876 877 878 879 880 881 882 883 884 885 886

    // Visit account setting translation page, this should not
    // throw any notices.
    $this->drupalGet('admin/config/people/accounts/translate');
    $this->assertResponse(200);
  }

  /**
   * Tests the config_translation_info_alter() hook.
   */
  public function testAlterInfo() {
887
    $this->drupalLogin($this->adminUser);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

    $this->container->get('state')->set('config_translation_test_config_translation_info_alter', TRUE);
    $this->container->get('plugin.manager.config_translation.mapper')->clearCachedDefinitions();

    // Check out if the translation page has the altered in settings.
    $this->drupalGet('admin/config/system/site-information/translate/fr/add');
    $this->assertText(t('Feed channel'));
    $this->assertText(t('Feed description'));

    // Check if the translation page does not have the altered out settings.
    $this->drupalGet('admin/config/people/accounts/translate/fr/add');
    $this->assertText(t('Name'));
    $this->assertNoText(t('Account cancellation confirmation'));
    $this->assertNoText(t('Password recovery'));
  }

904 905 906 907 908 909 910 911
  /**
   * Tests the sequence data type translation.
   */
  public function testSequenceTranslation() {
    $this->drupalLogin($this->adminUser);
    /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
    $config_factory = $this->container->get('config.factory');

912
    $expected = [
913 914
      'kitten',
      'llama',
915
      'elephant',
916
    ];
917 918 919 920 921
    $actual = $config_factory
      ->getEditable('config_translation_test.content')
      ->get('animals');
    $this->assertEqual($expected, $actual);

922
    $edit = [
923 924 925 926
      'translation[config_names][config_translation_test.content][content][value]' => '<p><strong>Hello World</strong> - FR</p>',
      'translation[config_names][config_translation_test.content][animals][0]' => 'kitten - FR',
      'translation[config_names][config_translation_test.content][animals][1]' => 'llama - FR',
      'translation[config_names][config_translation_test.content][animals][2]' => 'elephant - FR',
927
    ];
928 929 930
    $this->drupalPostForm('admin/config/media/file-system/translate/fr/add', $edit, t('Save translation'));

    $this->container->get('language.config_factory_override')
931
      ->setLanguage(new Language(['id' => 'fr']));
932

933
    $expected = [
934 935 936
      'kitten - FR',
      'llama - FR',
      'elephant - FR',
937
    ];
938 939 940 941 942 943
    $actual = $config_factory
      ->get('config_translation_test.content')
      ->get('animals');
    $this->assertEqual($expected, $actual);
  }

944 945 946 947
  /**
   * Test text_format translation.
   */
  public function testTextFormatTranslation() {
948
    $this->drupalLogin($this->adminUser);
949 950 951
    /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
    $config_factory = $this->container->get('config.factory');

952
    $expected = [
953 954
      'value' => '<p><strong>Hello World</strong></p>',
      'format' => 'plain_text',
955
    ];
956 957
    $actual = $config_factory
      ->get('config_translation_test.content')
958
      ->getOriginal('content', FALSE);
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
    $this->assertEqual($expected, $actual);

    $translation_base_url = 'admin/config/media/file-system/translate';
    $this->drupalGet($translation_base_url);

    // 'Add' link should be present for French translation.
    $translation_page_url = "$translation_base_url/fr/add";
    $this->assertLinkByHref($translation_page_url);

    $this->drupalGet($translation_page_url);

    // Assert that changing the text format is not possible, even for an
    // administrator.
    $this->assertNoFieldByName('translation[config_names][config_translation_test.content][content][format]');

    // Update translatable fields.
975
    $edit = [
976
      'translation[config_names][config_translation_test.content][content][value]' => '<p><strong>Hello World</strong> - FR</p>',
977
    ];
978 979 980 981 982

    // Save language specific version of form.
    $this->drupalPostForm($translation_page_url, $edit, t('Save translation'));

    // Get translation and check we've got the right value.
983
    $expected = [
984 985
      'value' => '<p><strong>Hello World</strong> - FR</p>',
      'format' => 'plain_text',
986
    ];
987
    $this->container->get('language.config_factory_override')
988
      ->setLanguage(new Language(['id' => 'fr']));
989 990 991 992 993 994 995 996 997
    $actual = $config_factory
      ->get('config_translation_test.content')
      ->get('content');
    $this->assertEqual($expected, $actual);

    // Change the text format of the source configuration and verify that the
    // text format of the translation does not change because that could lead to
    // security vulnerabilities.
    $config_factory
998
      ->getEditable('config_translation_test.content')
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
      ->set('content.format', 'full_html')
      ->save();

    $actual = $config_factory
      ->get('config_translation_test.content')
      ->get('content');
    // The translation should not have changed, so re-use $expected.
    $this->assertEqual($expected, $actual);

    // Because the text is now in a text format that the translator does not
    // have access to, the translator should not be able to translate it.
    $translation_page_url = "$translation_base_url/fr/edit";
1011
    $this->drupalLogin($this->translatorUser);
alexpott's avatar