From bf0030fbbd49ce1c40a17bd1f0717a9dffeb326d Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Thu, 25 Oct 2018 13:12:00 +0530 Subject: [PATCH 1/6] Update blocks --- app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php | 5 ++++- .../Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php index 962127e16f716..d7c0008c7d927 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -78,7 +78,10 @@ private function getBlocksData(array $blockIdentifiers): array $blocksData = []; try { foreach ($blockIdentifiers as $blockIdentifier) { - $blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier); + $blockData = $this->blockDataProvider->getData($blockIdentifier); + if (!empty($blockData)) { + $blocksData[$blockIdentifier] = $blockData; + } } } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index 5b7e632a73cb0..d47a0dca27157 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -49,7 +49,7 @@ public function getData(string $blockIdentifier): array $block = $this->blockRepository->getById($blockIdentifier); if (false === $block->isActive()) { - throw new NoSuchEntityException(); + return []; } $renderedContent = $this->widgetFilter->filter($block->getContent()); From 4eac0fce975759e961793d88c77d35f61bd9d94e Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 26 Oct 2018 13:06:44 +0530 Subject: [PATCH 2/6] Added warning log --- .../Magento/CmsGraphQl/Model/Resolver/Blocks.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php index d7c0008c7d927..fabb479763e60 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -14,6 +14,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Psr\Log\LoggerInterface; /** * CMS blocks field resolver, used for GraphQL request processing @@ -25,13 +26,20 @@ class Blocks implements ResolverInterface */ private $blockDataProvider; + /** + * @var LoggerInterface + */ + private $logger; + /** * @param BlockDataProvider $blockDataProvider */ public function __construct( - BlockDataProvider $blockDataProvider + BlockDataProvider $blockDataProvider, + LoggerInterface $logger ) { $this->blockDataProvider = $blockDataProvider; + $this->logger = $logger; } /** @@ -81,6 +89,10 @@ private function getBlocksData(array $blockIdentifiers): array $blockData = $this->blockDataProvider->getData($blockIdentifier); if (!empty($blockData)) { $blocksData[$blockIdentifier] = $blockData; + } else { + $this->logger->warning( + sprintf('The CMS block with the "%s" Identifier is disabled.', $blockIdentifier) + ); } } } catch (NoSuchEntityException $e) { From 922d398fdbaba69ad54ace1d3291f851e6f33aa7 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 22 Nov 2018 15:14:22 +0200 Subject: [PATCH 3/6] GraphQL-176: Show only active CMS Blocks --- .../CmsGraphQl/Model/Resolver/Blocks.php | 27 +++++-------------- .../Model/Resolver/DataProvider/Block.php | 4 ++- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php index fabb479763e60..2f1c84848eb52 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -14,7 +14,6 @@ use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Psr\Log\LoggerInterface; /** * CMS blocks field resolver, used for GraphQL request processing @@ -26,20 +25,13 @@ class Blocks implements ResolverInterface */ private $blockDataProvider; - /** - * @var LoggerInterface - */ - private $logger; - /** * @param BlockDataProvider $blockDataProvider */ public function __construct( - BlockDataProvider $blockDataProvider, - LoggerInterface $logger + BlockDataProvider $blockDataProvider ) { $this->blockDataProvider = $blockDataProvider; - $this->logger = $logger; } /** @@ -84,19 +76,12 @@ private function getBlockIdentifiers(array $args): array private function getBlocksData(array $blockIdentifiers): array { $blocksData = []; - try { - foreach ($blockIdentifiers as $blockIdentifier) { - $blockData = $this->blockDataProvider->getData($blockIdentifier); - if (!empty($blockData)) { - $blocksData[$blockIdentifier] = $blockData; - } else { - $this->logger->warning( - sprintf('The CMS block with the "%s" Identifier is disabled.', $blockIdentifier) - ); - } + foreach ($blockIdentifiers as $blockIdentifier) { + try { + $blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier); + } catch (NoSuchEntityException $e) { + $blocksData[$blockIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } return $blocksData; } diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index d47a0dca27157..c1a694dcd73b7 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -49,7 +49,9 @@ public function getData(string $blockIdentifier): array $block = $this->blockRepository->getById($blockIdentifier); if (false === $block->isActive()) { - return []; + throw new NoSuchEntityException( + __('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier) + ); } $renderedContent = $this->widgetFilter->filter($block->getContent()); From 6d984eb0ff9d596ece553ff50d77d68c6749ff33 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 22 Nov 2018 16:29:17 +0200 Subject: [PATCH 4/6] GraphQL-176: Show only active CMS Blocks --- .../TestFramework/TestCase/GraphQl/Client.php | 5 +- .../ResponseContainsErrorsException.php | 41 +++++++ .../Magento/GraphQl/Cms/CmsBlockTest.php | 116 +++++++++++------- .../testsuite/Magento/Cms/_files/blocks.php | 52 ++++++++ .../Magento/Cms/_files/blocks_rollback.php | 23 ++++ 5 files changed, 192 insertions(+), 45 deletions(-) create mode 100644 dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/ResponseContainsErrorsException.php create mode 100644 dev/tests/integration/testsuite/Magento/Cms/_files/blocks.php create mode 100644 dev/tests/integration/testsuite/Magento/Cms/_files/blocks_rollback.php diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php index 64ad44528572d..d34a72707b89e 100644 --- a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php +++ b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php @@ -102,7 +102,10 @@ private function processErrors($responseBodyArray) } } - throw new \Exception('GraphQL response contains errors: ' . $errorMessage); + throw new ResponseContainsErrorsException( + 'GraphQL response contains errors: ' . $errorMessage, + $responseBodyArray + ); } throw new \Exception('GraphQL responded with an unknown error: ' . json_encode($responseBodyArray)); } diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/ResponseContainsErrorsException.php b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/ResponseContainsErrorsException.php new file mode 100644 index 0000000000000..568de57543d84 --- /dev/null +++ b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/ResponseContainsErrorsException.php @@ -0,0 +1,41 @@ +responseData = $responseData; + } + + /** + * Get response data + * + * @return array + */ + public function getResponseData(): array + { + return $this->responseData; + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php index d8e06fd08385c..57f526b1cb2f7 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php @@ -7,43 +7,45 @@ namespace Magento\GraphQl\Cms; -use Magento\Cms\Model\Block; -use Magento\Cms\Model\GetBlockByIdentifier; -use Magento\Store\Model\StoreManagerInterface; +use Magento\Cms\Api\BlockRepositoryInterface; +use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; use Magento\TestFramework\TestCase\GraphQlAbstract; use Magento\Widget\Model\Template\FilterEmulate; class CmsBlockTest extends GraphQlAbstract { /** - * @var \Magento\TestFramework\ObjectManager + * @var BlockRepositoryInterface */ - private $objectManager; + private $blockRepository; + + /** + * @var FilterEmulate + */ + private $filterEmulate; protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->blockRepository = Bootstrap::getObjectManager()->get(BlockRepositoryInterface::class); + $this->filterEmulate = Bootstrap::getObjectManager()->get(FilterEmulate::class); } /** * Verify the fields of CMS Block selected by identifiers * - * @magentoApiDataFixture Magento/Cms/_files/block.php + * @magentoApiDataFixture Magento/Cms/_files/blocks.php */ - public function testGetCmsBlocksByIdentifiers() + public function testGetCmsBlock() { - /** @var StoreManagerInterface $storeManager */ - $storeManager = $this->objectManager->get(StoreManagerInterface::class); - $storeId = (int)$storeManager->getStore()->getId(); - $cmsBlock = $this->objectManager->get(GetBlockByIdentifier::class)->execute("fixture_block", $storeId); + $cmsBlock = $this->blockRepository->getById('enabled_block'); $cmsBlockData = $cmsBlock->getData(); - /** @var FilterEmulate $widgetFilter */ - $widgetFilter = $this->objectManager->get(FilterEmulate::class); - $renderedContent = $widgetFilter->setUseSessionInUrl(false)->filter($cmsBlock->getContent()); + $renderedContent = $this->filterEmulate->setUseSessionInUrl(false)->filter($cmsBlock->getContent()); + $query = <<graphQlQuery($query); - $this->assertArrayHasKey('cmsBlocks', $response); - $this->assertArrayHasKey('items', $response['cmsBlocks']); - $this->assertArrayHasKey('content', $response['cmsBlocks']['items'][0]); - $this->assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']); - $this->assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']); - $this->assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']); + + self::assertArrayHasKey('cmsBlocks', $response); + self::assertArrayHasKey('items', $response['cmsBlocks']); + + self::assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']); + self::assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']); + self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']); } /** * Verify the message when CMS Block is disabled * - * @magentoApiDataFixture Magento/Cms/_files/block.php + * @expectedException \Exception + * @expectedExceptionMessage The CMS block with the "disabled_block" ID doesn't exist + * + * @magentoApiDataFixture Magento/Cms/_files/blocks.php */ - public function testGetDisabledCmsBlockByIdentifiers() + public function testGetDisabledCmsBlock() { - /** @var StoreManagerInterface $storeManager */ - $storeManager = $this->objectManager->get(StoreManagerInterface::class); - $storeId = (int)$storeManager->getStore()->getId(); - $cmsBlockId = $this->objectManager->get(GetBlockByIdentifier::class) - ->execute("fixture_block", $storeId) - ->getId(); - $this->objectManager->get(Block::class)->load($cmsBlockId)->setIsActive(0)->save(); $query = <<expectException(\Exception::class); - $this->expectExceptionMessage('No such entity.'); $this->graphQlQuery($query); } /** * Verify the message when identifiers were not specified + * + * @expectedException \Exception + * @expectedExceptionMessage "identifiers" of CMS blocks should be specified */ - public function testGetCmsBlockBypassingIdentifiers() + public function testGetCmsBlocksWithoutIdentifiers() { $query = <<expectException(\Exception::class); - $this->expectExceptionMessage('"identifiers" of CMS blocks should be specified'); $this->graphQlQuery($query); } /** * Verify the message when CMS Block with such identifiers does not exist + * + * @expectedException \Exception + * @expectedExceptionMessage The CMS block with the "nonexistent_id" ID doesn't exist. */ public function testGetCmsBlockByNonExistentIdentifier() { $query = <<expectException(\Exception::class); - $this->expectExceptionMessage('The CMS block with the "0" ID doesn\'t exist.'); $this->graphQlQuery($query); } + + /** + * Verify the fields of CMS Block selected by identifiers + * + * @magentoApiDataFixture Magento/Cms/_files/blocks.php + */ + public function testGetEnabledAndDisabledCmsBlockInOneRequest() + { + $query = + <<graphQlQuery($query); + self::fail('Response should contains errors.'); + } catch (ResponseContainsErrorsException $e) { + $responseData = $e->getResponseData(); + } + + self::assertNotEmpty($responseData); + self::assertEquals('enabled_block', $responseData['data']['cmsBlocks']['items'][0]['identifier']); + self::assertEquals( + 'The CMS block with the "disabled_block" ID doesn\'t exist.', + $responseData['errors'][0]['message'] + ); + } } diff --git a/dev/tests/integration/testsuite/Magento/Cms/_files/blocks.php b/dev/tests/integration/testsuite/Magento/Cms/_files/blocks.php new file mode 100644 index 0000000000000..bbdc697645a42 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Cms/_files/blocks.php @@ -0,0 +1,52 @@ +get(BlockRepositoryInterface::class); +/** @var BlockInterfaceFactory $blockFactory */ +$blockFactory = Bootstrap::getObjectManager()->get(BlockInterfaceFactory::class); +$storeId = Bootstrap::getObjectManager()->get(StoreManagerInterface::class)->getStore()->getId(); + +/** @var BlockInterface $block */ +$block = $blockFactory->create([ + 'data' => [ + BlockInterface::IDENTIFIER => 'enabled_block', + BlockInterface::TITLE => 'Enabled CMS Block Title', + BlockInterface::CONTENT => ' +

Enabled Block

+ store url +

Config value: "{{config path="web/unsecure/base_url"}}".

+

Custom variable: "{{customvar code="variable_code"}}".

+ ', + BlockInterface::IS_ACTIVE => 1, + 'store_id' => [$storeId], + ] +]); +$blockRepository->save($block); + +/** @var BlockInterface $block */ +$block = $blockFactory->create([ + 'data' => [ + BlockInterface::IDENTIFIER => 'disabled_block', + BlockInterface::TITLE => 'Disabled CMS Block Title', + BlockInterface::CONTENT => ' +

Disabled Block

+ store url +

Config value: "{{config path="web/unsecure/base_url"}}".

+

Custom variable: "{{customvar code="variable_code"}}".

+ ', + BlockInterface::IS_ACTIVE => 0, + 'store_id' => [$storeId], + ] +]); +$blockRepository->save($block); diff --git a/dev/tests/integration/testsuite/Magento/Cms/_files/blocks_rollback.php b/dev/tests/integration/testsuite/Magento/Cms/_files/blocks_rollback.php new file mode 100644 index 0000000000000..a7a2a51524cf0 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Cms/_files/blocks_rollback.php @@ -0,0 +1,23 @@ +get(BlockRepositoryInterface::class); + +foreach (['enabled_block', 'disabled_block'] as $blockId) { + try { + $blockRepository->deleteById($blockId); + } catch (NoSuchEntityException $e) { + /** + * Tests which are wrapped with MySQL transaction clear all data by transaction rollback. + */ + } +} From d3a22e9e7a10f069989b633f3b5fffe09b6ec8d2 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 22 Nov 2018 16:48:53 +0200 Subject: [PATCH 5/6] GraphQL-176: Show only active CMS Blocks -- Fix static tests --- app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php | 4 ++++ .../Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php | 2 ++ .../Magento/TestFramework/TestCase/GraphQl/Client.php | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php index 2f1c84848eb52..e55db2a3fa42a 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -55,6 +55,8 @@ public function resolve( } /** + * Get block identifiers + * * @param array $args * @return string[] * @throws GraphQlInputException @@ -69,6 +71,8 @@ private function getBlockIdentifiers(array $args): array } /** + * Get blocks data + * * @param array $blockIdentifiers * @return array * @throws GraphQlNoSuchEntityException diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index c1a694dcd73b7..47a2439c4fad0 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -40,6 +40,8 @@ public function __construct( } /** + * Get block data + * * @param string $blockIdentifier * @return array * @throws NoSuchEntityException diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php index d34a72707b89e..e94cc9613b1c9 100644 --- a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php +++ b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php @@ -29,8 +29,6 @@ class Client private $json; /** - * CurlClient constructor. - * * @param CurlClient|null $curlClient * @param JsonSerializer|null $json */ @@ -81,6 +79,8 @@ public function postQuery(string $query, array $variables = [], string $operatio } /** + * Process errors + * * @param array $responseBodyArray * @throws \Exception */ From 772d4da83ee63044a64ad49e19c8052f6f54e109 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Thu, 22 Nov 2018 16:58:41 +0200 Subject: [PATCH 6/6] GraphQL-176: Show only active CMS Blocks -- Fix static tests --- .../framework/Magento/TestFramework/TestCase/GraphQl/Client.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php index e94cc9613b1c9..5458b5cfbb731 100644 --- a/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php +++ b/dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php @@ -112,6 +112,8 @@ private function processErrors($responseBodyArray) } /** + * Get endpoint url + * * @return string resource URL * @throws \Exception */