From 689c1f62069f7cb4520820013cebcb3b8b906dc6 Mon Sep 17 00:00:00 2001 From: Eden Date: Sun, 15 Dec 2019 11:26:12 +0700 Subject: [PATCH 1/5] Issue with reorder when disabled reorder setting from admin issue25130 --- .../Controller/AbstractController/Reorder.php | 23 ++- .../Unit/Controller/Guest/ReorderTest.php | 141 ++++++++++++++++++ app/code/Magento/Sales/i18n/en_US.csv | 2 + 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php diff --git a/app/code/Magento/Sales/Controller/AbstractController/Reorder.php b/app/code/Magento/Sales/Controller/AbstractController/Reorder.php index 65cb537e89fec..9a3c710c60b9f 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/Reorder.php +++ b/app/code/Magento/Sales/Controller/AbstractController/Reorder.php @@ -4,16 +4,20 @@ * 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 + * Class Magento\Sales\Controller\AbstractController\Reorder */ abstract class Reorder extends Action\Action implements HttpPostActionInterface { @@ -28,17 +32,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 +71,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..62c3b46379ae5 --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php @@ -0,0 +1,141 @@ +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); + + $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock); + $this->contextMock->expects($this->once())->method('getResultRedirectFactory') + ->willReturn($this->resultRedirectFactoryMock); + $this->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' => $this->contextMock, + 'orderLoader' => $this->orderLoaderMock, + 'registry' => $this->registryMock + ] + ); + } + + /** + * Test execute() with the reorder is not allowed + */ + public function testExecuteWithReorderIsNotAllowed() + { + $this->orderLoaderMock->method('load') + ->with($this->requestMock) + ->willReturn($this->resultRedirectFactoryMock); + $orderMock = $this->createMock(Order::class); + $orderMock->method('getId')->willReturn(self::STUB_ORDER_ID); + $this->registryMock->expects($this->once())->method('registry') + ->with('current_order') + ->willReturn($orderMock); + + $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..e468235ee38ed 100644 --- a/app/code/Magento/Sales/i18n/en_US.csv +++ b/app/code/Magento/Sales/i18n/en_US.csv @@ -799,3 +799,5 @@ 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!" +"Please enter a coupon code!","Please enter a coupon code!" +"Reorder is not available.","Reorder is not available." From 3e456b29a421a03f635f0d22701bf378c935f9b0 Mon Sep 17 00:00:00 2001 From: Eden Date: Sun, 15 Dec 2019 12:30:00 +0700 Subject: [PATCH 2/5] Fix static test --- .../Test/Unit/Controller/Guest/ReorderTest.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php index 62c3b46379ae5..1ba8ba0638cca 100644 --- a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php @@ -36,11 +36,6 @@ class ReorderTest extends TestCase */ private $reorder; - /** - * @var Context|MockObject - */ - private $contextMock; - /** * @var Registry|MockObject */ @@ -76,7 +71,7 @@ class ReorderTest extends TestCase */ protected function setUp() { - $this->contextMock = $this->createMock(Context::class); + $contextMock = $this->createMock(Context::class); $this->registryMock = $this->createMock(Registry::class); $this->orderLoaderMock = $this->createMock(OrderLoader::class); $this->requestMock = $this->createMock(RequestInterface::class); @@ -84,10 +79,10 @@ protected function setUp() $this->reorderHelperMock = $this->createMock(ReorderHelper::class); $this->messageManagerMock = $this->createMock(MessageManagerInterface::class); - $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock); - $this->contextMock->expects($this->once())->method('getResultRedirectFactory') + $contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock); + $contextMock->expects($this->once())->method('getResultRedirectFactory') ->willReturn($this->resultRedirectFactoryMock); - $this->contextMock->expects($this->once())->method('getMessageManager') + $contextMock->expects($this->once())->method('getMessageManager') ->willReturn($this->messageManagerMock); $objectManagerMock = $this->createMock(ObjectManagerInterface::class); @@ -101,7 +96,7 @@ protected function setUp() $this->reorder = $objectManager->getObject( Reorder::class, [ - 'context' => $this->contextMock, + 'context' => $contextMock, 'orderLoader' => $this->orderLoaderMock, 'registry' => $this->registryMock ] From 8928539dea7d79501b888d2affdd00220952bb72 Mon Sep 17 00:00:00 2001 From: Eden Date: Sun, 15 Dec 2019 14:39:46 +0700 Subject: [PATCH 3/5] Fix static test --- .../Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php index 1ba8ba0638cca..c8d9ae53d4e47 100644 --- a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php @@ -24,6 +24,11 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +/** + * Test class for Reorder + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class ReorderTest extends TestCase { /** From 74ffaf38a237204517d51e6cfd21421949335979 Mon Sep 17 00:00:00 2001 From: Eden Date: Sun, 15 Dec 2019 17:14:59 +0700 Subject: [PATCH 4/5] Fix static test --- .../Sales/Controller/AbstractController/Reorder.php | 2 -- .../Sales/Test/Unit/Controller/Guest/ReorderTest.php | 10 ++++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Controller/AbstractController/Reorder.php b/app/code/Magento/Sales/Controller/AbstractController/Reorder.php index 9a3c710c60b9f..f53ecaa625bf5 100644 --- a/app/code/Magento/Sales/Controller/AbstractController/Reorder.php +++ b/app/code/Magento/Sales/Controller/AbstractController/Reorder.php @@ -16,8 +16,6 @@ /** * Abstract class for controllers Reorder(Customer) and Reorder(Guest) - * - * Class Magento\Sales\Controller\AbstractController\Reorder */ abstract class Reorder extends Action\Action implements HttpPostActionInterface { diff --git a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php index c8d9ae53d4e47..964a10f232daf 100644 --- a/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Controller/Guest/ReorderTest.php @@ -113,17 +113,23 @@ protected function setUp() */ 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); - $orderMock = $this->createMock(Order::class); - $orderMock->method('getId')->willReturn(self::STUB_ORDER_ID); + $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); From c4baf83e4d811ba436b4fdb1205f9abdf4f655f9 Mon Sep 17 00:00:00 2001 From: Eden Date: Wed, 18 Dec 2019 15:57:24 +0700 Subject: [PATCH 5/5] Remove redundant line --- app/code/Magento/Sales/i18n/en_US.csv | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Sales/i18n/en_US.csv b/app/code/Magento/Sales/i18n/en_US.csv index e468235ee38ed..970df2770a524 100644 --- a/app/code/Magento/Sales/i18n/en_US.csv +++ b/app/code/Magento/Sales/i18n/en_US.csv @@ -799,5 +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!" -"Please enter a coupon code!","Please enter a coupon code!" "Reorder is not available.","Reorder is not available."