From 3079d62a02e3b3a179ddc78dcc24306f45e13c62 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Fri, 15 May 2020 11:12:48 +0200 Subject: [PATCH] Fixed EZP-31630: error when running location query Resolved by testing the type of the returned Query object, and executing the applicable SearchService method. --- composer.json | 5 +++- spec/API/QueryFieldServiceSpec.php | 14 +++++++++- src/API/QueryFieldService.php | 45 +++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 9966aa8..f234c6a 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php": ">=7.1", "ext-json": "*", "ezsystems/ezplatform-graphql": "^1.0@dev", - "ezsystems/ezpublish-kernel": "^7.0||^8.0" + "ezsystems/ezpublish-kernel": "^7.0" }, "autoload": { "psr-4": { @@ -34,5 +34,8 @@ "branch-alias": { "dev-master": "1.0.x-dev" } + }, + "scripts": { + "run-specs": "@php vendor/bin/phpspec run" } } diff --git a/spec/API/QueryFieldServiceSpec.php b/spec/API/QueryFieldServiceSpec.php index 85d46f3..6a6d3d3 100644 --- a/spec/API/QueryFieldServiceSpec.php +++ b/spec/API/QueryFieldServiceSpec.php @@ -9,11 +9,11 @@ use eZ\Publish\API\Repository\SearchService; use eZ\Publish\API\Repository\Values\Content\ContentInfo; use eZ\Publish\API\Repository\Values\Content\Query as ApiQuery; +use eZ\Publish\API\Repository\Values\Content\LocationQuery as ApiLocationQuery; use eZ\Publish\API\Repository\Values\Content\Search\SearchResult; use eZ\Publish\Core\QueryType\QueryType; use eZ\Publish\Core\QueryType\QueryTypeRegistry; use eZ\Publish\Core\Repository\Values; -use EzSystems\EzPlatformGraphQL\GraphQL\Value\Field; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -61,6 +61,7 @@ function let( $queryType->getQuery(Argument::any())->willReturn(new ApiQuery()); // @todo this should fail. It does not. $searchService->findContent(Argument::any())->willReturn($this->searchResult); + $searchService->findLocations(Argument::any())->willReturn($this->searchResult); $this->beConstructedWith($searchService, $contentTypeService, $locationService, $queryTypeRegistry); } @@ -79,6 +80,17 @@ function it_counts_items_from_a_query_field_for_a_given_content_item() $this->countContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER)->shouldBe($this->totalCount); } + function it_counts_using_findLocations_if_the_Query_is_a_LocationQuery() + { + $this->loadContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER); + } + + function it_loads_item_using_findLocations_if_the_Query_is_a_LocationQuery(QueryType $queryType) + { + $queryType->getQuery(Argument::any())->willReturn(new ApiLocationQuery()); + $this->loadContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER); + } + /** * @return \eZ\Publish\Core\Repository\Values\Content\Content */ diff --git a/src/API/QueryFieldService.php b/src/API/QueryFieldService.php index 388e402..60a5a0b 100644 --- a/src/API/QueryFieldService.php +++ b/src/API/QueryFieldService.php @@ -10,6 +10,7 @@ use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\SearchService; use eZ\Publish\API\Repository\Values\Content\Content; +use eZ\Publish\API\Repository\Values\Content\LocationQuery; use eZ\Publish\API\Repository\Values\Content\Query; use eZ\Publish\API\Repository\Values\Content\Search\SearchHit; use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; @@ -59,12 +60,7 @@ public function loadContentItems(Content $content, string $fieldDefinitionIdenti { $query = $this->prepareQuery($content, $fieldDefinitionIdentifier); - return array_map( - function (SearchHit $searchHit) { - return $searchHit->valueObject; - }, - $this->searchService->findContent($query)->searchHits - ); + return $this->runQuery($query); } public function countContentItems(Content $content, string $fieldDefinitionIdentifier): int @@ -72,7 +68,7 @@ public function countContentItems(Content $content, string $fieldDefinitionIdent $query = $this->prepareQuery($content, $fieldDefinitionIdentifier); $query->limit = 0; - return $this->searchService->findContent($query)->totalCount; + return $this->runCountQuery($query); } public function loadContentItemsSlice(Content $content, string $fieldDefinitionIdentifier, int $offset, int $limit): iterable @@ -81,12 +77,7 @@ public function loadContentItemsSlice(Content $content, string $fieldDefinitionI $query->offset = $offset; $query->limit = $limit; - return array_map( - function (SearchHit $searchHit) { - return $searchHit->valueObject; - }, - $this->searchService->findContent($query)->searchHits - ); + return $this->runQuery($query); } public function getPaginationConfiguration(Content $content, string $fieldDefinitionIdentifier): int @@ -181,4 +172,32 @@ private function loadFieldDefinition(Content $content, string $fieldDefinitionId return $fieldDefinition; } + + private function runQuery(Query $query): iterable + { + if ($query instanceof LocationQuery) { + return array_map( + function (SearchHit $searchHit) { + return $searchHit->valueObject->getContent(); + }, + $this->searchService->findLocations($query)->searchHits + ); + } else { + return array_map( + function (SearchHit $searchHit) { + return $searchHit->valueObject; + }, + $this->searchService->findContent($query)->searchHits + ); + } + } + + private function runCountQuery(Query $query): int + { + if ($query instanceof LocationQuery) { + return $this->searchService->findLocations($query)->totalCount; + } else { + return $this->searchService->findContent($query)->totalCount; + } + } }