public function ImageViewBuilderTest::testImageViewBuilder in Twig Tweak 3.x
Same name and namespace in other branches
- 3.1.x tests/src/Kernel/ImageViewBuilderTest.php \Drupal\Tests\twig_tweak\Kernel\ImageViewBuilderTest::testImageViewBuilder()
Test callback.
File
- tests/
src/ Kernel/ ImageViewBuilderTest.php, line 56
Class
- ImageViewBuilderTest
- A test for ImageViewBuilderTest.
Namespace
Drupal\Tests\twig_tweak\KernelCode
public function testImageViewBuilder() : void {
$view_builder = $this->container
->get('twig_tweak.image_view_builder');
/** @var \Drupal\file\FileInterface $public_image */
$public_image = File::create([
'uri' => 'public://ocean.jpg',
]);
$public_image
->save();
/** @var \Drupal\file\FileInterface $private_image */
$private_image = File::create([
'uri' => 'private://sea.jpg',
]);
$private_image
->save();
// -- Without style.
$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));
// -- With style.
$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));
// -- With responsive style.
$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));
// -- Private image with access check.
$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));
// -- Private image without access check.
$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));
}