public function ImageFormatterTest::testImageFormatterSvg in Drupal 10
Same name and namespace in other branches
- 8 core/modules/image/tests/src/Kernel/ImageFormatterTest.php \Drupal\Tests\image\Kernel\ImageFormatterTest::testImageFormatterSvg()
- 9 core/modules/image/tests/src/Kernel/ImageFormatterTest.php \Drupal\Tests\image\Kernel\ImageFormatterTest::testImageFormatterSvg()
Tests ImageFormatter's handling of SVG images.
@requires extension gd
File
- core/
modules/ image/ tests/ src/ Kernel/ ImageFormatterTest.php, line 110
Class
- ImageFormatterTest
- Tests the image field rendering using entity fields of the image field type.
Namespace
Drupal\Tests\image\KernelCode
public function testImageFormatterSvg() {
// Install the default image styles.
$this
->installConfig([
'image',
]);
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$png = File::create([
'uri' => 'public://test-image.png',
]);
$png
->save();
// We need to create an actual empty PNG, or the GD toolkit will not
// consider the image valid.
$png_resource = imagecreate(300, 300);
imagefill($png_resource, 0, 0, imagecolorallocate($png_resource, 0, 0, 0));
imagepng($png_resource, $png
->getFileUri());
$svg = File::create([
'uri' => 'public://test-image.svg',
]);
$svg
->save();
// We don't have to put any real SVG data in here, because the GD toolkit
// won't be able to load it anyway.
touch($svg
->getFileUri());
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
$this->fieldName => [
$png,
$svg,
],
]);
$entity
->save();
// Ensure that the display is using the medium image style.
$component = $this->display
->getComponent($this->fieldName);
$component['settings']['image_style'] = 'medium';
$this->display
->setComponent($this->fieldName, $component)
->save();
$build = $this->display
->build($entity);
// The first image is a PNG, so it is supported by the GD image toolkit.
// The image style should be applied with its cache tags, image derivative
// computed with its URI and dimensions.
$this
->assertCacheTags($build[$this->fieldName][0], ImageStyle::load('medium')
->getCacheTags());
$renderer
->renderRoot($build[$this->fieldName][0]);
$this
->assertEquals('medium', $build[$this->fieldName][0]['#image_style']);
// We check that the image URL contains the expected style directory
// structure.
$this
->assertStringContainsString('styles/medium/public/test-image.png', $build[$this->fieldName][0]['#markup']);
$this
->assertStringContainsString('width="220"', $build[$this->fieldName][0]['#markup']);
$this
->assertStringContainsString('height="220"', $build[$this->fieldName][0]['#markup']);
// The second image is an SVG, which is not supported by the GD toolkit.
// The image style should still be applied with its cache tags, but image
// derivative will not be available so <img> tag will point to the original
// image.
$this
->assertCacheTags($build[$this->fieldName][1], ImageStyle::load('medium')
->getCacheTags());
$renderer
->renderRoot($build[$this->fieldName][1]);
$this
->assertEquals('medium', $build[$this->fieldName][1]['#image_style']);
// We check that the image URL does not contain the style directory
// structure.
$this
->assertStringNotContainsString('styles/medium/public/test-image.svg', $build[$this->fieldName][1]['#markup']);
// Since we did not store original image dimensions, width and height
// HTML attributes will not be present.
$this
->assertStringNotContainsString('width', $build[$this->fieldName][1]['#markup']);
$this
->assertStringNotContainsString('height', $build[$this->fieldName][1]['#markup']);
}