Skip to content

Commit 821efa9

Browse files
author
Oleksii Korshenko
authored
MAGETWO-81428: Prevent invoice cancelation multiple times 2.2-develop [Backport] #11261
2 parents 83a8ef5 + 03f71c1 commit 821efa9

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

app/code/Magento/Sales/Model/Order/Invoice.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ public function void()
407407
*/
408408
public function cancel()
409409
{
410+
if (!$this->canCancel()) {
411+
return $this;
412+
}
410413
$order = $this->getOrder();
411414
$order->getPayment()->cancelInvoice($this);
412415
foreach ($this->getAllItems() as $item) {

app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceTest.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\Sales\Test\Unit\Model\Order;
1010

11+
use Magento\Sales\Api\Data\InvoiceInterface;
1112
use Magento\Sales\Model\Order\Invoice;
1213
use Magento\Sales\Model\ResourceModel\OrderFactory;
1314
use Magento\Sales\Model\Order;
@@ -72,7 +73,7 @@ protected function setUp()
7273
->setMethods(
7374
[
7475
'getPayment', '__wakeup', 'load', 'setHistoryEntityName', 'getStore', 'getBillingAddress',
75-
'getShippingAddress'
76+
'getShippingAddress', 'getConfig',
7677
]
7778
)
7879
->getMock();
@@ -83,7 +84,7 @@ protected function setUp()
8384
$this->paymentMock = $this->getMockBuilder(
8485
\Magento\Sales\Model\Order\Payment::class
8586
)->disableOriginalConstructor()->setMethods(
86-
['canVoid', '__wakeup', 'canCapture', 'capture', 'pay']
87+
['canVoid', '__wakeup', 'canCapture', 'capture', 'pay', 'cancelInvoice']
8788
)->getMock();
8889

8990
$this->orderFactory = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, ['create']);
@@ -407,4 +408,58 @@ private function getOrderInvoiceCollection()
407408

408409
return $collection;
409410
}
411+
412+
/**
413+
* Assert open invoice can be canceled, and its status changes
414+
*/
415+
public function testCancelOpenInvoice()
416+
{
417+
$orderConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Config::class)
418+
->disableOriginalConstructor()->setMethods(
419+
['getStateDefaultStatus']
420+
)->getMock();
421+
$orderConfigMock->expects($this->once())->method('getStateDefaultStatus')
422+
->with(Order::STATE_PROCESSING)
423+
->willReturn(Order::STATE_PROCESSING);
424+
$this->order->expects($this->once())->method('getPayment')->willReturn($this->paymentMock);
425+
$this->order->expects($this->once())->method('getConfig')->willReturn($orderConfigMock);
426+
$this->paymentMock->expects($this->once())->method('cancelInvoice')->willReturn($this->paymentMock);
427+
$this->eventManagerMock->expects($this->once())
428+
->method('dispatch')
429+
->with('sales_order_invoice_cancel');
430+
$this->model->setData(InvoiceInterface::ITEMS, []);
431+
$this->model->setState(Invoice::STATE_OPEN);
432+
$this->model->cancel();
433+
self::assertEquals(Invoice::STATE_CANCELED, $this->model->getState());
434+
}
435+
436+
/**
437+
* Assert open invoice can be canceled, and its status changes
438+
*
439+
* @param $initialInvoiceStatus
440+
* @param $finalInvoiceStatus
441+
* @dataProvider getNotOpenedInvoiceStatuses
442+
*/
443+
public function testCannotCancelNotOpenedInvoice($initialInvoiceStatus, $finalInvoiceStatus)
444+
{
445+
$this->order->expects($this->never())->method('getPayment');
446+
$this->paymentMock->expects($this->never())->method('cancelInvoice');
447+
$this->eventManagerMock->expects($this->never())
448+
->method('dispatch')
449+
->with('sales_order_invoice_cancel');
450+
$this->model->setState($initialInvoiceStatus);
451+
$this->model->cancel();
452+
self::assertEquals($finalInvoiceStatus, $this->model->getState());
453+
}
454+
455+
/**
456+
* @return array
457+
*/
458+
public function getNotOpenedInvoiceStatuses()
459+
{
460+
return [
461+
[Invoice::STATE_PAID, Invoice::STATE_PAID],
462+
[Invoice::STATE_CANCELED, Invoice::STATE_CANCELED],
463+
];
464+
}
410465
}

0 commit comments

Comments
 (0)