function ImageTestBase::testAttributeRetentionDuringUpcasting

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

Ensures that attributes are retained on conversion.

File

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

Class

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

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testAttributeRetentionDuringUpcasting() : void {
  // Run test cases in a single test to make the test run faster.
  $attributes_to_retain = [
    '-none-' => 'inline',
    'data-caption="test caption 🦙"' => 'block',
    'data-align="left"' => 'inline',
  ];
  foreach ($attributes_to_retain as $attribute_to_retain => $expected_upcast_behavior_when_wrapped_in_block_element) {
    if ($attribute_to_retain === '-none-') {
      $attribute_to_retain = '';
    }
    $img_tag = '<img ' . $attribute_to_retain . ' alt="drupalimage test image" ' . $this->imageAttributesAsString() . ' />';
    $test_cases = [
      // Plain image tag for a baseline.
[
        $img_tag,
        $img_tag,
      ],
      // Image tag wrapped with <p>.
[
        "<p>{$img_tag}</p>",
        $expected_upcast_behavior_when_wrapped_in_block_element === 'inline' ? "<p>{$img_tag}</p>" : $img_tag,
      ],
      // Image tag wrapped with a disallowed paragraph-like element (<div).
      // When inline is the expected upcast behavior, it will wrap in <p>
      // because it still must wrap in a paragraph-like element, and <p> is
      // available to be that element.
[
        "<div>{$img_tag}</div>",
        $expected_upcast_behavior_when_wrapped_in_block_element === 'inline' ? "<p>{$img_tag}</p>" : $img_tag,
      ],
    ];
    foreach ($test_cases as $test_case) {
      [
        $markup,
        $expected,
      ] = $test_case;
      $this->host->body->value = $markup;
      $this->host
        ->save();
      $this->drupalGet($this->host
        ->toUrl('edit-form'));
      $this->waitForEditor();
      // Ensure that the image is rendered in preview.
      $this->assertNotEmpty($this->assertSession()
        ->waitForElementVisible('css', ".ck-content .ck-widget img"));
      $editor_dom = $this->getEditorDataAsDom();
      $expected_dom = Html::load($expected);
      $xpath = new \DOMXPath($this->getEditorDataAsDom());
      $this->assertEquals($expected_dom->getElementsByTagName('body')
        ->item(0)
        ->C14N(), $editor_dom->getElementsByTagName('body')
        ->item(0)
        ->C14N());
      // Ensure the test attribute is persisted on downcast.
      if ($attribute_to_retain) {
        $this->assertNotEmpty($xpath->query("//img[@{$attribute_to_retain}]"));
      }
    }
  }
}

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