From 03ac5b5b9f66dea7b50eccd4ee275579c50c5ebe Mon Sep 17 00:00:00 2001 From: Oscar Recio Date: Sun, 5 Nov 2017 13:23:21 +0100 Subject: [PATCH 1/3] Generate new FormKey and replace for oldRequestParams --- .../Model/Plugin/CustomerFlushFormKey.php | 53 +++++++++++++++++++ app/code/Magento/Customer/etc/di.xml | 3 ++ 2 files changed, 56 insertions(+) create mode 100644 app/code/Magento/Customer/Model/Plugin/CustomerFlushFormKey.php diff --git a/app/code/Magento/Customer/Model/Plugin/CustomerFlushFormKey.php b/app/code/Magento/Customer/Model/Plugin/CustomerFlushFormKey.php new file mode 100644 index 0000000000000..b7b462b3cc317 --- /dev/null +++ b/app/code/Magento/Customer/Model/Plugin/CustomerFlushFormKey.php @@ -0,0 +1,53 @@ +session = $session; + $this->dataFormKey = $dataFormKey; + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @param FlushFormKey $subject + * @param callable $proceed + * @param $args + */ + public function aroundExecute(FlushFormKey $subject, callable $proceed, ...$args) + { + $currentFormKey = $this->dataFormKey->getFormKey(); + $proceed(...$args); + $beforeParams = $this->session->getBeforeRequestParams(); + if ($beforeParams['form_key'] == $currentFormKey) { + $beforeParams['form_key'] = $this->dataFormKey->getFormKey(); + $this->session->setBeforeRequestParams($beforeParams); + } + } +} diff --git a/app/code/Magento/Customer/etc/di.xml b/app/code/Magento/Customer/etc/di.xml index 6eea4e1582a97..40ef730120783 100644 --- a/app/code/Magento/Customer/etc/di.xml +++ b/app/code/Magento/Customer/etc/di.xml @@ -323,6 +323,9 @@ + + + Magento\Customer\Model\Cache\Type\Notification From 7edc2d46c69894d378bb35c7ced0d300f323a84c Mon Sep 17 00:00:00 2001 From: Oscar Recio Date: Mon, 27 Nov 2017 22:12:45 +0100 Subject: [PATCH 2/3] Add Test CustomerFlushFormKey Plugin --- .../Model/Plugin/CustomerFlushFormKeyTest.php | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php diff --git a/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php b/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php new file mode 100644 index 0000000000000..c06805e94eade --- /dev/null +++ b/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php @@ -0,0 +1,110 @@ +cookieFormKey = $this->getMockBuilder(CookieFormKey::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var DataFormKey | MockObject */ + $this->dataFormKey = $this->getMockBuilder(DataFormKey::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Session | MockObject */ + $this->customerSession = $this->getMockBuilder(Session::class) + ->disableOriginalConstructor() + ->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams']) + ->getMock(); + + $this->closure = function () { + return static::CLOSURE_VALUE; + }; + } + + /** + * @dataProvider aroundFlushFormKeyProvider + * @param $beforeFormKey + * @param $currentFormKey + * @param $getFormKeyTimes + * @param $setBeforeParamsTimes + */ + public function testAroundFlushFormKey( + $beforeFormKey, + $currentFormKey, + $getFormKeyTimes, + $setBeforeParamsTimes + ) { + $observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey); + $plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey); + + $beforeParams['form_key'] = $beforeFormKey; + + $this->dataFormKey->expects($this->exactly($getFormKeyTimes)) + ->method('getFormKey') + ->willReturn($currentFormKey); + + $this->customerSession->expects($this->once()) + ->method('getBeforeRequestParams') + ->willReturn($beforeParams); + + $this->customerSession->expects($this->exactly($setBeforeParamsTimes)) + ->method('setBeforeRequestParams') + ->with($beforeParams); + + $plugin->aroundExecute($observer, $this->closure, $observer); + } + + /** + * Data provider for testAroundFlushFormKey + * + * @return array + */ + public function aroundFlushFormKeyProvider() + { + return [ + ['form_key_value', 'form_key_value', 2, 1], + ['form_old_key_value', 'form_key_value', 1, 0], + [null, 'form_key_value', 1, 0] + ]; + } +} From 04a1c77802fb41ced137f274b2aceb03a3f3aa26 Mon Sep 17 00:00:00 2001 From: Volodymyr Kublytskyi Date: Fri, 1 Dec 2017 16:55:07 +0200 Subject: [PATCH 3/3] MAGETWO-83287: #11825: Generate new FormKey and replace for oldRequestParams Wishlist #12038 - fixed unit test --- .../Model/Plugin/CustomerFlushFormKeyTest.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php b/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php index c06805e94eade..1b30fb5c60e9c 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php @@ -9,14 +9,13 @@ use Magento\Customer\Model\Session; use Magento\Framework\App\PageCache\FormKey as CookieFormKey; use Magento\Framework\Data\Form\FormKey as DataFormKey; +use Magento\Framework\Event\Observer; use Magento\PageCache\Observer\FlushFormKey; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; class CustomerFlushFormKeyTest extends TestCase { - const CLOSURE_VALUE = 'CLOSURE'; - /** * @var CookieFormKey | MockObject */ @@ -32,11 +31,6 @@ class CustomerFlushFormKeyTest extends TestCase */ private $dataFormKey; - /** - * @var \Closure - */ - private $closure; - protected function setUp() { @@ -55,10 +49,6 @@ protected function setUp() ->disableOriginalConstructor() ->setMethods(['getBeforeRequestParams', 'setBeforeRequestParams']) ->getMock(); - - $this->closure = function () { - return static::CLOSURE_VALUE; - }; } /** @@ -74,6 +64,7 @@ public function testAroundFlushFormKey( $getFormKeyTimes, $setBeforeParamsTimes ) { + $observerDto = new Observer(); $observer = new FlushFormKey($this->cookieFormKey, $this->dataFormKey); $plugin = new CustomerFlushFormKey($this->customerSession, $this->dataFormKey); @@ -91,7 +82,11 @@ public function testAroundFlushFormKey( ->method('setBeforeRequestParams') ->with($beforeParams); - $plugin->aroundExecute($observer, $this->closure, $observer); + $proceed = function ($observerDto) use ($observer) { + return $observer->execute($observerDto); + }; + + $plugin->aroundExecute($observer, $proceed, $observerDto); } /**