Skip to content

Commit 037f7d6

Browse files
authored
ENGCOM-5218: Return error when invalid currentPage is provided #731
2 parents 9d4885b + 0090efd commit 037f7d6

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/Products.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public function resolve(
6666
]
6767
];
6868
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args);
69+
if ($args['currentPage'] < 1) {
70+
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
71+
}
72+
if ($args['pageSize'] < 1) {
73+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
74+
}
75+
6976
$searchCriteria->setCurrentPage($args['currentPage']);
7077
$searchCriteria->setPageSize($args['pageSize']);
7178
$searchResult = $this->filterQuery->getResult($searchCriteria, $info);

app/code/Magento/CatalogGraphQl/Model/Resolver/Products.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public function resolve(
7171
array $args = null
7272
) {
7373
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args);
74+
if ($args['currentPage'] < 1) {
75+
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
76+
}
77+
if ($args['pageSize'] < 1) {
78+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
79+
}
80+
7481
$searchCriteria->setCurrentPage($args['currentPage']);
7582
$searchCriteria->setPageSize($args['pageSize']);
7683
if (!isset($args['search']) && !isset($args['filter'])) {

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,10 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
381381
}
382382
QUERY;
383383
$this->expectException(\Exception::class);
384-
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
385-
'than the 1 page(s) available');
384+
$this->expectExceptionMessage(
385+
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
386+
'than the 1 page(s) available'
387+
);
386388
$this->graphQlQuery($query);
387389
}
388390

@@ -1043,8 +1045,10 @@ public function testQueryPageOutOfBoundException()
10431045
QUERY;
10441046

10451047
$this->expectException(\Exception::class);
1046-
$this->expectExceptionMessage('GraphQL response contains errors: currentPage value 2 specified is greater ' .
1047-
'than the 1 page(s) available.');
1048+
$this->expectExceptionMessage(
1049+
'GraphQL response contains errors: currentPage value 2 specified is greater ' .
1050+
'than the 1 page(s) available.'
1051+
);
10481052
$this->graphQlQuery($query);
10491053
}
10501054

@@ -1075,8 +1079,10 @@ public function testQueryWithNoSearchOrFilterArgumentException()
10751079
QUERY;
10761080

10771081
$this->expectException(\Exception::class);
1078-
$this->expectExceptionMessage('GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
1079-
'required.');
1082+
$this->expectExceptionMessage(
1083+
'GraphQL response contains errors: \'search\' or \'filter\' input argument is ' .
1084+
'required.'
1085+
);
10801086
$this->graphQlQuery($query);
10811087
}
10821088

@@ -1131,6 +1137,66 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
11311137
$this->assertEquals(1, $response['products']['total_count']);
11321138
}
11331139

1140+
/**
1141+
* Verify that invalid page numbers return an error
1142+
*
1143+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1144+
* @expectedException \Exception
1145+
* @expectedExceptionMessage currentPage value must be greater than 0
1146+
*/
1147+
public function testInvalidPageNumbers()
1148+
{
1149+
$query = <<<QUERY
1150+
{
1151+
products (
1152+
filter: {
1153+
sku: {
1154+
like:"simple%"
1155+
}
1156+
}
1157+
pageSize: 4
1158+
currentPage: 0
1159+
) {
1160+
items {
1161+
sku
1162+
}
1163+
}
1164+
}
1165+
QUERY;
1166+
1167+
$this->graphQlQuery($query);
1168+
}
1169+
1170+
/**
1171+
* Verify that invalid page size returns an error
1172+
*
1173+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1174+
* @expectedException \Exception
1175+
* @expectedExceptionMessage pageSize value must be greater than 0
1176+
*/
1177+
public function testInvalidPageSize()
1178+
{
1179+
$query = <<<QUERY
1180+
{
1181+
products (
1182+
filter: {
1183+
sku: {
1184+
like:"simple%"
1185+
}
1186+
}
1187+
pageSize: 0
1188+
currentPage: 1
1189+
) {
1190+
items {
1191+
sku
1192+
}
1193+
}
1194+
}
1195+
QUERY;
1196+
1197+
$this->graphQlQuery($query);
1198+
}
1199+
11341200
/**
11351201
* Asserts the different fields of items returned after search query is executed
11361202
*
@@ -1140,7 +1206,7 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
11401206
private function assertProductItems(array $filteredProducts, array $actualResponse)
11411207
{
11421208
$productItemsInResponse = array_map(null, $actualResponse['products']['items'], $filteredProducts);
1143-
1209+
// phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
11441210
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
11451211
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
11461212
$this->assertResponseFields(

0 commit comments

Comments
 (0)