Skip to content

Commit 70884a4

Browse files
author
Michael Logvin
committed
Merge branch 'MAGETWO-34773' into MAGETWO-29307
2 parents 0e9ac18 + b495a34 commit 70884a4

File tree

2 files changed

+194
-7
lines changed
  • app/code/Magento/Checkout/Controller/Cart
  • dev/tests/unit/testsuite/Magento/Checkout/Controller/Cart

2 files changed

+194
-7
lines changed

app/code/Magento/Checkout/Controller/Cart/Index.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,15 @@ public function execute()
8989
}
9090

9191
// Compose array of messages to add
92-
$messages = [];
93-
/** @var \Magento\Framework\Message\MessageInterface $message */
9492
foreach ($this->cart->getQuote()->getMessages() as $message) {
95-
if ($message) {
96-
// Escape HTML entities in quote message to prevent XSS
97-
$message->setText($this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($message->getText()));
98-
$messages[] = $message;
93+
if (!$message) {
94+
continue;
9995
}
96+
if ($message instanceof \Magento\Framework\Phrase) {
97+
$message = $message->__toString();
98+
}
99+
$this->messageManager->addError($message);
100100
}
101-
$this->messageManager->addUniqueMessages($messages);
102101

103102
/**
104103
* if customer enteres shopping cart we should mark quote
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Checkout\Controller\Cart;
7+
8+
class IndexTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var Index
12+
*/
13+
protected $controller;
14+
15+
/**
16+
* @var \Magento\Checkout\Model\Session | \PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
protected $checkoutSession;
19+
20+
/**
21+
* @var \Magento\Framework\App\Request\Http | \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $request;
24+
25+
/**
26+
* @var \Magento\Framework\App\Response\Http | \PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $response;
29+
30+
/**
31+
* @var \Magento\Quote\Model\Quote | \PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
protected $quote;
34+
35+
/**
36+
* @var \Magento\Framework\Event\Manager | \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
protected $eventManager;
39+
40+
/**
41+
* @var \Magento\Framework\Event\Manager | \PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $objectManagerMock;
44+
45+
/**
46+
* @var \PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
protected $cart;
49+
50+
/**
51+
* @var \PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
protected $scopeConfig;
54+
55+
/**
56+
* @var \PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
protected $messageManager;
59+
60+
/**
61+
* @var \PHPUnit_Framework_MockObject_MockObject
62+
*/
63+
protected $resultPageFactory;
64+
65+
public function setUp()
66+
{
67+
$this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
68+
$this->response = $this->getMock('Magento\Framework\App\Response\Http', [], [], '', false);
69+
$this->quote = $this->getMock('Magento\Quote\Model\Quote', [], [], '', false);
70+
$this->eventManager = $this->getMock('Magento\Framework\Event\Manager', [], [], '', false);
71+
$this->checkoutSession = $this->getMock('Magento\Checkout\Model\Session', [], [], '', false);
72+
73+
$this->objectManagerMock = $this->getMock('Magento\Framework\ObjectManager\ObjectManager', [], [], '', false);
74+
75+
$this->messageManager = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface')
76+
->disableOriginalConstructor()
77+
->getMock();
78+
79+
$context = $this->getMock('Magento\Framework\App\Action\Context', [], [], '', false);
80+
$context->expects($this->once())
81+
->method('getObjectManager')
82+
->willReturn($this->objectManagerMock);
83+
$context->expects($this->once())
84+
->method('getRequest')
85+
->willReturn($this->request);
86+
$context->expects($this->once())
87+
->method('getResponse')
88+
->willReturn($this->response);
89+
$context->expects($this->once())
90+
->method('getEventManager')
91+
->willReturn($this->eventManager);
92+
$context->expects($this->once())
93+
->method('getMessageManager')
94+
->willReturn($this->messageManager);
95+
96+
$this->cart = $this->getMockBuilder('Magento\Checkout\Model\Cart')
97+
->disableOriginalConstructor()
98+
->getMock();
99+
$this->scopeConfig = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
100+
->disableOriginalConstructor()
101+
->getMock();
102+
$this->resultPageFactory = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
103+
->disableOriginalConstructor()
104+
->setMethods(['create'])
105+
->getMock();
106+
107+
$objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this);
108+
109+
$this->controller = $objectManagerHelper->getObject(
110+
'Magento\Checkout\Controller\Cart\Index',
111+
[
112+
'context' => $context,
113+
'checkoutSession' => $this->checkoutSession,
114+
'cart' => $this->cart,
115+
'scopeConfig' => $this->scopeConfig,
116+
'resultPageFactory' => $this->resultPageFactory
117+
]
118+
);
119+
}
120+
121+
public function testExecuteWithMessages()
122+
{
123+
$phrase = $this->getMockBuilder('Magento\Framework\Phrase')
124+
->disableOriginalConstructor()
125+
->getMock();
126+
$phrase->expects($this->once())
127+
->method('__toString')
128+
->willReturn('test_phrase');
129+
130+
$messages = [
131+
'',
132+
'test',
133+
$phrase
134+
];
135+
$this->cart->expects($this->any())
136+
->method('getQuote')
137+
->willReturn($this->quote);
138+
$this->quote->expects($this->once())
139+
->method('getItemsCount')
140+
->willReturn(0);
141+
$this->quote->expects($this->once())
142+
->method('getMessages')
143+
->willReturn($messages);
144+
$map = [
145+
['test'],
146+
['test_phrase']
147+
];
148+
$this->messageManager->expects($this->exactly(2))
149+
->method('addError')
150+
->willReturnMap($map);
151+
152+
$layout = $this->getMockBuilder('Magento\Framework\View\Layout')
153+
->disableOriginalConstructor()
154+
->getMock();
155+
$layout->expects($this->once())
156+
->method('initMessages');
157+
158+
$title = $this->getMockBuilder('Magento\Framework\View\Page\Title')
159+
->disableOriginalConstructor()
160+
->getMock();
161+
$title->expects($this->once())
162+
->method('set')
163+
->with('Shopping Cart');
164+
165+
$config = $this->getMockBuilder('Magento\Framework\View\Page\Config')
166+
->disableOriginalConstructor()
167+
->getMock();
168+
$config->expects($this->once())
169+
->method('getTitle')
170+
->willReturn($title);
171+
172+
$page = $this->getMockBuilder('Magento\Framework\View\Result\Page')
173+
->disableOriginalConstructor()
174+
->getMock();
175+
$page->expects($this->once())
176+
->method('getLayout')
177+
->willReturn($layout);
178+
$page->expects($this->once())
179+
->method('getConfig')
180+
->willReturn($config);
181+
182+
$this->resultPageFactory->expects($this->once())
183+
->method('create')
184+
->willReturn($page);
185+
$result = $this->controller->execute();
186+
$this->assertInstanceOf('Magento\Framework\View\Result\Page', $result);
187+
}
188+
}

0 commit comments

Comments
 (0)