Skip to content

Commit 8c1829d

Browse files
committed
GraphQl-727: currentPage: 0, currentPage: 1 and currentPage: -1 produces the same output for products query when filtering is used
- added pageSize validation; - fixed code style issues.
1 parent f361029 commit 8c1829d

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function resolve(
6969
if ($args['currentPage'] < 1) {
7070
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
7171
}
72+
if ($args['pageSize'] < 1) {
73+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
74+
}
7275

7376
$searchCriteria->setCurrentPage($args['currentPage']);
7477
$searchCriteria->setPageSize($args['pageSize']);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public function resolve(
7474
if ($args['currentPage'] < 1) {
7575
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
7676
}
77+
if ($args['pageSize'] < 1) {
78+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
79+
}
7780

7881
$searchCriteria->setCurrentPage($args['currentPage']);
7982
$searchCriteria->setPageSize($args['pageSize']);

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

Lines changed: 44 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

@@ -1162,6 +1168,37 @@ public function testInvalidPageNumbers()
11621168
$this->graphQlQuery($query);
11631169
}
11641170

1171+
/**
1172+
* Verify that invalid page size returns an error
1173+
*
1174+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1175+
* @expectedException \Exception
1176+
* @expectedExceptionMessage pageSize value must be greater than 0
1177+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1178+
*/
1179+
public function testInvalidPageSize()
1180+
{
1181+
$query = <<<QUERY
1182+
{
1183+
products (
1184+
filter: {
1185+
sku: {
1186+
like:"simple%"
1187+
}
1188+
}
1189+
pageSize: 0
1190+
currentPage: 1
1191+
) {
1192+
items {
1193+
sku
1194+
}
1195+
}
1196+
}
1197+
QUERY;
1198+
1199+
$this->graphQlQuery($query);
1200+
}
1201+
11651202
/**
11661203
* Asserts the different fields of items returned after search query is executed
11671204
*
@@ -1171,7 +1208,7 @@ public function testInvalidPageNumbers()
11711208
private function assertProductItems(array $filteredProducts, array $actualResponse)
11721209
{
11731210
$productItemsInResponse = array_map(null, $actualResponse['products']['items'], $filteredProducts);
1174-
1211+
// phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
11751212
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
11761213
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
11771214
$this->assertResponseFields(

0 commit comments

Comments
 (0)