|
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 function iterator_to_array; |
| 16 | +use Ibexa\Contracts\Core\Repository\LocationService; |
13 | 17 | use Ibexa\Contracts\Core\Repository\Values\Content\Content; |
14 | 18 | use Ibexa\Contracts\Core\Repository\Values\Content\ContentList; |
| 19 | +use Ibexa\Contracts\Core\Repository\Values\Content\Location; |
15 | 20 | use Ibexa\Contracts\Core\Repository\Values\Content\Query; |
16 | 21 | use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; |
17 | 22 | use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; |
@@ -81,6 +86,154 @@ 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 = array_values( |
| 118 | + array_filter( |
| 119 | + iterator_to_array($locationService->loadLocations($contentWithAdditionalLocation->contentInfo)), |
| 120 | + static function ($location) use ($contentWithAdditionalLocation) { |
| 121 | + return $location->id !== $contentWithAdditionalLocation->contentInfo->mainLocationId; |
| 122 | + } |
| 123 | + ) |
| 124 | + ); |
| 125 | + self::assertNotEmpty($nonMainLocations); |
| 126 | + $nonMainLocation = $nonMainLocations[0]; |
| 127 | + $referenceLocation = $this->loadMainLocation($locationService, $referenceContent); |
| 128 | + |
| 129 | + self::assertLessThan($referenceLocation->depth, $nonMainLocation->depth); |
| 130 | + self::assertLessThan($mainLocation->depth, $referenceLocation->depth); |
| 131 | + |
| 132 | + $filter = (new Filter()) |
| 133 | + ->withCriterion( |
| 134 | + new Criterion\ContentId( |
| 135 | + [ |
| 136 | + $contentWithAdditionalLocation->id, |
| 137 | + $referenceContent->id, |
| 138 | + ] |
| 139 | + ) |
| 140 | + ) |
| 141 | + ->withSortClause(new SortClause\Location\Depth(Query::SORT_ASC)); |
| 142 | + |
| 143 | + $contentList = $contentService->find($filter); |
| 144 | + |
| 145 | + self::assertSame( |
| 146 | + [$referenceContent->id, $contentWithAdditionalLocation->id], |
| 147 | + array_map( |
| 148 | + static function (Content $content): int { |
| 149 | + return $content->id; |
| 150 | + }, |
| 151 | + iterator_to_array($contentList) |
| 152 | + ) |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + public function testLocationSortClausesStayDeterministicWithComplexCriteria(): void |
| 157 | + { |
| 158 | + $repository = $this->getRepository(false); |
| 159 | + $locationService = $repository->getLocationService(); |
| 160 | + $contentService = $repository->getContentService(); |
| 161 | + |
| 162 | + $shallowParent = $this->createFolder( |
| 163 | + ['eng-GB' => 'Complex Root'], |
| 164 | + 2 |
| 165 | + ); |
| 166 | + $referenceContent = $this->createFolder( |
| 167 | + ['eng-GB' => 'Ref folder'], |
| 168 | + $shallowParent->contentInfo->mainLocationId |
| 169 | + ); |
| 170 | + $middleContent = $this->createFolder( |
| 171 | + ['eng-GB' => 'Middle folder'], |
| 172 | + $referenceContent->contentInfo->mainLocationId |
| 173 | + ); |
| 174 | + $deepParent = $this->createFolder( |
| 175 | + ['eng-GB' => 'Deep intermediate'], |
| 176 | + $middleContent->contentInfo->mainLocationId |
| 177 | + ); |
| 178 | + $contentWithAdditionalLocation = $this->createFolder( |
| 179 | + ['eng-GB' => 'Folder with randomizing location'], |
| 180 | + $deepParent->contentInfo->mainLocationId |
| 181 | + ); |
| 182 | + $locationService->createLocation( |
| 183 | + $contentWithAdditionalLocation->contentInfo, |
| 184 | + $locationService->newLocationCreateStruct(2) |
| 185 | + ); |
| 186 | + |
| 187 | + $referenceLocation = $this->loadMainLocation($locationService, $referenceContent); |
| 188 | + $middleLocation = $this->loadMainLocation($locationService, $middleContent); |
| 189 | + $mainLocation = $this->loadMainLocation($locationService, $contentWithAdditionalLocation); |
| 190 | + $nonMainLocations = array_values( |
| 191 | + array_filter( |
| 192 | + iterator_to_array($locationService->loadLocations($contentWithAdditionalLocation->contentInfo)), |
| 193 | + static function ($location) use ($contentWithAdditionalLocation) { |
| 194 | + return $location->id !== $contentWithAdditionalLocation->contentInfo->mainLocationId; |
| 195 | + } |
| 196 | + ) |
| 197 | + ); |
| 198 | + self::assertNotEmpty($nonMainLocations); |
| 199 | + $nonMainLocation = $nonMainLocations[0]; |
| 200 | + self::assertNotEquals($mainLocation->depth, $nonMainLocation->depth); |
| 201 | + |
| 202 | + $shallowParentLocation = $this->loadMainLocation($locationService, $shallowParent); |
| 203 | + |
| 204 | + $filter = (new Filter()) |
| 205 | + ->withCriterion(new Criterion\Subtree($shallowParentLocation->pathString)) |
| 206 | + ->andWithCriterion(new Criterion\ContentTypeIdentifier('folder')) |
| 207 | + ->andWithCriterion( |
| 208 | + new Criterion\ContentId( |
| 209 | + [ |
| 210 | + $referenceContent->id, |
| 211 | + $middleContent->id, |
| 212 | + $contentWithAdditionalLocation->id, |
| 213 | + ] |
| 214 | + ) |
| 215 | + ) |
| 216 | + ->withSortClause(new SortClause\Location\Depth(Query::SORT_ASC)) |
| 217 | + ->withSortClause(new SortClause\ContentId(Query::SORT_ASC)) |
| 218 | + ->withLimit(10); |
| 219 | + |
| 220 | + $contentList = $contentService->find($filter); |
| 221 | + |
| 222 | + self::assertSame( |
| 223 | + [ |
| 224 | + $referenceContent->id, |
| 225 | + $middleContent->id, |
| 226 | + $contentWithAdditionalLocation->id, |
| 227 | + ], |
| 228 | + array_map( |
| 229 | + static function (Content $content): int { |
| 230 | + return $content->id; |
| 231 | + }, |
| 232 | + iterator_to_array($contentList) |
| 233 | + ) |
| 234 | + ); |
| 235 | + } |
| 236 | + |
84 | 237 | /** |
85 | 238 | * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
86 | 239 | * @throws \Exception |
@@ -447,6 +600,14 @@ private function buildSearchQueryFromFilter(Filter $filter): Query |
447 | 600 | ] |
448 | 601 | ); |
449 | 602 | } |
| 603 | + |
| 604 | + private function loadMainLocation(LocationService $locationService, Content $content): Location |
| 605 | + { |
| 606 | + $mainLocationId = $content->contentInfo->mainLocationId; |
| 607 | + self::assertNotNull($mainLocationId); |
| 608 | + |
| 609 | + return $locationService->loadLocation($mainLocationId); |
| 610 | + } |
450 | 611 | } |
451 | 612 |
|
452 | 613 | class_alias(ContentFilteringTest::class, 'eZ\Publish\API\Repository\Tests\Filtering\ContentFilteringTest'); |
0 commit comments