You are here

public function ConfigTranslationUiTest::testTextFormatTranslation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php \Drupal\config_translation\Tests\ConfigTranslationUiTest::testTextFormatTranslation()

Test text_format translation.

File

core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php, line 875
Contains \Drupal\config_translation\Tests\ConfigTranslationUiTest.

Class

ConfigTranslationUiTest
Translate settings and entities to various languages.

Namespace

Drupal\config_translation\Tests

Code

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

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = $this->container
    ->get('config.factory');
  $expected = array(
    '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 = array(
    '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 = array(
    'value' => '<p><strong>Hello World</strong> - FR</p>',
    'format' => 'plain_text',
  );
  $this->container
    ->get('language.config_factory_override')
    ->setLanguage(new Language(array(
    '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, array(), 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 = array(
    'translation[config_names][config_translation_test.content][content][format]' => 'full_html',
  );
  $this
    ->drupalPostForm($translation_page_url, $edit, t('Save translation'));
  $expected = array(
    '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);
}