Skip to content

Commit 1f3c7e0

Browse files
author
Olexandr Lysenko
committed
Merge branch 'MAGETWO-30913' into bugfixes
2 parents a1ab120 + fcc7d9b commit 1f3c7e0

File tree

2 files changed

+267
-5
lines changed
  • app/code/Magento/Sales/Controller/Adminhtml/Order
  • dev/tests/unit/testsuite/Magento/Sales/Controller/Adminhtml/Order/Create

2 files changed

+267
-5
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/Create.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,25 @@
1313
*/
1414
class Create extends \Magento\Backend\App\Action
1515
{
16+
/**
17+
* @var \Magento\Framework\Escaper
18+
*/
19+
protected $escaper;
20+
1621
/**
1722
* @param Action\Context $context
1823
* @param \Magento\Catalog\Helper\Product $productHelper
24+
* @param \Magento\Framework\Escaper $escaper
1925
*/
20-
public function __construct(Action\Context $context, \Magento\Catalog\Helper\Product $productHelper)
26+
public function __construct(
27+
Action\Context $context,
28+
\Magento\Catalog\Helper\Product $productHelper,
29+
\Magento\Framework\Escaper $escaper
30+
)
2131
{
2232
parent::__construct($context);
2333
$productHelper->setSkipSaleableCheck(true);
34+
$this->escaper = $escaper;
2435
}
2536

2637
/**
@@ -270,16 +281,33 @@ protected function _processActionData($action = null)
270281
if (isset($data) && isset($data['coupon']['code'])) {
271282
$couponCode = trim($data['coupon']['code']);
272283
}
284+
273285
if (!empty($couponCode)) {
274-
if ($this->_getQuote()->getCouponCode() !== $couponCode) {
286+
$isApplyDiscount = false;
287+
foreach ($this->_getQuote()->getAllItems() as $item) {
288+
if (!$item->getNoDiscount()) {
289+
$isApplyDiscount = true;
290+
break;
291+
}
292+
}
293+
if (!$isApplyDiscount) {
275294
$this->messageManager->addError(
276295
__(
277-
'"%1" coupon code is not valid.',
278-
$this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($couponCode)
296+
'"%1" coupon code was not applied. Do not apply discount is selected for item(s)',
297+
$this->escaper->escapeHtml($couponCode)
279298
)
280299
);
281300
} else {
282-
$this->messageManager->addSuccess(__('The coupon code has been accepted.'));
301+
if ($this->_getQuote()->getCouponCode() !== $couponCode) {
302+
$this->messageManager->addError(
303+
__(
304+
'"%1" coupon code is not valid.',
305+
$this->escaper->escapeHtml($couponCode)
306+
)
307+
);
308+
} else {
309+
$this->messageManager->addSuccess(__('The coupon code has been accepted.'));
310+
}
283311
}
284312
}
285313

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
4+
*/
5+
6+
namespace Magento\Sales\Controller\Adminhtml\Order\Create;
7+
8+
use Magento\TestFramework\Helper\ObjectManager as ObjectManagerHelper;
9+
10+
/**
11+
* Class ProcessDataTest
12+
*
13+
*/
14+
class ProcessDataTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var ProcessData
18+
*/
19+
protected $processData;
20+
21+
/**
22+
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $objectManager;
25+
26+
/**
27+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $request;
30+
31+
/**
32+
* @var \Magento\Backend\Model\Session\Quote|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $session;
35+
36+
/**
37+
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $eventManager;
40+
41+
/**
42+
* @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $actionFlag;
45+
46+
/**
47+
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $messageManager;
50+
51+
/**
52+
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
protected $escaper;
55+
56+
protected function setUp()
57+
{
58+
$objectManagerHelper = new ObjectManagerHelper($this);
59+
$context = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false);
60+
61+
$this->request = $this->getMockForAbstractClass(
62+
'Magento\Framework\App\RequestInterface',
63+
[],
64+
'',
65+
false,
66+
true,
67+
true,
68+
[
69+
'getParam',
70+
'getPost',
71+
'get',
72+
'has',
73+
'setModuleName',
74+
'setActionName',
75+
'initForward',
76+
'setDispatched',
77+
'getModuleName',
78+
'getActionName',
79+
'getCookie'
80+
]
81+
);
82+
$response = $this->getMockForAbstractClass(
83+
'Magento\Framework\App\ResponseInterface',
84+
[],
85+
'',
86+
false,
87+
true,
88+
true,
89+
[]
90+
);
91+
$context->expects($this->any())->method('getResponse')->willReturn($response);
92+
$context->expects($this->any())->method('getRequest')->willReturn($this->request);
93+
94+
$this->actionFlag = $this->getMock('Magento\Framework\App\ActionFlag', [], [], '', false);
95+
$context->expects($this->any())->method('getActionFlag')->willReturn($this->actionFlag);
96+
97+
$this->messageManager = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false);
98+
$context->expects($this->any())->method('getMessageManager')->willReturn($this->messageManager);
99+
100+
$this->eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false);
101+
$context->expects($this->any())->method('getEventManager')->willReturn($this->eventManager);
102+
103+
$this->objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface');
104+
$context->expects($this->any())->method('getObjectManager')->willReturn($this->objectManager);
105+
106+
$this->session = $this->getMock(
107+
'Magento\Backend\Model\Session\Quote',
108+
['setIsUrlNotice', 'getQuote'],
109+
[],
110+
'',
111+
false
112+
);
113+
$context->expects($this->any())->method('getSession')->willReturn($this->session);
114+
$this->escaper = $this->getMock('Magento\Framework\Escaper', ['escapeHtml'], [], '', false);
115+
116+
$this->processData = $objectManagerHelper->getObject(
117+
'Magento\Sales\Controller\Adminhtml\Order\Create\ProcessData',
118+
[
119+
'context' => $context,
120+
'escaper' => $this->escaper
121+
]
122+
);
123+
}
124+
125+
/**
126+
* @param bool $noDiscount
127+
* @param string $couponCode
128+
* @param string $errorMessage
129+
* @param string $actualCouponCode
130+
* @dataProvider isApplyDiscountDataProvider
131+
*/
132+
public function testExecute($noDiscount, $couponCode, $errorMessage, $actualCouponCode)
133+
{
134+
$quote = $this->getMock(
135+
'Magento\Sales\Model\Quote',
136+
['getCouponCode', 'isVirtual', 'getAllItems'],
137+
[],
138+
'',
139+
false
140+
);
141+
$create = $this->getMock('Magento\Sales\Model\AdminOrder\Create', [], [], '', false);
142+
143+
$paramReturnMap = [
144+
['customer_id', null, null],
145+
['store_id', null, null],
146+
['currency_id', null, null]
147+
];
148+
$this->request->expects($this->atLeastOnce())->method('getParam')->willReturnMap($paramReturnMap);
149+
150+
$objectManagerParamMap = [
151+
['Magento\Sales\Model\AdminOrder\Create', $create],
152+
['Magento\Backend\Model\Session\Quote', $this->session]
153+
];
154+
$this->objectManager->expects($this->atLeastOnce())->method('get')->willReturnMap($objectManagerParamMap);
155+
156+
$this->eventManager->expects($this->any())->method('dispatch');
157+
158+
$data = ['coupon' => ['code' => $couponCode]];
159+
$postReturnMap = [
160+
['order', $data],
161+
['reset_shipping', false],
162+
['collect_shipping_rates', false],
163+
['sidebar', false],
164+
['add_product', false],
165+
['', false],
166+
['update_items', false],
167+
['remove_item', 1],
168+
['from', 2],
169+
['move_item', 1],
170+
['to', 2],
171+
['qty', 3],
172+
['payment', false],
173+
[null, 'request'],
174+
['payment', false],
175+
['giftmessage', false],
176+
['add_products', false],
177+
['update_items', false],
178+
179+
];
180+
$this->request->expects($this->atLeastOnce())->method('getPost')->willReturnMap($postReturnMap);
181+
182+
$create->expects($this->once())->method('importPostData')->willReturnSelf();
183+
$create->expects($this->once())->method('initRuleData')->willReturnSelf();
184+
$create->expects($this->any())->method('getQuote')->willReturn($quote);
185+
186+
$address = $this->getMock('Magento\Sales\Model\Quote\Address', [], [], '', false);
187+
$create->expects($this->once())->method('getBillingAddress')->willReturn($address);
188+
189+
$quote->expects($this->any())->method('isVirtual')->willReturn(true);
190+
191+
$this->request->expects($this->once())->method('has')->with('item')->willReturn(false);
192+
193+
$create->expects($this->once())->method('saveQuote')->willReturnSelf();
194+
195+
$this->session->expects($this->any())->method('getQuote')->willReturn($quote);
196+
$item = $this->getMockForAbstractClass(
197+
'Magento\Eav\Model\Entity\Collection\AbstractCollection',
198+
[],
199+
'',
200+
false,
201+
true,
202+
true,
203+
['getNoDiscount']
204+
);
205+
$quote->expects($this->any())->method('getAllItems')->willReturn([$item]);
206+
$item->expects($this->any())->method('getNoDiscount')->willReturn($noDiscount);
207+
if (!$noDiscount) {
208+
$quote->expects($this->once())->method('getCouponCode')->willReturn($actualCouponCode);
209+
}
210+
211+
$errorMessageManager = __(
212+
$errorMessage,
213+
$couponCode
214+
);
215+
$this->escaper->expects($this->once())->method('escapeHtml')->with($couponCode)->willReturn($couponCode);
216+
217+
$this->messageManager->expects($this->once())->method('addError')->with($errorMessageManager)->willReturnSelf();
218+
219+
$this->actionFlag->expects($this->once())->method('get')->willReturn(false);
220+
$this->session->expects($this->once())->method('setIsUrlNotice')->with(false)->willReturn(false);
221+
$this->request->expects($this->once())->method('initForward')->willReturnSelf();
222+
$this->request->expects($this->once())->method('setActionName')->willReturnSelf();
223+
$this->request->expects($this->once())->method('setDispatched')->willReturnSelf();
224+
$this->assertNull($this->processData->execute());
225+
}
226+
227+
public function isApplyDiscountDataProvider()
228+
{
229+
return [
230+
[true, '123', '"%1" coupon code was not applied. Do not apply discount is selected for item(s)', null],
231+
[false, '123', '"%1" coupon code is not valid.', '132'],
232+
];
233+
}
234+
}

0 commit comments

Comments
 (0)