-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tests] Added integration test coverage for image criterions
- Loading branch information
Showing
7 changed files
with
373 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
345 changes: 345 additions & 0 deletions
345
tests/integration/Core/Repository/SearchServiceImageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |