Skip to content

Commit

Permalink
[Tests] Added integration test coverage for image criterions
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Oct 25, 2023
1 parent 4a876d1 commit 465fd19
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpunit-integration-legacy-solr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="Ibexa\Contracts\Core\Test\IbexaTestKernel"/>
<env name="SEARCH_ENGINE" value="solr"/>
</php>
<testsuites>
<!-- Search service is used all over the place, so we must run entire integration test suite -->
Expand Down
1 change: 1 addition & 0 deletions phpunit-integration-legacy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ini name="error_reporting" value="-1" />
<env name="DATABASE_URL" value="sqlite://:memory:" />
<env name="KERNEL_CLASS" value="Ibexa\Contracts\Core\Test\IbexaTestKernel"/>
<env name="SEARCH_ENGINE" value="legacy"/>
</php>
<testsuites>
<testsuite name="integration_core">
Expand Down
320 changes: 320 additions & 0 deletions tests/integration/Core/Repository/SearchServiceImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository;

use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Contracts\Core\Test\IbexaKernelTestCase;
use Ibexa\Core\FieldType\Image\Orientation;
use Ibexa\Core\FieldType\Image\Value as ImageValue;
use Ibexa\Core\FieldType\TextLine\Value as TextValue;

final class SearchServiceImageTest extends IbexaKernelTestCase
{
private const IMAGE_CONTENT_TYPE = 'image';
private const IMAGE_FIELD_DEF_IDENTIFIER = 'image';
private const IMAGE_FILES = [
'landscape.jpg',
'portrait.jpg',
'square.png',
];

private const IMAGE_FIXTURES_DIR_PATH = __DIR__ . '/_fixtures/image/';

protected function setUp(): void
{
parent::setUp();

self::loadSchema();
self::loadFixtures();

self::setAdministratorUser();
}

/**
* @dataProvider provideDataForTestCriterion
* @dataProvider provideInvalidDataForTestCriterion
*/
public function testCriterion(
int $expectedCount,
Query\Criterion $imageCriterion
): void {
if (getenv('SEARCH_ENGINE') === 'legacy') {
self::markTestSkipped('Image criteria are not supported in Legacy Search Engine');
}

$this->createImages();

$query = new Query();
$query->filter = new Query\Criterion\LogicalAnd(
[
new Query\Criterion\ContentTypeIdentifier(self::IMAGE_CONTENT_TYPE),
$imageCriterion,
]
);

$searchHits = self::getSearchService()->findContent($query);

self::assertSame(
$expectedCount,
$searchHits->totalCount
);
}

/**
* @return iterable<array{
* int,
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion
* }>
*/
public function provideDataForTestCriterion(): iterable
{
yield 'Dimensions' => [
3,
new Query\Criterion\Image\Dimensions(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
'width' => [
'min' => 0,
'max' => 100,
],
'height' => [
'min' => 0,
'max' => 100,
],
]
),
];

yield 'FileSize' => [
3,
new Query\Criterion\Image\FileSize(
self::IMAGE_FIELD_DEF_IDENTIFIER,
0,
1
),
];

yield 'Width' => [
3,
new Query\Criterion\Image\Width(
self::IMAGE_FIELD_DEF_IDENTIFIER,
0,
100
),
];

yield 'Height' => [
3,
new Query\Criterion\Image\Height(
self::IMAGE_FIELD_DEF_IDENTIFIER,
0,
100
),
];

yield 'MimeType - single' => [
2,
new Query\Criterion\Image\MimeType(
self::IMAGE_FIELD_DEF_IDENTIFIER,
'image/jpeg',
),
];

yield 'MimeType - multiple' => [
3,
new Query\Criterion\Image\MimeType(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
'image/jpeg',
'image/png',
],
),
];

yield 'Orientation - landscape' => [
1,
new Query\Criterion\Image\Orientation(
self::IMAGE_FIELD_DEF_IDENTIFIER,
Orientation::LANDSCAPE
),
];

yield 'Orientation - portrait' => [
1,
new Query\Criterion\Image\Orientation(
self::IMAGE_FIELD_DEF_IDENTIFIER,
Orientation::PORTRAIT
),
];

yield 'Orientation - square' => [
1,
new Query\Criterion\Image\Orientation(
self::IMAGE_FIELD_DEF_IDENTIFIER,
Orientation::SQUARE
),
];

yield 'Orientation - multiple' => [
3,
new Query\Criterion\Image\Orientation(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
Orientation::LANDSCAPE,
Orientation::PORTRAIT,
Orientation::SQUARE,
]
),
];

yield 'Image' => [
2,
new Query\Criterion\Image(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
'mimeTypes' => [
'image/jpeg',
'image/png',
],
'size' => [
'min' => 0,
'max' => 1,
],
'width' => [
'min' => 0,
'max' => 100,
],
'height' => [
'min' => 0,
'max' => 100,
],
'orientation' => [
Orientation::LANDSCAPE,
Orientation::PORTRAIT,
],
]
),
];
}

/**
* @return iterable<array{
* int,
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion
* }>
*/
public function provideInvalidDataForTestCriterion(): iterable
{
yield 'Dimensions - width and height values too large' => [
0,
new Query\Criterion\Image\Dimensions(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
'width' => [
'min' => 101,
'max' => 200,
],
'height' => [
'min' => 101,
'max' => 300,
],
]
),
];

yield 'FileSize - size value too large' => [
0,
new Query\Criterion\Image\FileSize(
self::IMAGE_FIELD_DEF_IDENTIFIER,
1,
2
),
];

yield 'Width - width value to large' => [
0,
new Query\Criterion\Image\Width(
self::IMAGE_FIELD_DEF_IDENTIFIER,
101,
200
),
];

yield 'Height - height value to large' => [
0,
new Query\Criterion\Image\Height(
self::IMAGE_FIELD_DEF_IDENTIFIER,
101,
300
),
];

yield 'MimeType - invalid single mime type' => [
0,
new Query\Criterion\Image\MimeType(
self::IMAGE_FIELD_DEF_IDENTIFIER,
'image/invalid',
),
];

yield 'MimeType - invalid multiple mime types' => [
0,
new Query\Criterion\Image\MimeType(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
'image/invalid',
'image/gif',
],
),
];
}

private function createImages(): void
{
$contentType = $this->loadContentTypeImage();
foreach (self::IMAGE_FILES as $image) {
$this->createContentImage(
$contentType,
self::IMAGE_FIXTURES_DIR_PATH . $image,
$image
);
}
}

private function createContentImage(
ContentType $contentType,
string $path,
string $fileName
): void {
$contentCreateStruct = self::getContentService()->newContentCreateStruct(
$contentType,
'eng-GB'
);

$imageValue = new ImageValue();
$imageValue->fileName = $fileName;
$imageValue->path = $path;

$contentCreateStruct->setField('name', new TextValue('Image'), 'eng-GB');
$contentCreateStruct->setField('image', $imageValue, 'eng-GB');

$contentService = self::getContentService();
$contentService->publishVersion(
$contentService
->createContent($contentCreateStruct)
->getVersionInfo()
);
}

private function loadContentTypeImage(): ContentType
{
return self::getContentTypeService()->loadContentTypeByIdentifier(self::IMAGE_CONTENT_TYPE);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 465fd19

Please sign in to comment.