Skip to content

Commit bce1dfe

Browse files
committed
CMS: Add unit test for block model class
1 parent 8858fea commit bce1dfe

File tree

1 file changed

+342
-0
lines changed

1 file changed

+342
-0
lines changed
Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
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\Cms\Test\Unit\Model;
9+
10+
use Magento\Cms\Model\Block;
11+
use Magento\Cms\Model\ResourceModel\Block as BlockResource;
12+
use Magento\Framework\Event\ManagerInterface;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Model\Context;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
17+
/**
18+
* @covers \Magento\Cms\Model\Block
19+
*/
20+
class BlockTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* Testable Object
24+
*
25+
* @var Block
26+
*/
27+
private $blockModel;
28+
29+
/**
30+
* Object Manager
31+
*
32+
* @var ObjectManager
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $eventManagerMock;
40+
41+
/**
42+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $contextMock;
45+
46+
/**
47+
* @var BlockResource|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $resourceMock;
50+
51+
/**
52+
* Set Up
53+
*
54+
* @return void
55+
*/
56+
protected function setUp()
57+
{
58+
$this->resourceMock = $this->getMockBuilder(BlockResource::class)->disableOriginalConstructor()
59+
->getMock();
60+
$this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)->disableOriginalConstructor()
61+
->getMock();
62+
$this->contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()
63+
->getMock();
64+
$this->contextMock->expects($this->any())->method('getEventDispatcher')->willReturn($this->eventManagerMock);
65+
$this->objectManager = new ObjectManager($this);
66+
$this->blockModel = $this->objectManager->getObject(
67+
Block::class,
68+
[
69+
'context' => $this->contextMock,
70+
'resource' => $this->resourceMock,
71+
]
72+
);;
73+
}
74+
75+
/**
76+
* Test beforeSave method
77+
*
78+
* @return void
79+
*
80+
* @throws LocalizedException
81+
*/
82+
public function testBeforeSave()
83+
{
84+
$blockId = 7;
85+
$this->blockModel->setData(Block::BLOCK_ID, $blockId);
86+
$this->blockModel->setData(Block::CONTENT, 'test');
87+
$this->objectManager->setBackwardCompatibleProperty($this->blockModel, '_hasDataChanges', true);
88+
$this->eventManagerMock->expects($this->atLeastOnce())->method('dispatch');
89+
90+
$expected = $this->blockModel;
91+
$actual = $this->blockModel->beforeSave();
92+
self::assertEquals($expected, $actual);
93+
}
94+
95+
/**
96+
* Test beforeSave method
97+
*
98+
* @return void
99+
*
100+
* @throws LocalizedException
101+
*/
102+
public function testBeforeSaveWithException()
103+
{
104+
$blockId = 10;
105+
$this->blockModel->setData(Block::BLOCK_ID, $blockId);
106+
$this->blockModel->setData(Block::CONTENT, 'Test block_id="' . $blockId . '".');
107+
$this->objectManager->setBackwardCompatibleProperty($this->blockModel, '_hasDataChanges', false);
108+
$this->eventManagerMock->expects($this->never())->method('dispatch');
109+
$this->setExpectedException(LocalizedException::class);
110+
$this->blockModel->beforeSave();
111+
}
112+
113+
/**
114+
* Test getIdentities method
115+
*
116+
* @return void
117+
*/
118+
public function testGetIdentities()
119+
{
120+
$result = $this->blockModel->getIdentities();
121+
self::assertInternalType('array', $result);
122+
}
123+
124+
/**
125+
* Test getId method
126+
*
127+
* @return void
128+
*/
129+
public function testGetId()
130+
{
131+
$blockId = 12;
132+
$this->blockModel->setData(Block::BLOCK_ID, $blockId);
133+
$expected = $blockId;
134+
$actual = $this->blockModel->getId();
135+
self::assertEquals($expected, $actual);
136+
}
137+
138+
/**
139+
* Test getIdentifier method
140+
*
141+
* @return void
142+
*/
143+
public function testGetIdentifier()
144+
{
145+
$identifier = 'test01';
146+
$this->blockModel->setData(Block::IDENTIFIER, $identifier);
147+
148+
$expected = $identifier;
149+
$actual = $this->blockModel->getIdentifier();
150+
self::assertEquals($expected, $actual);
151+
}
152+
153+
/**
154+
* Test getTitle method
155+
*
156+
* @return void
157+
*/
158+
public function testGetTitle()
159+
{
160+
$title = 'test02';
161+
$this->blockModel->setData(Block::TITLE, $title);
162+
$expected = $title;
163+
$actual = $this->blockModel->getTitle();
164+
self::assertEquals($expected, $actual);
165+
}
166+
167+
/**
168+
* Test getContent method
169+
*
170+
* @return void
171+
*/
172+
public function testGetContent()
173+
{
174+
$content = 'test03';
175+
$this->blockModel->setData(Block::CONTENT, $content);
176+
$expected = $content;
177+
$actual = $this->blockModel->getContent();
178+
self::assertEquals($expected, $actual);
179+
}
180+
181+
/**
182+
* Test getCreationTime method
183+
*
184+
* @return void
185+
*/
186+
public function testGetCreationTime()
187+
{
188+
$creationTime = 'test04';
189+
$this->blockModel->setData(Block::CREATION_TIME, $creationTime);
190+
$expected = $creationTime;
191+
$actual = $this->blockModel->getCreationTime();
192+
self::assertEquals($expected, $actual);
193+
}
194+
195+
/**
196+
* Test getUpdateTime method
197+
*
198+
* @return void
199+
*/
200+
public function testGetUpdateTime()
201+
{
202+
$updateTime = 'test05';
203+
$this->blockModel->setData(Block::UPDATE_TIME, $updateTime);
204+
$expected = $updateTime;
205+
$actual = $this->blockModel->getUpdateTime();
206+
self::assertEquals($expected, $actual);
207+
}
208+
209+
/**
210+
* Test isActive method
211+
*
212+
* @return void
213+
*/
214+
public function testIsActive()
215+
{
216+
$isActive = true;
217+
$this->blockModel->setData(Block::IS_ACTIVE, $isActive);
218+
$result = $this->blockModel->isActive();
219+
self::assertTrue($result);
220+
}
221+
222+
/**
223+
* Test setId method
224+
*
225+
* @return void
226+
*/
227+
public function testSetId()
228+
{
229+
$blockId = 15;
230+
$this->blockModel->setId($blockId);
231+
$expected = $blockId;
232+
$actual = $this->blockModel->getData(Block::BLOCK_ID);
233+
self::assertEquals($expected, $actual);
234+
}
235+
236+
/**
237+
* Test setIdentifier method
238+
*
239+
* @return void
240+
*/
241+
public function testSetIdentifier()
242+
{
243+
$identifier = 'test06';
244+
$this->blockModel->setIdentifier($identifier);
245+
$expected = $identifier;
246+
$actual = $this->blockModel->getData(Block::IDENTIFIER);
247+
self::assertEquals($expected, $actual);
248+
}
249+
250+
/**
251+
* Test setTitle method
252+
*
253+
* @return void
254+
*/
255+
public function testSetTitle()
256+
{
257+
$title = 'test07';
258+
$this->blockModel->setTitle($title);
259+
$expected = $title;
260+
$actual = $this->blockModel->getData(Block::TITLE);
261+
self::assertEquals($expected, $actual);
262+
}
263+
264+
/**
265+
* Test setContent method
266+
*
267+
* @return void
268+
*/
269+
public function testSetContent()
270+
{
271+
$content = 'test08';
272+
$this->blockModel->setContent($content);
273+
$expected = $content;
274+
$actual = $this->blockModel->getData(Block::CONTENT);
275+
self::assertEquals($expected, $actual);
276+
}
277+
278+
/**
279+
* Test setCreationTime method
280+
*
281+
* @return void
282+
*/
283+
public function testSetCreationTime()
284+
{
285+
$creationTime = 'test09';
286+
$this->blockModel->setCreationTime($creationTime);
287+
$expected = $creationTime;
288+
$actual = $this->blockModel->getData(Block::CREATION_TIME);
289+
self::assertEquals($expected, $actual);
290+
}
291+
292+
/**
293+
* Test setUpdateTime method
294+
*
295+
* @return void
296+
*/
297+
public function testSetUpdateTime()
298+
{
299+
$updateTime = 'test10';
300+
$this->blockModel->setUpdateTime($updateTime);
301+
$expected = $updateTime;
302+
$actual = $this->blockModel->getData(Block::UPDATE_TIME);
303+
self::assertEquals($expected, $actual);
304+
}
305+
306+
/**
307+
* Test setIsActive method
308+
*
309+
* @return void
310+
*/
311+
public function testSetIsActive()
312+
{
313+
$this->blockModel->setIsActive(false);
314+
$result = $this->blockModel->getData(Block::IS_ACTIVE);
315+
self::assertFalse($result);
316+
}
317+
318+
/**
319+
* Test getStores method
320+
*
321+
* @return void
322+
*/
323+
public function testGetStores()
324+
{
325+
$stores = [1, 4, 9];
326+
$this->blockModel->setData('stores', $stores);
327+
$expected = $stores;
328+
$actual = $this->blockModel->getStores();
329+
self::assertEquals($expected, $actual);
330+
}
331+
332+
/**
333+
* Test getAvailableStatuses method
334+
*
335+
* @return void
336+
*/
337+
public function testGetAvailableStatuses()
338+
{
339+
$result = $this->blockModel->getAvailableStatuses();
340+
self::assertInternalType('array', $result);
341+
}
342+
}

0 commit comments

Comments
 (0)