Skip to content

Commit 689c1f6

Browse files
committed
Issue with reorder when disabled reorder setting from admin issue25130
1 parent 1ad65a3 commit 689c1f6

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

app/code/Magento/Sales/Controller/AbstractController/Reorder.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Sales\Controller\AbstractController;
810

911
use Magento\Framework\App\Action;
1012
use Magento\Framework\Registry;
1113
use Magento\Framework\App\Action\HttpPostActionInterface;
14+
use Magento\Framework\App\ObjectManager;
15+
use Magento\Sales\Helper\Reorder as ReorderHelper;
1216

1317
/**
1418
* Abstract class for controllers Reorder(Customer) and Reorder(Guest)
1519
*
16-
* @package Magento\Sales\Controller\AbstractController
20+
* Class Magento\Sales\Controller\AbstractController\Reorder
1721
*/
1822
abstract class Reorder extends Action\Action implements HttpPostActionInterface
1923
{
@@ -28,17 +32,27 @@ abstract class Reorder extends Action\Action implements HttpPostActionInterface
2832
protected $_coreRegistry;
2933

3034
/**
35+
* @var ReorderHelper
36+
*/
37+
private $reorderHelper;
38+
39+
/**
40+
* Constructor
41+
*
3142
* @param Action\Context $context
3243
* @param OrderLoaderInterface $orderLoader
3344
* @param Registry $registry
45+
* @param ReorderHelper|null $reorderHelper
3446
*/
3547
public function __construct(
3648
Action\Context $context,
3749
OrderLoaderInterface $orderLoader,
38-
Registry $registry
50+
Registry $registry,
51+
ReorderHelper $reorderHelper = null
3952
) {
4053
$this->orderLoader = $orderLoader;
4154
$this->_coreRegistry = $registry;
55+
$this->reorderHelper = $reorderHelper ?: ObjectManager::getInstance()->get(ReorderHelper::class);
4256
parent::__construct($context);
4357
}
4458

@@ -57,6 +71,11 @@ public function execute()
5771
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
5872
$resultRedirect = $this->resultRedirectFactory->create();
5973

74+
if (!$this->reorderHelper->canReorder($order->getId())) {
75+
$this->messageManager->addErrorMessage(__("Reorder is not available."));
76+
return $resultRedirect->setPath('checkout/cart');
77+
}
78+
6079
/* @var $cart \Magento\Checkout\Model\Cart */
6180
$cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
6281
$items = $order->getItemsCollection();
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Sales\Test\Unit\Controller\Guest;
10+
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\Controller\Result\Redirect;
15+
use Magento\Framework\Controller\Result\RedirectFactory;
16+
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
17+
use Magento\Framework\ObjectManagerInterface;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
use Magento\Sales\Controller\Guest\OrderLoader;
21+
use Magento\Sales\Controller\Guest\Reorder;
22+
use Magento\Sales\Helper\Reorder as ReorderHelper;
23+
use Magento\Sales\Model\Order;
24+
use PHPUnit\Framework\MockObject\MockObject;
25+
use PHPUnit\Framework\TestCase;
26+
27+
class ReorderTest extends TestCase
28+
{
29+
/**
30+
* Stub Order Id
31+
*/
32+
private const STUB_ORDER_ID = 1;
33+
34+
/**
35+
* @var Reorder
36+
*/
37+
private $reorder;
38+
39+
/**
40+
* @var Context|MockObject
41+
*/
42+
private $contextMock;
43+
44+
/**
45+
* @var Registry|MockObject
46+
*/
47+
private $registryMock;
48+
49+
/**
50+
* @var OrderLoader|MockObject
51+
*/
52+
private $orderLoaderMock;
53+
54+
/**
55+
* @var RequestInterface|MockObject
56+
*/
57+
private $requestMock;
58+
59+
/**
60+
* @var RedirectFactory|MockObject
61+
*/
62+
private $resultRedirectFactoryMock;
63+
64+
/**
65+
* @var ReorderHelper|MockObject
66+
*/
67+
private $reorderHelperMock;
68+
69+
/**
70+
* @var MessageManagerInterface|MockObject
71+
*/
72+
private $messageManagerMock;
73+
74+
/**
75+
* Setup environment for test
76+
*/
77+
protected function setUp()
78+
{
79+
$this->contextMock = $this->createMock(Context::class);
80+
$this->registryMock = $this->createMock(Registry::class);
81+
$this->orderLoaderMock = $this->createMock(OrderLoader::class);
82+
$this->requestMock = $this->createMock(RequestInterface::class);
83+
$this->resultRedirectFactoryMock = $this->createMock(RedirectFactory::class);
84+
$this->reorderHelperMock = $this->createMock(ReorderHelper::class);
85+
$this->messageManagerMock = $this->createMock(MessageManagerInterface::class);
86+
87+
$this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
88+
$this->contextMock->expects($this->once())->method('getResultRedirectFactory')
89+
->willReturn($this->resultRedirectFactoryMock);
90+
$this->contextMock->expects($this->once())->method('getMessageManager')
91+
->willReturn($this->messageManagerMock);
92+
93+
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
94+
$objectManagerMock->expects($this->once())->method('get')
95+
->with(ReorderHelper::class)
96+
->willReturn($this->reorderHelperMock);
97+
98+
ObjectManager::setInstance($objectManagerMock);
99+
100+
$objectManager = new ObjectManagerHelper($this);
101+
$this->reorder = $objectManager->getObject(
102+
Reorder::class,
103+
[
104+
'context' => $this->contextMock,
105+
'orderLoader' => $this->orderLoaderMock,
106+
'registry' => $this->registryMock
107+
]
108+
);
109+
}
110+
111+
/**
112+
* Test execute() with the reorder is not allowed
113+
*/
114+
public function testExecuteWithReorderIsNotAllowed()
115+
{
116+
$this->orderLoaderMock->method('load')
117+
->with($this->requestMock)
118+
->willReturn($this->resultRedirectFactoryMock);
119+
$orderMock = $this->createMock(Order::class);
120+
$orderMock->method('getId')->willReturn(self::STUB_ORDER_ID);
121+
$this->registryMock->expects($this->once())->method('registry')
122+
->with('current_order')
123+
->willReturn($orderMock);
124+
125+
$resultRedirectMock = $this->createMock(Redirect::class);
126+
$this->resultRedirectFactoryMock->expects($this->once())->method('create')->willReturn($resultRedirectMock);
127+
$this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID)
128+
->willReturn(false);
129+
130+
/** Expected Error Message */
131+
$this->messageManagerMock->expects($this->once())
132+
->method('addErrorMessage')
133+
->with('Reorder is not available.')->willReturnSelf();
134+
$resultRedirectMock->expects($this->once())
135+
->method('setPath')
136+
->with('checkout/cart')->willReturnSelf();
137+
138+
/** Assert result */
139+
$this->assertEquals($resultRedirectMock, $this->reorder->execute());
140+
}
141+
}

app/code/Magento/Sales/i18n/en_US.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,5 @@ Refunds,Refunds
799799
"Allow Zero GrandTotal","Allow Zero GrandTotal"
800800
"Could not save the shipment tracking","Could not save the shipment tracking"
801801
"Please enter a coupon code!","Please enter a coupon code!"
802+
"Please enter a coupon code!","Please enter a coupon code!"
803+
"Reorder is not available.","Reorder is not available."

0 commit comments

Comments
 (0)