Skip to content

Commit f7fbc9a

Browse files
committed
Merge branch '2.3-devlop#463' of github.com:XxXgeoXxX/graphql-ce into 463-md-fixes
2 parents 5177344 + 10ec412 commit f7fbc9a

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
1212
use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
1313
use Magento\Framework\EntityManager\MetadataPool;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1516
use Magento\Catalog\Api\Data\CategoryInterface;
1617
use Magento\Catalog\Model\ResourceModel\Category\Collection;
@@ -84,6 +85,10 @@ public function __construct(
8485
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterator
8586
{
8687
$categoryQuery = $resolveInfo->fieldNodes[0];
88+
if ($this->isCategoryActive($rootCategoryId) === false) {
89+
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
90+
}
91+
8792
$collection = $this->collectionFactory->create();
8893
$this->joinAttributesRecursively($collection, $categoryQuery);
8994
$depth = $this->depthCalculator->calculate($categoryQuery);
@@ -144,4 +149,30 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi
144149
$this->joinAttributesRecursively($collection, $node);
145150
}
146151
}
152+
153+
/**
154+
* Check if provided category active
155+
*
156+
* @param int $rootCategoryId
157+
* @return bool
158+
* @throws \Magento\Framework\Exception\LocalizedException
159+
*/
160+
private function isCategoryActive(int $rootCategoryId) : bool
161+
{
162+
if ($rootCategoryId == Category::TREE_ROOT_ID) {
163+
return true;
164+
}
165+
$collection = $this->collectionFactory->create();
166+
$collection->addAttributeToFilter(Category::KEY_IS_ACTIVE, ['eq' => 1])
167+
->getSelect()
168+
->where(
169+
$collection->getSelect()
170+
->getConnection()
171+
->quoteIdentifier(
172+
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
173+
) . ' = ?',
174+
$rootCategoryId
175+
);
176+
return (bool)$collection->count();
177+
}
147178
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ public function testGetCategoryById()
168168
self::assertEquals(13, $response['category']['id']);
169169
}
170170

171+
/**
172+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
173+
* @expectedException \Exception
174+
* @expectedExceptionMessage Category doesn't exist
175+
*/
176+
public function testGetDisabledCategory()
177+
{
178+
$categoryId = 8;
179+
$query = <<<QUERY
180+
{
181+
category(id: {$categoryId}) {
182+
id
183+
name
184+
}
185+
}
186+
QUERY;
187+
$this->graphQlQuery($query);
188+
}
189+
171190
public function testNonExistentCategoryWithProductCount()
172191
{
173192
$query = <<<QUERY

0 commit comments

Comments
 (0)