You are here

public function MediaTest::testMediaArbitraryHtml in Drupal 10

Tests that arbitrary attributes are allowed via GHS.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php, line 280

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media @group ckeditor5 @internal

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testMediaArbitraryHtml() {
  $assert_session = $this
    ->assertSession();
  $editor = Editor::load('test_format');
  $settings = $editor
    ->getSettings();

  // Allow the data-foo attribute in drupal-media via GHS. Also, add support
  // for div's with data-foo attribute to ensure that drupal-media elements
  // can be wrapped with other block elements.
  $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags'] = [
    '<drupal-media data-foo>',
    '<div data-bar>',
  ];
  $editor
    ->setSettings($settings);
  $editor
    ->save();
  $filter_format = $editor
    ->getFilterFormat();
  $filter_format
    ->setFilterConfig('filter_html', [
    'status' => TRUE,
    'settings' => [
      'allowed_html' => '<p> <br> <strong> <em> <a href> <drupal-media data-entity-type data-entity-uuid data-align data-caption alt data-foo data-view-mode> <div data-bar>',
    ],
  ]);
  $filter_format
    ->save();
  $this
    ->assertSame([], array_map(function (ConstraintViolation $v) {
    return (string) $v
      ->getMessage();
  }, iterator_to_array(CKEditor5::validatePair(Editor::load('test_format'), FilterFormat::load('test_format')))));

  // Add data-foo use to an existing drupal-media tag.
  $original_value = $this->host->body->value;
  $this->host->body->value = '<div data-bar="baz">' . str_replace('drupal-media', 'drupal-media data-foo="bar" ', $original_value) . '</div>';
  $this->host
    ->save();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));

  // Confirm data-foo is present in the drupal-media preview.
  $this
    ->assertNotEmpty($upcasted_media = $assert_session
    ->waitForElementVisible('css', '.ck-widget.drupal-media'));
  $this
    ->assertFalse($upcasted_media
    ->hasAttribute('data-foo'));
  $this
    ->assertNotEmpty($preview = $assert_session
    ->waitForElementVisible('css', '.ck-widget.drupal-media > [data-drupal-media-preview="ready"] > .media', 30000));
  $this
    ->assertEquals('bar', $preview
    ->getAttribute('data-foo'));

  // Confirm that the media is wrapped by the div on the editing view.
  $assert_session
    ->elementExists('css', 'div[data-bar="baz"] > .drupal-media');

  // Confirm data-foo is not stripped from source.
  $this
    ->assertSourceAttributeSame('data-foo', 'bar');

  // Confirm that drupal-media is wrapped by the div.
  $editor_dom = new \DOMXPath($this
    ->getEditorDataAsDom());
  $this
    ->assertNotEmpty($editor_dom
    ->query('//div[@data-bar="baz"]/drupal-media'));
}