You are here

public function ConfigTranslationUiTest::testContactConfigEntityTranslation in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationUiTest::testContactConfigEntityTranslation()

Tests the contact form translation.

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationUiTest.php, line 311

Class

ConfigTranslationUiTest
Translate settings and entities to various languages.

Namespace

Drupal\Tests\config_translation\Functional

Code

public function testContactConfigEntityTranslation() {
  $this
    ->drupalLogin($this->adminUser);
  $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';
  $edit = [
    'label' => $label,
    'recipients' => 'sales@example.com,support@example.com',
    'reply' => 'Thank you for your mail',
  ];
  $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);

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

  // Visit the form to confirm the changes.
  $this
    ->drupalGet('contact/feedback');
  $this
    ->assertText($label);
  foreach ($this->langcodes as $langcode) {
    $this
      ->drupalGet($translation_base_url);
    $this
      ->assertSession()
      ->linkExists(t('Translate @type', [
      '@type' => 'contact form',
    ]));

    // '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.
    $edit = [
      'translation[config_names][contact.form.feedback][label]' => 'Website feedback - ' . $langcode,
      'translation[config_names][contact.form.feedback][reply]' => 'Thank you for your mail - ' . $langcode,
    ];

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

    // Expect translated values in language specific file.
    $override = \Drupal::languageManager()
      ->getLanguageConfigOverride($langcode, 'contact.form.feedback');
    $expected = [
      'label' => 'Website feedback - ' . $langcode,
      'reply' => 'Thank you for your mail - ' . $langcode,
    ];
    $this
      ->assertEqual($expected, $override
      ->get());

    // 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.
    $edit = [
      'subject[0][value]' => 'Test subject',
      'message[0][value]' => 'Test message',
    ];
    $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) {
    $langcode_prefixes = array_merge([
      '',
    ], $this->langcodes);
    foreach ($langcode_prefixes as $langcode_prefix) {
      $this
        ->drupalGet(ltrim("{$langcode_prefix}/{$translation_base_url}/{$langcode}/edit", '/'));
      $this
        ->assertFieldByName('translation[config_names][contact.form.feedback][label]', 'Website feedback - ' . $langcode);
      $this
        ->assertText($label);
    }
  }

  // We get all emails so no need to check inside the loop.
  $captured_emails = $this
    ->getMails();

  // 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) {
    $replacements = [
      '%label' => t('@label @entity_type', [
        '@label' => $label,
        '@entity_type' => mb_strtolower(t('Contact form')),
      ]),
      '@language' => \Drupal::languageManager()
        ->getLanguage($langcode)
        ->getName(),
    ];
    $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);
    $this
      ->drupalPostForm(NULL, [], t('Delete'));
    $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.
    $override = \Drupal::languageManager()
      ->getLanguageConfigOverride($langcode, 'contact.form.feedback');
    $this
      ->assertTrue($override
      ->isNew());
  }

  // Check configuration page with translator user. Should have no access.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->translatorUser);
  $this
    ->drupalGet('admin/structure/contact/manage/feedback');
  $this
    ->assertSession()
    ->statusCodeEquals(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
    ->assertSession()
    ->linkNotExists(t('Edit'));

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