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 Nov 6, 2023
1 parent db371d5 commit b32b5e9
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 1 deletion.
3 changes: 2 additions & 1 deletion phpunit-integration-legacy-solr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<env name="CORES_SETUP" value="dedicated" />
<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="KERNEL_CLASS" value="Ibexa\Tests\Integration\Solr\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
345 changes: 345 additions & 0 deletions tests/integration/Core/Repository/SearchServiceImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
<?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\Core\FieldType\Image\Orientation;
use Ibexa\Core\FieldType\Image\Value as ImageValue;
use Ibexa\Core\FieldType\TextLine\Value as TextValue;
use Ibexa\Tests\Integration\Core\RepositorySearchTestCase;

final class SearchServiceImageTest extends RepositorySearchTestCase
{
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();

$this->createImages();

$this->refreshSearch();
}

/**
* @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');
}

$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,
$this->createDimensionsCriterion(
0,
100,
0,
100
),
];

yield 'FileSize - default values min 0 and max 1' => [
3,
$this->createFileSizeCriterion(),
];

yield 'FileSize' => [
3,
$this->createFileSizeCriterion(0, 2),
];

yield 'Width' => [
3,
$this->createWidthCriterion(0, 100),
];

yield 'Height' => [
3,
$this->createHeightCriterion(0, 100),
];

yield 'MimeType - single' => [
2,
$this->createMimeTypeCriterion('image/jpeg'),
];

yield 'MimeType - multiple' => [
3,
$this->createMimeTypeCriterion(
[
'image/jpeg',
'image/png',
],
),
];

yield 'Orientation - landscape' => [
1,
$this->createOrientationCriterion(Orientation::LANDSCAPE),
];

yield 'Orientation - portrait' => [
1,
$this->createOrientationCriterion(Orientation::PORTRAIT),
];

yield 'Orientation - square' => [
1,
$this->createOrientationCriterion(Orientation::SQUARE),
];

yield 'Orientation - multiple' => [
3,
$this->createOrientationCriterion(
[
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,
$this->createDimensionsCriterion(
101,
200,
101,
300
),
];

yield 'FileSize - size value too large' => [
0,
$this->createFileSizeCriterion(
1,
2
),
];

yield 'Width - width value to large' => [
0,
$this->createWidthCriterion(101, 200),
];

yield 'Height - height value to large' => [
0,
$this->createHeightCriterion(101, 300),
];

yield 'MimeType - invalid single mime type' => [
0,
$this->createMimeTypeCriterion('image/invalid'),
];

yield 'MimeType - invalid multiple mime types' => [
0,
$this->createMimeTypeCriterion(
[
'image/invalid',
'image/gif',
]
),
];
}

/**
* @param string|array<string> $value
*/
private function createMimeTypeCriterion($value): Query\Criterion\Image\MimeType
{
return new Query\Criterion\Image\MimeType(
self::IMAGE_FIELD_DEF_IDENTIFIER,
$value
);
}

private function createFileSizeCriterion(
int $min = 0,
?int $max = null
): Query\Criterion\Image\FileSize {
return new Query\Criterion\Image\FileSize(
self::IMAGE_FIELD_DEF_IDENTIFIER,
$min,
$max
);
}

private function createWidthCriterion(
int $min = 0,
?int $max = null
): Query\Criterion\Image\Width {
return new Query\Criterion\Image\Width(
self::IMAGE_FIELD_DEF_IDENTIFIER,
$min,
$max
);
}

private function createHeightCriterion(
int $min = 0,
?int $max = null
): Query\Criterion\Image\Height {
return new Query\Criterion\Image\Height(
self::IMAGE_FIELD_DEF_IDENTIFIER,
$min,
$max
);
}

private function createDimensionsCriterion(
int $minWidth,
int $maxWidth,
int $minHeight,
int $maxHeight
): Query\Criterion\Image\Dimensions {
return new Query\Criterion\Image\Dimensions(
self::IMAGE_FIELD_DEF_IDENTIFIER,
[
'width' => [
'min' => $minWidth,
'max' => $maxWidth,
],
'height' => [
'min' => $minHeight,
'max' => $maxHeight,
],
]
);
}

/**
* @param string|array<string> $value
*/
private function createOrientationCriterion($value): Query\Criterion\Image\Orientation
{
return new Query\Criterion\Image\Orientation(
self::IMAGE_FIELD_DEF_IDENTIFIER,
$value
);
}

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.
25 changes: 25 additions & 0 deletions tests/integration/Core/RepositorySearchTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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;

use Ibexa\Solr\Handler as SolrHandler;

abstract class RepositorySearchTestCase extends RepositoryTestCase
{
protected function refreshSearch(): void
{
$handler = self::getContainer()->get('ibexa.spi.search');
if (
class_exists(SolrHandler::class)
&& $handler instanceof SolrHandler
) {
$handler->commit();
}
}
}

0 comments on commit b32b5e9

Please sign in to comment.