Skip to content

Commit

Permalink
ENGCOM-5218: Return error when invalid currentPage is provided magent…
Browse files Browse the repository at this point in the history
…o#731

 - Merge Pull Request magento/graphql-ce#731 from pmclain/graphql-ce:issue/727
 - Merged commits:
   1. 064a4ef
   2. f361029
   3. 8c1829d
   4. 5d4520b
  • Loading branch information
magento-engcom-team committed Jun 10, 2019
2 parents b5b66fe + 5d4520b commit 0090efd
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public function resolve(
]
];
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args);
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']);
$searchResult = $this->filterQuery->getResult($searchCriteria, $info);
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public function resolve(
array $args = null
) {
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args);
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']);
if (!isset($args['search']) && !isset($args['filter'])) {
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 @@ -1131,6 +1137,66 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
$this->assertEquals(1, $response['products']['total_count']);
}

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

$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
*/
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 @@ -1140,7 +1206,7 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
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 0090efd

Please sign in to comment.