diff --git a/app/code/Magento/Sales/Model/Service/OrderService.php b/app/code/Magento/Sales/Model/Service/OrderService.php index d187f7d8e4d24..1eb3fad11278f 100644 --- a/app/code/Magento/Sales/Model/Service/OrderService.php +++ b/app/code/Magento/Sales/Model/Service/OrderService.php @@ -87,7 +87,8 @@ public function __construct( public function cancel($id) { $order = $this->orderRepository->get($id); - if ((bool)$order->cancel()) { + if ($order->canCancel()) { + $order->cancel(); $this->orderRepository->save($order); return true; } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Service/OrderServiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/Service/OrderServiceTest.php index 14e1d3bac6a35..067f83d1e5b32 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Service/OrderServiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Service/OrderServiceTest.php @@ -163,9 +163,30 @@ public function testCancel() $this->orderMock->expects($this->once()) ->method('cancel') ->willReturn($this->orderMock); + $this->orderMock->expects($this->once()) + ->method('canCancel') + ->willReturn(true); $this->assertTrue($this->orderService->cancel(123)); } + /** + * test for Order::cancel() fail case + */ + public function testCancelFailed() + { + $this->orderRepositoryMock->expects($this->once()) + ->method('get') + ->with(123) + ->willReturn($this->orderMock); + $this->orderMock->expects($this->never()) + ->method('cancel') + ->willReturn($this->orderMock); + $this->orderMock->expects($this->once()) + ->method('canCancel') + ->willReturn(false); + $this->assertFalse($this->orderService->cancel(123)); + } + public function testGetCommentsList() { $this->filterBuilderMock->expects($this->once())