Skip to content

Commit e40bc76

Browse files
committed
product graphql query returns incorrect category data in aggregation
1 parent 2e45de7 commit e40bc76

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/LayeredNavigation/Builder/Category.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Api\Search\AggregationValueInterface;
1818
use Magento\Framework\Api\Search\BucketInterface;
1919
use Magento\Framework\App\ResourceConnection;
20+
use Magento\Framework\GraphQl\Query\Uid;
2021

2122
/**
2223
* Category layer builder
@@ -73,7 +74,10 @@ class Category implements LayerBuilderInterface
7374
/**
7475
* @var Aggregations\Category\IncludeDirectChildrenOnly
7576
*/
76-
private Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly;
77+
private Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly;
78+
79+
/** @var Uid */
80+
private Uid $uidEncoder;
7781

7882
/**
7983
* @param CategoryAttributeQuery $categoryAttributeQuery
@@ -91,7 +95,8 @@ public function __construct(
9195
ResourceConnection $resourceConnection,
9296
LayerFormatter $layerFormatter,
9397
Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly,
94-
CollectionFactory $categoryCollectionFactory
98+
CollectionFactory $categoryCollectionFactory,
99+
Uid $uidEncoder
95100
) {
96101
$this->categoryAttributeQuery = $categoryAttributeQuery;
97102
$this->attributesMapper = $attributesMapper;
@@ -100,6 +105,7 @@ public function __construct(
100105
$this->layerFormatter = $layerFormatter;
101106
$this->includeDirectChildrenOnly = $includeDirectChildrenOnly;
102107
$this->categoryCollectionFactory = $categoryCollectionFactory;
108+
$this->uidEncoder = $uidEncoder;
103109
}
104110

105111
/**
@@ -155,7 +161,7 @@ function (AggregationValueInterface $value) {
155161
}
156162
$result['options'][] = $this->layerFormatter->buildItem(
157163
$categoryLabels[$categoryId] ?? $categoryId,
158-
$categoryId,
164+
$this->uidEncoder->encode((string) $categoryId),
159165
$value->getMetrics()['count']
160166
);
161167
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,30 @@
77

88
namespace Magento\GraphQl\Catalog;
99

10+
use Magento\Framework\GraphQl\Query\Uid;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\ObjectManager;
1013
use Magento\TestFramework\TestCase\GraphQlAbstract;
1114

1215
/**
1316
* Test of search by category ID aggregation.
1417
*/
1518
class ProductSearchCategoryAggregationsTest extends GraphQlAbstract
1619
{
20+
/** @var ObjectManager */
21+
private $objectManager;
22+
23+
/** @var Uid */
24+
private $uid;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function setUp(): void
30+
{
31+
$this->objectManager = Bootstrap::getObjectManager();
32+
$this->uid = $this->objectManager->get(Uid::class);
33+
}
1734
/**
1835
* Test category_id aggregation on filter by "eq" category ID condition.
1936
*
@@ -84,6 +101,7 @@ private function aggregationCategoryTesting(string $filterValue, string $include
84101
{
85102
$query = $this->getGraphQlQuery($filterValue, $includeDirectChildrenOnly);
86103
$result = $this->graphQlQuery($query);
104+
87105
$this->assertArrayNotHasKey('errors', $result);
88106
$this->assertArrayHasKey('aggregations', $result['products']);
89107
$categoryAggregation = array_filter(
@@ -99,7 +117,7 @@ function ($a) {
99117
foreach ($categoryAggregation['options'] as $option) {
100118
$this->assertNotEmpty($option['value']);
101119
$this->assertNotEmpty($option['label']);
102-
$categoryAggregationIdsLabel[(int)$option['value']] = $option['label'];
120+
$categoryAggregationIdsLabel[$this->uid->decode($option['value'])] = $option['label'];
103121
}
104122
return $categoryAggregationIdsLabel;
105123
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogGraphQl/CategoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testCheckCategoryUidAsAggregation(): void
2424
$query = $this->getSearchQueryWithSCategoryUID();
2525
$response = $this->graphQlQuery($query);
2626
$this->assertNotEmpty($response['products']);
27-
$this->assertEquals(1, count($response['products']['aggregations']));
27+
$this->assertEquals(2, count($response['products']['aggregations']));
2828
$this->assertNotEmpty($response['products']['aggregations']);
2929
$this->assertEquals('price', $response['products']['aggregations'][0]['attribute_code']);
3030
}

0 commit comments

Comments
 (0)