|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Integration\Core\Repository; |
| 10 | + |
| 11 | +use Ibexa\Contracts\Core\Repository\Values\Content\Query; |
| 12 | +use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType; |
| 13 | +use Ibexa\Core\FieldType\Image\Orientation; |
| 14 | +use Ibexa\Core\FieldType\Image\Value as ImageValue; |
| 15 | +use Ibexa\Core\FieldType\TextLine\Value as TextValue; |
| 16 | +use Ibexa\Tests\Integration\Core\RepositorySearchTestCase; |
| 17 | + |
| 18 | +final class SearchServiceImageTest extends RepositorySearchTestCase |
| 19 | +{ |
| 20 | + private const IMAGE_CONTENT_TYPE = 'image'; |
| 21 | + private const IMAGE_FIELD_DEF_IDENTIFIER = 'image'; |
| 22 | + private const IMAGE_FILES = [ |
| 23 | + 'landscape.jpg', |
| 24 | + 'portrait.jpg', |
| 25 | + 'square.png', |
| 26 | + ]; |
| 27 | + |
| 28 | + private const IMAGE_FIXTURES_DIR_PATH = __DIR__ . '/_fixtures/image/'; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->createImages(); |
| 35 | + |
| 36 | + $this->refreshSearch(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider provideDataForTestCriterion |
| 41 | + * @dataProvider provideInvalidDataForTestCriterion |
| 42 | + */ |
| 43 | + public function testCriterion( |
| 44 | + int $expectedCount, |
| 45 | + Query\Criterion $imageCriterion |
| 46 | + ): void { |
| 47 | + if (getenv('SEARCH_ENGINE') === 'legacy') { |
| 48 | + self::markTestSkipped('Image criteria are not supported in Legacy Search Engine'); |
| 49 | + } |
| 50 | + |
| 51 | + $query = new Query(); |
| 52 | + $query->filter = new Query\Criterion\LogicalAnd( |
| 53 | + [ |
| 54 | + new Query\Criterion\ContentTypeIdentifier(self::IMAGE_CONTENT_TYPE), |
| 55 | + $imageCriterion, |
| 56 | + ] |
| 57 | + ); |
| 58 | + |
| 59 | + $searchHits = self::getSearchService()->findContent($query); |
| 60 | + |
| 61 | + self::assertSame( |
| 62 | + $expectedCount, |
| 63 | + $searchHits->totalCount |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return iterable<array{ |
| 69 | + * int, |
| 70 | + * \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion |
| 71 | + * }> |
| 72 | + */ |
| 73 | + public function provideDataForTestCriterion(): iterable |
| 74 | + { |
| 75 | + yield 'Dimensions' => [ |
| 76 | + 3, |
| 77 | + $this->createDimensionsCriterion( |
| 78 | + 0, |
| 79 | + 100, |
| 80 | + 0, |
| 81 | + 100 |
| 82 | + ), |
| 83 | + ]; |
| 84 | + |
| 85 | + yield 'FileSize - default values min 0 and max 1' => [ |
| 86 | + 3, |
| 87 | + $this->createFileSizeCriterion(), |
| 88 | + ]; |
| 89 | + |
| 90 | + yield 'FileSize' => [ |
| 91 | + 3, |
| 92 | + $this->createFileSizeCriterion(0, 2), |
| 93 | + ]; |
| 94 | + |
| 95 | + yield 'Width' => [ |
| 96 | + 3, |
| 97 | + $this->createWidthCriterion(0, 100), |
| 98 | + ]; |
| 99 | + |
| 100 | + yield 'Height' => [ |
| 101 | + 3, |
| 102 | + $this->createHeightCriterion(0, 100), |
| 103 | + ]; |
| 104 | + |
| 105 | + yield 'MimeType - single' => [ |
| 106 | + 2, |
| 107 | + $this->createMimeTypeCriterion('image/jpeg'), |
| 108 | + ]; |
| 109 | + |
| 110 | + yield 'MimeType - multiple' => [ |
| 111 | + 3, |
| 112 | + $this->createMimeTypeCriterion( |
| 113 | + [ |
| 114 | + 'image/jpeg', |
| 115 | + 'image/png', |
| 116 | + ], |
| 117 | + ), |
| 118 | + ]; |
| 119 | + |
| 120 | + yield 'Orientation - landscape' => [ |
| 121 | + 1, |
| 122 | + $this->createOrientationCriterion(Orientation::LANDSCAPE), |
| 123 | + ]; |
| 124 | + |
| 125 | + yield 'Orientation - portrait' => [ |
| 126 | + 1, |
| 127 | + $this->createOrientationCriterion(Orientation::PORTRAIT), |
| 128 | + ]; |
| 129 | + |
| 130 | + yield 'Orientation - square' => [ |
| 131 | + 1, |
| 132 | + $this->createOrientationCriterion(Orientation::SQUARE), |
| 133 | + ]; |
| 134 | + |
| 135 | + yield 'Orientation - multiple' => [ |
| 136 | + 3, |
| 137 | + $this->createOrientationCriterion( |
| 138 | + [ |
| 139 | + Orientation::LANDSCAPE, |
| 140 | + Orientation::PORTRAIT, |
| 141 | + Orientation::SQUARE, |
| 142 | + ] |
| 143 | + ), |
| 144 | + ]; |
| 145 | + |
| 146 | + yield 'Image' => [ |
| 147 | + 2, |
| 148 | + new Query\Criterion\Image( |
| 149 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 150 | + [ |
| 151 | + 'mimeTypes' => [ |
| 152 | + 'image/jpeg', |
| 153 | + 'image/png', |
| 154 | + ], |
| 155 | + 'size' => [ |
| 156 | + 'min' => 0, |
| 157 | + 'max' => 1, |
| 158 | + ], |
| 159 | + 'width' => [ |
| 160 | + 'min' => 0, |
| 161 | + 'max' => 100, |
| 162 | + ], |
| 163 | + 'height' => [ |
| 164 | + 'min' => 0, |
| 165 | + 'max' => 100, |
| 166 | + ], |
| 167 | + 'orientation' => [ |
| 168 | + Orientation::LANDSCAPE, |
| 169 | + Orientation::PORTRAIT, |
| 170 | + ], |
| 171 | + ] |
| 172 | + ), |
| 173 | + ]; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return iterable<array{ |
| 178 | + * int, |
| 179 | + * \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion |
| 180 | + * }> |
| 181 | + */ |
| 182 | + public function provideInvalidDataForTestCriterion(): iterable |
| 183 | + { |
| 184 | + yield 'Dimensions - width and height values too large' => [ |
| 185 | + 0, |
| 186 | + $this->createDimensionsCriterion( |
| 187 | + 101, |
| 188 | + 200, |
| 189 | + 101, |
| 190 | + 300 |
| 191 | + ), |
| 192 | + ]; |
| 193 | + |
| 194 | + yield 'FileSize - size value too large' => [ |
| 195 | + 0, |
| 196 | + $this->createFileSizeCriterion( |
| 197 | + 1, |
| 198 | + 2 |
| 199 | + ), |
| 200 | + ]; |
| 201 | + |
| 202 | + yield 'Width - width value to large' => [ |
| 203 | + 0, |
| 204 | + $this->createWidthCriterion(101, 200), |
| 205 | + ]; |
| 206 | + |
| 207 | + yield 'Height - height value to large' => [ |
| 208 | + 0, |
| 209 | + $this->createHeightCriterion(101, 300), |
| 210 | + ]; |
| 211 | + |
| 212 | + yield 'MimeType - invalid single mime type' => [ |
| 213 | + 0, |
| 214 | + $this->createMimeTypeCriterion('image/invalid'), |
| 215 | + ]; |
| 216 | + |
| 217 | + yield 'MimeType - invalid multiple mime types' => [ |
| 218 | + 0, |
| 219 | + $this->createMimeTypeCriterion( |
| 220 | + [ |
| 221 | + 'image/invalid', |
| 222 | + 'image/gif', |
| 223 | + ] |
| 224 | + ), |
| 225 | + ]; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @param string|array<string> $value |
| 230 | + */ |
| 231 | + private function createMimeTypeCriterion($value): Query\Criterion\Image\MimeType |
| 232 | + { |
| 233 | + return new Query\Criterion\Image\MimeType( |
| 234 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 235 | + $value |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + private function createFileSizeCriterion( |
| 240 | + int $min = 0, |
| 241 | + ?int $max = null |
| 242 | + ): Query\Criterion\Image\FileSize { |
| 243 | + return new Query\Criterion\Image\FileSize( |
| 244 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 245 | + $min, |
| 246 | + $max |
| 247 | + ); |
| 248 | + } |
| 249 | + |
| 250 | + private function createWidthCriterion( |
| 251 | + int $min = 0, |
| 252 | + ?int $max = null |
| 253 | + ): Query\Criterion\Image\Width { |
| 254 | + return new Query\Criterion\Image\Width( |
| 255 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 256 | + $min, |
| 257 | + $max |
| 258 | + ); |
| 259 | + } |
| 260 | + |
| 261 | + private function createHeightCriterion( |
| 262 | + int $min = 0, |
| 263 | + ?int $max = null |
| 264 | + ): Query\Criterion\Image\Height { |
| 265 | + return new Query\Criterion\Image\Height( |
| 266 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 267 | + $min, |
| 268 | + $max |
| 269 | + ); |
| 270 | + } |
| 271 | + |
| 272 | + private function createDimensionsCriterion( |
| 273 | + int $minWidth, |
| 274 | + int $maxWidth, |
| 275 | + int $minHeight, |
| 276 | + int $maxHeight |
| 277 | + ): Query\Criterion\Image\Dimensions { |
| 278 | + return new Query\Criterion\Image\Dimensions( |
| 279 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 280 | + [ |
| 281 | + 'width' => [ |
| 282 | + 'min' => $minWidth, |
| 283 | + 'max' => $maxWidth, |
| 284 | + ], |
| 285 | + 'height' => [ |
| 286 | + 'min' => $minHeight, |
| 287 | + 'max' => $maxHeight, |
| 288 | + ], |
| 289 | + ] |
| 290 | + ); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * @param string|array<string> $value |
| 295 | + */ |
| 296 | + private function createOrientationCriterion($value): Query\Criterion\Image\Orientation |
| 297 | + { |
| 298 | + return new Query\Criterion\Image\Orientation( |
| 299 | + self::IMAGE_FIELD_DEF_IDENTIFIER, |
| 300 | + $value |
| 301 | + ); |
| 302 | + } |
| 303 | + |
| 304 | + private function createImages(): void |
| 305 | + { |
| 306 | + $contentType = $this->loadContentTypeImage(); |
| 307 | + foreach (self::IMAGE_FILES as $image) { |
| 308 | + $this->createContentImage( |
| 309 | + $contentType, |
| 310 | + self::IMAGE_FIXTURES_DIR_PATH . $image, |
| 311 | + $image |
| 312 | + ); |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + private function createContentImage( |
| 317 | + ContentType $contentType, |
| 318 | + string $path, |
| 319 | + string $fileName |
| 320 | + ): void { |
| 321 | + $contentCreateStruct = self::getContentService()->newContentCreateStruct( |
| 322 | + $contentType, |
| 323 | + 'eng-GB' |
| 324 | + ); |
| 325 | + |
| 326 | + $imageValue = new ImageValue(); |
| 327 | + $imageValue->fileName = $fileName; |
| 328 | + $imageValue->path = $path; |
| 329 | + |
| 330 | + $contentCreateStruct->setField('name', new TextValue('Image'), 'eng-GB'); |
| 331 | + $contentCreateStruct->setField('image', $imageValue, 'eng-GB'); |
| 332 | + |
| 333 | + $contentService = self::getContentService(); |
| 334 | + $contentService->publishVersion( |
| 335 | + $contentService |
| 336 | + ->createContent($contentCreateStruct) |
| 337 | + ->getVersionInfo() |
| 338 | + ); |
| 339 | + } |
| 340 | + |
| 341 | + private function loadContentTypeImage(): ContentType |
| 342 | + { |
| 343 | + return self::getContentTypeService()->loadContentTypeByIdentifier(self::IMAGE_CONTENT_TYPE); |
| 344 | + } |
| 345 | +} |
0 commit comments