Skip to content

Commit 0cf6931

Browse files
committed
Merge branch 'ACP2E-363' of https://github.com/magento-l3/magento2ce into PR-2021-12-21
2 parents 6e2210d + 7520ffe commit 0cf6931

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Categories/DataProvider/Category/CollectionProcessor/CatalogProcessor.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,27 @@ public function process(
4949
ContextInterface $context = null
5050
): Collection {
5151
$this->collectionProcessor->process($searchCriteria, $collection);
52+
$store = $context->getExtensionAttributes()->getStore();
53+
$this->addRootCategoryFilterForStore($collection, (string) $store->getRootCategoryId());
5254

5355
return $collection;
5456
}
57+
58+
/**
59+
* Add filtration based on the store root category id
60+
*
61+
* @param Collection $collection
62+
* @param string $rootCategoryId
63+
*/
64+
private function addRootCategoryFilterForStore(Collection $collection, string $rootCategoryId) : void
65+
{
66+
$select = $collection->getSelect();
67+
$connection = $collection->getConnection();
68+
$select->where(
69+
$connection->quoteInto(
70+
'e.path LIKE ? OR e.entity_id=' . $connection->quote($rootCategoryId, 'int'),
71+
'%/' . $rootCategoryId . '/%'
72+
)
73+
);
74+
}
5575
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,36 @@ public function testFilterCategoryRecursiveFragment() : void
887887
$this->assertArrayHasKey('id', $result['categoryList'][0]['children'][0]);
888888
$this->assertEquals($result['categoryList'][0]['children'][0]['id'], '3');
889889
}
890+
891+
/**
892+
* Test category list is filtered based on store in header
893+
*
894+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
895+
* @magentoApiDataFixture Magento/Store/_files/store_with_second_root_category.php
896+
*/
897+
public function testFilterStoreRootCategory() : void
898+
{
899+
$query = <<<'QUERY'
900+
{
901+
categoryList(filters: {name: {match: "Category"}}) {
902+
uid
903+
level
904+
name
905+
breadcrumbs {
906+
category_uid
907+
category_name
908+
category_level
909+
category_url_key
910+
}
911+
}
912+
}
913+
QUERY;
914+
$result = $this->graphQlQuery($query);
915+
$this->assertArrayNotHasKey('errors', $result);
916+
$this->assertCount(7, $result['categoryList']);
917+
918+
$result = $this->graphQlQuery($query, [], '', ['store' => 'test_store_1']);
919+
$this->assertArrayNotHasKey('errors', $result);
920+
$this->assertCount(1, $result['categoryList']);
921+
}
890922
}

0 commit comments

Comments
 (0)