Skip to content

Commit c1e2218

Browse files
ENGCOM-4576: Query category for disabled catalog with products returns the product count #476
- Merge Pull Request magento/graphql-ce#476 from XxXgeoXxX/graphql-ce:2.3-devlop#463 - Merged commits: 1. d452a36 2. 058135b 3. 6c6047d 4. e1aa929 5. 53d087a 6. 8ca4590 7. 201cdf2 8. 4c0b3df 9. c47213a 10. b676132 11. 5287f98 12. 10ec412 13. f7fbc9a 14. 525a1e9 15. af96830 16. 0b2bdc6
2 parents 1e58f70 + 0b2bdc6 commit c1e2218

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
namespace Magento\CatalogGraphQl\Model\Resolver\Category;
9+
10+
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
12+
use Magento\Framework\EntityManager\MetadataPool;
13+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
14+
15+
/**
16+
* Check if category is active.
17+
*/
18+
class CheckCategoryIsActive
19+
{
20+
/**
21+
* @var CollectionFactory
22+
*/
23+
private $collectionFactory;
24+
25+
/**
26+
* @var MetadataPool
27+
*/
28+
private $metadata;
29+
30+
/**
31+
* @param CollectionFactory $collectionFactory
32+
* @param MetadataPool $metadata
33+
*/
34+
public function __construct(
35+
CollectionFactory $collectionFactory,
36+
MetadataPool $metadata
37+
) {
38+
$this->collectionFactory = $collectionFactory;
39+
$this->metadata = $metadata;
40+
}
41+
42+
/**
43+
* Check if category is active.
44+
*
45+
* @param int $categoryId
46+
* @throws GraphQlNoSuchEntityException
47+
*/
48+
public function execute(int $categoryId): void
49+
{
50+
$collection = $this->collectionFactory->create();
51+
$collection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1])
52+
->getSelect()
53+
->where(
54+
$collection->getSelect()
55+
->getConnection()
56+
->quoteIdentifier(
57+
'e.' .
58+
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
59+
) . ' = ?',
60+
$categoryId
61+
);
62+
63+
if ($collection->count() === 0) {
64+
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
65+
}
66+
}
67+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Model\Category;
11+
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1012
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
1113
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -34,16 +36,24 @@ class CategoryTree implements ResolverInterface
3436
*/
3537
private $extractDataFromCategoryTree;
3638

39+
/**
40+
* @var CheckCategoryIsActive
41+
*/
42+
private $checkCategoryIsActive;
43+
3744
/**
3845
* @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
3946
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
47+
* @param CheckCategoryIsActive $checkCategoryIsActive
4048
*/
4149
public function __construct(
4250
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree,
43-
ExtractDataFromCategoryTree $extractDataFromCategoryTree
51+
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
52+
CheckCategoryIsActive $checkCategoryIsActive
4453
) {
4554
$this->categoryTree = $categoryTree;
4655
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
56+
$this->checkCategoryIsActive = $checkCategoryIsActive;
4757
}
4858

4959
/**
@@ -72,6 +82,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7282
}
7383

7484
$rootCategoryId = $this->getCategoryId($args);
85+
if ($rootCategoryId !== Category::TREE_ROOT_ID) {
86+
$this->checkCategoryIsActive->execute($rootCategoryId);
87+
}
7588
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
7689

7790
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {

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)