Skip to content

Commit a51aeeb

Browse files
ENGCOM-6492: Issue with reorder when disabled reorder setting from admin issue25130 #26051
2 parents 4d0bb13 + 756e059 commit a51aeeb

File tree

3 files changed

+168
-3
lines changed

3 files changed

+168
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
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)
15-
*
16-
* @package Magento\Sales\Controller\AbstractController
1719
*/
1820
abstract class Reorder extends Action\Action implements HttpPostActionInterface
1921
{
@@ -28,17 +30,27 @@ abstract class Reorder extends Action\Action implements HttpPostActionInterface
2830
protected $_coreRegistry;
2931

3032
/**
33+
* @var ReorderHelper
34+
*/
35+
private $reorderHelper;
36+
37+
/**
38+
* Constructor
39+
*
3140
* @param Action\Context $context
3241
* @param OrderLoaderInterface $orderLoader
3342
* @param Registry $registry
43+
* @param ReorderHelper|null $reorderHelper
3444
*/
3545
public function __construct(
3646
Action\Context $context,
3747
OrderLoaderInterface $orderLoader,
38-
Registry $registry
48+
Registry $registry,
49+
ReorderHelper $reorderHelper = null
3950
) {
4051
$this->orderLoader = $orderLoader;
4152
$this->_coreRegistry = $registry;
53+
$this->reorderHelper = $reorderHelper ?: ObjectManager::getInstance()->get(ReorderHelper::class);
4254
parent::__construct($context);
4355
}
4456

@@ -57,6 +69,11 @@ public function execute()
5769
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
5870
$resultRedirect = $this->resultRedirectFactory->create();
5971

72+
if (!$this->reorderHelper->canReorder($order->getId())) {
73+
$this->messageManager->addErrorMessage(__("Reorder is not available."));
74+
return $resultRedirect->setPath('checkout/cart');
75+
}
76+
6077
/* @var $cart \Magento\Checkout\Model\Cart */
6178
$cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
6279
$items = $order->getItemsCollection();
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
/**
28+
* Test class for Reorder
29+
*
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class ReorderTest extends TestCase
33+
{
34+
/**
35+
* Stub Order Id
36+
*/
37+
private const STUB_ORDER_ID = 1;
38+
39+
/**
40+
* @var Reorder
41+
*/
42+
private $reorder;
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+
$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+
$contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
88+
$contextMock->expects($this->once())->method('getResultRedirectFactory')
89+
->willReturn($this->resultRedirectFactoryMock);
90+
$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' => $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+
$orderMock = $this->createMock(Order::class);
117+
$orderMock->method('getId')->willReturn(self::STUB_ORDER_ID);
118+
119+
$this->orderLoaderMock->method('load')
120+
->with($this->requestMock)
121+
->willReturn($this->resultRedirectFactoryMock);
122+
123+
$this->registryMock->expects($this->once())->method('registry')
124+
->with('current_order')
125+
->willReturn($orderMock);
126+
127+
$this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID)
128+
->willReturn(false);
129+
130+
$resultRedirectMock = $this->createMock(Redirect::class);
131+
$this->resultRedirectFactoryMock->expects($this->once())->method('create')->willReturn($resultRedirectMock);
132+
133+
$this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID)
134+
->willReturn(false);
135+
136+
/** Expected Error Message */
137+
$this->messageManagerMock->expects($this->once())
138+
->method('addErrorMessage')
139+
->with('Reorder is not available.')->willReturnSelf();
140+
$resultRedirectMock->expects($this->once())
141+
->method('setPath')
142+
->with('checkout/cart')->willReturnSelf();
143+
144+
/** Assert result */
145+
$this->assertEquals($resultRedirectMock, $this->reorder->execute());
146+
}
147+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,4 @@ 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+
"Reorder is not available.","Reorder is not available."

0 commit comments

Comments
 (0)