Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions app/code/Magento/Sales/Controller/Adminhtml/Order/MassCancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,37 @@
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Sales\Api\OrderManagementInterface;

class MassCancel extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
{
/**
* Authorization level of a basic admin session
*/
const ADMIN_RESOURCE = 'Magento_Sales::cancel';

/**
* @var OrderManagementInterface
*/
private $orderManagement;

/**
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param OrderManagementInterface|null $orderManagement
*/
public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
{
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory,
OrderManagementInterface $orderManagement = null
) {
parent::__construct($context, $filter);
$this->collectionFactory = $collectionFactory;
$this->orderManagement = $orderManagement ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Sales\Api\OrderManagementInterface::class
);
}

/**
Expand All @@ -38,11 +52,10 @@ protected function massAction(AbstractCollection $collection)
{
$countCancelOrder = 0;
foreach ($collection->getItems() as $order) {
if (!$order->canCancel()) {
$isCanceled = $this->orderManagement->cancel($order->getEntityId());
if ($isCanceled === false) {
continue;
}
$order->cancel();
$order->save();
$countCancelOrder++;
}
$countNonCancelOrder = $collection->count() - $countCancelOrder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class MassCancelTest extends \PHPUnit\Framework\TestCase
*/
protected $filterMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $orderManagementMock;

protected function setUp()
{
$objectManagerHelper = new ObjectManagerHelper($this);
Expand Down Expand Up @@ -145,12 +150,15 @@ protected function setUp()
$this->orderCollectionFactoryMock->expects($this->once())
->method('create')
->willReturn($this->orderCollectionMock);
$this->orderManagementMock = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);

$this->massAction = $objectManagerHelper->getObject(
\Magento\Sales\Controller\Adminhtml\Order\MassCancel::class,
[
'context' => $this->contextMock,
'filter' => $this->filterMock,
'collectionFactory' => $this->orderCollectionFactoryMock
'collectionFactory' => $this->orderCollectionFactoryMock,
'orderManagement' => $this->orderManagementMock
]
);
}
Expand All @@ -161,6 +169,9 @@ protected function setUp()
*/
public function testExecuteCanCancelOneOrder()
{
$order1id = 100;
$order2id = 200;

$order1 = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -175,20 +186,19 @@ public function testExecuteCanCancelOneOrder()
->willReturn($orders);

$order1->expects($this->once())
->method('canCancel')
->willReturn(true);
$order1->expects($this->once())
->method('cancel');
$order1->expects($this->once())
->method('save');
->method('getEntityId')
->willReturn($order1id);

$order2->expects($this->once())
->method('getEntityId')
->willReturn($order2id);

$this->orderCollectionMock->expects($this->once())
->method('count')
->willReturn($countOrders);

$order2->expects($this->once())
->method('canCancel')
->willReturn(false);
$this->orderManagementMock->expects($this->at(0))->method('cancel')->with($order1id)->willReturn(true);
$this->orderManagementMock->expects($this->at(1))->method('cancel')->with($order2id)->willReturn(false);

$this->messageManagerMock->expects($this->once())
->method('addError')
Expand Down Expand Up @@ -222,21 +232,23 @@ public function testExcludedCannotCancelOrders()
$orders = [$order1, $order2];
$countOrders = count($orders);

$order1->expects($this->once())
->method('getEntityId')
->willReturn(100);

$order2->expects($this->once())
->method('getEntityId')
->willReturn(200);

$this->orderCollectionMock->expects($this->any())
->method('getItems')
->willReturn([$order1, $order2]);

$order1->expects($this->once())
->method('canCancel')
->willReturn(false);

$this->orderCollectionMock->expects($this->once())
->method('count')
->willReturn($countOrders);

$order2->expects($this->once())
->method('canCancel')
->willReturn(false);
$this->orderManagementMock->expects($this->atLeastOnce())->method('cancel')->willReturn(false);

$this->messageManagerMock->expects($this->once())
->method('addError')
Expand Down Expand Up @@ -265,11 +277,10 @@ public function testException()
->willReturn([$order1]);

$order1->expects($this->once())
->method('canCancel')
->willReturn(true);
$order1->expects($this->once())
->method('cancel')
->willThrowException($exception);
->method('getEntityId')
->willReturn(100);

$this->orderManagementMock->expects($this->atLeastOnce())->method('cancel')->willThrowException($exception);

$this->messageManagerMock->expects($this->once())
->method('addError')
Expand Down