Skip to content

Commit d4e3a8b

Browse files
committed
ACP2E-3166: [Cloud] Graphql Product sorting do not work
1 parent eca4ae5 commit d4e3a8b

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

app/code/Magento/CatalogGraphQl/Plugin/ProductAttributeSortInput.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function beforeResolve(
8888
*/
8989
private function getDataFromRequest(RequestInterface $request): array
9090
{
91+
$data = [];
9192
if ($request->isPost()) {
9293
$data = $this->jsonSerializer->unserialize($request->getContent());
9394
} elseif ($request->isGet()) {
@@ -96,10 +97,7 @@ private function getDataFromRequest(RequestInterface $request): array
9697
$this->jsonSerializer->unserialize($data['variables']) : null;
9798
$data['variables'] = is_array($data['variables']) ?
9899
$data['variables'] : null;
99-
} else {
100-
return [];
101100
}
102-
103101
return $data;
104102
}
105103

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function setUp(): void
122122
*/
123123
public function testSortMultipleFieldsSentInVariables(): void
124124
{
125-
$query = <<<'QUERY'
125+
$queryAsc = <<<'QUERY'
126126
query GetProductsQuery(
127127
$search: String,
128128
$filter: ProductAttributeFilterInput,
@@ -164,7 +164,7 @@ public function testSortMultipleFieldsSentInVariables(): void
164164
'currentPage' => 1,
165165
'sort' => ['price' => 'ASC', 'name' => 'ASC']
166166
];
167-
$response = $this->graphQlQuery($query, $variables);
167+
$response = $this->graphQlQuery($queryAsc, $variables);
168168
$this->assertEquals(5, $response['products']['total_count']);
169169
$prod1 = $this->productRepository->get('search_product_1');
170170
$prod2 = $this->productRepository->get('search_product_2');
@@ -183,6 +183,64 @@ public function testSortMultipleFieldsSentInVariables(): void
183183
]
184184
);
185185
}
186+
187+
//price DESC and name ASC order
188+
$queryPriceDescNameAsc = <<<'QUERY'
189+
query GetProductsQuery(
190+
$search: String,
191+
$filter: ProductAttributeFilterInput,
192+
$pageSize: Int,
193+
$currentPage: Int,
194+
$sort: ProductAttributeSortInput
195+
) {
196+
products(
197+
search: $search,
198+
filter: $filter,
199+
pageSize: $pageSize,
200+
currentPage: $currentPage,
201+
sort: $sort
202+
) {
203+
total_count
204+
page_info{total_pages}
205+
items{
206+
__typename
207+
url_key
208+
sku
209+
name
210+
stock_status
211+
price_range {
212+
minimum_price {
213+
final_price {
214+
value
215+
currency
216+
}
217+
}
218+
}
219+
}
220+
}
221+
}
222+
QUERY;
223+
$variables = [
224+
'search' => null,
225+
'filter' => [],
226+
'pageSize' => 24,
227+
'currentPage' => 1,
228+
'sort' => ['price' => 'DESC', 'name' => 'ASC']
229+
];
230+
$response = $this->graphQlQuery($queryPriceDescNameAsc, $variables);
231+
$this->assertEquals(5, $response['products']['total_count']);
232+
233+
$filteredProducts = [$prod5, $prod4, $prod3, $prod1, $prod2];
234+
$productItemsInResponse = array_map(null, $response['products']['items'], $filteredProducts);
235+
foreach ($productItemsInResponse as $itemIndex => $itemArray) {
236+
$this->assertNotEmpty($itemArray);
237+
$this->assertResponseFields(
238+
$productItemsInResponse[$itemIndex][0],
239+
[
240+
'name' => $filteredProducts[$itemIndex]->getName(),
241+
]
242+
);
243+
}
186244
}
187245

188246
/**

0 commit comments

Comments
 (0)