diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Category/CheckCategoryIsActive.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Category/CheckCategoryIsActive.php new file mode 100644 index 0000000000000..16f816a967123 --- /dev/null +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Category/CheckCategoryIsActive.php @@ -0,0 +1,67 @@ +collectionFactory = $collectionFactory; + $this->metadata = $metadata; + } + + /** + * Check if category is active. + * + * @param int $categoryId + * @throws GraphQlNoSuchEntityException + */ + public function execute(int $categoryId): void + { + $collection = $this->collectionFactory->create(); + $collection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1]) + ->getSelect() + ->where( + $collection->getSelect() + ->getConnection() + ->quoteIdentifier( + 'e.' . + $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() + ) . ' = ?', + $categoryId + ); + + if ($collection->count() === 0) { + throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist')); + } + } +} diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php index 1783a5cd9a7e5..89d3805383e1a 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php @@ -7,6 +7,8 @@ namespace Magento\CatalogGraphQl\Model\Resolver; +use Magento\Catalog\Model\Category; +use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive; use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\GraphQl\Config\Element\Field; @@ -34,16 +36,24 @@ class CategoryTree implements ResolverInterface */ private $extractDataFromCategoryTree; + /** + * @var CheckCategoryIsActive + */ + private $checkCategoryIsActive; + /** * @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree * @param ExtractDataFromCategoryTree $extractDataFromCategoryTree + * @param CheckCategoryIsActive $checkCategoryIsActive */ public function __construct( \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree, - ExtractDataFromCategoryTree $extractDataFromCategoryTree + ExtractDataFromCategoryTree $extractDataFromCategoryTree, + CheckCategoryIsActive $checkCategoryIsActive ) { $this->categoryTree = $categoryTree; $this->extractDataFromCategoryTree = $extractDataFromCategoryTree; + $this->checkCategoryIsActive = $checkCategoryIsActive; } /** @@ -72,6 +82,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value } $rootCategoryId = $this->getCategoryId($args); + if ($rootCategoryId !== Category::TREE_ROOT_ID) { + $this->checkCategoryIsActive->execute($rootCategoryId); + } $categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId); if (empty($categoriesTree) || ($categoriesTree->count() == 0)) { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryTest.php index 63073a389f278..df8e399ce6c61 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryTest.php @@ -168,6 +168,25 @@ public function testGetCategoryById() self::assertEquals(13, $response['category']['id']); } + /** + * @magentoApiDataFixture Magento/Catalog/_files/categories.php + * @expectedException \Exception + * @expectedExceptionMessage Category doesn't exist + */ + public function testGetDisabledCategory() + { + $categoryId = 8; + $query = <<graphQlQuery($query); + } + public function testNonExistentCategoryWithProductCount() { $query = <<