Skip to content

Commit 6a14ccf

Browse files
committed
Merge branch 'develop' of https://github.corp.ebay.com/magento2/magento2ce into develop
2 parents 2654240 + 94f9665 commit 6a14ccf

File tree

2 files changed

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

2 files changed

+268
-6
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@
1414
*/
1515
class Create extends \Magento\Backend\App\Action
1616
{
17+
/**
18+
* @var \Magento\Framework\Escaper
19+
*/
20+
protected $escaper;
21+
1722
/**
1823
* @param Action\Context $context
1924
* @param \Magento\Catalog\Helper\Product $productHelper
25+
* @param \Magento\Framework\Escaper $escaper
2026
*/
21-
public function __construct(Action\Context $context, \Magento\Catalog\Helper\Product $productHelper)
22-
{
27+
public function __construct(
28+
Action\Context $context,
29+
\Magento\Catalog\Helper\Product $productHelper,
30+
\Magento\Framework\Escaper $escaper
31+
) {
2332
parent::__construct($context);
2433
$productHelper->setSkipSaleableCheck(true);
34+
$this->escaper = $escaper;
2535
}
2636

2737
/**
@@ -271,16 +281,33 @@ protected function _processActionData($action = null)
271281
if (isset($data) && isset($data['coupon']['code'])) {
272282
$couponCode = trim($data['coupon']['code']);
273283
}
284+
274285
if (!empty($couponCode)) {
275-
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) {
276294
$this->messageManager->addError(
277295
__(
278-
'"%1" coupon code is not valid.',
279-
$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)
280298
)
281299
);
282300
} else {
283-
$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+
}
284311
}
285312
}
286313

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

0 commit comments

Comments
 (0)