BlockTest.php 8.4 KB

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