function ConfigTranslationUiModulesTest::testTextFormatTranslation

Same name and namespace in other branches
  1. 10 core/modules/config_translation/tests/src/Functional/ConfigTranslationUiModulesTest.php \Drupal\Tests\config_translation\Functional\ConfigTranslationUiModulesTest::testTextFormatTranslation()

Tests text_format translation.

File

core/modules/config_translation/tests/src/Functional/ConfigTranslationUiModulesTest.php, line 290

Class

ConfigTranslationUiModulesTest
Translate settings and entities to various languages.

Namespace

Drupal\Tests\config_translation\Functional

Code

public function testTextFormatTranslation() : void {
    $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->assertEquals($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->assertSession()
        ->linkByHrefExists($translation_page_url);
    $this->drupalGet($translation_page_url);
    // Assert that changing the text format is not possible, even for an
    // administrator.
    $this->assertSession()
        ->fieldNotExists('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->drupalGet($translation_page_url);
    $this->submitForm($edit, '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->assertEquals($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->assertEquals($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->submitForm([], '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->assertEquals($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->drupalGet($translation_page_url);
    $this->submitForm($edit, '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->assertEquals($expected, $actual);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.