Skip to content

Commit d97ebe5

Browse files
committed
MAGETWO-46014: delayed error messages
1 parent af2a832 commit d97ebe5

File tree

3 files changed

+70
-18
lines changed

3 files changed

+70
-18
lines changed

dev/tests/integration/framework/Magento/TestFramework/TestCase/AbstractController.php

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Magento\TestFramework\TestCase;
1111

1212
use Magento\Framework\Stdlib\CookieManagerInterface;
13+
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
1314
use Magento\Theme\Controller\Result\MessagePlugin;
1415

1516
/**
@@ -181,11 +182,11 @@ public function assertRedirect(\PHPUnit_Framework_Constraint $urlConstraint = nu
181182
}
182183
}
183184

184-
/**
185+
/**ishlist/Controller/IndexTest
185186
* Assert that actual session messages meet expectations:
186187
* Usage examples:
187188
* $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
188-
* $this->assertSessionMessages($this->equalTo(array('Entity has been saved.')),
189+
* $this->assertSessionMessages($this->equalTo(['Entity has been saved.'],
189190
* \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS);
190191
*
191192
* @param \PHPUnit_Framework_Constraint $constraint Constraint to compare actual messages against
@@ -199,6 +200,44 @@ public function assertSessionMessages(
199200
$messageManagerClass = 'Magento\Framework\Message\Manager'
200201
) {
201202
$this->_assertSessionErrors = false;
203+
204+
$messages = $this->getMessages($messageType, $messageManagerClass);
205+
206+
$this->assertThat(
207+
$messages,
208+
$constraint,
209+
'Session messages do not meet expectations ' . var_export($messages, true)
210+
);
211+
}
212+
213+
/**
214+
* Return all stored messages
215+
*
216+
* @param string|null $messageType
217+
* @param string $messageManagerClass
218+
* @return array
219+
*/
220+
protected function getMessages(
221+
$messageType = null,
222+
$messageManagerClass = 'Magento\Framework\Message\Manager'
223+
) {
224+
return array_merge(
225+
$this->getSessionMessages($messageType, $messageManagerClass),
226+
$this->getCookieMessages($messageType)
227+
);
228+
}
229+
230+
/**
231+
* Return messages stored in session
232+
*
233+
* @param string|null $messageType
234+
* @param string $messageManagerClass
235+
* @return array
236+
*/
237+
protected function getSessionMessages(
238+
$messageType = null,
239+
$messageManagerClass = 'Magento\Framework\Message\Manager'
240+
) {
202241
/** @var $messageManager \Magento\Framework\Message\ManagerInterface */
203242
$messageManager = $this->_objectManager->get($messageManagerClass);
204243
/** @var $messages \Magento\Framework\Message\AbstractMessage[] */
@@ -208,18 +247,15 @@ public function assertSessionMessages(
208247
$messages = $messageManager->getMessages()->getItemsByType($messageType);
209248
}
210249

250+
/** @var $messageManager InterpretationStrategyInterface */
251+
$interpretationStrategy = $this->_objectManager->get(InterpretationStrategyInterface::class);
252+
211253
$actualMessages = [];
212254
foreach ($messages as $message) {
213-
$actualMessages[] = $message->getText();
255+
$actualMessages[] = $interpretationStrategy->interpret($message);
214256
}
215257

216-
$actualMessages = array_merge($actualMessages, $this->getCookieMessages($messageType));
217-
218-
$this->assertThat(
219-
$actualMessages,
220-
$constraint,
221-
'Session messages do not meet expectations ' . var_export($actualMessages, true)
222-
);
258+
return $actualMessages;
223259
}
224260

225261
/**
@@ -228,7 +264,7 @@ public function assertSessionMessages(
228264
* @param string|null $messageType
229265
* @return array
230266
*/
231-
private function getCookieMessages($messageType = null)
267+
protected function getCookieMessages($messageType = null)
232268
{
233269
/** @var $cookieManager CookieManagerInterface */
234270
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/TestCase/ControllerAbstractTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Test\TestCase;
7+
use Magento\Framework\Message\MessageInterface;
78
use Magento\Framework\Stdlib\CookieManagerInterface;
9+
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
810

911
/**
1012
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -16,6 +18,9 @@ class ControllerAbstractTest extends \Magento\TestFramework\TestCase\AbstractCon
1618
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Message\Manager */
1719
private $messageManager;
1820

21+
/** @var \PHPUnit_Framework_MockObject_MockObject | InterpretationStrategyInterface */
22+
private $interpretationStrategyMock;
23+
1924
/** @var \PHPUnit_Framework_MockObject_MockObject | CookieManagerInterface */
2025
private $cookieManagerMock;
2126

@@ -25,6 +30,15 @@ protected function setUp()
2530

2631
$this->messageManager = $this->getMock('\Magento\Framework\Message\Manager', [], [], '', false);
2732
$this->cookieManagerMock = $this->getMock(CookieManagerInterface::class, [], [], '', false);
33+
$this->interpretationStrategyMock = $this->getMock(InterpretationStrategyInterface::class, [], [], '', false);
34+
$this->interpretationStrategyMock->expects($this->any())
35+
->method('interpret')
36+
->willReturnCallback(
37+
function (MessageInterface $message) {
38+
return $message->getText();
39+
}
40+
);
41+
2842
$request = $testObjectManager->getObject('Magento\TestFramework\Request');
2943
$response = $testObjectManager->getObject('Magento\TestFramework\Response');
3044
$this->_objectManager = $this->getMock(
@@ -43,6 +57,7 @@ protected function setUp()
4357
['Magento\Framework\App\ResponseInterface', $response],
4458
['Magento\Framework\Message\Manager', $this->messageManager],
4559
[CookieManagerInterface::class, $this->cookieManagerMock],
60+
[InterpretationStrategyInterface::class, $this->interpretationStrategyMock],
4661
]
4762
)
4863
);
@@ -147,19 +162,19 @@ public function assertSessionMessagesDataProvider()
147162
return [
148163
'message warning type filtering' => [
149164
['some_warning', 'warning_cookie'],
150-
\Magento\Framework\Message\MessageInterface::TYPE_WARNING,
165+
MessageInterface::TYPE_WARNING,
151166
],
152167
'message error type filtering' => [
153168
['error_one', 'error_two', 'error_cookie'],
154-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR,
169+
MessageInterface::TYPE_ERROR,
155170
],
156171
'message notice type filtering' => [
157172
['some_notice', 'notice_cookie'],
158-
\Magento\Framework\Message\MessageInterface::TYPE_NOTICE,
173+
MessageInterface::TYPE_NOTICE,
159174
],
160175
'message success type filtering' => [
161176
['success!', 'success_cookie'],
162-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS,
177+
MessageInterface::TYPE_SUCCESS,
163178
],
164179
];
165180
}

dev/tests/integration/testsuite/Magento/Wishlist/Controller/IndexTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Wishlist\Controller;
77

8-
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
9-
108
class IndexTest extends \Magento\TestFramework\TestCase\AbstractController
119
{
1210
/**
@@ -99,7 +97,10 @@ public function testAddActionProductNameXss()
9997

10098
$this->assertSessionMessages(
10199
$this->equalTo(
102-
['You removed product <script>alert("xss");</script> from the comparison list.']
100+
[
101+
"\n<script>alert("xss");</script> has been added to your Wish List. "
102+
. 'Click <a href="http://localhost/index.php/">here</a> to continue shopping.',
103+
]
103104
),
104105
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
105106
);

0 commit comments

Comments
 (0)