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