diff --git a/src/lib/FieldType/Image/Type.php b/src/lib/FieldType/Image/Type.php index 90fa8fb0cc..655aa2fc4f 100644 --- a/src/lib/FieldType/Image/Type.php +++ b/src/lib/FieldType/Image/Type.php @@ -438,12 +438,12 @@ public function fromPersistenceValue(FieldValue $fieldValue) 'imageId' => (isset($fieldValue->data['imageId']) ? $fieldValue->data['imageId'] : null), - 'width' => (isset($fieldValue->data['width']) + 'width' => ($fieldValue->data['width'] ?? '') !== '' ? $fieldValue->data['width'] - : null), - 'height' => (isset($fieldValue->data['height']) + : null, + 'height' => ($fieldValue->data['height'] ?? '') !== '' ? $fieldValue->data['height'] - : null), + : null, 'additionalData' => $fieldValue->data['additionalData'] ?? [], 'mime' => $fieldValue->data['mime'] ?? null, ] diff --git a/tests/bundle/Core/Imagine/AliasGeneratorTest.php b/tests/bundle/Core/Imagine/AliasGeneratorTest.php index 129686cfe0..d701acb302 100644 --- a/tests/bundle/Core/Imagine/AliasGeneratorTest.php +++ b/tests/bundle/Core/Imagine/AliasGeneratorTest.php @@ -247,6 +247,57 @@ public function testGetVariationOriginal(): void ); } + public function testGetVariationOriginalWithNullWidthAndHeight(): void + { + $originalPath = 'foo/bar/image.jpg'; + $variationName = 'original'; + $imageId = '123-45'; + $imageValue = new ImageValue( + [ + 'id' => $originalPath, + 'imageId' => $imageId, + 'width' => null, + 'height' => null, + 'fileSize' => 1024, + 'mime' => 'image/jpeg', + ] + ); + $field = new Field([ + 'value' => $imageValue, + 'fieldDefIdentifier' => 'image_field', + ]); + $expectedUrl = 'http://localhost/foo/bar/image.jpg'; + + $this->ioResolver + ->expects(self::once()) + ->method('resolve') + ->with($originalPath, $variationName) + ->will(self::returnValue($expectedUrl)); + + $expected = new ImageVariation( + [ + 'name' => $variationName, + 'fileName' => 'image.jpg', + 'dirPath' => 'http://localhost/foo/bar', + 'uri' => $expectedUrl, + 'imageId' => $imageId, + 'height' => null, + 'width' => null, + 'fileSize' => 1024, + 'mimeType' => 'image/jpeg', + ] + ); + + self::assertEquals( + $expected, + $this->aliasGenerator->getVariation( + $field, + new VersionInfo(), + $variationName + ) + ); + } + /** * Test obtaining Image Variation that hasn't been stored yet and has multiple references. * diff --git a/tests/lib/FieldType/ImageTest.php b/tests/lib/FieldType/ImageTest.php index 92ecb5434a..f806ea6fa3 100644 --- a/tests/lib/FieldType/ImageTest.php +++ b/tests/lib/FieldType/ImageTest.php @@ -8,6 +8,7 @@ namespace Ibexa\Tests\Core\FieldType; use Ibexa\Contracts\Core\IO\MimeTypeDetector; +use Ibexa\Contracts\Core\Persistence\Content\FieldValue; use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; use Ibexa\Core\Base\Exceptions\InvalidArgumentException; use Ibexa\Core\FieldType\Image\Type as ImageType; @@ -347,6 +348,62 @@ public function provideInputForFromHash(): iterable ]; } + /** + * @phpstan-return iterable, mixed, mixed}> + */ + public function provideDataForFromPersistenceValue(): iterable + { + yield 'width and height as empty string are converted to null' => [ + [ + 'width' => '', + 'height' => '', + ], + null, + null, + ]; + + yield 'width and height as zero are preserved' => [ + [ + 'width' => 0, + 'height' => '0', + ], + 0, + '0', + ]; + + yield 'width and height as valid values are preserved' => [ + [ + 'width' => 100, + 'height' => '200', + ], + 100, + '200', + ]; + + yield 'missing width and height keys default to null' => [ + [], + null, + null, + ]; + } + + /** + * @param array $data + * + * @dataProvider provideDataForFromPersistenceValue + */ + public function testFromPersistenceValue(array $data, mixed $expectedWidth, mixed $expectedHeight): void + { + $fieldType = $this->getFieldTypeUnderTest(); + + $fieldValue = new FieldValue(['data' => $data]); + $result = $fieldType->fromPersistenceValue($fieldValue); + + self::assertInstanceOf(ImageValue::class, $result); + self::assertSame($expectedWidth, $result->width); + self::assertSame($expectedHeight, $result->height); + } + protected function provideFieldTypeIdentifier(): string { return 'ibexa_image'; diff --git a/tests/lib/Persistence/Legacy/FieldValue/Converter/ImageConverterTest.php b/tests/lib/Persistence/Legacy/FieldValue/Converter/ImageConverterTest.php index 7d25304b03..63f2e0f41f 100644 --- a/tests/lib/Persistence/Legacy/FieldValue/Converter/ImageConverterTest.php +++ b/tests/lib/Persistence/Legacy/FieldValue/Converter/ImageConverterTest.php @@ -93,16 +93,16 @@ public function fieldValueToXmlProvider(): array ], ]), <<< XML - - - - - 50100John Smith - -XML, + + + + + 50100John Smith + + XML, ], 'without_additional_data_stored' => [ new FieldValue([ @@ -118,16 +118,16 @@ public function fieldValueToXmlProvider(): array ], ]), <<< XML - - - - - - -XML, + + + + + + + XML, ], ]; } @@ -169,21 +169,21 @@ public function xmlToFieldValueProvider(): array return [ 'with_additional_data' => [ -<<< XML - - - - - - 50 - 100 - John Smith - - -XML, + <<< XML + + + + + + 50 + 100 + John Smith + + + XML, new FieldValue([ 'data' => [ 'width' => '100', @@ -201,16 +201,16 @@ public function xmlToFieldValueProvider(): array ]), ], 'without_additional_data_stored' => [ -<<< XML - - - - - -XML, + <<< XML + + + + + + XML, new FieldValue([ 'data' => [ 'width' => '100', @@ -223,6 +223,29 @@ public function xmlToFieldValueProvider(): array ], ]), ], + 'with_empty_width_and_height' => [ + <<< XML + + + + + + XML, + new FieldValue([ + 'data' => [ + 'width' => '', + 'height' => '', + 'alternativeText' => 'test', + 'mime' => 'image/png', + 'id' => 1, + 'fileName' => 'ibexa_fav.png', + 'additionalData' => [], + ], + ]), + ], ]; } }