Skip to content

Issue with reorder when disabled reorder setting from admin issue25130 #26051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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: 20 additions & 3 deletions app/code/Magento/Sales/Controller/AbstractController/Reorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Sales\Controller\AbstractController;

use Magento\Framework\App\Action;
use Magento\Framework\Registry;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Sales\Helper\Reorder as ReorderHelper;

/**
* Abstract class for controllers Reorder(Customer) and Reorder(Guest)
*
* @package Magento\Sales\Controller\AbstractController
*/
abstract class Reorder extends Action\Action implements HttpPostActionInterface
{
Expand All @@ -28,17 +30,27 @@ abstract class Reorder extends Action\Action implements HttpPostActionInterface
protected $_coreRegistry;

/**
* @var ReorderHelper
*/
private $reorderHelper;

/**
* Constructor
*
* @param Action\Context $context
* @param OrderLoaderInterface $orderLoader
* @param Registry $registry
* @param ReorderHelper|null $reorderHelper
*/
public function __construct(
Action\Context $context,
OrderLoaderInterface $orderLoader,
Registry $registry
Registry $registry,
ReorderHelper $reorderHelper = null
) {
$this->orderLoader = $orderLoader;
$this->_coreRegistry = $registry;
$this->reorderHelper = $reorderHelper ?: ObjectManager::getInstance()->get(ReorderHelper::class);
parent::__construct($context);
}

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

if (!$this->reorderHelper->canReorder($order->getId())) {
$this->messageManager->addErrorMessage(__("Reorder is not available."));
return $resultRedirect->setPath('checkout/cart');
}

/* @var $cart \Magento\Checkout\Model\Cart */
$cart = $this->_objectManager->get(\Magento\Checkout\Model\Cart::class);
$items = $order->getItemsCollection();
Expand Down
147 changes: 147 additions & 0 deletions app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Controller\Guest;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Sales\Controller\Guest\OrderLoader;
use Magento\Sales\Controller\Guest\Reorder;
use Magento\Sales\Helper\Reorder as ReorderHelper;
use Magento\Sales\Model\Order;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test class for Reorder
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ReorderTest extends TestCase
{
/**
* Stub Order Id
*/
private const STUB_ORDER_ID = 1;

/**
* @var Reorder
*/
private $reorder;

/**
* @var Registry|MockObject
*/
private $registryMock;

/**
* @var OrderLoader|MockObject
*/
private $orderLoaderMock;

/**
* @var RequestInterface|MockObject
*/
private $requestMock;

/**
* @var RedirectFactory|MockObject
*/
private $resultRedirectFactoryMock;

/**
* @var ReorderHelper|MockObject
*/
private $reorderHelperMock;

/**
* @var MessageManagerInterface|MockObject
*/
private $messageManagerMock;

/**
* Setup environment for test
*/
protected function setUp()
{
$contextMock = $this->createMock(Context::class);
$this->registryMock = $this->createMock(Registry::class);
$this->orderLoaderMock = $this->createMock(OrderLoader::class);
$this->requestMock = $this->createMock(RequestInterface::class);
$this->resultRedirectFactoryMock = $this->createMock(RedirectFactory::class);
$this->reorderHelperMock = $this->createMock(ReorderHelper::class);
$this->messageManagerMock = $this->createMock(MessageManagerInterface::class);

$contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
$contextMock->expects($this->once())->method('getResultRedirectFactory')
->willReturn($this->resultRedirectFactoryMock);
$contextMock->expects($this->once())->method('getMessageManager')
->willReturn($this->messageManagerMock);

$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
$objectManagerMock->expects($this->once())->method('get')
->with(ReorderHelper::class)
->willReturn($this->reorderHelperMock);

ObjectManager::setInstance($objectManagerMock);

$objectManager = new ObjectManagerHelper($this);
$this->reorder = $objectManager->getObject(
Reorder::class,
[
'context' => $contextMock,
'orderLoader' => $this->orderLoaderMock,
'registry' => $this->registryMock
]
);
}

/**
* Test execute() with the reorder is not allowed
*/
public function testExecuteWithReorderIsNotAllowed()
{
$orderMock = $this->createMock(Order::class);
$orderMock->method('getId')->willReturn(self::STUB_ORDER_ID);

$this->orderLoaderMock->method('load')
->with($this->requestMock)
->willReturn($this->resultRedirectFactoryMock);

$this->registryMock->expects($this->once())->method('registry')
->with('current_order')
->willReturn($orderMock);

$this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID)
->willReturn(false);

$resultRedirectMock = $this->createMock(Redirect::class);
$this->resultRedirectFactoryMock->expects($this->once())->method('create')->willReturn($resultRedirectMock);

$this->reorderHelperMock->method('canReorder')->with(self::STUB_ORDER_ID)
->willReturn(false);

/** Expected Error Message */
$this->messageManagerMock->expects($this->once())
->method('addErrorMessage')
->with('Reorder is not available.')->willReturnSelf();
$resultRedirectMock->expects($this->once())
->method('setPath')
->with('checkout/cart')->willReturnSelf();

/** Assert result */
$this->assertEquals($resultRedirectMock, $this->reorder->execute());
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Sales/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,4 @@ Refunds,Refunds
"Allow Zero GrandTotal","Allow Zero GrandTotal"
"Could not save the shipment tracking","Could not save the shipment tracking"
"Please enter a coupon code!","Please enter a coupon code!"
"Reorder is not available.","Reorder is not available."