|
| 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\GraphQl\Cms; |
| 9 | + |
| 10 | +use Magento\Cms\Model\Block; |
| 11 | +use Magento\Cms\Model\GetBlockByIdentifier; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | +use Magento\Widget\Model\Template\FilterEmulate; |
| 15 | + |
| 16 | +class CmsBlockTest extends GraphQlAbstract |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \Magento\TestFramework\ObjectManager |
| 20 | + */ |
| 21 | + private $objectManager; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Verify the fields of CMS Block selected by identifiers |
| 30 | + * |
| 31 | + * @magentoApiDataFixture Magento/Cms/_files/block.php |
| 32 | + */ |
| 33 | + public function testGetCmsBlocksByIdentifiers() |
| 34 | + { |
| 35 | + /** @var StoreManagerInterface $storeManager */ |
| 36 | + $storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 37 | + $storeId = (int)$storeManager->getStore()->getId(); |
| 38 | + $cmsBlock = $this->objectManager->get(GetBlockByIdentifier::class)->execute("fixture_block", $storeId); |
| 39 | + $cmsBlockData = $cmsBlock->getData(); |
| 40 | + /** @var FilterEmulate $widgetFilter */ |
| 41 | + $widgetFilter = $this->objectManager->get(FilterEmulate::class); |
| 42 | + $renderedContent = $widgetFilter->setUseSessionInUrl(false)->filter($cmsBlock->getContent()); |
| 43 | + $query = |
| 44 | + <<<QUERY |
| 45 | +{ |
| 46 | + cmsBlocks(identifiers: "fixture_block") { |
| 47 | + items { |
| 48 | + identifier |
| 49 | + title |
| 50 | + content |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +QUERY; |
| 55 | + |
| 56 | + $response = $this->graphQlQuery($query); |
| 57 | + $this->assertArrayHasKey('cmsBlocks', $response); |
| 58 | + $this->assertArrayHasKey('items', $response['cmsBlocks']); |
| 59 | + $this->assertArrayHasKey('content', $response['cmsBlocks']['items'][0]); |
| 60 | + $this->assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']); |
| 61 | + $this->assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']); |
| 62 | + $this->assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Verify the message when CMS Block is disabled |
| 67 | + */ |
| 68 | + public function testGetDisabledCmsBlockByIdentifiers() |
| 69 | + { |
| 70 | + /** @var StoreManagerInterface $storeManager */ |
| 71 | + $storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 72 | + $storeId = (int)$storeManager->getStore()->getId(); |
| 73 | + $cmsBlockId = $this->objectManager->get(GetBlockByIdentifier::class)->execute("fixture_block", $storeId)->getId(); |
| 74 | + $this->objectManager->get(Block::class)->load($cmsBlockId)->setIsActive(0)->save(); |
| 75 | + $query = |
| 76 | + <<<QUERY |
| 77 | +{ |
| 78 | + cmsBlocks(identifiers: "fixture_block") { |
| 79 | + items { |
| 80 | + identifier |
| 81 | + title |
| 82 | + content |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +QUERY; |
| 87 | + |
| 88 | + $this->expectException(\Exception::class); |
| 89 | + $this->expectExceptionMessage('No such entity.'); |
| 90 | + $this->graphQlQuery($query); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Verify the message when identifiers were not specified |
| 95 | + */ |
| 96 | + public function testGetCmsBlockBypassingIdentifiers() |
| 97 | + { |
| 98 | + $query = |
| 99 | + <<<QUERY |
| 100 | +{ |
| 101 | + cmsBlocks(identifiers: []) { |
| 102 | + items { |
| 103 | + identifier |
| 104 | + title |
| 105 | + content |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +QUERY; |
| 110 | + |
| 111 | + $this->expectException(\Exception::class); |
| 112 | + $this->expectExceptionMessage('"identifiers" of CMS blocks should be specified'); |
| 113 | + $this->graphQlQuery($query); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Verify the message when CMS Block with such identifiers does not exist |
| 118 | + */ |
| 119 | + public function testGetCmsBlockByNonExistentIdentifier() |
| 120 | + { |
| 121 | + $query = |
| 122 | + <<<QUERY |
| 123 | +{ |
| 124 | + cmsBlocks(identifiers: "0") { |
| 125 | + items { |
| 126 | + identifier |
| 127 | + title |
| 128 | + content |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +QUERY; |
| 133 | + |
| 134 | + $this->expectException(\Exception::class); |
| 135 | + $this->expectExceptionMessage('The CMS block with the "0" ID doesn\'t exist.'); |
| 136 | + $this->graphQlQuery($query); |
| 137 | + } |
| 138 | +} |
0 commit comments