Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib/FieldType/Image/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '') !== ''
Comment thread
konradoboza marked this conversation as resolved.
? $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,
]
Expand Down
51 changes: 51 additions & 0 deletions tests/bundle/Core/Imagine/AliasGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/lib/FieldType/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -347,6 +348,62 @@ public function provideInputForFromHash(): iterable
];
}

/**
* @phpstan-return iterable<string, array{array<string, mixed>, 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<string, mixed> $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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ public function fieldValueToXmlProvider(): array
],
]),
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
<additional_data><attribute key="focalPointX">50</attribute><attribute key="focalPointY">100</attribute><attribute key="author">John Smith</attribute></additional_data>
</ezimage>
XML,
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
<additional_data><attribute key="focalPointX">50</attribute><attribute key="focalPointY">100</attribute><attribute key="author">John Smith</attribute></additional_data>
</ezimage>
XML,
],
'without_additional_data_stored' => [
new FieldValue([
Expand All @@ -118,16 +118,16 @@ public function fieldValueToXmlProvider(): array
],
]),
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
<additional_data/>
</ezimage>
XML,
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
<additional_data/>
</ezimage>
XML,
],
];
}
Expand Down Expand Up @@ -169,21 +169,21 @@ public function xmlToFieldValueProvider(): array

return [
'with_additional_data' => [
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
<additional_data>
<attribute key="focalPointX">50</attribute>
<attribute key="focalPointY">100</attribute>
<attribute key="author">John Smith</attribute>
</additional_data>
</ezimage>
XML,
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
<additional_data>
<attribute key="focalPointX">50</attribute>
<attribute key="focalPointY">100</attribute>
<attribute key="author">John Smith</attribute>
</additional_data>
</ezimage>
XML,
new FieldValue([
'data' => [
'width' => '100',
Expand All @@ -201,16 +201,16 @@ public function xmlToFieldValueProvider(): array
]),
],
'without_additional_data_stored' => [
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
</ezimage>
XML,
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width="100"
height="200" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="200" Width="100" IsColor="1"/>
</ezimage>
XML,
new FieldValue([
'data' => [
'width' => '100',
Expand All @@ -223,6 +223,29 @@ public function xmlToFieldValueProvider(): array
],
]),
],
'with_empty_width_and_height' => [
<<< XML
<?xml version="1.0" encoding="utf-8"?>
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
original_filename="ibexa_fav.png" mime_type="image/png" width=""
height="" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
<information Height="" Width="" IsColor="1"/>
</ezimage>
XML,
new FieldValue([
'data' => [
'width' => '',
'height' => '',
'alternativeText' => 'test',
'mime' => 'image/png',
'id' => 1,
'fileName' => 'ibexa_fav.png',
'additionalData' => [],
],
]),
],
];
}
}
Loading