ConfigTranslationUiTest.php 47.6 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
    $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.
148
    $this->assertSession()->linkByHrefExists($translation_base_url);
149 150 151 152 153 154 155

    $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, [], 'Save configuration');
157
    $this->assertSession()->addressEquals($translation_base_url);
158 159

    // Check 'Add' link of French to visit add page.
160
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/add");
161 162 163 164 165 166 167
    $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, 'Save translation');
174
    $this->assertRaw(t('Successfully saved @language translation.', ['@language' => 'French']));
175 176

    // Check for edit, delete links (and no 'add' link) for French language.
177 178 179
    $this->assertSession()->linkByHrefNotExists("$translation_base_url/fr/add");
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/edit");
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/delete");
180 181 182

    // Check translation saved proper.
    $this->drupalGet("$translation_base_url/fr/edit");
183 184
    $this->assertSession()->fieldValueEquals('translation[config_names][system.site][name]', $fr_site_name);
    $this->assertSession()->fieldValueEquals('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
    $this->drupalPostForm('admin/config/regional/translate', $search, 'Filter');
206 207

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

    // 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
    // First time, no overrides, so just Add link.
250
    $this->drupalPostForm("$translation_base_url/fr/add", $edit, 'Save translation');
251 252

    // 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, '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, '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
    $this->drupalGet('admin/config/system/site-information');
297
    $this->assertSession()->statusCodeEquals(403);
298 299 300 301

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

    // Check 'Add' link for French.
305
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/add");
306 307 308
  }

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

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

    // Check for default contact form configuration entity from Contact module.
317
    $this->assertSession()->linkByHrefExists('admin/structure/contact/manage/feedback');
318 319 320

    // 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
    $this->drupalPostForm('admin/structure/contact/manage/feedback', $edit, 'Save');
327 328 329

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

332 333
    // Make sure translate tab is present.
    $this->drupalGet('admin/structure/contact/manage/feedback');
334
    $this->assertSession()->linkExists('Translate 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->assertSession()->linkExists('Translate contact form');
343 344 345

      // 'Add' link should be present for $langcode translation.
      $translation_page_url = "$translation_base_url/$langcode/add";
346
      $this->assertSession()->linkByHrefExists($translation_page_url);
347 348 349 350 351 352

      // 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

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

      // 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

      // Check for edit, delete links (and no 'add' link) for $langcode.
370 371 372
      $this->assertSession()->linkByHrefNotExists("$translation_base_url/$langcode/add");
      $this->assertSession()->linkByHrefExists("$translation_base_url/$langcode/edit");
      $this->assertSession()->linkByHrefExists("$translation_base_url/$langcode/delete");
373 374 375 376 377 378

      // 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
      $this->drupalPostForm(NULL, $edit, 'Send message');
384 385 386 387 388 389
    }

    // 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->assertSession()->fieldValueEquals('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

      $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.
416
      $this->assertSession()->linkByHrefExists($translation_base_url);
417

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

      // 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
    $this->drupalGet('admin/structure/contact/manage/feedback');
433
    $this->assertSession()->statusCodeEquals(403);
434 435 436 437

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

    // Check 'Add' link for French.
441
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/add");
442 443 444 445 446 447
  }

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

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

    // Check for medium format.
453
    $this->assertSession()->linkByHrefExists('admin/config/regional/date-time/formats/manage/medium');
454 455

    // 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
    $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, 'Add format');
462 463

    // 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
      $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";
475
      $this->assertSession()->linkByHrefExists($translation_page_url);
476 477 478 479 480

      // 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

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

      // 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->assertSession()->linkExists('Translate account settings');
520

521
    $this->drupalGet('admin/config/people/accounts/translate');
522
    $this->assertSession()->linkExists('Translate account settings');
523
    $this->assertSession()->linkByHrefExists('admin/config/people/accounts/translate/fr/add');
524 525

    // 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
    $this->drupalPostForm('admin/config/people/accounts/translate/fr/add', $edit, 'Save translation');
533 534 535 536 537

    // 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
      $this->assertSession()->fieldValueEquals($key, $value);
539 540
    }
    // Check that labels for email settings appear.
541 542
    $this->assertText('Account cancellation confirmation');
    $this->assertText('Password recovery');
