Skip to content

Commit

Permalink
GraphQl-727: currentPage: 0, currentPage: 1 and currentPage: -1 produ…
Browse files Browse the repository at this point in the history
…ces the same output for products query when filtering is used

- added pageSize validation;
- fixed code style issues.
  • Loading branch information
lenaorobei committed Jun 7, 2019
1 parent f361029 commit 8c1829d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function resolve(
if ($args['currentPage'] < 1) {
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
}
if ($args['pageSize'] < 1) {
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
}

$searchCriteria->setCurrentPage($args['currentPage']);
$searchCriteria->setPageSize($args['pageSize']);
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public function resolve(
if ($args['currentPage'] < 1) {
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
}
if ($args['pageSize'] < 1) {
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
}

$searchCriteria->setCurrentPage($args['currentPage']);
$searchCriteria->setPageSize($args['pageSize']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,10 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
}
QUERY;
$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available');
$this->expectExceptionMessage(
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available'
);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -1043,8 +1045,10 @@ public function testQueryPageOutOfBoundException()
QUERY;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available.');
$this->expectExceptionMessage(
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
'than the 1 page(s) available.'
);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -1075,8 +1079,10 @@ public function testQueryWithNoSearchOrFilterArgumentException()
QUERY;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
'required.');
$this->expectExceptionMessage(
'GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
'required.'
);
$this->graphQlQuery($query);
}

Expand Down Expand Up @@ -1162,6 +1168,37 @@ public function testInvalidPageNumbers()
$this->graphQlQuery($query);
}

/**
* Verify that invalid page size returns an error
*
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
* @expectedException \Exception
* @expectedExceptionMessage pageSize value must be greater than 0
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testInvalidPageSize()
{
$query = <<<QUERY
{
products (
filter: {
sku: {
like:"simple%"
}
}
pageSize: 0
currentPage: 1
) {
items {
sku
}
}
}
QUERY;

$this->graphQlQuery($query);
}

/**
* Asserts the different fields of items returned after search query is executed
*
Expand All @@ -1171,7 +1208,7 @@ public function testInvalidPageNumbers()
private function assertProductItems(array $filteredProducts, array $actualResponse)
{
$productItemsInResponse = array_map(null, $actualResponse['products']['items'], $filteredProducts);

// phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
$this->assertResponseFields(
Expand Down

0 comments on commit 8c1829d

Please sign in to comment.