inline_image_saver-1.0.x-dev/tests/src/Traits/InlineImageTestTrait.php
tests/src/Traits/InlineImageTestTrait.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\inline_image_saver\Traits;
use Drupal\Component\Utility\Html;
/**
* Provides methods to create image markup and extract image from markup.
*/
trait InlineImageTestTrait {
/**
* The text format being tested.
*/
protected string $processableFormatId;
/**
* Extracts <img> element from markup.
*/
protected function extractImageElement(string $markup): \DOMElement {
$img = Html::load($markup)->getElementsByTagName('img')->item(0);
$this->assertInstanceOf(\DOMElement::class, $img);
return $img;
}
/**
* Creates markup with image.
*/
protected function createImageMarkup(array $attributes): string {
$element['some_text_1'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->randomMachineName(16),
];
$element['image_container'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'image' => [
'#type' => 'html_tag',
'#tag' => 'img',
'#attributes' => $attributes,
],
];
$element['some_text_2'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->randomMachineName(32),
];
$markup = (string) $this->container->get('renderer')->renderInIsolation($element);
return Html::normalize((string) check_markup($markup, $this->processableFormatId));
}
}
