View source
<?php
namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
final class ImageViewBuilderTest extends KernelTestBase {
protected static $modules = [
'twig_tweak',
'twig_tweak_test',
'user',
'system',
'file',
'image',
'responsive_image',
'breakpoint',
];
public function setUp() : void {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installSchema('file', 'file_usage');
ImageStyle::create([
'name' => 'large',
])
->save();
ResponsiveImageStyle::create([
'id' => 'wide',
])
->save();
}
public function register(ContainerBuilder $container) {
parent::register($container);
$container
->register('stream_wrapper.private', 'Drupal\\Core\\StreamWrapper\\PrivateStream')
->addTag('stream_wrapper', [
'scheme' => 'private',
]);
}
public function testImageViewBuilder() : void {
$view_builder = $this->container
->get('twig_tweak.image_view_builder');
$public_image = File::create([
'uri' => 'public://ocean.jpg',
]);
$public_image
->save();
$private_image = File::create([
'uri' => 'private://sea.jpg',
]);
$private_image
->save();
$build = $view_builder
->build($public_image);
$expected_build = [
'#uri' => 'public://ocean.jpg',
'#attributes' => [],
'#theme' => 'image',
'#cache' => [
'contexts' => [
'user',
'user.permissions',
],
'tags' => [
'tag_for_public://ocean.jpg',
],
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertSame('<img src="/files/ocean.jpg" alt="" />', $this
->renderPlain($build));
$build = $view_builder
->build($public_image, 'large', [
'alt' => 'Ocean',
]);
$expected_build = [
'#uri' => 'public://ocean.jpg',
'#attributes' => [
'alt' => 'Ocean',
],
'#theme' => 'image_style',
'#style_name' => 'large',
'#cache' => [
'contexts' => [
'user',
'user.permissions',
],
'tags' => [
'tag_for_public://ocean.jpg',
],
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertSame('<img alt="Ocean" src="/files/styles/large/public/ocean.jpg?itok=abc" />', $this
->renderPlain($build));
$build = $view_builder
->build($public_image, 'wide', [
'alt' => 'Ocean',
], TRUE);
$expected_build = [
'#uri' => 'public://ocean.jpg',
'#attributes' => [
'alt' => 'Ocean',
],
'#type' => 'responsive_image',
'#responsive_image_style_id' => 'wide',
'#cache' => [
'contexts' => [
'user',
'user.permissions',
],
'tags' => [
'tag_for_public://ocean.jpg',
],
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertSame('<picture><img src="/files/ocean.jpg" alt="Ocean" /></picture>', $this
->renderPlain($build));
$build = $view_builder
->build($private_image);
$expected_build = [
'#cache' => [
'contexts' => [
'user',
],
'tags' => [
'tag_for_private://sea.jpg',
],
'max-age' => 70,
],
];
self::assertSame($expected_build, $build);
self::assertSame('', $this
->renderPlain($build));
$build = $view_builder
->build($private_image, NULL, [], FALSE, FALSE);
$expected_build = [
'#uri' => 'private://sea.jpg',
'#attributes' => [],
'#theme' => 'image',
'#cache' => [
'contexts' => [],
'tags' => [],
'max-age' => Cache::PERMANENT,
],
];
self::assertSame($expected_build, $build);
self::assertSame('<img src="/files/sea.jpg" alt="" />', $this
->renderPlain($build));
}
private function renderPlain(array $build) : string {
$html = $this->container
->get('renderer')
->renderPlain($build);
$html = preg_replace('#src=".+/files/#s', 'src="/files/', $html);
$html = preg_replace('#\\?itok=.+"#', '?itok=abc"', $html);
$html = preg_replace([
'#\\s{2,}#',
'#\\n#',
], '', $html);
return rtrim($html);
}
}