Skip to content

Commit d9c305f

Browse files
author
Oleksandr Iegorov
committed
ACP2E-470: Products Aggregate for GraphQL returning data for all stores
1 parent e8494f5 commit d9c305f

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

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)