WidgetTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Test class for \Magento\Widget\Model\Widget
  10. */
  11. class WidgetTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Widget\Model\Config\Data|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $dataStorageMock;
  17. /**
  18. * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $escaperMock;
  21. /**
  22. * @var \Magento\Widget\Model\Widget
  23. */
  24. protected $widget;
  25. /**
  26. * @var \Magento\Widget\Helper\Conditions
  27. */
  28. private $conditionsHelper;
  29. /**
  30. * @inheritdoc
  31. */
  32. protected function setUp()
  33. {
  34. $this->dataStorageMock = $this->getMockBuilder(\Magento\Widget\Model\Config\Data::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->conditionsHelper = $this->getMockBuilder(\Magento\Widget\Helper\Conditions::class)
  38. ->setMethods(['encode'])
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  45. $this->widget = $objectManagerHelper->getObject(
  46. \Magento\Widget\Model\Widget::class,
  47. [
  48. 'dataStorage' => $this->dataStorageMock,
  49. 'conditionsHelper' => $this->conditionsHelper,
  50. 'escaper' => $this->escaperMock,
  51. ]
  52. );
  53. }
  54. /**
  55. * Unit test for getWidget
  56. */
  57. public function testGetWidgets()
  58. {
  59. $expected = ['val1', 'val2'];
  60. $this->dataStorageMock->expects($this->once())
  61. ->method('get')
  62. ->willReturn($expected);
  63. $result = $this->widget->getWidgets();
  64. $this->assertEquals($expected, $result);
  65. }
  66. /**
  67. * Unit test for getWidgetsWithFilter
  68. */
  69. public function testGetWidgetsWithFilter()
  70. {
  71. $configFile = __DIR__ . '/_files/mappedConfigArrayAll.php';
  72. $widgets = include $configFile;
  73. $this->dataStorageMock->expects($this->once())
  74. ->method('get')
  75. ->willReturn($widgets);
  76. $result = $this->widget->getWidgets(['name' => 'CMS Page Link', 'description' => 'Link to a CMS Page']);
  77. $configFileOne = __DIR__ . '/_files/mappedConfigArray1.php';
  78. $expected = ['cms_page_link' => include $configFileOne];
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * Unit test for getWidgetsWithUnknownFilter
  83. */
  84. public function testGetWidgetsWithUnknownFilter()
  85. {
  86. $configFile = __DIR__ . '/_files/mappedConfigArrayAll.php';
  87. $widgets = include $configFile;
  88. $this->dataStorageMock->expects($this->once())
  89. ->method('get')
  90. ->willReturn($widgets);
  91. $result = $this->widget->getWidgets(['name' => 'unknown', 'description' => 'unknown']);
  92. $expected = [];
  93. $this->assertEquals($expected, $result);
  94. }
  95. /**
  96. * Unit test for getWidgetByClassType
  97. */
  98. public function testGetWidgetByClassType()
  99. {
  100. $widgetOne = ['@' => ['type' => 'type1']];
  101. $widgets = ['widget1' => $widgetOne];
  102. $this->dataStorageMock->expects($this->any())
  103. ->method('get')
  104. ->willReturn($widgets);
  105. $this->assertEquals($widgetOne, $this->widget->getWidgetByClassType('type1'));
  106. $this->assertNull($this->widget->getWidgetByClassType('type2'));
  107. }
  108. /**
  109. * Unit test for getConfigAsObject
  110. */
  111. public function testGetConfigAsObject()
  112. {
  113. $configFile = __DIR__ . '/_files/mappedConfigArrayAll.php';
  114. $widgets = include $configFile;
  115. $this->dataStorageMock->expects($this->once())
  116. ->method('get')
  117. ->willReturn($widgets);
  118. $resultObject = $this->widget->getConfigAsObject(\Magento\Cms\Block\Widget\Page\Link::class);
  119. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $resultObject);
  120. $this->assertSame('CMS Page Link', $resultObject->getName());
  121. $this->assertSame('Link to a CMS Page', $resultObject->getDescription());
  122. $this->assertSame('1', $resultObject->getIsEmailCompatible());
  123. $this->assertSame('Magento_Cms::images/widget_page_link.png', $resultObject->getPlaceholderImage());
  124. $resultParameters = $resultObject->getParameters();
  125. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $resultParameters['page_id']);
  126. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $resultParameters['anchor_text']);
  127. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $resultParameters['template']);
  128. $supportedContainersExpected = [
  129. '0' => [
  130. 'container_name' => 'left',
  131. 'template' => ['default' => 'default', 'names_only' => 'link_inline'],
  132. ],
  133. '1' => [
  134. 'container_name' => 'content',
  135. 'template' => ['grid' => 'default', 'list' => 'list']
  136. ],
  137. ];
  138. $this->assertSame($supportedContainersExpected, $resultObject->getSupportedContainers());
  139. }
  140. /**
  141. * Unit test for getConfigAsObjectWidgetNoFound
  142. */
  143. public function testGetConfigAsObjectWidgetNoFound()
  144. {
  145. $this->dataStorageMock->expects($this->once())
  146. ->method('get')
  147. ->willReturn([]);
  148. $resultObject = $this->widget->getConfigAsObject(\Magento\Cms\Block\Widget\Page\Link::class);
  149. $this->assertInstanceOf(\Magento\Framework\DataObject::class, $resultObject);
  150. $this->assertSame([], $resultObject->getData());
  151. }
  152. /**
  153. * Unit test for getWidgetDeclaration
  154. */
  155. public function testGetWidgetDeclaration()
  156. {
  157. $mathRandomMock = $this->createPartialMock(\Magento\Framework\Math\Random::class, ['getRandomString']);
  158. $mathRandomMock->expects($this->any())->method('getRandomString')->willReturn('asdf');
  159. $reflection = new \ReflectionClass(get_class($this->widget));
  160. $reflectionProperty = $reflection->getProperty('mathRandom');
  161. $reflectionProperty->setAccessible(true);
  162. $reflectionProperty->setValue($this->widget, $mathRandomMock);
  163. $conditions = [
  164. [
  165. 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
  166. 'aggregator' => 'all',
  167. 'value' => '1',
  168. 'new_child' => ''
  169. ]
  170. ];
  171. $params = [
  172. 'title' => 'my "widget"',
  173. 'show_pager' => '1',
  174. 'products_per_page' => '5',
  175. 'products_count' => '10',
  176. 'template' => 'Magento_CatalogWidget::product/widget/content/grid.phtml',
  177. 'conditions' => $conditions
  178. ];
  179. $this->conditionsHelper->expects($this->once())->method('encode')->with($conditions)
  180. ->willReturn('encoded-conditions-string');
  181. $this->escaperMock->expects($this->atLeastOnce())
  182. ->method('escapeHtmlAttr')
  183. ->willReturnMap([
  184. ['my "widget"', false, 'my &quot;widget&quot;'],
  185. ['1', false, '1'],
  186. ['5', false, '5'],
  187. ['10', false, '10'],
  188. ['Magento_CatalogWidget::product/widget/content/grid.phtml',
  189. false,
  190. 'Magento_CatalogWidget::product/widget/content/grid.phtml'
  191. ],
  192. ['encoded-conditions-string', false, 'encoded-conditions-string'],
  193. ]);
  194. $this->dataStorageMock->expects($this->once())
  195. ->method('get')
  196. ->willReturn([]);
  197. $result = $this->widget->getWidgetDeclaration(
  198. \Magento\CatalogWidget\Block\Product\ProductsList::class,
  199. $params
  200. );
  201. $this->assertContains('{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"', $result);
  202. $this->assertContains('title="my &quot;widget&quot;"', $result);
  203. $this->assertContains('conditions_encoded="encoded-conditions-string"', $result);
  204. $this->assertContains('page_var_name="pasdf"', $result);
  205. $this->assertContains('type_name=""}}', $result);
  206. }
  207. /**
  208. * Unit test for getWidgetDeclarationWithZeroValueParam
  209. */
  210. public function testGetWidgetDeclarationWithZeroValueParam()
  211. {
  212. $mathRandomMock = $this->createPartialMock(\Magento\Framework\Math\Random::class, ['getRandomString']);
  213. $mathRandomMock->expects($this->any())
  214. ->method('getRandomString')
  215. ->willReturn('asdf');
  216. (new ObjectManager($this))->setBackwardCompatibleProperty(
  217. $this->widget,
  218. 'mathRandom',
  219. $mathRandomMock
  220. );
  221. $conditions = [
  222. [
  223. 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
  224. 'aggregator' => 'all',
  225. 'value' => '1',
  226. 'new_child' => ''
  227. ]
  228. ];
  229. $params = [
  230. 'title' => 'my widget',
  231. 'show_pager' => '1',
  232. 'products_per_page' => '5',
  233. 'products_count' => '0',
  234. 'template' => 'Magento_CatalogWidget::product/widget/content/grid.phtml',
  235. 'conditions' => $conditions
  236. ];
  237. $this->conditionsHelper->expects($this->once())
  238. ->method('encode')
  239. ->with($conditions)
  240. ->willReturn('encoded-conditions-string');
  241. $this->dataStorageMock->expects($this->once())
  242. ->method('get')
  243. ->willReturn([]);
  244. $result = $this->widget->getWidgetDeclaration(
  245. \Magento\CatalogWidget\Block\Product\ProductsList::class,
  246. $params
  247. );
  248. $this->assertContains('{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"', $result);
  249. $this->assertContains('page_var_name="pasdf"', $result);
  250. $this->assertContains('type_name=""}}', $result);
  251. $this->assertContains('products_count=""', $result);
  252. }
  253. }