function ImageTestBase::testImageArbitraryHtml

Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/tests/src/FunctionalJavascript/ImageTestBase.php \Drupal\Tests\ckeditor5\FunctionalJavascript\ImageTestBase::testImageArbitraryHtml()

Tests that arbitrary attributes are allowed via GHS.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/ImageTestBase.php, line 164

Class

ImageTestBase
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Image[[api-linebreak]] @group ckeditor5 @internal

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.