Skip to content

Commit 2e79ec2

Browse files
committed
Merge branch 'ACP2E-470' of https://github.com/magento-l3/magento2ce into L3-PR-20220202
2 parents 386f879 + d9c305f commit 2e79ec2

File tree

3 files changed

+161
-3
lines changed

3 files changed

+161
-3
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder;
99

10+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1011
use Magento\CatalogGraphQl\DataProvider\CategoryAttributesMapper;
1112
use Magento\CatalogGraphQl\DataProvider\Category\Query\CategoryAttributeQuery;
1213
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\LayerBuilderInterface;
@@ -19,7 +20,9 @@
1920
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations;
2021

2122
/**
22-
* @inheritdoc
23+
* Category layer builder
24+
*
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2326
*/
2427
class Category implements LayerBuilderInterface
2528
{
@@ -63,6 +66,11 @@ class Category implements LayerBuilderInterface
6366
*/
6467
private $layerFormatter;
6568

69+
/**
70+
* @var CollectionFactory
71+
*/
72+
private $categoryCollectionFactory;
73+
6674
/**
6775
* @var Aggregations\Category\IncludeDirectChildrenOnly
6876
*/
@@ -75,21 +83,24 @@ class Category implements LayerBuilderInterface
7583
* @param ResourceConnection $resourceConnection
7684
* @param LayerFormatter $layerFormatter
7785
* @param Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly
86+
* @param CollectionFactory $categoryCollectionFactory
7887
*/
7988
public function __construct(
8089
CategoryAttributeQuery $categoryAttributeQuery,
8190
CategoryAttributesMapper $attributesMapper,
8291
RootCategoryProvider $rootCategoryProvider,
8392
ResourceConnection $resourceConnection,
8493
LayerFormatter $layerFormatter,
85-
Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly
94+
Aggregations\Category\IncludeDirectChildrenOnly $includeDirectChildrenOnly,
95+
CollectionFactory $categoryCollectionFactory
8696
) {
8797
$this->categoryAttributeQuery = $categoryAttributeQuery;
8898
$this->attributesMapper = $attributesMapper;
8999
$this->resourceConnection = $resourceConnection;
90100
$this->rootCategoryProvider = $rootCategoryProvider;
91101
$this->layerFormatter = $layerFormatter;
92102
$this->includeDirectChildrenOnly = $includeDirectChildrenOnly;
103+
$this->categoryCollectionFactory = $categoryCollectionFactory;
93104
}
94105

95106
/**
@@ -112,6 +123,11 @@ function (AggregationValueInterface $value) {
112123
$bucket->getValues()
113124
);
114125

126+
if ($storeId) {
127+
$storeFilteredCategoryIds = $this->getStoreCategoryIds($storeId);
128+
$categoryIds = \array_intersect($categoryIds, $storeFilteredCategoryIds);
129+
}
130+
115131
$categoryIds = \array_diff($categoryIds, [$this->rootCategoryProvider->getRootCategory($storeId)]);
116132
$categoryLabels = \array_column(
117133
$this->attributesMapper->getAttributesValues(
@@ -158,4 +174,25 @@ private function isBucketEmpty(?BucketInterface $bucket): bool
158174
{
159175
return null === $bucket || !$bucket->getValues();
160176
}
177+
178+
/**
179+
* List of store categories
180+
*
181+
* @param int $storeId
182+
* @return array
183+
*/
184+
private function getStoreCategoryIds(int $storeId): array
185+
{
186+
$storeRootCategoryId = $this->rootCategoryProvider->getRootCategory($storeId);
187+
$collection = $this->categoryCollectionFactory->create();
188+
$select = $collection->getSelect();
189+
$connection = $collection->getConnection();
190+
$select->where(
191+
$connection->quoteInto(
192+
'e.path LIKE ? OR e.entity_id=' . $connection->quote($storeRootCategoryId, 'int'),
193+
'%/' . $storeRootCategoryId . '/%'
194+
)
195+
);
196+
return $collection->getAllIds();
197+
}
161198
}

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

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\GraphQl\Catalog;
99

