Skip to content

Commit 6be54c0

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-59966' into BUGS
2 parents 02e1c6b + 45baf50 commit 6be54c0

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,18 +80,48 @@ public function afterRenderResult(
7580
ResultInterface $result
7681
) {
7782
if (!($subject instanceof Json)) {
83+
$this->setCookie($this->getMessages());
84+
}
85+
return $result;
86+
}
87+
88+
/**
89+
* Set 'mage-messages' cookie with 'messages' array
90+
*
91+
* Checks the $messages argument. If $messages is not an empty array, then
92+
* sets 'mage-messages' public cookie:
93+
*
94+
* Cookie Name: 'mage-messages';
95+
* Cookie Duration: 1 year;
96+
* Cookie Path: /;
97+
* Cookie HTTP Only flag: FALSE. Cookie can be accessed by client-side APIs.
98+
*
99+
* The 'messages' list has format:
100+
* [
101+
* [
102+
* 'type' => 'type_value',
103+
* 'text' => 'cookie_value',
104+
* ],
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
/**

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public function testAfterRenderResultJson()
7979

8080
public function testAfterRenderResult()
8181
{
82-
8382
$existingMessages = [
8483
[
8584
'type' => 'message0type',
@@ -125,14 +124,14 @@ public function testAfterRenderResult()
125124
)
126125
->willReturn(\Zend_Json::encode($existingMessages));
127126

128-
$this->dataMock->expects($this->any())
127+
$this->dataMock->expects($this->once())
129128
->method('jsonDecode')
130129
->willReturnCallback(
131130
function ($data) {
132131
return \Zend_Json::decode($data);
133132
}
134133
);
135-
$this->dataMock->expects($this->any())
134+
$this->dataMock->expects($this->exactly(2))
136135
->method('jsonEncode')
137136
->willReturnCallback(
138137
function ($data) {
@@ -168,6 +167,56 @@ function ($data) {
168167
$this->assertEquals($resultMock, $this->model->afterRenderResult($resultMock, $resultMock));
169168
}
170169

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

0 commit comments

Comments
 (0)