Skip to content

Commit

Permalink
ENGCOM-4576: Query category for disabled catalog with products return…
Browse files Browse the repository at this point in the history
…s the product count magento#476
  • Loading branch information
naydav authored Jun 24, 2019
2 parents 068c1ce + c1e2218 commit 2ea2202
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
* Check if category is active.
*/
class CheckCategoryIsActive
{
/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @var MetadataPool
*/
private $metadata;

/**
* @param CollectionFactory $collectionFactory
* @param MetadataPool $metadata
*/
public function __construct(
CollectionFactory $collectionFactory,
MetadataPool $metadata
) {
$this->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'));
}
}
}
15 changes: 14 additions & 1 deletion app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<QUERY
{
category(id: {$categoryId}) {
id
name
}
}
QUERY;
$this->graphQlQuery($query);
}

public function testNonExistentCategoryWithProductCount()
{
$query = <<<QUERY
Expand Down

0 comments on commit 2ea2202

Please sign in to comment.