Skip to content

Commit

Permalink
OEL-1362: ImageValueObject add cache available. Adapt code and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
escuriola committed Apr 26, 2022
1 parent 9bb2cb2 commit c52f7ba
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
5 changes: 1 addition & 4 deletions oe_theme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions src/ValueObject/ImageValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\oe_theme\ValueObject;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\image\Plugin\Field\FieldType\ImageItem;

/**
Expand Down Expand Up @@ -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()),
Expand All @@ -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) {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @group batch2
*/
class ImageTest extends KernelTestBase {
class ImageValueObjectTest extends KernelTestBase {

/**
* {@inheritdoc}
Expand All @@ -29,6 +29,13 @@ class ImageTest extends KernelTestBase {
'field',
];

/**
* Entity object.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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());
}

}
6 changes: 3 additions & 3 deletions tests/src/Unit/ValueObject/ImageValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 = [
Expand All @@ -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 */
Expand Down

0 comments on commit c52f7ba

Please sign in to comment.