function CKEditor5UpdateImageToolbarItemTest::test

Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateImageToolbarItemTest.php \Drupal\Tests\ckeditor5\Functional\Update\CKEditor5UpdateImageToolbarItemTest::test()
  2. 10 core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateImageToolbarItemTest.php \Drupal\Tests\ckeditor5\Functional\Update\CKEditor5UpdateImageToolbarItemTest::test()

Tests that `uploadImage` toolbar item is updated to `drupalInsertImage`.

@dataProvider provider

File

core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateImageToolbarItemTest.php, line 47

Class

CKEditor5UpdateImageToolbarItemTest
Tests the update path for the CKEditor 5 image toolbar item.

Namespace

Drupal\Tests\ckeditor5\Functional\Update

Code

public function test(bool $filter_html_is_enabled, bool $image_uploads_are_enabled, bool $source_editing_is_already_enabled, array $expected_source_editing_additions) : void {
    // Apply tweaks for the currently provided test case.
    $format = FilterFormat::load('test_format_image');
    if (!$filter_html_is_enabled) {
        $format->setFilterConfig('filter_html', [
            'status' => FALSE,
        ]);
    }
    $editor = Editor::load('test_format_image');
    if (!$image_uploads_are_enabled) {
        $editor->setImageUploadSettings([
            'status' => FALSE,
        ]);
    }
    if (!$source_editing_is_already_enabled) {
        $settings = $editor->getSettings();
        // Remove the `sourceEditing` toolbar item.
        unset($settings['toolbar']['items'][3]);
        // Remove the corresponding plugin settings (allowing `<img data-foo>`).
        unset($settings['plugins']['ckeditor5_sourceEditing']);
        $editor->setSettings($settings);
        if ($filter_html_is_enabled) {
            // Stop allowing `<img data-foo>`.
            $filter_html_config = $format->filters('filter_html')
                ->getConfiguration();
            $filter_html_config['settings']['allowed_html'] = str_replace('data-foo', '', $filter_html_config['settings']['allowed_html']);
            $format->setFilterConfig('filter_html', $filter_html_config);
        }
    }
    $format->trustData()
        ->save();
    $editor->trustData()
        ->save();
    // Run update path; snapshot the Text Format and Editor before and after.
    $editor_before = Editor::load('test_format_image');
    $filter_format_before = $editor->getFilterFormat();
    $this->runUpdates();
    $editor_after = Editor::load('test_format_image');
    $filter_format_after = $editor->getFilterFormat();
    // 1. Toolbar item: `uploadImage` -> `drupalInsertImage`, position must be
    // unchanged.
    $this->assertContains('uploadImage', $editor_before->getSettings()['toolbar']['items']);
    $this->assertNotContains('drupalInsertImage', $editor_before->getSettings()['toolbar']['items']);
    $this->assertNotContains('uploadImage', $editor_after->getSettings()['toolbar']['items']);
    $this->assertContains('drupalInsertImage', $editor_after->getSettings()['toolbar']['items']);
    $this->assertSame(array_search('uploadImage', $editor_before->getSettings()['toolbar']['items']), array_search('drupalInsertImage', $editor_after->getSettings()['toolbar']['items']));
    // 2. Even though `sourceEditing` may not be enabled before this update, it
    // must be after, at least if image uploads are disabled: extra mark-up will
    // be added to its configuration to avoid breaking backwards compatibility.
    if (!$image_uploads_are_enabled) {
        if (!$source_editing_is_already_enabled) {
            $this->assertNotContains('sourceEditing', $editor_before->getSettings()['toolbar']['items']);
        }
        $this->assertContains('sourceEditing', $editor_after->getSettings()['toolbar']['items']);
        $source_editing_before = $source_editing_is_already_enabled ? static::getSourceEditingRestrictions($editor_before) : HTMLRestrictions::emptySet();
        $source_editing_after = static::getSourceEditingRestrictions($editor_after);
        if ($source_editing_is_already_enabled) {
            // Nothing has been removed from the allowed source editing tags.
            $this->assertFalse($source_editing_before->allowsNothing());
            $this->assertTrue($source_editing_before->diff($source_editing_after)
                ->allowsNothing());
        }
        $this->assertSame($expected_source_editing_additions, $source_editing_after->diff($source_editing_before)
            ->toCKEditor5ElementsArray());
    }
    else {
        if (!$source_editing_is_already_enabled) {
            $this->assertNotContains('sourceEditing', $editor_before->getSettings()['toolbar']['items']);
        }
        else {
            $this->assertContains('sourceEditing', $editor_before->getSettings()['toolbar']['items']);
            $this->assertSame(static::getSourceEditingRestrictions($editor_before)->toCKEditor5ElementsArray(), static::getSourceEditingRestrictions($editor_after)->toCKEditor5ElementsArray());
        }
    }
    // 3. `filter_html` restrictions MUST remain unchanged.
    if ($filter_html_is_enabled) {
        $filter_html_before = static::getFilterHtmlRestrictions($filter_format_before);
        $filter_html_after = static::getFilterHtmlRestrictions($filter_format_after);
        $this->assertTrue($filter_html_before->diff($filter_html_after)
            ->allowsNothing());
        $this->assertTrue($filter_html_after->diff($filter_html_before)
            ->allowsNothing());
    }
    // 4. After: text format and editor still form a valid pair.
    $this->assertSame([], array_map(function (ConstraintViolation $v) {
        return (string) $v->getMessage();
    }, array_filter(iterator_to_array(CKEditor5::validatePair($editor_after, $filter_format_after)), fn(ConstraintViolation $v) => $v->getMessage() != 'The file storage you selected is not a visible, readable and writable stream wrapper. Possible choices: <em class="placeholder"></em>.')));
}

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