Skip to content

Commit

Permalink
Merge branch '2.3-devlop#463' of github.com:XxXgeoXxX/graphql-ce into…
Browse files Browse the repository at this point in the history
… 463-md-fixes
  • Loading branch information
lenaorobei committed Jun 21, 2019
2 parents 5177344 + 10ec412 commit f7fbc9a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
Expand Down Expand Up @@ -84,6 +85,10 @@ public function __construct(
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterator
{
$categoryQuery = $resolveInfo->fieldNodes[0];
if ($this->isCategoryActive($rootCategoryId) === false) {
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
}

$collection = $this->collectionFactory->create();
$this->joinAttributesRecursively($collection, $categoryQuery);
$depth = $this->depthCalculator->calculate($categoryQuery);
Expand Down Expand Up @@ -144,4 +149,30 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi
$this->joinAttributesRecursively($collection, $node);
}
}

/**
* Check if provided category active
*
* @param int $rootCategoryId
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function isCategoryActive(int $rootCategoryId) : bool
{
if ($rootCategoryId == Category::TREE_ROOT_ID) {
return true;
}
$collection = $this->collectionFactory->create();
$collection->addAttributeToFilter(Category::KEY_IS_ACTIVE, ['eq' => 1])
->getSelect()
->where(
$collection->getSelect()
->getConnection()
->quoteIdentifier(
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
) . ' = ?',
$rootCategoryId
);
return (bool)$collection->count();
}
}
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 f7fbc9a

Please sign in to comment.