Skip to content

Commit

Permalink
[Tests] Adjusted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Dec 1, 2023
1 parent dc1ff99 commit 1017f64
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public function getTypeName()
*/
public function getSettingsSchema()
{
return [];
return [
'mimeTypes' => [
'type' => 'choice',
'default' => [],
],
];
}

/**
Expand All @@ -87,7 +92,12 @@ public function getSettingsSchema()
*/
public function getValidFieldSettings()
{
return [];
return [
'mimeTypes' => [
'image/jpeg',
'image/png',
],
];
}

/**
Expand Down Expand Up @@ -231,7 +241,7 @@ public function provideInvalidCreationFieldData()
/**
* Get update field externals data.
*
* @return array
* @return \Ibexa\Core\FieldType\Image\Value
*/
public function getValidUpdateFieldData()
{
Expand Down
54 changes: 49 additions & 5 deletions tests/lib/FieldType/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
*/
class ImageTest extends FieldTypeTest
{
private const MIME_TYPES = [
'image/png',
'image/jpeg',
'image/gif',
];

protected $blackListedExtensions = [
'php',
'php3',
Expand Down Expand Up @@ -61,10 +67,13 @@ protected function getMimeTypeDetectorMock()
*/
protected function createFieldTypeUnderTest()
{
$fieldType = new ImageType([
$this->getBlackListValidatorMock(),
$this->getImageValidatorMock(),
]);
$fieldType = new ImageType(
self::MIME_TYPES,
[
$this->getBlackListValidatorMock(),
$this->getImageValidatorMock(),
]
);
$fieldType->setTransformationProcessor($this->getTransformationProcessorMock());

return $fieldType;
Expand Down Expand Up @@ -132,7 +141,12 @@ protected function getValidatorConfigurationSchemaExpectation()
*/
protected function getSettingsSchemaExpectation()
{
return [];
return [
'mimeTypes' => [
'type' => 'choice',
'default' => [],
],
];
}

/**
Expand Down Expand Up @@ -856,6 +870,36 @@ public function provideInvalidDataForValidate()
),
],
],
'Image with not allowed mime type' => [
[
'fieldSettings' => [
'mimeTypes' => [
'image/png',
'image/gif',
],
],
],
new ImageValue(
[
'id' => $this->getImageInputPath(),
'fileName' => basename($this->getImageInputPath()),
'fileSize' => filesize($this->getImageInputPath()),
'alternativeText' => '',
'uri' => '',
]
),
[
new ValidationError(
'The mime type of the file is invalid (%mimeType%). Allowed mime types are %mimeTypes%.',
null,
[
'%mimeType%' => 'image/jpeg',
'%mimeTypes%' => 'image/png, image/gif',
],
'id'
),
],
],
];
}

Expand Down
43 changes: 39 additions & 4 deletions tests/lib/Persistence/FieldValue/Converter/ImageConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

final class ImageConverterTest extends TestCase
{
private const MIME_TYPES = [
'image/png',
'image/jpeg',
];

private const MIME_TYPES_STORAGE_VALUE = '["image\/png","image\/jpeg"]';

/** @var \Ibexa\Core\Persistence\Legacy\Content\FieldValue\Converter\ImageConverter */
private $imageConverter;

Expand Down Expand Up @@ -57,7 +64,7 @@ public function testToStorageFieldDefinition(

public function dataProviderForTestToStorageFieldDefinition(): iterable
{
yield [
yield 'No validators' => [
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
'validators' => [],
Expand All @@ -67,10 +74,11 @@ public function dataProviderForTestToStorageFieldDefinition(): iterable
'dataFloat1' => 0.0,
'dataInt2' => 0,
'dataText1' => 'MB',
'dataText5' => '[]',
]),
];

yield [
yield 'FileSizeValidator' => [
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
'validators' => [
Expand All @@ -84,10 +92,11 @@ public function dataProviderForTestToStorageFieldDefinition(): iterable
'dataFloat1' => 1.0,
'dataInt2' => 0,
'dataText1' => 'MB',
'dataText5' => '[]',
]),
];

yield [
yield 'AlternativeTextValidator - required' => [
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
'validators' => [
Expand All @@ -101,10 +110,11 @@ public function dataProviderForTestToStorageFieldDefinition(): iterable
'dataFloat1' => 0.0,
'dataInt2' => 1,
'dataText1' => 'MB',
'dataText5' => '[]',
]),
];

yield [
yield 'AlternativeTextValidator - not required' => [
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
'validators' => [
Expand All @@ -118,6 +128,23 @@ public function dataProviderForTestToStorageFieldDefinition(): iterable
'dataFloat1' => 0.0,
'dataInt2' => 0,
'dataText1' => 'MB',
'dataText5' => '[]',
]),
];

yield 'mimeTypes' => [
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
'fieldSettings' => [
'mimeTypes' => self::MIME_TYPES,
],
]),
]),
new StorageFieldDefinition([
'dataFloat1' => 0.0,
'dataInt2' => 0,
'dataText1' => 'MB',
'dataText5' => self::MIME_TYPES_STORAGE_VALUE,
]),
];
}
Expand Down Expand Up @@ -145,6 +172,7 @@ public function dataProviderForTestToFieldDefinition(): iterable
new StorageFieldDefinition([
'dataFloat1' => 0.0,
'dataInt2' => 0,
'dataText5' => [],
]),
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
Expand All @@ -156,6 +184,9 @@ public function dataProviderForTestToFieldDefinition(): iterable
'required' => false,
],
],
'fieldSettings' => [
'mimeTypes' => [],
],
]),
]),
];
Expand All @@ -164,6 +195,7 @@ public function dataProviderForTestToFieldDefinition(): iterable
new StorageFieldDefinition([
'dataFloat1' => 1.0,
'dataInt2' => 1,
'dataText5' => self::MIME_TYPES_STORAGE_VALUE,
]),
new FieldDefinition([
'fieldTypeConstraints' => new FieldTypeConstraints([
Expand All @@ -175,6 +207,9 @@ public function dataProviderForTestToFieldDefinition(): iterable
'required' => true,
],
],
'fieldSettings' => [
'mimeTypes' => self::MIME_TYPES,
],
]),
]),
];
Expand Down

0 comments on commit 1017f64

Please sign in to comment.