Skip to content

Commit bbe43fd

Browse files
committed
MC-21542: Category query does not handle disabled children properly
- added test function to trace back active status of it's parents back to root category being queried
1 parent 9656d08 commit bbe43fd

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function resolve(
4141
array $args = null
4242
) {
4343
if (!isset($value['model'])) {
44-
return null;
44+
throw new LocalizedException(__('"model" value should be specified'));
4545
}
4646
/** @var \Magento\Catalog\Model\Category $category */
4747
$category = $value['model'];

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,18 @@ public function __construct(
4848
public function execute(\Iterator $iterator): array
4949
{
5050
$tree = [];
51+
/** @var CategoryInterface $rootCategory */
52+
$rootCategory = $iterator->current();
5153
while ($iterator->valid()) {
52-
/** @var CategoryInterface $category */
53-
$category = $iterator->current();
54+
/** @var CategoryInterface $currentCategory */
55+
$currentCategory = $iterator->current();
5456
$iterator->next();
55-
if ($category->getIsActive()) {
56-
$pathElements = explode("/", $category->getPath());
57+
if ($this->areParentsActive($currentCategory, $rootCategory, (array)$iterator)) {
58+
$pathElements = explode("/", $currentCategory->getPath());
5759
if (empty($tree)) {
5860
$this->startCategoryFetchLevel = count($pathElements) - 1;
5961
}
60-
$this->iteratingCategory = $category;
62+
$this->iteratingCategory = $currentCategory;
6163
$currentLevelTree = $this->explodePathToArray($pathElements, $this->startCategoryFetchLevel);
6264
if (empty($tree)) {
6365
$tree = $currentLevelTree;
@@ -68,6 +70,34 @@ public function execute(\Iterator $iterator): array
6870
return $tree;
6971
}
7072

73+
74+
/**
75+
* Test that all parents of the current category are active
76+
*
77+
* Assumes that $categoriesArray are key-pair values and key is the ID of the category
78+
*
79+
* @param CategoryInterface $currentCategory
80+
* @param CategoryInterface $rootCategory
81+
* @param $categoriesArray
82+
*/
83+
private function areParentsActive(
84+
CategoryInterface $currentCategory,
85+
CategoryInterface $rootCategory,
86+
array $categoriesArray
87+
): bool {
88+
if ($currentCategory === $rootCategory) {
89+
return true;
90+
} elseif (array_key_exists($currentCategory->getParentId(), $categoriesArray)) {
91+
return $this->areParentsActive(
92+
$categoriesArray[$currentCategory->getParentId()],
93+
$rootCategory,
94+
$categoriesArray
95+
);
96+
} else {
97+
return false;
98+
}
99+
}
100+
71101
/**
72102
* Merge together complex categories trees
73103
*

0 commit comments

Comments
 (0)