From c52f7ba59be5497fae6561901613a9183800364b Mon Sep 17 00:00:00 2001 From: escuriola Date: Tue, 26 Apr 2022 17:44:42 +0200 Subject: [PATCH] OEL-1362: ImageValueObject add cache available. Adapt code and tests. --- oe_theme.theme | 5 +- src/ValueObject/ImageValueObject.php | 25 ++++++- ...ImageTest.php => ImageValueObjectTest.php} | 68 +++++++++++++------ .../Unit/ValueObject/ImageValueObjectTest.php | 6 +- 4 files changed, 73 insertions(+), 31 deletions(-) rename tests/src/Kernel/ValueObject/{ImageTest.php => ImageValueObjectTest.php} (59%) diff --git a/oe_theme.theme b/oe_theme.theme index 337138be64..8cd250dfa2 100644 --- a/oe_theme.theme +++ b/oe_theme.theme @@ -813,10 +813,7 @@ function oe_theme_preprocess_paragraph__oe_list_item(array &$variables): void { /** @var \Drupal\file\Plugin\Field\FieldType\FileItem $image_item */ $image_item = $paragraph->get('field_oe_image')->first(); $file = $image_item->get('entity')->getValue(); - $variables['image'] = [ - 'src' => file_url_transform_relative(file_create_url($file->getFileUri())), - 'alt' => $image_item->get('alt')->getValue(), - ]; + $variables['image'] = ImageValueObject::fromImageItem($image_item); // Caches are handled by the formatter usually. Since we are not rendering // the original render arrays, we need to propagate our caches to the diff --git a/src/ValueObject/ImageValueObject.php b/src/ValueObject/ImageValueObject.php index 06b12fc8e3..618b7abd3d 100644 --- a/src/ValueObject/ImageValueObject.php +++ b/src/ValueObject/ImageValueObject.php @@ -4,6 +4,7 @@ namespace Drupal\oe_theme\ValueObject; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\image\Plugin\Field\FieldType\ImageItem; /** @@ -123,7 +124,7 @@ public function getArray(): array { * @return $this */ public static function fromImageItem(ImageItem $image_item): ValueObjectInterface { - $image_file = $image_item->get('entity')->getTarget(); + $image_file = $image_item->get('entity')->getValue(); $image_object = new static( file_create_url($image_file->get('uri')->getString()), @@ -145,13 +146,13 @@ public static function fromImageItem(ImageItem $image_item): ValueObjectInterfac * The image style name. * * @return $this - * A image value object instance. + * An image value object instance. * * @throws \InvalidArgumentException * Thrown when the image style is not found. */ public static function fromStyledImageItem(ImageItem $image_item, string $style_name): ValueObjectInterface { - $image_file = $image_item->get('entity')->getTarget(); + $image_file = $image_item->get('entity')->getValue(); $style = \Drupal::entityTypeManager()->getStorage('image_style')->load($style_name); if (!$style) { @@ -170,4 +171,22 @@ public static function fromStyledImageItem(ImageItem $image_item, string $style_ return $image_object; } + /** + * Transforms the value object into an image render array. + * + * @return array + * An image render array. + */ + public function toRenderArray(): array { + $build = [ + '#theme' => 'image', + '#uri' => $this->getSource(), + '#title' => $this->getName(), + '#alt' => $this->getAlt(), + ]; + CacheableMetadata::createFromObject($build)->applyTo($build); + + return $build; + } + } diff --git a/tests/src/Kernel/ValueObject/ImageTest.php b/tests/src/Kernel/ValueObject/ImageValueObjectTest.php similarity index 59% rename from tests/src/Kernel/ValueObject/ImageTest.php rename to tests/src/Kernel/ValueObject/ImageValueObjectTest.php index d6286839ad..bee9e1c49a 100644 --- a/tests/src/Kernel/ValueObject/ImageTest.php +++ b/tests/src/Kernel/ValueObject/ImageValueObjectTest.php @@ -17,7 +17,7 @@ * * @group batch2 */ -class ImageTest extends KernelTestBase { +class ImageValueObjectTest extends KernelTestBase { /** * {@inheritdoc} @@ -29,6 +29,13 @@ class ImageTest extends KernelTestBase { 'field', ]; + /** + * Entity object. + * + * @var \Drupal\Core\Entity\EntityInterface + */ + protected $entity; + /** * {@inheritdoc} */ @@ -56,44 +63,63 @@ protected function setUp(): void { // Copy file in public files to use it for styling. \Drupal::service('file_system')->copy(__DIR__ . '/../../../fixtures/example_1.jpeg', 'public://example_1.jpg'); - } - /** - * Test the image value object has the correct style applied. - */ - public function testFromStyledImageItem() { // Create image file. $image = File::create([ 'uri' => 'public://example_1.jpg', ]); $image->save(); - // Create a test style. - /** @var \Drupal\image\ImageStyleInterface $style */ - $style = ImageStyle::create(['name' => 'main_style']); - $style->save(); - // Create an image item. - $alt = $this->randomString(); - $title = $this->randomString(); - $entity = EntityTest::create([ + $this->entity = EntityTest::create([ 'name' => $this->randomString(), 'field_image' => [ 'target_id' => $image->id(), - 'alt' => $alt, - 'title' => $title, + 'alt' => 'This is an alternative title', + 'title' => 'This is a title', ], ]); - $entity->save(); + $this->entity->save(); + } - $object = ImageValueObject::fromStyledImageItem($entity->get('field_image')->first(), $style->getName()); - $this->assertEquals($title, $object->getName()); - $this->assertEquals($alt, $object->getAlt()); + /** + * Tests the ::fromStyledImageItem method. + */ + public function testFromStyledImageItem() { + // Create a test style. + /** @var \Drupal\image\ImageStyleInterface $style */ + $style = ImageStyle::create(['name' => 'main_style']); + $style->save(); + + $object = ImageValueObject::fromStyledImageItem($this->entity->get('field_image')->first(), $style->getName()); + $this->assertEquals('This is a title', $object->getName()); + $this->assertEquals('This is an alternative title', $object->getAlt()); $this->assertStringContainsString('/styles/main_style/public/example_1.jpg', $object->getSource()); + // Test that all the cache tags have present and bubbled up. + $this->assertEqualsCanonicalizing([ + 'config:image.style.main_style', + 'file:1', + ], $object->getCacheTags()); + $invalid_image_style = $this->randomMachineName(); $this->expectExceptionObject(new \InvalidArgumentException(sprintf('Could not load image style with name "%s".', $invalid_image_style))); - ImageValueObject::fromStyledImageItem($entity->get('field_image')->first(), $invalid_image_style); + ImageValueObject::fromStyledImageItem($this->entity->get('field_image')->first(), $invalid_image_style); + } + + /** + * Tests the ::fromImageItem method. + */ + public function testFromImageItem() { + $object = ImageValueObject::fromImageItem($this->entity->get('field_image')->first()); + $this->assertEquals('This is a title', $object->getName()); + $this->assertEquals('This is an alternative title', $object->getAlt()); + $this->assertStringContainsString('/files/example_1.jpg', $object->getSource()); + + // Test that all the cache tags have present and bubbled up. + $this->assertEqualsCanonicalizing([ + 'file:1', + ], $object->getCacheTags()); } } diff --git a/tests/src/Unit/ValueObject/ImageValueObjectTest.php b/tests/src/Unit/ValueObject/ImageValueObjectTest.php index 01ce4a9ff9..7ee896a180 100644 --- a/tests/src/Unit/ValueObject/ImageValueObjectTest.php +++ b/tests/src/Unit/ValueObject/ImageValueObjectTest.php @@ -67,7 +67,7 @@ protected function setUp(): void { $this->imageFile->get('uri')->willReturn($this->imageUri->reveal()); $this->imageEntity = $this->prophesize(EntityReference::class); - $this->imageEntity->getTarget()->willReturn($this->imageFile->reveal()); + $this->imageEntity->getValue()->willReturn($this->imageFile->reveal()); $this->imageReferenceItem = $this->prophesize(ImageItem::class); $this->imageReferenceItem->get('entity')->willReturn($this->imageEntity->reveal()); @@ -84,7 +84,7 @@ protected function setUp(): void { } /** - * Test constructing a image value object from an array. + * Test constructing an image value object from an array. */ public function testFromArray() { $data = [ @@ -104,7 +104,7 @@ public function testFromArray() { } /** - * Test constructing a image value object from an image item. + * Test constructing an image value object from an image item. */ public function testFromImageItem() { /** @var \Drupal\oe_theme\ValueObject\ImageValueObject $object */