forked from ConvertGroupsAS/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatch-Magento_Customer-M2.1.9-generate-new-formkey-wishlist.patch
176 lines (176 loc) · 5.28 KB
/
Patch-Magento_Customer-M2.1.9-generate-new-formkey-wishlist.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
--- /dev/null
+++ Model/Plugin/CustomerFlushFormKey.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Customer\Model\Plugin;
+
+use Magento\Customer\Model\Session;
+use Magento\Framework\Data\Form\FormKey as DataFormKey;
+use Magento\PageCache\Observer\FlushFormKey;
+
+class CustomerFlushFormKey
+{
+ /**
+ * @var Session
+ */
+ private $session;
+
+ /**
+ * @var DataFormKey
+ */
+ private $dataFormKey;
+
+ /**
+ * Initialize dependencies.
+ *
+ * @param Session $session
+ * @param DataFormKey $dataFormKey
+ */
+ public function __construct(Session $session, DataFormKey $dataFormKey)
+ {
+ $this->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);
+ }
+ }
+}
--- /dev/null
+++ Test/Unit/Model/Plugin/CustomerFlushFormKeyTest.php
@@ -0,0 +1,105 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+namespace Magento\Customer\Test\Unit\Model\Plugin;
+
+use Magento\Customer\Model\Plugin\CustomerFlushFormKey;
+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
+{
+ /**
+ * @var CookieFormKey | MockObject
+ */
+ private $cookieFormKey;
+
+ /**
+ * @var Session | MockObject
+ */
+ private $customerSession;
+
+ /**
+ * @var DataFormKey | MockObject
+ */
+ private $dataFormKey;
+
+ protected function setUp()
+ {
+
+ /** @var CookieFormKey | MockObject */
+ $this->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();
+ }
+
+ /**
+ * @dataProvider aroundFlushFormKeyProvider
+ * @param $beforeFormKey
+ * @param $currentFormKey
+ * @param $getFormKeyTimes
+ * @param $setBeforeParamsTimes
+ */
+ public function testAroundFlushFormKey(
+ $beforeFormKey,
+ $currentFormKey,
+ $getFormKeyTimes,
+ $setBeforeParamsTimes
+ ) {
+ $observerDto = new Observer();
+ $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);
+
+ $proceed = function ($observerDto) use ($observer) {
+ return $observer->execute($observerDto);
+ };
+
+ $plugin->aroundExecute($observer, $proceed, $observerDto);
+ }
+
+ /**
+ * 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]
+ ];
+ }
+}
--- etc/di.xml
+++ etc/di.xml
@@ -323,6 +323,9 @@
<type name="Magento\Framework\App\Action\AbstractAction">
<plugin name="customerNotification" type="Magento\Customer\Model\Plugin\CustomerNotification"/>
</type>
+ <type name="Magento\PageCache\Observer\FlushFormKey">
+ <plugin name="customerFlushFormKey" type="Magento\Customer\Model\Plugin\CustomerFlushFormKey"/>
+ </type>
<type name="Magento\Customer\Model\Customer\NotificationStorage">
<arguments>
<argument name="cache" xsi:type="object">Magento\Customer\Model\Cache\Type\Notification</argument>