33 private $conditionsHelper;
37 $this->dataStorageMock = $this->getMockBuilder(\
Magento\Widget\Model\Config\Data::class)
38 ->disableOriginalConstructor()
40 $this->conditionsHelper = $this->getMockBuilder(\
Magento\Widget\Helper\Conditions::class)
41 ->setMethods([
'encode'])
42 ->disableOriginalConstructor()
44 $this->escaperMock = $this->getMockBuilder(\
Magento\Framework\Escaper::class)
45 ->disableOriginalConstructor()
47 $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48 $this->widget = $objectManagerHelper->getObject(
49 \
Magento\Widget\Model\Widget::class,
51 'dataStorage' => $this->dataStorageMock,
52 'conditionsHelper' => $this->conditionsHelper,
53 'escaper' => $this->escaperMock,
60 $expected = [
'val1',
'val2'];
61 $this->dataStorageMock->expects($this->once())
63 ->willReturn($expected);
64 $result = $this->widget->getWidgets();
65 $this->assertEquals($expected,
$result);
70 $configFile =
__DIR__ .
'/_files/mappedConfigArrayAll.php';
71 $widgets = include $configFile;
72 $this->dataStorageMock->expects($this->once())
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);
83 $configFile =
__DIR__ .
'/_files/mappedConfigArrayAll.php';
84 $widgets = include $configFile;
85 $this->dataStorageMock->expects($this->once())
87 ->willReturn($widgets);
88 $result = $this->widget->getWidgets([
'name' =>
'unknown',
'description' =>
'unknown']);
90 $this->assertEquals($expected,
$result);
95 $widgetOne = [
'@' => [
'type' =>
'type1']];
96 $widgets = [
'widget1' => $widgetOne];
97 $this->dataStorageMock->expects($this->any())
99 ->willReturn($widgets);
100 $this->assertEquals($widgetOne, $this->widget->getWidgetByClassType(
'type1'));
101 $this->assertNull($this->widget->getWidgetByClassType(
'type2'));
106 $configFile =
__DIR__ .
'/_files/mappedConfigArrayAll.php';
107 $widgets = include $configFile;
108 $this->dataStorageMock->expects($this->once())
110 ->willReturn($widgets);
112 $resultObject = $this->widget->getConfigAsObject(\
Magento\Cms\Block\Widget\Page\Link::class);
113 $this->assertInstanceOf(\
Magento\Framework\DataObject::class, $resultObject);
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());
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']);
125 $supportedContainersExpected = [
127 'container_name' =>
'left',
128 'template' => [
'default' =>
'default',
'names_only' =>
'link_inline'],
131 'container_name' =>
'content',
132 'template' => [
'grid' =>
'default',
'list' =>
'list']
135 $this->assertSame($supportedContainersExpected, $resultObject->getSupportedContainers());
140 $this->dataStorageMock->expects($this->once())
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());
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);
160 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
161 'aggregator' =>
'all',
167 'title' =>
'my "widget"',
169 'products_per_page' =>
'5',
170 'products_count' =>
'10',
171 'template' =>
'Magento_CatalogWidget::product/widget/content/grid.phtml',
172 'conditions' => $conditions
175 $this->conditionsHelper->expects($this->once())->method(
'encode')->with($conditions)
176 ->willReturn(
'encoded-conditions-string');
177 $this->escaperMock->expects($this->atLeastOnce())
178 ->method(
'escapeQuote')
180 [
'my "widget"',
false,
'my "widget"'],
184 [
'Magento_CatalogWidget::product/widget/content/grid.phtml',
186 'Magento_CatalogWidget::product/widget/content/grid.phtml' 188 [
'encoded-conditions-string',
false,
'encoded-conditions-string'],
191 $this->dataStorageMock->expects($this->once())
195 $result = $this->widget->getWidgetDeclaration(
196 \
Magento\CatalogWidget\Block\Product\ProductsList::class,
199 $this->assertContains(
'{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"',
$result);
200 $this->assertContains(
'title="my "widget""',
$result);
201 $this->assertContains(
'conditions_encoded="encoded-conditions-string"',
$result);
202 $this->assertContains(
'page_var_name="pasdf"',
$result);
203 $this->assertContains(
'type_name=""}}',
$result);
208 $mathRandomMock = $this->createPartialMock(\
Magento\Framework\Math\Random::class, [
'getRandomString']);
209 $mathRandomMock->expects($this->any())
210 ->method(
'getRandomString')
211 ->willReturn(
'asdf');
221 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
222 'aggregator' =>
'all',
228 'title' =>
'my widget',
230 'products_per_page' =>
'5',
231 'products_count' =>
'0',
232 'template' =>
'Magento_CatalogWidget::product/widget/content/grid.phtml',
233 'conditions' => $conditions
236 $this->conditionsHelper->expects($this->once())
239 ->willReturn(
'encoded-conditions-string');
241 $this->dataStorageMock->expects($this->once())
245 $result = $this->widget->getWidgetDeclaration(
246 \
Magento\CatalogWidget\Block\Product\ProductsList::class,
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);
defined('TESTS_BP')||define('TESTS_BP' __DIR__
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]