Skip to content

Commit bc8be5d

Browse files
ENGCOM-2836: [Forwardport] CMS: Add missing unit tests for model classes #17681
- Merge Pull Request #17681 from dmytro-ch/magento2:2.3-unit-test-cms-model-block - Merged commits: 1. 26fd773
2 parents 6d3dab2 + 26fd773 commit bc8be5d

File tree

1 file changed

+337
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)