Skip to content

Commit f46da7b

Browse files
author
Yaroslav Onischenko
committed
MAGETWO-36035: Pagination in Catalog Products List widget works incorrect with FPC
1 parent 9cdc523 commit f46da7b

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

app/code/Magento/Widget/Model/Widget.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Widget
5353
/**
5454
* @var \Magento\Framework\Math\Random
5555
*/
56-
protected $mathRandom;
56+
private $mathRandom;
5757

5858
/**
5959
* @param \Magento\Framework\Escaper $escaper
@@ -62,24 +62,35 @@ class Widget
6262
* @param \Magento\Framework\View\Asset\Source $assetSource
6363
* @param \Magento\Framework\View\FileSystem $viewFileSystem
6464
* @param \Magento\Widget\Helper\Conditions $conditionsHelper
65-
* @param \Magento\Framework\Math\Random $mathRandom
6665
*/
6766
public function __construct(
6867
\Magento\Framework\Escaper $escaper,
6968
\Magento\Widget\Model\Config\Data $dataStorage,
7069
\Magento\Framework\View\Asset\Repository $assetRepo,
7170
\Magento\Framework\View\Asset\Source $assetSource,
7271
\Magento\Framework\View\FileSystem $viewFileSystem,
73-
\Magento\Widget\Helper\Conditions $conditionsHelper,
74-
\Magento\Framework\Math\Random $mathRandom
72+
\Magento\Widget\Helper\Conditions $conditionsHelper
7573
) {
7674
$this->escaper = $escaper;
7775
$this->dataStorage = $dataStorage;
7876
$this->assetRepo = $assetRepo;
7977
$this->assetSource = $assetSource;
8078
$this->viewFileSystem = $viewFileSystem;
8179
$this->conditionsHelper = $conditionsHelper;
82-
$this->mathRandom = $mathRandom;
80+
}
81+
82+
/**
83+
* @return \Magento\Framework\Math\Random
84+
*
85+
* @deprecated
86+
*/
87+
private function getMathRandom()
88+
{
89+
if ($this->mathRandom === null) {
90+
$this->mathRandom = \Magento\Framework\App\ObjectManager::getInstance()
91+
->get('\Magento\Framework\Math\Random');
92+
}
93+
return $this->mathRandom;
8394
}
8495

8596
/**
@@ -310,7 +321,7 @@ public function getWidgetDeclaration($type, $params = [], $asIs = true)
310321
$directive .= sprintf(
311322
' %s="%s"',
312323
'page_var_name',
313-
'p' . $this->mathRandom->getRandomString(5, \Magento\Framework\Math\Random::CHARS_LOWERS)
324+
'p' . $this->getMathRandom()->getRandomString(5, \Magento\Framework\Math\Random::CHARS_LOWERS)
314325
);
315326
}
316327

app/code/Magento/Widget/Test/Unit/Model/WidgetTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,24 @@ class WidgetTest extends \PHPUnit_Framework_TestCase
1717
*/
1818
protected $widget;
1919

20+
/**
21+
* @var \Magento\Widget\Helper\Conditions
22+
*/
23+
private $conditionsHelper;
24+
2025
public function setUp()
2126
{
2227
$this->dataStorageMock = $this->getMockBuilder('Magento\Widget\Model\Config\Data')
2328
->disableOriginalConstructor()
2429
->getMock();
30+
$this->conditionsHelper = $this->getMockBuilder('\Magento\Widget\Helper\Conditions')
31+
->setMethods(['encode'])
32+
->disableOriginalConstructor()
33+
->getMock();
2534
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
2635
$this->widget = $objectManagerHelper->getObject(
2736
'Magento\Widget\Model\Widget',
28-
['dataStorage' => $this->dataStorageMock]
37+
['dataStorage' => $this->dataStorageMock, 'conditionsHelper' => $this->conditionsHelper]
2938
);
3039
}
3140

@@ -119,4 +128,37 @@ public function testGetConfigAsObjectWidgetNoFound()
119128
$this->assertInstanceOf('Magento\Framework\DataObject', $resultObject);
120129
$this->assertSame([], $resultObject->getData());
121130
}
131+
132+
public function testGetWidgetDeclaration()
133+
{
134+
$mathRandomMock = $this->getMock('\Magento\Framework\Math\Random', ['getRandomString'], [], '', false);
135+
$mathRandomMock->expects($this->any())->method('getRandomString')->willReturn('asdf');
136+
$reflection = new \ReflectionClass(get_class($this->widget));
137+
$reflectionProperty = $reflection->getProperty('mathRandom');
138+
$reflectionProperty->setAccessible(true);
139+
$reflectionProperty->setValue($this->widget, $mathRandomMock);
140+
141+
$conditions = [
142+
[
143+
'type' => 'Magento\CatalogWidget\Model\Rule\Condition\Combine',
144+
'aggregator' => 'all',
145+
'value' => '1',
146+
'new_child' => ''
147+
]
148+
];
149+
$params = [
150+
'title' => 'my widget',
151+
'show_pager' => '1',
152+
'products_per_page' => '5',
153+
'products_count' => '10',
154+
'template' => 'product/widget/content/grid.phtml',
155+
'conditions' => $conditions
156+
];
157+
$this->conditionsHelper->expects($this->once())->method('encode')->with($conditions)
158+
->willReturn('encoded-conditions-string');
159+
$result = $this->widget->getWidgetDeclaration('Magento\CatalogWidget\Block\Product\ProductsList', $params);
160+
$this->assertContains('{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"', $result);
161+
$this->assertContains('conditions_encoded="encoded-conditions-string"', $result);
162+
$this->assertContains('page_var_name="pasdf"}}', $result);
163+
}
122164
}

0 commit comments

Comments
 (0)