InstanceTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * \Magento\Widget\Model\Widget\Instance
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Widget\Test\Unit\Model\Widget;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class InstanceTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Widget\Model\Config\Data|PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $_widgetModelMock;
  19. /**
  20. * @var \Magento\Framework\View\FileSystem|PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $_viewFileSystemMock;
  23. /** @var \Magento\Widget\Model\NamespaceResolver |PHPUnit_Framework_MockObject_MockObject */
  24. protected $_namespaceResolver;
  25. /**
  26. * @var \Magento\Widget\Model\Widget\Instance
  27. */
  28. protected $_model;
  29. /** @var \Magento\Widget\Model\Config\Reader */
  30. protected $_readerMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $_cacheTypesListMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $_directoryMock;
  39. /** @var \Magento\Framework\Serialize\Serializer\Json | \PHPUnit_Framework_MockObject_MockObject */
  40. private $serializer;
  41. protected function setUp()
  42. {
  43. $this->_widgetModelMock = $this->getMockBuilder(
  44. \Magento\Widget\Model\Widget::class
  45. )->disableOriginalConstructor()->getMock();
  46. $this->_viewFileSystemMock = $this->getMockBuilder(
  47. \Magento\Framework\View\FileSystem::class
  48. )->disableOriginalConstructor()->getMock();
  49. $this->_namespaceResolver = $this->getMockBuilder(
  50. \Magento\Widget\Model\NamespaceResolver::class
  51. )->disableOriginalConstructor()->getMock();
  52. $this->_cacheTypesListMock = $this->createMock(\Magento\Framework\App\Cache\TypeListInterface::class);
  53. $this->_readerMock = $this->getMockBuilder(
  54. \Magento\Widget\Model\Config\Reader::class
  55. )->disableOriginalConstructor()->getMock();
  56. $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  57. $this->_directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  58. $filesystemMock->expects(
  59. $this->any()
  60. )->method(
  61. 'getDirectoryRead'
  62. )->will(
  63. $this->returnValue($this->_directoryMock)
  64. );
  65. $this->_directoryMock->expects($this->any())->method('isReadable')->will($this->returnArgument(0));
  66. $this->_directoryMock->expects($this->any())->method('getRelativePath')->will($this->returnArgument(0));
  67. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  68. $this->serializer = $this->createMock(Json::class);
  69. $args = $objectManagerHelper->getConstructArguments(
  70. \Magento\Widget\Model\Widget\Instance::class,
  71. [
  72. 'filesystem' => $filesystemMock,
  73. 'viewFileSystem' => $this->_viewFileSystemMock,
  74. 'cacheTypeList' => $this->_cacheTypesListMock,
  75. 'reader' => $this->_readerMock,
  76. 'widgetModel' => $this->_widgetModelMock,
  77. 'namespaceResolver' => $this->_namespaceResolver,
  78. 'serializer' => $this->serializer,
  79. ]
  80. );
  81. /** @var \Magento\Widget\Model\Widget\Instance _model */
  82. $this->_model = $this->getMockBuilder(\Magento\Widget\Model\Widget\Instance::class)
  83. ->setMethods(['_construct'])
  84. ->setConstructorArgs($args)
  85. ->getMock();
  86. }
  87. public function testGetWidgetConfigAsArray()
  88. {
  89. $widget = [
  90. '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
  91. 'name' => 'CMS Page Link',
  92. 'description' => 'Link to a CMS Page',
  93. 'is_email_compatible' => 'true',
  94. 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
  95. 'parameters' => [
  96. 'page_id' => [
  97. '@' => ['type' => 'complex'],
  98. 'type' => 'label',
  99. 'helper_block' => [
  100. 'type' => \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser::class,
  101. 'data' => ['button' => ['open' => 'Select Page...']],
  102. ],
  103. 'visible' => 'true',
  104. 'required' => 'true',
  105. 'sort_order' => '10',
  106. 'label' => 'CMS Page',
  107. ],
  108. ],
  109. ];
  110. $this->_widgetModelMock->expects(
  111. $this->once()
  112. )->method(
  113. 'getWidgetByClassType'
  114. )->will(
  115. $this->returnValue($widget)
  116. );
  117. $xmlFile = __DIR__ . '/../_files/widget.xml';
  118. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue($xmlFile));
  119. $themeConfigFile = __DIR__ . '/../_files/mappedConfigArrayAll.php';
  120. $themeConfig = include $themeConfigFile;
  121. $this->_readerMock->expects(
  122. $this->once()
  123. )->method(
  124. 'readFile'
  125. )->with(
  126. $this->equalTo($xmlFile)
  127. )->will(
  128. $this->returnValue($themeConfig)
  129. );
  130. $result = $this->_model->getWidgetConfigAsArray();
  131. $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
  132. $expectedConfig = include $expectedConfigFile;
  133. $this->assertEquals($expectedConfig, $result);
  134. }
  135. public function testGetWidgetTemplates()
  136. {
  137. $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
  138. $widget = include $expectedConfigFile;
  139. $this->_widgetModelMock->expects(
  140. $this->once()
  141. )->method(
  142. 'getWidgetByClassType'
  143. )->will(
  144. $this->returnValue($widget)
  145. );
  146. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  147. $expectedTemplates = [
  148. 'default' => [
  149. 'value' => 'product/widget/link/link_block.phtml',
  150. 'label' => 'Product Link Block Template',
  151. ],
  152. 'link_inline' => [
  153. 'value' => 'product/widget/link/link_inline.phtml',
  154. 'label' => 'Product Link Inline Template',
  155. ],
  156. ];
  157. $this->assertEquals($expectedTemplates, $this->_model->getWidgetTemplates());
  158. }
  159. public function testGetWidgetTemplatesValueOnly()
  160. {
  161. $widget = [
  162. '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
  163. 'name' => 'CMS Page Link',
  164. 'description' => 'Link to a CMS Page',
  165. 'is_email_compatible' => 'true',
  166. 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
  167. 'parameters' => [
  168. 'template' => [
  169. 'values' => [
  170. 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
  171. ],
  172. 'type' => 'select',
  173. 'visible' => 'true',
  174. 'label' => 'Template',
  175. 'value' => 'product/widget/link/link_block.phtml',
  176. ],
  177. ],
  178. ];
  179. $this->_widgetModelMock->expects(
  180. $this->once()
  181. )->method(
  182. 'getWidgetByClassType'
  183. )->will(
  184. $this->returnValue($widget)
  185. );
  186. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  187. $expectedTemplates = [
  188. 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
  189. ];
  190. $this->assertEquals($expectedTemplates, $this->_model->getWidgetTemplates());
  191. }
  192. public function testGetWidgetTemplatesNoTemplate()
  193. {
  194. $widget = [
  195. '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
  196. 'name' => 'CMS Page Link',
  197. 'description' => 'Link to a CMS Page',
  198. 'is_email_compatible' => 'true',
  199. 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
  200. 'parameters' => [],
  201. ];
  202. $this->_widgetModelMock->expects(
  203. $this->once()
  204. )->method(
  205. 'getWidgetByClassType'
  206. )->will(
  207. $this->returnValue($widget)
  208. );
  209. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  210. $expectedTemplates = [];
  211. $this->assertEquals($expectedTemplates, $this->_model->getWidgetTemplates());
  212. }
  213. public function testGetWidgetSupportedContainers()
  214. {
  215. $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
  216. $widget = include $expectedConfigFile;
  217. $this->_widgetModelMock->expects(
  218. $this->once()
  219. )->method(
  220. 'getWidgetByClassType'
  221. )->will(
  222. $this->returnValue($widget)
  223. );
  224. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  225. $expectedContainers = ['left', 'content'];
  226. $this->assertEquals($expectedContainers, $this->_model->getWidgetSupportedContainers());
  227. }
  228. public function testGetWidgetSupportedContainersNoContainer()
  229. {
  230. $widget = [
  231. '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
  232. 'name' => 'CMS Page Link',
  233. 'description' => 'Link to a CMS Page',
  234. 'is_email_compatible' => 'true',
  235. 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
  236. ];
  237. $this->_widgetModelMock->expects(
  238. $this->once()
  239. )->method(
  240. 'getWidgetByClassType'
  241. )->will(
  242. $this->returnValue($widget)
  243. );
  244. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  245. $expectedContainers = [];
  246. $this->assertEquals($expectedContainers, $this->_model->getWidgetSupportedContainers());
  247. }
  248. public function testGetWidgetSupportedTemplatesByContainers()
  249. {
  250. $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
  251. $widget = include $expectedConfigFile;
  252. $this->_widgetModelMock->expects(
  253. $this->once()
  254. )->method(
  255. 'getWidgetByClassType'
  256. )->will(
  257. $this->returnValue($widget)
  258. );
  259. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  260. $expectedTemplates = [
  261. ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Product Link Block Template'],
  262. ['value' => 'product/widget/link/link_inline.phtml', 'label' => 'Product Link Inline Template'],
  263. ];
  264. $this->assertEquals($expectedTemplates, $this->_model->getWidgetSupportedTemplatesByContainer('left'));
  265. }
  266. public function testGetWidgetSupportedTemplatesByContainers2()
  267. {
  268. $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
  269. $widget = include $expectedConfigFile;
  270. $this->_widgetModelMock->expects(
  271. $this->once()
  272. )->method(
  273. 'getWidgetByClassType'
  274. )->will(
  275. $this->returnValue($widget)
  276. );
  277. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  278. $expectedTemplates = [
  279. ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Product Link Block Template'],
  280. ];
  281. $this->assertEquals($expectedTemplates, $this->_model->getWidgetSupportedTemplatesByContainer('content'));
  282. }
  283. public function testGetWidgetSupportedTemplatesByContainersNoSupportedContainersSpecified()
  284. {
  285. $widget = [
  286. '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
  287. 'name' => 'CMS Page Link',
  288. 'description' => 'Link to a CMS Page',
  289. 'is_email_compatible' => 'true',
  290. 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
  291. 'parameters' => [
  292. 'template' => [
  293. 'values' => [
  294. 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
  295. ],
  296. 'type' => 'select',
  297. 'visible' => 'true',
  298. 'label' => 'Template',
  299. 'value' => 'product/widget/link/link_block.phtml',
  300. ],
  301. ],
  302. ];
  303. $this->_widgetModelMock->expects(
  304. $this->once()
  305. )->method(
  306. 'getWidgetByClassType'
  307. )->will(
  308. $this->returnValue($widget)
  309. );
  310. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  311. $expectedContainers = [
  312. 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
  313. ];
  314. $this->assertEquals($expectedContainers, $this->_model->getWidgetSupportedTemplatesByContainer('content'));
  315. }
  316. public function testGetWidgetSupportedTemplatesByContainersUnknownContainer()
  317. {
  318. $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
  319. $widget = include $expectedConfigFile;
  320. $this->_widgetModelMock->expects(
  321. $this->once()
  322. )->method(
  323. 'getWidgetByClassType'
  324. )->will(
  325. $this->returnValue($widget)
  326. );
  327. $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
  328. $expectedTemplates = [];
  329. $this->assertEquals($expectedTemplates, $this->_model->getWidgetSupportedTemplatesByContainer('unknown'));
  330. }
  331. public function testGetWidgetParameters()
  332. {
  333. $serializedArray = '{"anchor_text":"232323232323232323","title":"232323232323232","page_id":"2"}';
  334. $this->serializer->expects($this->once())
  335. ->method('unserialize')
  336. ->willReturn(json_decode($serializedArray, true));
  337. $this->_model->setData('widget_parameters', $serializedArray);
  338. $this->assertEquals(
  339. json_decode($serializedArray, true),
  340. $this->_model->getWidgetParameters()
  341. );
  342. }
  343. public function testBeforeSave()
  344. {
  345. $widgetParameters = [
  346. 'anchor_text' => 'Test',
  347. 'title' => 'Test',
  348. 'page_id' => '2'
  349. ];
  350. $this->serializer->expects($this->once())
  351. ->method('serialize')
  352. ->willReturn(json_encode($widgetParameters));
  353. $this->_model->setData('widget_parameters', $widgetParameters);
  354. $this->_model->beforeSave();
  355. }
  356. }