CmsBlockTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Cms;
  8. use Magento\Cms\Api\BlockRepositoryInterface;
  9. use Magento\TestFramework\Helper\Bootstrap;
  10. use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
  11. use Magento\TestFramework\TestCase\GraphQlAbstract;
  12. use Magento\Widget\Model\Template\FilterEmulate;
  13. class CmsBlockTest extends GraphQlAbstract
  14. {
  15. /**
  16. * @var BlockRepositoryInterface
  17. */
  18. private $blockRepository;
  19. /**
  20. * @var FilterEmulate
  21. */
  22. private $filterEmulate;
  23. protected function setUp()
  24. {
  25. $this->blockRepository = Bootstrap::getObjectManager()->get(BlockRepositoryInterface::class);
  26. $this->filterEmulate = Bootstrap::getObjectManager()->get(FilterEmulate::class);
  27. }
  28. /**
  29. * Verify the fields of CMS Block selected by identifiers
  30. *
  31. * @magentoApiDataFixture Magento/Cms/_files/blocks.php
  32. */
  33. public function testGetCmsBlock()
  34. {
  35. $cmsBlock = $this->blockRepository->getById('enabled_block');
  36. $cmsBlockData = $cmsBlock->getData();
  37. $renderedContent = $this->filterEmulate->setUseSessionInUrl(false)->filter($cmsBlock->getContent());
  38. $query =
  39. <<<QUERY
  40. {
  41. cmsBlocks(identifiers: "enabled_block") {
  42. items {
  43. identifier
  44. title
  45. content
  46. }
  47. }
  48. }
  49. QUERY;
  50. $response = $this->graphQlQuery($query);
  51. self::assertArrayHasKey('cmsBlocks', $response);
  52. self::assertArrayHasKey('items', $response['cmsBlocks']);
  53. self::assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']);
  54. self::assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']);
  55. self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']);
  56. }
  57. /**
  58. * Verify the message when CMS Block is disabled
  59. *
  60. * @expectedException \Exception
  61. * @expectedExceptionMessage The CMS block with the "disabled_block" ID doesn't exist
  62. *
  63. * @magentoApiDataFixture Magento/Cms/_files/blocks.php
  64. */
  65. public function testGetDisabledCmsBlock()
  66. {
  67. $query =
  68. <<<QUERY
  69. {
  70. cmsBlocks(identifiers: "disabled_block") {
  71. items {
  72. identifier
  73. title
  74. content
  75. }
  76. }
  77. }
  78. QUERY;
  79. $this->graphQlQuery($query);
  80. }
  81. /**
  82. * Verify the message when identifiers were not specified
  83. *
  84. * @expectedException \Exception
  85. * @expectedExceptionMessage "identifiers" of CMS blocks should be specified
  86. */
  87. public function testGetCmsBlocksWithoutIdentifiers()
  88. {
  89. $query =
  90. <<<QUERY
  91. {
  92. cmsBlocks(identifiers: []) {
  93. items {
  94. identifier
  95. title
  96. content
  97. }
  98. }
  99. }
  100. QUERY;
  101. $this->graphQlQuery($query);
  102. }
  103. /**
  104. * Verify the message when CMS Block with such identifiers does not exist
  105. *
  106. * @expectedException \Exception
  107. * @expectedExceptionMessage The CMS block with the "nonexistent_id" ID doesn't exist.
  108. */
  109. public function testGetCmsBlockByNonExistentIdentifier()
  110. {
  111. $query =
  112. <<<QUERY
  113. {
  114. cmsBlocks(identifiers: "nonexistent_id") {
  115. items {
  116. identifier
  117. title
  118. content
  119. }
  120. }
  121. }
  122. QUERY;
  123. $this->graphQlQuery($query);
  124. }
  125. /**
  126. * Verify the fields of CMS Block selected by identifiers
  127. *
  128. * @magentoApiDataFixture Magento/Cms/_files/blocks.php
  129. */
  130. public function testGetEnabledAndDisabledCmsBlockInOneRequest()
  131. {
  132. $query =
  133. <<<QUERY
  134. {
  135. cmsBlocks(identifiers: ["enabled_block", "disabled_block"]) {
  136. items {
  137. identifier
  138. }
  139. }
  140. }
  141. QUERY;
  142. try {
  143. $this->graphQlQuery($query);
  144. self::fail('Response should contains errors.');
  145. } catch (ResponseContainsErrorsException $e) {
  146. $responseData = $e->getResponseData();
  147. }
  148. self::assertNotEmpty($responseData);
  149. self::assertEquals('enabled_block', $responseData['data']['cmsBlocks']['items'][0]['identifier']);
  150. self::assertEquals(
  151. 'The CMS block with the "disabled_block" ID doesn\'t exist.',
  152. $responseData['errors'][0]['message']
  153. );
  154. }
  155. }