10-
use Exception;
1110
use Magento\TestFramework\TestCase\GraphQlAbstract;
1211

1312
/**
@@ -28,6 +27,41 @@ public function testAggregationEqCategory()
2827
$this->assertEquals($expectedSubcategorie, $categoryAggregation);
2928
}
3029

30+
/**
31+
* Test to check aggregation with the store header
32+
*
33+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
34+
* @magentoApiDataFixture Magento/Store/_files/store_with_second_root_category.php
35+
* @magentoApiDataFixture Magento/Store/_files/assign_products_to_categories_and_websites.php
36+
* @return void
37+
*/
38+
public function testAggregationWithStoreFiltration()
39+
{
40+
$query = $this->getAggregationQuery();
41+
$result = $this->graphQlQuery($query);
42+
$categoryAggregation = $this->getCategoryAggregation($result);
43+
$this->assertNotEmpty($categoryAggregation);
44+
$result = $this->graphQlQuery($query, [], '', ['store' => 'test_store_1']);
45+
$categoryAggregation = $this->getCategoryAggregation($result);
46+
$this->assertEmpty($categoryAggregation);
47+
}
48+
49+
/**
50+
* Extract category aggregation from the result
51+
*
52+
* @param array $result
53+
* @return array|null
54+
*/
55+
private function getCategoryAggregation(array $result) : ?array
56+
{
57+
return array_filter(
58+
$result['products']['aggregations'],
59+
function ($a) {
60+
return $a['attribute_code'] == 'category_id';
61+
}
62+
);
63+
}
64+
3165
/**
3266
* Test category_id aggregation on filter by "in" category ID condition.
3367
*
@@ -98,6 +132,55 @@ private function getSubcategoriesOfCategoryThree(): array
98132
];
99133
}
100134

135+
private function getAggregationQuery() : string
136+
{
137+
return <<<QUERY
138+
query {
139+
products(filter: { category_id: { eq: "3" } }) {
140+
total_count
141+
142+
aggregations {
143+
attribute_code
144+
145+
label
146+
147+
count
148+
149+
options {
150+
count
151+
152+
label
153+
154+
value
155+
}
156+
}
157+
158+
items {
159+
name
160+
161+
sku
162+
163+
price_range {
164+
minimum_price {
165+
regular_price {
166+
value
167+
168+
currency
169+
}
170+
}
171+
}
172+
}
173+
174+
page_info {
175+
page_size
176+
177+
current_page
178+
}
179+
}
180+
}
181+
QUERY;
182+
}
183+
101184
/**
102185
* Get graphQl query.
103186
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Store\Api\WebsiteRepositoryInterface;
9+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Api\CategoryLinkManagementInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
$objectManager = Bootstrap::getObjectManager();
15+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
16+
$websites = $websiteRepository->getList();
17+
$websiteIds = [];
18+
foreach ($websites as $website) {
19+
$websiteIds[] = $website->getId();
20+
}
21+
22+
$categoryCollectionFactory = $objectManager->get(CollectionFactory::class);
23+
$categoryCollection = $categoryCollectionFactory->create();
24+
$categoryIds = [];
25+
foreach ($categoryCollection as $category) {
26+
$categoryIds[] = $category->getId();
27+
}
28+
29+
$productSkus = ['simple-4', 'simple-3', '12345', 'simple'];
30+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
31+
$categoryLinkManagement = $objectManager->get(CategoryLinkManagementInterface::class);
32+
33+
foreach ($productSkus as $sku) {
34+
$product = $productRepository->get($sku);
35+
$product->setWebsiteIds($websiteIds);
36+
$productRepository->save($product);
37+
$categoryLinkManagement->assignProductToCategories($sku, $categoryIds);
38+
}

0 commit comments

Comments
 (0)