Skip to content

Commit 34fe91b

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-60538' into 2.1.8-develop-pr19
2 parents 4e6a3bc + 73028cf commit 34fe91b

File tree

2 files changed

+98
-10
lines changed

2 files changed

+98
-10
lines changed

app/code/Magento/Theme/Controller/Result/MessagePlugin.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use Magento\Framework\Message\MessageInterface;
1111

1212
/**
13-
* Plugin for putting messages to cookies
13+
* Plugin for putting messages to cookies.
1414
*/
1515
class MessagePlugin
1616
{
1717
/**
18-
* Cookies name for messages
18+
* Cookies name for messages.
1919
*/
2020
const MESSAGES_COOKIES_NAME = 'mage-messages';
2121

@@ -66,6 +66,11 @@ public function __construct(
6666
}
6767

6868
/**
69+
* Set 'mage-messages' cookie.
70+
*
71+
* Checks the result that controller actions must return. If result is not JSON type, then
72+
* sets 'mage-messages' cookie.
73+
*
6974
* @param ResultInterface $subject
7075
* @param ResultInterface $result
7176
* @return ResultInterface
@@ -75,22 +80,52 @@ public function afterRenderResult(
7580
ResultInterface $result
7681
) {
7782
if (!($subject instanceof Json)) {
83+
$this->setCookie($this->getMessages());
84+
}
85+
86+
return $result;
87+
}
88+
89+
/**
90+
* Set 'mage-messages' cookie with 'messages' array.
91+
*
92+
* Checks the $messages argument. If $messages is not an empty array, then
93+
* sets 'mage-messages' public cookie:
94+
*
95+
* Cookie Name: 'mage-messages';
96+
* Cookie Duration: 1 year;
97+
* Cookie Path: /;
98+
* Cookie HTTP Only flag: FALSE. Cookie can be accessed by client-side APIs.
99+
*
100+
* The 'messages' list has format:
101+
* [
102+
* [
103+
* 'type' => 'type_value',
104+
* 'text' => 'cookie_value',
105+
* ],
106+
* ]
107+
*
108+
* @param array $messages List of Magento messages that must be set as 'mage-messages' cookie.
109+
* @return void
110+
*/
111+
private function setCookie(array $messages)
112+
{
113+
if (!empty($messages)) {
78114
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
79115
$publicCookieMetadata->setDurationOneYear();
80116
$publicCookieMetadata->setPath('/');
81117
$publicCookieMetadata->setHttpOnly(false);
118+
82119
$this->cookieManager->setPublicCookie(
83120
self::MESSAGES_COOKIES_NAME,
84-
$this->jsonHelper->jsonEncode($this->getMessages()),
121+
$this->jsonHelper->jsonEncode($messages),
85122
$publicCookieMetadata
86123
);
87124
}
88-
89-
return $result;
90125
}
91126

92127
/**
93-
* Return messages array and clean message manager messages
128+
* Return messages array and clean message manager messages.
94129
*
95130
* @return array
96131
*/
@@ -104,11 +139,12 @@ protected function getMessages()
104139
'text' => $this->interpretationStrategy->interpret($message),
105140
];
106141
}
142+
107143
return $messages;
108144
}
109145

110146
/**
111-
* Return messages stored in cookies
147+
* Return messages stored in cookies.
112148
*
113149
* @return array
114150
*/
@@ -124,6 +160,7 @@ protected function getCookiesMessages()
124160
} catch (\Zend_Json_Exception $e) {
125161
$messages = [];
126162
}
163+
127164
return $messages;
128165
}
129166
}

app/code/Magento/Theme/Test/Unit/Controller/Result/MessagePluginTest.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Theme\Controller\Result\MessagePlugin;
1919

2020
/**
21+
* Test for Magento\Theme\Controller\Result\MessagePlugin.
22+
*
2123
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2224
*/
2325
class MessagePluginTest extends \PHPUnit_Framework_TestCase
@@ -79,7 +81,6 @@ public function testAfterRenderResultJson()
7981

8082
public function testAfterRenderResult()
8183
{
82-
8384
$existingMessages = [
8485
[
8586
'type' => 'message0type',
@@ -125,14 +126,14 @@ public function testAfterRenderResult()
125126
)
126127
->willReturn(\Zend_Json::encode($existingMessages));
127128

128-
$this->dataMock->expects($this->any())
129+
$this->dataMock->expects($this->once())
129130
->method('jsonDecode')
130131
->willReturnCallback(
131132
function ($data) {
132133
return \Zend_Json::decode($data);
133134
}
134135
);
135-
$this->dataMock->expects($this->any())
136+
$this->dataMock->expects($this->exactly(2))
136137
->method('jsonEncode')
137138
->willReturnCallback(
138139
function ($data) {
@@ -168,6 +169,56 @@ function ($data) {
168169
$this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
169170
}
170171

172+
public function testAfterRenderResultWithNoMessages()
173+
{
174+
/** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $resultMock */
175+
$resultMock = $this->getMockBuilder(Redirect::class)
176+
->disableOriginalConstructor()
177+
->getMock();
178+
179+
$this->cookieManagerMock->expects($this->once())
180+
->method('getCookie')
181+
->with(
182+
MessagePlugin::MESSAGES_COOKIES_NAME,
183+
\Zend_Json::encode([])
184+
)
185+
->willReturn(\Zend_Json::encode([]));
186+
187+
$this->dataMock->expects($this->once())
188+
->method('jsonDecode')
189+
->willReturnCallback(
190+
function ($data) {
191+
return \Zend_Json::decode($data);
192+
}
193+
);
194+
$this->dataMock->expects($this->once())
195+
->method('jsonEncode')
196+
->willReturnCallback(
197+
function ($data) {
198+
return \Zend_Json::encode($data);
199+
}
200+
);
201+
202+
/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $collectionMock */
203+
$collectionMock = $this->getMockBuilder(Collection::class)
204+
->disableOriginalConstructor()
205+
->getMock();
206+
$collectionMock->expects($this->once())
207+
->method('getItems')
208+
->willReturn([]);
209+
210+
$this->managerMock->expects($this->once())
211+
->method('getMessages')
212+
->with(true, null)
213+
->willReturn($collectionMock);
214+
215+
$this->cookieMetadataFactoryMock->expects($this->never())
216+
->method('createPublicCookieMetadata')
217+
->willReturn(null);
218+
219+
$this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
220+
}
221+
171222
public function testAfterRenderResultWithoutExisting()
172223
{
173224
$messageType = 'message1type';

0 commit comments

Comments
 (0)