From f46da7b33f2ade983222419230a7c2bc27f77370 Mon Sep 17 00:00:00 2001
From: Yaroslav Onischenko <yonischenko@magento.com>
Date: Tue, 15 Mar 2016 12:27:28 +0200
Subject: [PATCH] MAGETWO-36035: Pagination in Catalog Products List widget
 works incorrect with FPC

---
 app/code/Magento/Widget/Model/Widget.php      | 23 +++++++---
 .../Widget/Test/Unit/Model/WidgetTest.php     | 44 ++++++++++++++++++-
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/app/code/Magento/Widget/Model/Widget.php b/app/code/Magento/Widget/Model/Widget.php
index 846287f8849..c25b76f8251 100644
--- a/app/code/Magento/Widget/Model/Widget.php
+++ b/app/code/Magento/Widget/Model/Widget.php
@@ -53,7 +53,7 @@ class Widget
     /**
      * @var \Magento\Framework\Math\Random
      */
-    protected $mathRandom;
+    private $mathRandom;
 
     /**
      * @param \Magento\Framework\Escaper $escaper
@@ -62,7 +62,6 @@ class Widget
      * @param \Magento\Framework\View\Asset\Source $assetSource
      * @param \Magento\Framework\View\FileSystem $viewFileSystem
      * @param \Magento\Widget\Helper\Conditions $conditionsHelper
-     * @param \Magento\Framework\Math\Random $mathRandom
      */
     public function __construct(
         \Magento\Framework\Escaper $escaper,
@@ -70,8 +69,7 @@ class Widget
         \Magento\Framework\View\Asset\Repository $assetRepo,
         \Magento\Framework\View\Asset\Source $assetSource,
         \Magento\Framework\View\FileSystem $viewFileSystem,
-        \Magento\Widget\Helper\Conditions $conditionsHelper,
-        \Magento\Framework\Math\Random $mathRandom
+        \Magento\Widget\Helper\Conditions $conditionsHelper
     ) {
         $this->escaper = $escaper;
         $this->dataStorage = $dataStorage;
@@ -79,7 +77,20 @@ class Widget
         $this->assetSource = $assetSource;
         $this->viewFileSystem = $viewFileSystem;
         $this->conditionsHelper = $conditionsHelper;
-        $this->mathRandom = $mathRandom;
+    }
+
+    /**
+     * @return \Magento\Framework\Math\Random
+     *
+     * @deprecated
+     */
+    private function getMathRandom()
+    {
+        if ($this->mathRandom === null) {
+            $this->mathRandom = \Magento\Framework\App\ObjectManager::getInstance()
+                ->get('\Magento\Framework\Math\Random');
+        }
+        return $this->mathRandom;
     }
 
     /**
@@ -310,7 +321,7 @@ class Widget
             $directive .= sprintf(
                 ' %s="%s"',
                 'page_var_name',
-                'p' . $this->mathRandom->getRandomString(5, \Magento\Framework\Math\Random::CHARS_LOWERS)
+                'p' . $this->getMathRandom()->getRandomString(5, \Magento\Framework\Math\Random::CHARS_LOWERS)
             );
         }
 
diff --git a/app/code/Magento/Widget/Test/Unit/Model/WidgetTest.php b/app/code/Magento/Widget/Test/Unit/Model/WidgetTest.php
index 7ccea8d305c..16bcb545324 100644
--- a/app/code/Magento/Widget/Test/Unit/Model/WidgetTest.php
+++ b/app/code/Magento/Widget/Test/Unit/Model/WidgetTest.php
@@ -17,15 +17,24 @@ class WidgetTest extends \PHPUnit_Framework_TestCase
      */
     protected $widget;
 
+    /**
+     * @var \Magento\Widget\Helper\Conditions
+     */
+    private $conditionsHelper;
+
     public function setUp()
     {
         $this->dataStorageMock = $this->getMockBuilder('Magento\Widget\Model\Config\Data')
             ->disableOriginalConstructor()
             ->getMock();
+        $this->conditionsHelper = $this->getMockBuilder('\Magento\Widget\Helper\Conditions')
+            ->setMethods(['encode'])
+            ->disableOriginalConstructor()
+            ->getMock();
         $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
         $this->widget = $objectManagerHelper->getObject(
             'Magento\Widget\Model\Widget',
-            ['dataStorage' => $this->dataStorageMock]
+            ['dataStorage' => $this->dataStorageMock, 'conditionsHelper' => $this->conditionsHelper]
         );
     }
 
@@ -119,4 +128,37 @@ class WidgetTest extends \PHPUnit_Framework_TestCase
         $this->assertInstanceOf('Magento\Framework\DataObject', $resultObject);
         $this->assertSame([], $resultObject->getData());
     }
+
+    public function testGetWidgetDeclaration()
+    {
+        $mathRandomMock = $this->getMock('\Magento\Framework\Math\Random', ['getRandomString'], [], '', false);
+        $mathRandomMock->expects($this->any())->method('getRandomString')->willReturn('asdf');
+        $reflection = new \ReflectionClass(get_class($this->widget));
+        $reflectionProperty = $reflection->getProperty('mathRandom');
+        $reflectionProperty->setAccessible(true);
+        $reflectionProperty->setValue($this->widget, $mathRandomMock);
+
+        $conditions = [
+            [
+                'type' => 'Magento\CatalogWidget\Model\Rule\Condition\Combine',
+                'aggregator' => 'all',
+                'value' => '1',
+                'new_child' => ''
+            ]
+        ];
+        $params = [
+            'title' => 'my widget',
+            'show_pager' => '1',
+            'products_per_page' => '5',
+            'products_count' => '10',
+            'template' => 'product/widget/content/grid.phtml',
+            'conditions' => $conditions
+        ];
+        $this->conditionsHelper->expects($this->once())->method('encode')->with($conditions)
+            ->willReturn('encoded-conditions-string');
+        $result = $this->widget->getWidgetDeclaration('Magento\CatalogWidget\Block\Product\ProductsList', $params);
+        $this->assertContains('{{widget type="Magento\CatalogWidget\Block\Product\ProductsList"', $result);
+        $this->assertContains('conditions_encoded="encoded-conditions-string"', $result);
+        $this->assertContains('page_var_name="pasdf"}}', $result);
+    }
 }
-- 
GitLab