Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CmsBlockTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 
17 {
21  private $objectManager;
22 
23  protected function setUp()
24  {
26  }
27 
33  public function testGetCmsBlocksByIdentifiers()
34  {
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();
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 
70  public function testGetDisabledCmsBlockByIdentifiers()
71  {
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  $query =
80  <<<QUERY
81 {
82  cmsBlocks(identifiers: "fixture_block") {
83  items {
84  identifier
85  title
86  content
87  }
88  }
89 }
90 QUERY;
91 
92  $this->expectException(\Exception::class);
93  $this->expectExceptionMessage('No such entity.');
94  $this->graphQlQuery($query);
95  }
96 
101  {
102  $query =
103  <<<QUERY
104 {
105  cmsBlocks(identifiers: []) {
106  items {
107  identifier
108  title
109  content
110  }
111  }
112 }
113 QUERY;
114 
115  $this->expectException(\Exception::class);
116  $this->expectExceptionMessage('"identifiers" of CMS blocks should be specified');
117  $this->graphQlQuery($query);
118  }
119 
124  {
125  $query =
126  <<<QUERY
127 {
128  cmsBlocks(identifiers: "0") {
129  items {
130  identifier
131  title
132  content
133  }
134  }
135 }
136 QUERY;
137 
138  $this->expectException(\Exception::class);
139  $this->expectExceptionMessage('The CMS block with the "0" ID doesn\'t exist.');
140  $this->graphQlQuery($query);
141  }
142 }
$response
Definition: 404.php:11
$storeManager
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])