7
7
8
8
namespace Magento \GraphQl \Cms ;
9
9
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 ;
13
13
use Magento \TestFramework \TestCase \GraphQlAbstract ;
14
14
use Magento \Widget \Model \Template \FilterEmulate ;
15
15
16
16
class CmsBlockTest extends GraphQlAbstract
17
17
{
18
18
/**
19
- * @var \Magento\TestFramework\ObjectManager
19
+ * @var BlockRepositoryInterface
20
20
*/
21
- private $ objectManager ;
21
+ private $ blockRepository ;
22
+
23
+ /**
24
+ * @var FilterEmulate
25
+ */
26
+ private $ filterEmulate ;
22
27
23
28
protected function setUp ()
24
29
{
25
- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
30
+ $ this ->blockRepository = Bootstrap::getObjectManager ()->get (BlockRepositoryInterface::class);
31
+ $ this ->filterEmulate = Bootstrap::getObjectManager ()->get (FilterEmulate::class);
26
32
}
27
33
28
34
/**
29
35
* Verify the fields of CMS Block selected by identifiers
30
36
*
31
- * @magentoApiDataFixture Magento/Cms/_files/block .php
37
+ * @magentoApiDataFixture Magento/Cms/_files/blocks .php
32
38
*/
33
- public function testGetCmsBlocksByIdentifiers ()
39
+ public function testGetCmsBlock ()
34
40
{
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 ' );
39
42
$ 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
+
43
45
$ query =
44
46
<<<QUERY
45
47
{
46
- cmsBlocks(identifiers: "fixture_block ") {
48
+ cmsBlocks(identifiers: "enabled_block ") {
47
49
items {
48
50
identifier
49
51
title
@@ -52,34 +54,30 @@ public function testGetCmsBlocksByIdentifiers()
52
54
}
53
55
}
54
56
QUERY ;
55
-
56
57
$ 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 ' ]);
63
65
}
64
66
65
67
/**
66
68
* Verify the message when CMS Block is disabled
67
69
*
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
69
74
*/
70
- public function testGetDisabledCmsBlockByIdentifiers ()
75
+ public function testGetDisabledCmsBlock ()
71
76
{
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 ();
79
77
$ query =
80
78
<<<QUERY
81
79
{
82
- cmsBlocks(identifiers: "fixture_block ") {
80
+ cmsBlocks(identifiers: "disabled_block ") {
83
81
items {
84
82
identifier
85
83
title
@@ -88,16 +86,16 @@ public function testGetDisabledCmsBlockByIdentifiers()
88
86
}
89
87
}
90
88
QUERY ;
91
-
92
- $ this ->expectException (\Exception::class);
93
- $ this ->expectExceptionMessage ('No such entity. ' );
94
89
$ this ->graphQlQuery ($ query );
95
90
}
96
91
97
92
/**
98
93
* Verify the message when identifiers were not specified
94
+ *
95
+ * @expectedException \Exception
96
+ * @expectedExceptionMessage "identifiers" of CMS blocks should be specified
99
97
*/
100
- public function testGetCmsBlockBypassingIdentifiers ()
98
+ public function testGetCmsBlocksWithoutIdentifiers ()
101
99
{
102
100
$ query =
103
101
<<<QUERY
@@ -111,21 +109,21 @@ public function testGetCmsBlockBypassingIdentifiers()
111
109
}
112
110
}
113
111
QUERY ;
114
-
115
- $ this ->expectException (\Exception::class);
116
- $ this ->expectExceptionMessage ('"identifiers" of CMS blocks should be specified ' );
117
112
$ this ->graphQlQuery ($ query );
118
113
}
119
114
120
115
/**
121
116
* 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.
122
120
*/
123
121
public function testGetCmsBlockByNonExistentIdentifier ()
124
122
{
125
123
$ query =
126
124
<<<QUERY
127
125
{
128
- cmsBlocks(identifiers: "0 ") {
126
+ cmsBlocks(identifiers: "nonexistent_id ") {
129
127
items {
130
128
identifier
131
129
title
@@ -134,9 +132,39 @@ public function testGetCmsBlockByNonExistentIdentifier()
134
132
}
135
133
}
136
134
QUERY ;
137
-
138
- $ this ->expectException (\Exception::class);
139
- $ this ->expectExceptionMessage ('The CMS block with the "0" ID doesn \'t exist. ' );
140
135
$ this ->graphQlQuery ($ query );
141
136
}
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
+ }
142
170
}
0 commit comments