Skip to content

Commit 5a7a18f

Browse files
author
Stanislav Idolov
authored
🔃 [EngCom] Public Pull Requests - 2.1-develop
Accepted Public Pull Requests: - #17682: [Backport] CMS: Add missing unit tests for model classes (by @dmytro-ch)
2 parents 438d444 + 0a4952d commit 5a7a18f

File tree

1 file changed

+341
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)