|
8 | 8 |
|
9 | 9 | namespace Ibexa\Tests\Integration\Core\Repository\Filtering; |
10 | 10 |
|
| 11 | +use function array_filter; |
11 | 12 | use function array_map; |
| 13 | +use function array_values; |
12 | 14 | use function count; |
| 15 | +use Ibexa\Contracts\Core\Repository\LocationService; |
13 | 16 | use Ibexa\Contracts\Core\Repository\Values\Content\Content; |
14 | 17 | use Ibexa\Contracts\Core\Repository\Values\Content\ContentList; |
| 18 | +use Ibexa\Contracts\Core\Repository\Values\Content\Location; |
15 | 19 | use Ibexa\Contracts\Core\Repository\Values\Content\Query; |
16 | 20 | use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; |
17 | 21 | use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; |
|
22 | 26 | use Ibexa\Contracts\Core\Repository\Values\ObjectState\ObjectStateGroupCreateStruct; |
23 | 27 | use Ibexa\Core\FieldType\Keyword; |
24 | 28 | use Ibexa\Tests\Core\Repository\Filtering\TestContentProvider; |
| 29 | +use function iterator_to_array; |
25 | 30 | use IteratorAggregate; |
26 | 31 | use function sprintf; |
27 | 32 |
|
@@ -81,6 +86,150 @@ public function testFindWithLocationSortClauses(): void |
81 | 86 | ); |
82 | 87 | } |
83 | 88 |
|
| 89 | + public function testLocationSortClausesUseMainLocationDuringContentFiltering(): void |
| 90 | + { |
| 91 | + $repository = $this->getRepository(false); |
| 92 | + $locationService = $repository->getLocationService(); |
| 93 | + $contentService = $repository->getContentService(); |
| 94 | + |
| 95 | + $shallowParent = $this->createFolder( |
| 96 | + ['eng-GB' => 'Shallow Parent'], |
| 97 | + 2 |
| 98 | + ); |
| 99 | + $referenceContent = $this->createFolder( |
| 100 | + ['eng-GB' => 'Reference folder'], |
| 101 | + $shallowParent->contentInfo->mainLocationId |
| 102 | + ); |
| 103 | + $deepParent = $this->createFolder( |
| 104 | + ['eng-GB' => 'Deep Parent'], |
| 105 | + $referenceContent->contentInfo->mainLocationId |
| 106 | + ); |
| 107 | + $contentWithAdditionalLocation = $this->createFolder( |
| 108 | + ['eng-GB' => 'Folder with extra location'], |
| 109 | + $deepParent->contentInfo->mainLocationId |
| 110 | + ); |
| 111 | + $locationService->createLocation( |
| 112 | + $contentWithAdditionalLocation->contentInfo, |
| 113 | + $locationService->newLocationCreateStruct(2) |
| 114 | + ); |
| 115 | + |
| 116 | + $mainLocation = $this->loadMainLocation($locationService, $contentWithAdditionalLocation); |
| 117 | + $nonMainLocations = []; |
| 118 | + foreach ($locationService->loadLocations($contentWithAdditionalLocation->contentInfo) as $location) { |
| 119 | + if ($location->id !== $contentWithAdditionalLocation->contentInfo->mainLocationId) { |
| 120 | + $nonMainLocations[] = $location; |
| 121 | + } |
| 122 | + } |
| 123 | + self::assertNotEmpty($nonMainLocations); |
| 124 | + $nonMainLocation = $nonMainLocations[0]; |
| 125 | + $referenceLocation = $this->loadMainLocation($locationService, $referenceContent); |
| 126 | + |
| 127 | + self::assertLessThan($referenceLocation->depth, $nonMainLocation->depth); |
| 128 | + self::assertLessThan($mainLocation->depth, $referenceLocation->depth); |
| 129 | + |
| 130 | + $filter = (new Filter()) |
| 131 | + ->withCriterion( |
| 132 | + new Criterion\ContentId( |
| 133 | + [ |
| 134 | + $contentWithAdditionalLocation->id, |
| 135 | + $referenceContent->id, |
| 136 | + ] |
| 137 | + ) |
| 138 | + ) |
| 139 | + ->withSortClause(new SortClause\Location\Depth(Query::SORT_ASC)); |
| 140 | + |
| 141 | + $contentList = $contentService->find($filter); |
| 142 | + |
| 143 | + self::assertSame( |
| 144 | + [$referenceContent->id, $contentWithAdditionalLocation->id], |
| 145 | + array_map( |
| 146 | + static function (Content $content): int { |
| 147 | + return $content->id; |
| 148 | + }, |
| 149 | + iterator_to_array($contentList) |
| 150 | + ) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + public function testLocationSortClausesStayDeterministicWithComplexCriteria(): void |
| 155 | + { |
| 156 | + $repository = $this->getRepository(false); |
| 157 | + $locationService = $repository->getLocationService(); |
| 158 | + $contentService = $repository->getContentService(); |
| 159 | + |
| 160 | + $shallowParent = $this->createFolder( |
| 161 | + ['eng-GB' => 'Complex Root'], |
| 162 | + 2 |
| 163 | + ); |
| 164 | + $referenceContent = $this->createFolder( |
| 165 | + ['eng-GB' => 'Ref folder'], |
| 166 | + $shallowParent->contentInfo->mainLocationId |
| 167 | + ); |
| 168 | + $middleContent = $this->createFolder( |
| 169 | + ['eng-GB' => 'Middle folder'], |
| 170 | + $referenceContent->contentInfo->mainLocationId |
| 171 | + ); |
| 172 | + $deepParent = $this->createFolder( |
| 173 | + ['eng-GB' => 'Deep intermediate'], |
| 174 | + $middleContent->contentInfo->mainLocationId |
| 175 | + ); |
| 176 | + $contentWithAdditionalLocation = $this->createFolder( |
| 177 | + ['eng-GB' => 'Folder with randomizing location'], |
| 178 | + $deepParent->contentInfo->mainLocationId |
| 179 | + ); |
| 180 | + $locationService->createLocation( |
| 181 | + $contentWithAdditionalLocation->contentInfo, |
| 182 | + $locationService->newLocationCreateStruct(2) |
| 183 | + ); |
| 184 | + |
| 185 | + $referenceLocation = $this->loadMainLocation($locationService, $referenceContent); |
| 186 | + $middleLocation = $this->loadMainLocation($locationService, $middleContent); |
| 187 | + $mainLocation = $this->loadMainLocation($locationService, $contentWithAdditionalLocation); |
| 188 | + $nonMainLocations = []; |
| 189 | + foreach ($locationService->loadLocations($contentWithAdditionalLocation->contentInfo) as $location) { |
| 190 | + if ($location->id !== $contentWithAdditionalLocation->contentInfo->mainLocationId) { |
| 191 | + $nonMainLocations[] = $location; |
| 192 | + } |
| 193 | + } |
| 194 | + self::assertNotEmpty($nonMainLocations); |
| 195 | + $nonMainLocation = $nonMainLocations[0]; |
| 196 | + self::assertNotEquals($mainLocation->depth, $nonMainLocation->depth); |
| 197 | + |
| 198 | + $shallowParentLocation = $this->loadMainLocation($locationService, $shallowParent); |
| 199 | + |
| 200 | + $filter = (new Filter()) |
| 201 | + ->withCriterion(new Criterion\Subtree($shallowParentLocation->pathString)) |
| 202 | + ->andWithCriterion(new Criterion\ContentTypeIdentifier('folder')) |
| 203 | + ->andWithCriterion( |
| 204 | + new Criterion\ContentId( |
| 205 | + [ |
| 206 | + $referenceContent->id, |
| 207 | + $middleContent->id, |
| 208 | + $contentWithAdditionalLocation->id, |
| 209 | + ] |
| 210 | + ) |
| 211 | + ) |
| 212 | + ->withSortClause(new SortClause\Location\Depth(Query::SORT_ASC)) |
| 213 | + ->withSortClause(new SortClause\ContentId(Query::SORT_ASC)) |
| 214 | + ->withLimit(10); |
| 215 | + |
| 216 | + $contentList = $contentService->find($filter); |
| 217 | + |
| 218 | + self::assertSame( |
| 219 | + [ |
| 220 | + $referenceContent->id, |
| 221 | + $middleContent->id, |
| 222 | + $contentWithAdditionalLocation->id, |
| 223 | + ], |
| 224 | + array_map( |
| 225 | + static function (Content $content): int { |
| 226 | + return $content->id; |
| 227 | + }, |
| 228 | + iterator_to_array($contentList) |
| 229 | + ) |
| 230 | + ); |
| 231 | + } |
| 232 | + |
84 | 233 | /** |
85 | 234 | * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
86 | 235 | * @throws \Exception |
@@ -447,6 +596,14 @@ private function buildSearchQueryFromFilter(Filter $filter): Query |
447 | 596 | ] |
448 | 597 | ); |
449 | 598 | } |
| 599 | + |
| 600 | + private function loadMainLocation(LocationService $locationService, Content $content): Location |
| 601 | + { |
| 602 | + $mainLocationId = $content->contentInfo->mainLocationId; |
| 603 | + self::assertNotNull($mainLocationId); |
| 604 | + |
| 605 | + return $locationService->loadLocation($mainLocationId); |
| 606 | + } |
450 | 607 | } |
451 | 608 |
|
452 | 609 | class_alias(ContentFilteringTest::class, 'eZ\Publish\API\Repository\Tests\Filtering\ContentFilteringTest'); |
0 commit comments