Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Added validation for store id in CMS Block #870

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\CmsGraphQl\Model\Resolver;

use Magento\CmsGraphQl\Model\Resolver\DataProvider\Block as BlockDataProvider;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand Down Expand Up @@ -75,15 +76,14 @@ private function getBlockIdentifiers(array $args): array
*
* @param array $blockIdentifiers
* @return array
* @throws GraphQlNoSuchEntityException
*/
private function getBlocksData(array $blockIdentifiers): array
{
$blocksData = [];
foreach ($blockIdentifiers as $blockIdentifier) {
try {
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier);
} catch (NoSuchEntityException $e) {
} catch (LocalizedException $e) {
Rus0 marked this conversation as resolved.
Show resolved Hide resolved
$blocksData[$blockIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
}
Expand Down
48 changes: 45 additions & 3 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

use Magento\Cms\Api\BlockRepositoryInterface;
Rus0 marked this conversation as resolved.
Show resolved Hide resolved
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Cms\Model\GetBlockByIdentifier;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Widget\Model\Template\FilterEmulate;

/**
Expand All @@ -27,29 +31,67 @@ class Block
*/
private $widgetFilter;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @param BlockRepositoryInterface $blockRepository
* @param FilterEmulate $widgetFilter
* @param StoreManagerInterface $storeManager
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
BlockRepositoryInterface $blockRepository,
FilterEmulate $widgetFilter
FilterEmulate $widgetFilter,
StoreManagerInterface $storeManager,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->blockRepository = $blockRepository;
$this->widgetFilter = $widgetFilter;
$this->storeManager = $storeManager;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* Get block data
*
* @param string $blockIdentifier
* @return array
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function getData(string $blockIdentifier): array
{
$block = $this->blockRepository->getById($blockIdentifier);
$filterBy = BlockInterface::IDENTIFIER;
$storeId = (int)$this->storeManager->getStore()->getId();
Rus0 marked this conversation as resolved.
Show resolved Hide resolved
if (is_numeric($blockIdentifier)) {
$filterBy = BlockInterface::BLOCK_ID;
}
$searchCriteria = $this->searchCriteriaBuilder->addFilter(
Rus0 marked this conversation as resolved.
Show resolved Hide resolved
'store_id',
$storeId,
'eq'
)->addFilter(
$filterBy,
$blockIdentifier,
'eq'
)->setPageSize(1)->setCurrentPage(1)->create();

$blocks = $this->blockRepository->getList($searchCriteria)->getItems();

if (count($blocks) != 1) {
throw new NoSuchEntityException(
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
);
}

$block = array_values($blocks)[0];
if (false === $block->isActive()) {
throw new NoSuchEntityException(
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/CmsGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/framework": "*",
"magento/module-store": "*",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"magento/module-store": "*",
"magento/module-store": "*",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??? I don't see any changes or comment @lenaorobei

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magento/module-store is redundant dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't set it, I get the error: Module Magento\CmsGraphQl has undeclared dependencies: hard [Magento\Store]

"magento/module-cms": "*",
"magento/module-widget": "*"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,32 @@ public function testGetEnabledAndDisabledCmsBlockInOneRequest()
$responseData['errors'][0]['message']
);
}

/**
* Verify the message when CMS Block exists but not available for a store view.
*
* @magentoApiDataFixture Magento/Cms/_files/blocks.php
* @magentoApiDataFixture Magento/Store/_files/second_store.php
* @expectedException \Exception
* @expectedExceptionMessage The CMS block with the "enabled_block" ID doesn't exist.
*/
public function testGetCmsBlockByIdentifierWithDifferentStoreView()
{
$query =
<<<QUERY
{
cmsBlocks(identifiers: "enabled_block") {
items {
identifier
title
content
}
}
}
QUERY;

$nonExistingStoreCode = "fixture_second_store";
$headerMapInvalidStoreCode = ['Store' => $nonExistingStoreCode];
$this->graphQlQuery($query, [], '', $headerMapInvalidStoreCode);
}
}