@ -4,17 +4,18 @@ namespace Drupal\Tests\twig_tweak\Kernel;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Entity\ImageStyle;
use Drupal\KernelTests\KernelTestBase;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
/**
/**
* A test for ImageViewBuilderTest .
* A test class for testing the image view builder .
*
*
* @group twig_tweak
* @group twig_tweak
*/
*/
final class ImageViewBuilderTest extends KernelTestB ase {
final class ImageViewBuilderTest extends AbstractTestC ase {
/**
/**
* {@inheritdoc}
* {@inheritdoc}
@ -30,15 +31,93 @@ final class ImageViewBuilderTest extends KernelTestBase {
'breakpoint',
'breakpoint',
];
];
/**
* The public image URI.
*
* @var string
*/
protected string $publicImageUri;
/**
* The private image URI.
*
* @var string
*/
protected string $privateImageUri;
/**
* The public image file.
*
* @var \Drupal\file\FileInterface
*/
protected $publicImage;
/**
* The private image file.
*
* @var \Drupal\file\FileInterface
*/
protected $privateImage;
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function setUp(): void {
public function setUp(): void {
parent::setUp();
parent::setUp();
$this->installEntitySchema('file');
$this->installEntitySchema('file');
$this->installSchema('file', 'file_usage');
$this->installSchema('file', 'file_usage');
ImageStyle::create(['name' => 'large'])->save();
ResponsiveImageStyle::create(['id' => 'wide'])->save();
$file_system = $this->container->get('file_system');
$file_system->prepareDirectory($this->siteDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$private_directory = $this->siteDirectory . '/private';
$file_system->prepareDirectory($private_directory, FileSystemInterface::CREATE_DIRECTORY);
$this->setSetting('file_private_path', $private_directory);
$image_style = ImageStyle::create([
'name' => 'small',
'label' => 'Small',
]);
// Add a crop effect:
$image_style->addImageEffect([
'id' => 'image_resize',
'data' => [
'width' => 10,
'height' => 10,
],
'weight' => 0,
]);
$image_style->save();
$responsive_image_style = ResponsiveImageStyle::create([
'id' => 'wide',
'label' => 'Wide',
'breakpoint_group' => 'twig_tweak_image_view_builder',
'fallback_image_style' => 'small',
]);
$responsive_image_style->save();
// Create a copy of a test image file in root. Original sizes: 40x20px.
$this->publicImageUri = 'public://image-test-do.jpg';
$file_system->copy('core/tests/fixtures/files/image-test.jpg', $this->publicImageUri, FileSystemInterface::EXISTS_REPLACE);
$this->assertFileExists($this->publicImageUri);
$this->publicImage = File::create([
'uri' => $this->publicImageUri,
'status' => FileInterface::STATUS_PERMANENT,
]);
$this->publicImage->save();
// Create a copy of a test image file in root. Original sizes: 40x20px.
$this->privateImageUri = 'private://image-test-do.png';
$file_system->copy('core/tests/fixtures/files/image-test.png', $this->privateImageUri, FileSystemInterface::EXISTS_REPLACE);
$this->assertFileExists($this->privateImageUri);
$this->privateImage = File::create([
'uri' => $this->privateImageUri,
'status' => FileInterface::STATUS_PERMANENT,
]);
$this->privateImage->save();
}
}
/**
/**
@ -54,21 +133,20 @@ final class ImageViewBuilderTest extends KernelTestBase {
* Test callback.
* Test callback.
*/
*/
public function testImageViewBuilder(): void {
public function testImageViewBuilder(): void {
$view_builder = $this->container->get('twig_tweak.image_view_builder');
$view_builder = $this->container->get('twig_tweak.image_view_builder');
/** @var \Drupal\file\FileInterface $public_image */
$uri = $this->publicImage->getFileUri();
$public_image = File::create(['uri' => 'public://ocean.jpg'] );
$image = \Drupal::service('image.factory')->get($uri );
$public_image->save ();
$imageOriginalWidth = $image->getWidth ();
$imageOriginalHeight = $image->getHeight();
/** @var \Drupal\file\FileInterface $private_image */
self::assertTrue($image->isValid());
$private_image = File::create(['uri' => 'private://sea.jpg'] );
self::assertEquals(40, $imageOriginalWidth );
$private_image->save( );
self::assertEquals(20, $imageOriginalHeight );
// -- Without style.
// -- Without style.
$build = $view_builder->build($public_i mage);
$build = $view_builder->build($this->publicI mage);
$expected_build = [
$expected_build = [
'#uri' => 'public://ocean.jpg' ,
'#uri' => $this->publicImageUri ,
'#attributes' => [],
'#attributes' => [],
'#theme' => 'image',
'#theme' => 'image',
'#cache' => [
'#cache' => [
@ -76,37 +154,47 @@ final class ImageViewBuilderTest extends KernelTestBase {
'user',
'user',
'user.permissions',
'user.permissions',
],
],
'tags' => ['tag_for_public://ocean.jpg'],
'tags' => [
'file:1',
'tag_for_' . $this->publicImageUri,
],
'max-age' => 70,
'max-age' => 70,
],
],
];
];
self::assertSame ($expected_build, $build);
self::assertRenderArray ($expected_build, $build);
self::assertSame('< img src = "/files/ocean .jpg" alt = "" / > ', $this->renderPlain($build));
self::assertSame('< img src = "/files/image-test-do .jpg" alt = "" / > ', $this->renderPlain($build));
// -- With style.
// -- With style.
$build = $view_builder->build($public_image, 'large', ['alt' => 'Ocean ']);
$build = $view_builder->build($this->publicImage, 'small', ['alt' => 'Image Test Do ']);
$expected_build = [
$expected_build = [
'#uri' => 'public://ocean.jpg',
'#uri' => $this->publicImageUri,
'#attributes' => ['alt' => 'Ocean'],
'#attributes' => ['alt' => 'Image Test Do'],
'#width' => $imageOriginalWidth,
'#height' => $imageOriginalHeight,
'#theme' => 'image_style',
'#theme' => 'image_style',
'#style_name' => 'large',
'#style_name' => 'small ',
'#cache' => [
'#cache' => [
'contexts' => [
'contexts' => [
'user',
'user',
'user.permissions',
'user.permissions',
],
],
'tags' => ['tag_for_public://ocean.jpg'],
'tags' => [
'file:1',
'tag_for_' . $this->publicImageUri,
],
'max-age' => 70,
'max-age' => 70,
],
],
];
];
self::assertSame ($expected_build, $build);
self::assertRenderArray ($expected_build, $build);
self::assertSame('< img alt = "Ocean" src = "/files/styles/large/public/ocean.jpg?itok=abc " / > ', $this->renderPlain($build));
self::assertSame('< img alt = "Image Test Do" src = "/files/styles/small/public/image-test-do.jpg?itok=abc" width = "10" height = "10" loading = "lazy " / > ', $this->renderPlain($build));
// -- With responsive style.
// -- With responsive style.
$build = $view_builder->build($public_image, 'wide', ['alt' => 'Ocean '], TRUE);
$build = $view_builder->build($this->publicImage, 'wide', ['alt' => 'Image Test Do '], TRUE);
$expected_build = [
$expected_build = [
'#uri' => 'public://ocean.jpg',
'#uri' => $this->publicImageUri,
'#attributes' => ['alt' => 'Ocean'],
'#attributes' => ['alt' => 'Image Test Do'],
'#width' => $imageOriginalWidth,
'#height' => $imageOriginalHeight,
'#type' => 'responsive_image',
'#type' => 'responsive_image',
'#responsive_image_style_id' => 'wide',
'#responsive_image_style_id' => 'wide',
'#cache' => [
'#cache' => [
@ -114,39 +202,45 @@ final class ImageViewBuilderTest extends KernelTestBase {
'user',
'user',
'user.permissions',
'user.permissions',
],
],
'tags' => ['tag_for_public://ocean.jpg'],
'tags' => [
'file:1',
'tag_for_' . $this->publicImageUri,
],
'max-age' => 70,
'max-age' => 70,
],
],
];
];
self::assertSame ($expected_build, $build);
self::assertRenderArray ($expected_build, $build);
self::assertSame('< picture > < img src = "/files/ocean.jpg" alt = "Ocean " / > < / picture > ', $this->renderPlain($build));
self::assertSame('< picture > < img src = "/files/styles/small/public/image-test-do.jpg?itok=abc" width = "10" height = "10" alt = "Image Test Do" loading = "lazy " / > < / picture > ', $this->renderPlain($build));
// -- Private image with access check.
// -- Private image with access check.
$build = $view_builder->build($private_i mage);
$build = $view_builder->build($this->privateI mage);
$expected_build = [
$expected_build = [
'#cache' => [
'#cache' => [
'contexts' => ['user'],
'contexts' => ['user'],
'tags' => ['tag_for_private://sea.jpg'],
'tags' => [
'file:2',
'tag_for_' . $this->privateImageUri,
],
'max-age' => 70,
'max-age' => 70,
],
],
];
];
self::assertSame ($expected_build, $build);
self::assertRenderArray ($expected_build, $build);
self::assertSame('', $this->renderPlain($build));
self::assertSame('', $this->renderPlain($build));
// -- Private image without access check.
// -- Private image without access check.
$build = $view_builder->build($private_i mage, NULL, [], FALSE, FALSE);
$build = $view_builder->build($this->privateI mage, NULL, [], FALSE, FALSE);
$expected_build = [
$expected_build = [
'#uri' => 'private://sea.jpg' ,
'#uri' => $this->privateImageUri ,
'#attributes' => [],
'#attributes' => [],
'#theme' => 'image',
'#theme' => 'image',
'#cache' => [
'#cache' => [
'contexts' => [],
'contexts' => [],
'tags' => [],
'tags' => ['file:2' ],
'max-age' => Cache::PERMANENT,
'max-age' => Cache::PERMANENT,
],
],
];
];
self::assertSame ($expected_build, $build);
self::assertRenderArray ($expected_build, $build);
self::assertSame('< img src = "/files/sea.jp g" alt = "" / > ', $this->renderPlain($build));
self::assertSame('< img src = "/files/image-test-do.pn g" alt = "" / > ', $this->renderPlain($build));
}
}
/**
/**
@ -155,7 +249,7 @@ final class ImageViewBuilderTest extends KernelTestBase {
private function renderPlain(array $build): string {
private function renderPlain(array $build): string {
$html = $this->container->get('renderer')->renderPlain($build);
$html = $this->container->get('renderer')->renderPlain($build);
$html = preg_replace('#src=".+/files/#s', 'src="/files/', $html);
$html = preg_replace('#src=".+/files/#s', 'src="/files/', $html);
$html = preg_replace('#\?itok=.+"#', '?itok=abc"', $html);
$html = preg_replace('#\?itok=.+? "#', '?itok=abc"', $html);
$html = preg_replace(['#\s{2,}#', '#\n#'], '', $html);
$html = preg_replace(['#\s{2,}#', '#\n#'], '', $html);
return rtrim($html);
return rtrim($html);
}
}