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