Skip to content

Commit 9aff5e6

Browse files
committed
Write tests for IBX-11610: Images with width= or height= in xml definition will cause crash
1 parent 8a0098d commit 9aff5e6

3 files changed

Lines changed: 131 additions & 0 deletions

File tree

tests/bundle/Core/Imagine/AliasGeneratorTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,57 @@ public function testGetVariationOriginal(): void
247247
);
248248
}
249249

250+
public function testGetVariationOriginalWithNullWidthAndHeight(): void
251+
{
252+
$originalPath = 'foo/bar/image.jpg';
253+
$variationName = 'original';
254+
$imageId = '123-45';
255+
$imageValue = new ImageValue(
256+
[
257+
'id' => $originalPath,
258+
'imageId' => $imageId,
259+
'width' => null,
260+
'height' => null,
261+
'fileSize' => 1024,
262+
'mime' => 'image/jpeg',
263+
]
264+
);
265+
$field = new Field([
266+
'value' => $imageValue,
267+
'fieldDefIdentifier' => 'image_field',
268+
]);
269+
$expectedUrl = 'http://localhost/foo/bar/image.jpg';
270+
271+
$this->ioResolver
272+
->expects(self::once())
273+
->method('resolve')
274+
->with($originalPath, $variationName)
275+
->will(self::returnValue($expectedUrl));
276+
277+
$expected = new ImageVariation(
278+
[
279+
'name' => $variationName,
280+
'fileName' => 'image.jpg',
281+
'dirPath' => 'http://localhost/foo/bar',
282+
'uri' => $expectedUrl,
283+
'imageId' => $imageId,
284+
'height' => null,
285+
'width' => null,
286+
'fileSize' => 1024,
287+
'mimeType' => 'image/jpeg',
288+
]
289+
);
290+
291+
self::assertEquals(
292+
$expected,
293+
$this->aliasGenerator->getVariation(
294+
$field,
295+
new VersionInfo(),
296+
$variationName
297+
)
298+
);
299+
}
300+
250301
/**
251302
* Test obtaining Image Variation that hasn't been stored yet and has multiple references.
252303
*

tests/lib/FieldType/ImageTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Ibexa\Tests\Core\FieldType;
99

1010
use Ibexa\Contracts\Core\IO\MimeTypeDetector;
11+
use Ibexa\Contracts\Core\Persistence\Content\FieldValue;
1112
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
1213
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
1314
use Ibexa\Core\FieldType\Image\Type as ImageType;
@@ -347,6 +348,62 @@ public function provideInputForFromHash(): iterable
347348
];
348349
}
349350

351+
/**
352+
* @phpstan-return iterable<string, array{array<string, mixed>, mixed, mixed}>
353+
*/
354+
public function provideDataForFromPersistenceValue(): iterable
355+
{
356+
yield 'width and height as empty string are converted to null' => [
357+
[
358+
'width' => '',
359+
'height' => '',
360+
],
361+
null,
362+
null,
363+
];
364+
365+
yield 'width and height as zero are preserved' => [
366+
[
367+
'width' => 0,
368+
'height' => '0',
369+
],
370+
0,
371+
'0',
372+
];
373+
374+
yield 'width and height as valid values are preserved' => [
375+
[
376+
'width' => 100,
377+
'height' => '200',
378+
],
379+
100,
380+
'200',
381+
];
382+
383+
yield 'missing width and height keys default to null' => [
384+
[],
385+
null,
386+
null,
387+
];
388+
}
389+
390+
/**
391+
* @param array<string, mixed> $data
392+
*
393+
* @dataProvider provideDataForFromPersistenceValue
394+
*/
395+
public function testFromPersistenceValue(array $data, mixed $expectedWidth, mixed $expectedHeight): void
396+
{
397+
$fieldType = $this->getFieldTypeUnderTest();
398+
399+
$fieldValue = new FieldValue(['data' => $data]);
400+
$result = $fieldType->fromPersistenceValue($fieldValue);
401+
402+
self::assertInstanceOf(ImageValue::class, $result);
403+
self::assertSame($expectedWidth, $result->width);
404+
self::assertSame($expectedHeight, $result->height);
405+
}
406+
350407
protected function provideFieldTypeIdentifier(): string
351408
{
352409
return 'ibexa_image';

tests/lib/Persistence/Legacy/FieldValue/Converter/ImageConverterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,29 @@ public function xmlToFieldValueProvider(): array
223223
],
224224
]),
225225
],
226+
'with_empty_width_and_height' => [
227+
<<< XML
228+
<?xml version="1.0" encoding="utf-8"?>
229+
<ezimage serial_number="1" is_valid="1" filename="ibexa_fav.png"
230+
suffix="png" basename="ibexa_fav" dirpath="{$dir}" url="{$pathToImg}"
231+
original_filename="ibexa_fav.png" mime_type="image/png" width=""
232+
height="" alternative_text="test" alias_key="1293033771" timestamp="{timestampToReplace}">
233+
<original attribute_id="1" attribute_version="1" attribute_language="eng-GB"/>
234+
<information Height="" Width="" IsColor="1"/>
235+
</ezimage>
236+
XML,
237+
new FieldValue([
238+
'data' => [
239+
'width' => '',
240+
'height' => '',
241+
'alternativeText' => 'test',
242+
'mime' => 'image/png',
243+
'id' => 1,
244+
'fileName' => 'ibexa_fav.png',
245+
'additionalData' => [],
246+
],
247+
]),
248+
],
226249
];
227250
}
228251
}

0 commit comments

Comments
 (0)