diff --git a/app/code/Magento/Sales/Controller/AbstractController/Reorder.php b/app/code/Magento/Sales/Controller/AbstractController/Reorder.php index 65cb537e89fec..f53ecaa625bf5 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/Reorder.php +++ b/app/code/Magento/Sales/Controller/AbstractController/Reorder.php @@ -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 { @@ -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); } @@ -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(); diff --git a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php new file mode 100644 index 0000000000000..964a10f232daf --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php @@ -0,0 +1,147 @@ +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()); + } +} diff --git a/app/code/Magento/Sales/i18n/en_US.csv b/app/code/Magento/Sales/i18n/en_US.csv index f30315437533f..970df2770a524 100644 --- a/app/code/Magento/Sales/i18n/en_US.csv +++ b/app/code/Magento/Sales/i18n/en_US.csv @@ -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."