Skip to content

Commit

Permalink
Fixed EZP-31630: error when running location query
Browse files Browse the repository at this point in the history
Resolved by testing the type of the returned Query object, and executing the applicable SearchService method.
  • Loading branch information
Bertrand Dunogier committed May 16, 2020
1 parent a9ff4fd commit 3079d62
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -34,5 +34,8 @@
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"scripts": {
"run-specs": "@php vendor/bin/phpspec run"
}
}
14 changes: 13 additions & 1 deletion spec/API/QueryFieldServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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
*/
Expand Down
45 changes: 32 additions & 13 deletions src/API/QueryFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,20 +60,15 @@ 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
{
$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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit 3079d62

Please sign in to comment.