77
88namespace Magento \GraphQl \Cms ;
99
10- use Magento \Cms \Model \ Block ;
11- use Magento \Cms \ Model \ GetBlockByIdentifier ;
12- use Magento \Store \ Model \ StoreManagerInterface ;
10+ use Magento \Cms \Api \ BlockRepositoryInterface ;
11+ use Magento \TestFramework \ Helper \ Bootstrap ;
12+ use Magento \TestFramework \ TestCase \ GraphQl \ ResponseContainsErrorsException ;
1313use Magento \TestFramework \TestCase \GraphQlAbstract ;
1414use Magento \Widget \Model \Template \FilterEmulate ;
1515
1616class CmsBlockTest extends GraphQlAbstract
1717{
1818 /**
19- * @var \Magento\TestFramework\ObjectManager
19+ * @var BlockRepositoryInterface
2020 */
21- private $ objectManager ;
21+ private $ blockRepository ;
22+
23+ /**
24+ * @var FilterEmulate
25+ */
26+ private $ filterEmulate ;
2227
2328 protected function setUp ()
2429 {
25- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
30+ $ this ->blockRepository = Bootstrap::getObjectManager ()->get (BlockRepositoryInterface::class);
31+ $ this ->filterEmulate = Bootstrap::getObjectManager ()->get (FilterEmulate::class);
2632 }
2733
2834 /**
2935 * Verify the fields of CMS Block selected by identifiers
3036 *
31- * @magentoApiDataFixture Magento/Cms/_files/block .php
37+ * @magentoApiDataFixture Magento/Cms/_files/blocks .php
3238 */
33- public function testGetCmsBlocksByIdentifiers ()
39+ public function testGetCmsBlock ()
3440 {
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 );
41+ $ cmsBlock = $ this ->blockRepository ->getById ('enabled_block ' );
3942 $ cmsBlockData = $ cmsBlock ->getData ();
40- /** @var FilterEmulate $widgetFilter */
41- $ widgetFilter = $ this ->objectManager ->get (FilterEmulate::class);
42- $ renderedContent = $ widgetFilter ->setUseSessionInUrl (false )->filter ($ cmsBlock ->getContent ());
43+ $ renderedContent = $ this ->filterEmulate ->setUseSessionInUrl (false )->filter ($ cmsBlock ->getContent ());
44+
4345 $ query =
4446 <<<QUERY
4547{
46- cmsBlocks(identifiers: "fixture_block ") {
48+ cmsBlocks(identifiers: "enabled_block ") {
4749 items {
4850 identifier
4951 title
@@ -52,34 +54,30 @@ public function testGetCmsBlocksByIdentifiers()
5254 }
5355}
5456QUERY ;
55-
5657 $ 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 ' ]);
58+
59+ self ::assertArrayHasKey ('cmsBlocks ' , $ response );
60+ self ::assertArrayHasKey ('items ' , $ response ['cmsBlocks ' ]);
61+
62+ self ::assertEquals ($ cmsBlockData ['identifier ' ], $ response ['cmsBlocks ' ]['items ' ][0 ]['identifier ' ]);
63+ self ::assertEquals ($ cmsBlockData ['title ' ], $ response ['cmsBlocks ' ]['items ' ][0 ]['title ' ]);
64+ self ::assertEquals ($ renderedContent , $ response ['cmsBlocks ' ]['items ' ][0 ]['content ' ]);
6365 }
6466
6567 /**
6668 * Verify the message when CMS Block is disabled
6769 *
68- * @magentoApiDataFixture Magento/Cms/_files/block.php
70+ * @expectedException \Exception
71+ * @expectedExceptionMessage The CMS block with the "disabled_block" ID doesn't exist
72+ *
73+ * @magentoApiDataFixture Magento/Cms/_files/blocks.php
6974 */
70- public function testGetDisabledCmsBlockByIdentifiers ()
75+ public function testGetDisabledCmsBlock ()
7176 {
72- /** @var StoreManagerInterface $storeManager */
73- $ storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
74- $ storeId = (int )$ storeManager ->getStore ()->getId ();
75- $ cmsBlockId = $ this ->objectManager ->get (GetBlockByIdentifier::class)
76- ->execute ("fixture_block " , $ storeId )
77- ->getId ();
78- $ this ->objectManager ->get (Block::class)->load ($ cmsBlockId )->setIsActive (0 )->save ();
7977 $ query =
8078 <<<QUERY
8179{
82- cmsBlocks(identifiers: "fixture_block ") {
80+ cmsBlocks(identifiers: "disabled_block ") {
8381 items {
8482 identifier
8583 title
@@ -88,16 +86,16 @@ public function testGetDisabledCmsBlockByIdentifiers()
8886 }
8987}
9088QUERY ;
91-
92- $ this ->expectException (\Exception::class);
93- $ this ->expectExceptionMessage ('No such entity. ' );
9489 $ this ->graphQlQuery ($ query );
9590 }
9691
9792 /**
9893 * Verify the message when identifiers were not specified
94+ *
95+ * @expectedException \Exception
96+ * @expectedExceptionMessage "identifiers" of CMS blocks should be specified
9997 */
100- public function testGetCmsBlockBypassingIdentifiers ()
98+ public function testGetCmsBlocksWithoutIdentifiers ()
10199 {
102100 $ query =
103101 <<<QUERY
@@ -111,21 +109,21 @@ public function testGetCmsBlockBypassingIdentifiers()
111109 }
112110}
113111QUERY ;
114-
115- $ this ->expectException (\Exception::class);
116- $ this ->expectExceptionMessage ('"identifiers" of CMS blocks should be specified ' );
117112 $ this ->graphQlQuery ($ query );
118113 }
119114
120115 /**
121116 * Verify the message when CMS Block with such identifiers does not exist
117+ *
118+ * @expectedException \Exception
119+ * @expectedExceptionMessage The CMS block with the "nonexistent_id" ID doesn't exist.
122120 */
123121 public function testGetCmsBlockByNonExistentIdentifier ()
124122 {
125123 $ query =
126124 <<<QUERY
127125{
128- cmsBlocks(identifiers: "0 ") {
126+ cmsBlocks(identifiers: "nonexistent_id ") {
129127 items {
130128 identifier
131129 title
@@ -134,9 +132,39 @@ public function testGetCmsBlockByNonExistentIdentifier()
134132 }
135133}
136134QUERY ;
137-
138- $ this ->expectException (\Exception::class);
139- $ this ->expectExceptionMessage ('The CMS block with the "0" ID doesn \'t exist. ' );
140135 $ this ->graphQlQuery ($ query );
141136 }
137+
138+ /**
139+ * Verify the fields of CMS Block selected by identifiers
140+ *
141+ * @magentoApiDataFixture Magento/Cms/_files/blocks.php
142+ */
143+ public function testGetEnabledAndDisabledCmsBlockInOneRequest ()
144+ {
145+ $ query =
146+ <<<QUERY
147+ {
148+ cmsBlocks(identifiers: ["enabled_block", "disabled_block"]) {
149+ items {
150+ identifier
151+ }
152+ }
153+ }
154+ QUERY ;
155+
156+ try {
157+ $ this ->graphQlQuery ($ query );
158+ self ::fail ('Response should contains errors. ' );
159+ } catch (ResponseContainsErrorsException $ e ) {
160+ $ responseData = $ e ->getResponseData ();
161+ }
162+
163+ self ::assertNotEmpty ($ responseData );
164+ self ::assertEquals ('enabled_block ' , $ responseData ['data ' ]['cmsBlocks ' ]['items ' ][0 ]['identifier ' ]);
165+ self ::assertEquals (
166+ 'The CMS block with the "disabled_block" ID doesn \'t exist. ' ,
167+ $ responseData ['errors ' ][0 ]['message ' ]
168+ );
169+ }
142170}
0 commit comments