Skip to content

Commit

Permalink
EZP-31676: keep the offset defined in the query (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier authored Jun 16, 2020
1 parent fa0240c commit 373a767
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
27 changes: 27 additions & 0 deletions spec/API/QueryFieldServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace spec\EzSystems\EzPlatformQueryFieldType\API;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Query as ApiContentQuery;
use EzSystems\EzPlatformQueryFieldType\API\QueryFieldService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\SearchService;
Expand Down Expand Up @@ -81,6 +82,32 @@ 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_deducts_any_offset_when_counting_results(QueryType $queryType, SearchService $searchService)
{
$query = new ApiContentQuery();
$query->offset = 5;

$searchResult = new SearchResult(['searchHits' => [], 'totalCount' => 7]);

$searchService->findContent($query)->willReturn($searchResult);
$queryType->getQuery(Argument::any())->willReturn($query);

$this->countContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER)->shouldBe(2);
}

function it_returns_zero_if_offset_is_bigger_than_count(QueryType $queryType, SearchService $searchService)
{
$query = new ApiContentQuery();
$query->offset = 8;

$searchResult = new SearchResult(['searchHits' => [], 'totalCount' => 5]);

$searchService->findContent($query)->willReturn($searchResult);
$queryType->getQuery(Argument::any())->willReturn($query);

$this->countContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER)->shouldBe(0);
}

/**
* @return \eZ\Publish\Core\Repository\Values\Content\Content
*/
Expand Down
6 changes: 4 additions & 2 deletions src/API/QueryFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ public function countContentItems(Content $content, string $fieldDefinitionIdent
$query = $this->prepareQuery($content, $fieldDefinitionIdentifier);
$query->limit = 0;

return $this->searchService->findContent($query)->totalCount;
$count = $this->searchService->findContent($query)->totalCount - $query->offset;

return $count < 0 ? 0 : $count;
}

public function loadContentItemsSlice(Content $content, string $fieldDefinitionIdentifier, int $offset, int $limit): iterable
{
$query = $this->prepareQuery($content, $fieldDefinitionIdentifier);
$query->offset = $offset;
$query->offset += $offset;
$query->limit = $limit;

return array_map(
Expand Down

0 comments on commit 373a767

Please # to comment.