Skip to content

Commit 651a556

Browse files
🔃 [EngCom] Public Pull Requests - 2.3-develop
Accepted Public Pull Requests: - #14630: Refactor Mass Order Cancel code to use Interface (by @AnshuMishra17) - #14683: [Forwardport] Allow to configure min and max dates for date picker component (by @rostyslav-hymon)
2 parents 9f76f7c + fefe9c9 commit 651a556

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,37 @@
99
use Magento\Backend\App\Action\Context;
1010
use Magento\Ui\Component\MassAction\Filter;
1111
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
12+
use Magento\Sales\Api\OrderManagementInterface;
1213

1314
class MassCancel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
1415
{
1516
/**
1617
* Authorization level of a basic admin session
1718
*/
1819
const ADMIN_RESOURCE = 'Magento_Sales::cancel';
20+
21+
/**
22+
* @var OrderManagementInterface
23+
*/
24+
private $orderManagement;
1925

2026
/**
2127
* @param Context $context
2228
* @param Filter $filter
2329
* @param CollectionFactory $collectionFactory
30+
* @param OrderManagementInterface|null $orderManagement
2431
*/
25-
public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
26-
{
32+
public function __construct(
33+
Context $context,
34+
Filter $filter,
35+
CollectionFactory $collectionFactory,
36+
OrderManagementInterface $orderManagement = null
37+
) {
2738
parent::__construct($context, $filter);
2839
$this->collectionFactory = $collectionFactory;
40+
$this->orderManagement = $orderManagement ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
41+
\Magento\Sales\Api\OrderManagementInterface::class
42+
);
2943
}
3044

3145
/**
@@ -38,11 +52,10 @@ protected function massAction(AbstractCollection $collection)
3852
{
3953
$countCancelOrder = 0;
4054
foreach ($collection->getItems() as $order) {
41-
if (!$order->canCancel()) {
55+
$isCanceled = $this->orderManagement->cancel($order->getEntityId());
56+
if ($isCanceled === false) {
4257
continue;
4358
}
44-
$order->cancel();
45-
$order->save();
4659
$countCancelOrder++;
4760
}
4861
$countNonCancelOrder = $collection->count() - $countCancelOrder;

app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Order/MassCancelTest.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class MassCancelTest extends \PHPUnit\Framework\TestCase
8585
*/
8686
protected $filterMock;
8787

88+
/**
89+
* @var \PHPUnit_Framework_MockObject_MockObject
90+
*/
91+
private $orderManagementMock;
92+
8893
protected function setUp()
8994
{
9095
$objectManagerHelper = new ObjectManagerHelper($this);
@@ -145,12 +150,15 @@ protected function setUp()
145150
$this->orderCollectionFactoryMock->expects($this->once())
146151
->method('create')
147152
->willReturn($this->orderCollectionMock);
153+
$this->orderManagementMock = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);
154+
148155
$this->massAction = $objectManagerHelper->getObject(
149156
\Magento\Sales\Controller\Adminhtml\Order\MassCancel::class,
150157
[
151158
'context' => $this->contextMock,
152159
'filter' => $this->filterMock,
153-
'collectionFactory' => $this->orderCollectionFactoryMock
160+
'collectionFactory' => $this->orderCollectionFactoryMock,
161+
'orderManagement' => $this->orderManagementMock
154162
]
155163
);
156164
}
@@ -161,6 +169,9 @@ protected function setUp()
161169
*/
162170
public function testExecuteCanCancelOneOrder()
163171
{
172+
$order1id = 100;
173+
$order2id = 200;
174+
164175
$order1 = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
165176
->disableOriginalConstructor()
166177
->getMock();
@@ -175,20 +186,19 @@ public function testExecuteCanCancelOneOrder()
175186
->willReturn($orders);
176187

177188
$order1->expects($this->once())
178-
->method('canCancel')
179-
->willReturn(true);
180-
$order1->expects($this->once())
181-
->method('cancel');
182-
$order1->expects($this->once())
183-
->method('save');
189+
->method('getEntityId')
190+
->willReturn($order1id);
191+
192+
$order2->expects($this->once())
193+
->method('getEntityId')
194+
->willReturn($order2id);
184195

185196
$this->orderCollectionMock->expects($this->once())
186197
->method('count')
187198
->willReturn($countOrders);
188199

189-
$order2->expects($this->once())
190-
->method('canCancel')
191-
->willReturn(false);
200+
$this->orderManagementMock->expects($this->at(0))->method('cancel')->with($order1id)->willReturn(true);
201+
$this->orderManagementMock->expects($this->at(1))->method('cancel')->with($order2id)->willReturn(false);
192202

193203
$this->messageManagerMock->expects($this->once())
194204
->method('addError')
@@ -222,21 +232,23 @@ public function testExcludedCannotCancelOrders()
222232
$orders = [$order1, $order2];
223233
$countOrders = count($orders);
224234

235+
$order1->expects($this->once())
236+
->method('getEntityId')
237+
->willReturn(100);
238+
239+
$order2->expects($this->once())
240+
->method('getEntityId')
241+
->willReturn(200);
242+
225243
$this->orderCollectionMock->expects($this->any())
226244
->method('getItems')
227245
->willReturn([$order1, $order2]);
228246

229-
$order1->expects($this->once())
230-
->method('canCancel')
231-
->willReturn(false);
232-
233247
$this->orderCollectionMock->expects($this->once())
234248
->method('count')
235249
->willReturn($countOrders);
236250

237-
$order2->expects($this->once())
238-
->method('canCancel')
239-
->willReturn(false);
251+
$this->orderManagementMock->expects($this->atLeastOnce())->method('cancel')->willReturn(false);
240252

241253
$this->messageManagerMock->expects($this->once())
242254
->method('addError')
@@ -265,11 +277,10 @@ public function testException()
265277
->willReturn([$order1]);
266278

267279
$order1->expects($this->once())
268-
->method('canCancel')
269-
->willReturn(true);
270-
$order1->expects($this->once())
271-
->method('cancel')
272-
->willThrowException($exception);
280+
->method('getEntityId')
281+
->willReturn(100);
282+
283+
$this->orderManagementMock->expects($this->atLeastOnce())->method('cancel')->willThrowException($exception);
273284

274285
$this->messageManagerMock->expects($this->once())
275286
->method('addError')

lib/internal/Magento/Framework/Data/Form/Element/Date.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public function getElementHtml()
167167
'buttonImage' => $this->getImage(),
168168
'buttonText' => 'Select Date',
169169
'disabled' => $this->getDisabled(),
170+
'minDate' => $this->getMinDate(),
171+
'maxDate' => $this->getMaxDate(),
170172
],
171173
]
172174
)

0 commit comments

Comments
 (0)