543 544 545 546 547 548
  }

  /**
   * Tests source and target language edge cases.
   */
  public function testSourceAndTargetLanguage() {
549
    $this->drupalLogin($this->adminUser);
550 551 552 553

    // Loading translation page for not-specified language (und)
    // should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/und/add');
554
    $this->assertSession()->statusCodeEquals(403);
555 556 557 558 559

    // 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');
560 561 562 563
    $this->assertSession()->linkByHrefNotExists('admin/config/system/site-information/translate/en/edit');
    $this->assertSession()->linkByHrefNotExists('admin/config/system/site-information/translate/en/add');
    $this->assertSession()->linkByHrefNotExists('admin/config/system/site-information/translate/en/delete');
    $this->assertSession()->linkByHrefExists('admin/config/system/site-information');
564 565 566

    // Translation addition to source language should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/en/add');
567
    $this->assertSession()->statusCodeEquals(403);
568 569 570

    // Translation editing in source language should return 403.
    $this->drupalGet('admin/config/system/site-information/translate/en/edit');
571
    $this->assertSession()->statusCodeEquals(403);
572 573 574

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

    // Set default language of site information to not-specified language (und).
578
    $this->config('system.site')
579
      ->set('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED)
580 581 582 583
      ->save();

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

    // If source language is not specified, translation page should be 403.
    $this->drupalGet('admin/config/system/site-information/translate');
588
    $this->assertSession()->statusCodeEquals(403);
589 590 591 592 593 594
  }

  /**
   * Tests the views translation interface.
   */
  public function testViewsTranslationUI() {
595
    $this->drupalLogin($this->adminUser);
596 597 598 599 600 601 602 603 604 605

    $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.
606
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/add");
607 608 609 610 611 612 613
    $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.
614
    $edit = [
615 616 617 618
      '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",
619
    ];
620
    $this->drupalPostForm("$translation_base_url/fr/add", $edit, 'Save translation');
621
    $this->assertRaw(t('Successfully saved @language translation.', ['@language' => 'French']));
622 623

    // Check for edit, delete links (and no 'add' link) for French language.
624 625 626
    $this->assertSession()->linkByHrefNotExists("$translation_base_url/fr/add");
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/edit");
    $this->assertSession()->linkByHrefExists("$translation_base_url/fr/delete");
627 628 629

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

636 637 638 639 640 641 642
  /**
   * 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.
643 644 645 646 647
    $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]],
    ];
648 649 650

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

      // 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.
664
      $translation_url = 'admin/structure/views/view/files/translate/en/add';
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
      $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.
687
    $name = \Drupal::service('file_system')->tempnam('temporary://', "sl_") . '.po';
688
    file_put_contents($name, $this->getPoFile(4));
689
    $this->drupalPostForm('admin/config/regional/translate/import', [
690 691
      'langcode' => 'sl',
      'files[file]' => $name,
692
    ], 'Import');
693 694 695 696 697 698 699 700 701 702 703

    // 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);
704 705 706 707
    $this->assertSession()->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][0]', $field_value);
    $this->assertSession()->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][1]', $field_value_plural);
    $this->assertSession()->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][2]', '');
    $this->assertSession()->fieldValueEquals('translation[config_names][views.view.files][display][default][display_options][fields][count][format_plural_string][3]', '');
708 709 710 711 712 713 714 715

    // 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',
    ];
716
    $this->drupalPostForm($translation_url, $edit, 'Save translation');
717 718 719

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

726 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
  /**
   * 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');
761
    $this->assertSession()->assertEscaped($translatable_field_setting);
762
    $this->assertText('Translatable storage setting');
763
    $this->assertSession()->assertEscaped($translatable_storage_setting);
764 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
  /**
   * 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.
802 803
    $this->assertSession()->assertEscaped($on_label);
    $this->assertSession()->assertEscaped($off_label);
804 805
  }

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

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

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

    // 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.
834
    $edit = [
835
      'translation[config_names][user.settings][anonymous]' => 'Anonyme',
836
    ];
837
    $this->drupalPostForm('admin/config/people/accounts/translate/fr/add', $edit, 'Save translation');
838 839 840 841 842 843 844

    // 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.
845
    $edit = [
846
      'translation[config_names][user.settings][anonymous]' => 'Anonymous',
847
    ];
848
    $this->drupalPostForm('admin/config/people/accounts/translate/fr/edit', $edit, 'Save translation');
849 850 851 852 853 854 855 856 857 858

    // 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() {
859
    $this->drupalLogin($this->adminUser);
860 861

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

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

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

    // Visit account setting translation page, this should not
    // throw any notices.
    $this->drupalGet('admin/config/people/accounts/translate');
879
    $this->assertSession()->statusCodeEquals(200);
880 881 882 883 884 885
  }

  /**
   * Tests the config_translation_info_alter() hook.
   */
  public function testAlterInfo() {
886
    $this->drupalLogin($this->adminUser);
887 888 889 890 891 892

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

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

898 899 900 901 902 903 904 905
  /**
   * 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');

906