function ImageTestBaselineTrait::testImageArbitraryHtml

Tests that arbitrary attributes are allowed via GHS.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/ImageTestBaselineTrait.php, line 81

Class

ImageTestBaselineTrait
Trait with common test methods for image tests.

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testImageArbitraryHtml() : void {
    $editor = Editor::load('test_format');
    $settings = $editor->getSettings();
    // Allow the data-foo attribute in img via GHS.
    $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags'] = [
        '<img data-foo>',
    ];
    $editor->setSettings($settings);
    $editor->save();
    $format = FilterFormat::load('test_format');
    $original_config = $format->filters('filter_html')
        ->getConfiguration();
    foreach ($this->providerLinkability() as $data) {
        [
            $image_type,
            $unrestricted,
        ] = $data;
        $format_config = $unrestricted ? [
            'status' => FALSE,
        ] : $original_config;
        $format->setFilterConfig('filter_html', $format_config)
            ->save();
        // Make the test content have either a block image or an inline image.
        $img_tag = '<img data-foo="bar" alt="drupalimage test image" data-entity-type="file" ' . $this->imageAttributesAsString() . ' />';
        $this->host->body->value .= $image_type === 'block' ? $img_tag : "<p>{$img_tag}</p>";
        $this->host
            ->save();
        $expected_widget_selector = $image_type === 'block' ? 'image img' : 'image-inline';
        $this->drupalGet($this->host
            ->toUrl('edit-form'));
        $this->waitForEditor();
        $drupalimage = $this->assertSession()
            ->waitForElementVisible('css', ".ck-content .ck-widget.{$expected_widget_selector}");
        $this->assertNotEmpty($drupalimage);
        $this->assertEquals('bar', $drupalimage->getAttribute('data-foo'));
        $xpath = new \DOMXPath($this->getEditorDataAsDom());
        $this->assertNotEmpty($xpath->query('//img[@data-foo="bar"]'));
    }
}

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