Skip to content

Commit 847ff88

Browse files
committed
Merge branch 'ACP2E-2809' of https://github.com/adobe-commerce-tier-4/magento2ce into T4-PR-03-18-2024
2 parents 6ad376c + b1ec29c commit 847ff88

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\CatalogGraphQl\Plugin;
20+
21+
use GraphQL\Language\AST\Node;
22+
use GraphQL\Language\AST\NodeKind;
23+
use GraphQL\Language\Visitor;
24+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
25+
use Magento\GraphQl\Model\Query\ContextInterface;
26+
27+
class ProductAttributeSortInput
28+
{
29+
/**
30+
* Plugin to preserve the original order of sort fields
31+
*
32+
* @param \Magento\Framework\GraphQl\Query\ResolverInterface $subject
33+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
34+
* @param ContextInterface $context
35+
* @param ResolveInfo $info
36+
* @param array|null $value
37+
* @param array|null $args
38+
* @return array
39+
* @throws \Exception
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function beforeResolve(
43+
\Magento\Framework\GraphQl\Query\ResolverInterface $subject,
44+
\Magento\Framework\GraphQl\Config\Element\Field $field,
45+
ContextInterface $context,
46+
ResolveInfo $info,
47+
array $value = null,
48+
array $args = null
49+
): array {
50+
if (isset($args['sort'])) {
51+
$args['sort'] = $this->getSortFieldsOrder($info, $args['sort']);
52+
}
53+
return [$field, $context, $info, $value, $args];
54+
}
55+
56+
/**
57+
* Get sort fields in the original order
58+
*
59+
* @param ResolveInfo $info
60+
* @param array $sortFields
61+
* @return array
62+
* @throws \Exception
63+
*/
64+
private function getSortFieldsOrder(ResolveInfo $info, array $sortFields)
65+
{
66+
$sortFieldsOriginal = [];
67+
Visitor::visit(
68+
$info->operation,
69+
[
70+
'enter' => [
71+
NodeKind::ARGUMENT => function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
72+
if ($node->name->value === 'sort') {
73+
Visitor::visit(
74+
$node->value,
75+
[
76+
'enter' => [
77+
NodeKind::OBJECT_FIELD =>
78+
function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
79+
if (isset($sortFields[$node->name->value])) {
80+
$sortFieldsOriginal[$node->name->value] =
81+
$sortFields[$node->name->value];
82+
}
83+
}
84+
]
85+
]
86+
);
87+
return Visitor::stop();
88+
}
89+
}
90+
]
91+
]
92+
);
93+
return $sortFieldsOriginal;
94+
}
95+
}

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,12 @@
283283
</argument>
284284
</arguments>
285285
</type>
286+
287+
<type name="Magento\CatalogGraphQl\Model\Resolver\Products">
288+
<plugin name="originalProductSortOrder" type="Magento\CatalogGraphQl\Plugin\ProductAttributeSortInput" />
289+
</type>
290+
291+
<type name="Magento\CatalogGraphQl\Model\Resolver\Category\Products">
292+
<plugin name="originalCategoryProductSortOrder" type="Magento\CatalogGraphQl\Plugin\ProductAttributeSortInput" />
293+
</type>
286294
</config>

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,54 @@ public function testFilterLn(): void
364364
);
365365
}
366366

367+
/**
368+
* Verify that products returned in a correct order
369+
*
370+
* @magentoApiDataFixture Magento/Catalog/_files/products_for_search.php
371+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
372+
*/
373+
public function testSortMultipleFields(): void
374+
{
375+
$query = <<<QUERY
376+
query products {
377+
products(currentPage: 1
378+
pageSize: 10
379+
filter: {
380+
category_id: {
381+
eq :"333"
382+
}
383+
}
384+
sort: { price: DESC, name: ASC}
385+
) {
386+
387+
items {
388+
name
389+
}
390+
total_count
391+
}
392+
}
393+
QUERY;
394+
$response = $this->graphQlQuery($query);
395+
$this->assertEquals(5, $response['products']['total_count']);
396+
$prod1 = $this->productRepository->get('search_product_5');
397+
$prod2 = $this->productRepository->get('search_product_4');
398+
$prod3 = $this->productRepository->get('search_product_3');
399+
$prod4 = $this->productRepository->get('search_product_1');
400+
$prod5 = $this->productRepository->get('search_product_2');
401+
402+
$filteredProducts = [$prod1, $prod2, $prod3, $prod4, $prod5];
403+
$productItemsInResponse = array_map(null, $response['products']['items'], $filteredProducts);
404+
foreach ($productItemsInResponse as $itemIndex => $itemArray) {
405+
$this->assertNotEmpty($itemArray);
406+
$this->assertResponseFields(
407+
$productItemsInResponse[$itemIndex][0],
408+
[
409+
'name' => $filteredProducts[$itemIndex]->getName(),
410+
]
411+
);
412+
}
413+
}
414+
367415
/**
368416
* Compare arrays by value in 'name' field.
369417
*
@@ -2158,9 +2206,9 @@ public function testProductBasicFullTextSearchQuery(): void
21582206
}
21592207
}
21602208
QUERY;
2161-
$prod1 = $this->productRepository->get('blue_briefs');
2209+
$prod1 = $this->productRepository->get('navy-striped-shoes');
21622210
$prod2 = $this->productRepository->get('grey_shorts');
2163-
$prod3 = $this->productRepository->get('navy-striped-shoes');
2211+
$prod3 = $this->productRepository->get('blue_briefs');
21642212
$response = $this->graphQlQuery($query);
21652213
$this->assertEquals(3, $response['products']['total_count']);
21662214

0 commit comments

Comments
 (0)