Skip to content

Commit b4d3739

Browse files
committed
MAGETWO-46014: delayed error messages
1 parent 55a6e51 commit b4d3739

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
namespace Magento\TestFramework\TestCase;
1111

12+
use Magento\Framework\Stdlib\CookieManagerInterface;
13+
use Magento\Theme\Controller\Result\MessagePlugin;
14+
1215
/**
1316
* @SuppressWarnings(PHPMD.NumberOfChildren)
1417
*/
@@ -204,14 +207,48 @@ public function assertSessionMessages(
204207
} else {
205208
$messages = $messageManager->getMessages()->getItemsByType($messageType);
206209
}
210+
207211
$actualMessages = [];
208212
foreach ($messages as $message) {
209213
$actualMessages[] = $message->getText();
210214
}
215+
216+
$actualMessages = array_merge($actualMessages, $this->getCookieMessages($messageType));
217+
211218
$this->assertThat(
212219
$actualMessages,
213220
$constraint,
214221
'Session messages do not meet expectations ' . var_export($actualMessages, true)
215222
);
216223
}
224+
225+
/**
226+
* Return messages stored in cookies by type
227+
*
228+
* @param string|null $messageType
229+
* @return array
230+
*/
231+
private function getCookieMessages($messageType = null) {
232+
/** @var $cookieManager CookieManagerInterface */
233+
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
234+
try {
235+
$messages = \Zend_Json::decode(
236+
$cookieManager->getCookie(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode([]))
237+
);
238+
if (!is_array($messages)) {
239+
$messages = [];
240+
}
241+
} catch (\Zend_Json_Exception $e) {
242+
$messages = [];
243+
}
244+
245+
$actualMessages = [];
246+
foreach ($messages as $message) {
247+
if ($messageType === null || $message['type'] == $messageType) {
248+
$actualMessages[] = $message['text'];
249+
}
250+
}
251+
252+
return $actualMessages;
253+
}
217254
}

dev/tests/integration/testsuite/Magento/Catalog/Controller/Product/CompareTest.php

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// @codingStandardsIgnoreFile
88

99
namespace Magento\Catalog\Controller\Product;
10+
use Magento\Framework\Message\MessageInterface;
1011

1112
/**
1213
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
@@ -43,16 +44,7 @@ public function testAddAction()
4344
)
4445
);
4546

46-
/** @var $messageManager \Magento\Framework\Message\Manager */
47-
$messageManager = $objectManager->get('Magento\Framework\Message\Manager');
48-
$this->assertInstanceOf(
49-
'Magento\Framework\Message\Success',
50-
$messageManager->getMessages()->getLastAddedMessage()
51-
);
52-
$this->assertContains(
53-
'Simple Product 1 Name',
54-
(string)$messageManager->getMessages()->getLastAddedMessage()->getText()
55-
);
47+
$this->assertSessionMessages($this->contains('Simple Product 1 Name'), MessageInterface::TYPE_SUCCESS);
5648

5749
$this->assertRedirect();
5850

@@ -76,17 +68,7 @@ public function testRemoveAction()
7668
$product = $this->productRepository->get('simple_product_2');
7769
$this->dispatch('catalog/product_compare/remove/product/' . $product->getEntityId());
7870

79-
/** @var $messageManager \Magento\Framework\Message\Manager */
80-
$messageManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
81-
->get('Magento\Framework\Message\Manager');
82-
$this->assertInstanceOf(
83-
'Magento\Framework\Message\Success',
84-
$messageManager->getMessages()->getLastAddedMessage()
85-
);
86-
$this->assertContains(
87-
'Simple Product 2 Name',
88-
(string)$messageManager->getMessages()->getLastAddedMessage()->getText()
89-
);
71+
$this->assertSessionMessages($this->contains('Simple Product 2 Name'), MessageInterface::TYPE_SUCCESS);
9072

9173
$this->assertRedirect();
9274
$restProduct = $this->productRepository->get('simple_product_1');
@@ -99,15 +81,8 @@ public function testRemoveActionWithSession()
9981
$product = $this->productRepository->get('simple_product_1');
10082
$this->dispatch('catalog/product_compare/remove/product/' . $product->getEntityId());
10183
$secondProduct = $this->productRepository->get('simple_product_2');
102-
/** @var $messageManager \Magento\Framework\Message\Manager */
103-
$messageManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
104-
->get('Magento\Framework\Message\Manager');
105-
$this->assertInstanceOf(
106-
'Magento\Framework\Message\Success',
107-
$messageManager->getMessages()->getLastAddedMessage()
108-
);
109-
$this->assertContains('Simple Product 1 Name',
110-
(string)$messageManager->getMessages()->getLastAddedMessage()->getText());
84+
85+
$this->assertSessionMessages($this->contains('Simple Product 1 Name'), MessageInterface::TYPE_SUCCESS);
11186

11287
$this->assertRedirect();
11388

@@ -146,12 +121,9 @@ public function testClearAction()
146121

147122
$this->dispatch('catalog/product_compare/clear');
148123

149-
/** @var $messageManager \Magento\Framework\Message\Manager */
150-
$messageManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
151-
->get('Magento\Framework\Message\Manager');
152-
$this->assertInstanceOf(
153-
'Magento\Framework\Message\Success',
154-
$messageManager->getMessages()->getLastAddedMessage()
124+
$this->assertSessionMessages(
125+
$this->contains('You cleared the comparison list.'),
126+
MessageInterface::TYPE_SUCCESS
155127
);
156128

157129
$this->assertRedirect();
@@ -167,17 +139,14 @@ public function testRemoveActionProductNameXss()
167139
$this->_prepareCompareListWithProductNameXss();
168140
$product = $this->productRepository->get('product-with-xss');
169141
$this->dispatch('catalog/product_compare/remove/product/' . $product->getEntityId() . '?nocookie=1');
170-
$messages = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
171-
'Magento\Framework\Message\Manager'
172-
)->getMessages()->getItems();
173-
$isProductNamePresent = false;
174-
foreach ($messages as $message) {
175-
if (strpos($message->getText(), '<script>alert("xss");</script>') !== false) {
176-
$isProductNamePresent = true;
177-
}
178-
$this->assertNotContains('<script>alert("xss");</script>', (string)$message->getText());
179-
}
180-
$this->assertTrue($isProductNamePresent, 'Product name was not found in session messages');
142+
143+
$this->assertSessionMessages(
144+
$this->logicalNot($this->contains('<script>alert("xss");</script>'))
145+
);
146+
$this->assertSessionMessages(
147+
$this->contains('&lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt;'),
148+
MessageInterface::TYPE_SUCCESS
149+
);
181150
}
182151

183152
protected function _prepareCompareListWithProductNameXss()

0 commit comments

Comments
 (0)