You are here

public function ConfigTranslationUiTest::testTextFormatTranslation 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::testTextFormatTranslation()

Test text_format translation.

File

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

Class

ConfigTranslationUiTest
Translate settings and entities to various languages.

Namespace

Drupal\Tests\config_translation\Functional

Code

public function testTextFormatTranslation() {
  $this
    ->drupalLogin($this->adminUser);

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = $this->container
    ->get('config.factory');
  $expected = [
    'value' => '<p><strong>Hello World</strong></p>',
    'format' => 'plain_text',
  ];
  $actual = $config_factory
    ->get('config_translation_test.content')
    ->getOriginal('content', FALSE);
  $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.
  $edit = [
    'translation[config_names][config_translation_test.content][content][value]' => '<p><strong>Hello World</strong> - FR</p>',
  ];

  // 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.
  $expected = [
    'value' => '<p><strong>Hello World</strong> - FR</p>',
    'format' => 'plain_text',
  ];
  $this->container
    ->get('language.config_factory_override')
    ->setLanguage(new Language([
    'id' => 'fr',
  ]));
  $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
    ->getEditable('config_translation_test.content')
    ->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";
  $this
    ->drupalLogin($this->translatorUser);
  $this
    ->drupalGet($translation_page_url);
  $this
    ->assertDisabledTextarea('edit-translation-config-names-config-translation-testcontent-content-value');
  $this
    ->drupalPostForm(NULL, [], t('Save translation'));

  // Check that submitting the form did not update the text format of the
  // translation.
  $actual = $config_factory
    ->get('config_translation_test.content')
    ->get('content');
  $this
    ->assertEqual($expected, $actual);

  // The administrator must explicitly change the text format.
  $this
    ->drupalLogin($this->adminUser);
  $edit = [
    'translation[config_names][config_translation_test.content][content][format]' => 'full_html',
  ];
  $this
    ->drupalPostForm($translation_page_url, $edit, t('Save translation'));
  $expected = [
    'value' => '<p><strong>Hello World</strong> - FR</p>',
    'format' => 'full_html',
  ];
  $actual = $config_factory
    ->get('config_translation_test.content')
    ->get('content');
  $this
    ->assertEqual($expected, $actual);
}