Skip to content

Commit 9bd2b73

Browse files
committed
MC-36907: Storefront: Create reorder from customer profile page.
1 parent e1c5c1d commit 9bd2b73

11 files changed

+649
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Checkout\Model\Type\Onepage;
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\Data\AddressInterface;
14+
use Magento\Quote\Api\Data\AddressInterfaceFactory;
15+
use Magento\Quote\Api\Data\CartInterface;
16+
use Magento\Quote\Api\Data\CartInterfaceFactory;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
19+
20+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
21+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_address.php');
22+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_duplicated.php');
23+
24+
$objectManager = Bootstrap::getObjectManager();
25+
/** @var ProductRepositoryInterface $productRepository */
26+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
27+
$productRepository->cleanCache();
28+
/** @var CartRepositoryInterface $quoteRepository */
29+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
30+
/** @var AddressInterface $quoteShippingAddress */
31+
$quoteShippingAddress = $objectManager->get(AddressInterfaceFactory::class)->create();
32+
/** @var CustomerRepositoryInterface $customerRepository */
33+
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
34+
/** @var AddressRepositoryInterface $addressRepository */
35+
$addressRepository = $objectManager->get(AddressRepositoryInterface::class);
36+
$quoteShippingAddress->importCustomerAddressData($addressRepository->getById(1));
37+
$customer = $customerRepository->getById(1);
38+
39+
/** @var CartInterface $quote */
40+
$quote = $objectManager->get(CartInterfaceFactory::class)->create();
41+
$quote->setStoreId(1)
42+
->setIsActive(true)
43+
->setIsMultiShipping(0)
44+
->assignCustomerWithAddressChange($customer)
45+
->setShippingAddress($quoteShippingAddress)
46+
->setBillingAddress($quoteShippingAddress)
47+
->setCheckoutMethod(Onepage::METHOD_CUSTOMER)
48+
->setReservedOrderId('55555555')
49+
->setEmail($customer->getEmail());
50+
$quote->addProduct($productRepository->get('simple-1'), 55);
51+
$quote->getShippingAddress()->setShippingMethod('flatrate_flatrate');
52+
$quote->getShippingAddress()->setCollectShippingRates(true);
53+
$quote->getShippingAddress()->collectShippingRates();
54+
$quote->getPayment()->setMethod('checkmo');
55+
$quoteRepository->save($quote);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Quote\Api\CartRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
11+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
/** @var CartRepositoryInterface $quoteRepository */
15+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
16+
/** @var GetQuoteByReservedOrderId $getQuoteByReservedOrderId */
17+
$getQuoteByReservedOrderId = $objectManager->get(GetQuoteByReservedOrderId::class);
18+
$quote = $getQuoteByReservedOrderId->execute('55555555');
19+
if ($quote) {
20+
$quoteRepository->delete($quote);
21+
}
22+
23+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_duplicated_rollback.php');
24+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_duplicated_rollback.php');
25+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_address_rollback.php');
26+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_rollback.php');
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Controller\Guest;
9+
10+
use Magento\Checkout\Model\Session as CheckoutSession;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\Framework\Stdlib\CookieManagerInterface;
15+
use Magento\Quote\Api\CartRepositoryInterface;
16+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
17+
use Magento\Sales\Helper\Guest;
18+
use Magento\TestFramework\Request;
19+
use Magento\TestFramework\TestCase\AbstractController;
20+
21+
/**
22+
* Test for guest reorder controller.
23+
*
24+
* @see \Magento\Sales\Controller\Guest\Reorder
25+
* @magentoAppArea frontend
26+
* @magentoDbIsolation enabled
27+
*/
28+
class ReorderTest extends AbstractController
29+
{
30+
/** @var CheckoutSession */
31+
private $checkoutSession;
32+
33+
/** @var OrderInterfaceFactory */
34+
private $orderFactory;
35+
36+
/** @var CookieManagerInterface */
37+
private $cookieManager;
38+
39+
/** @var Session */
40+
private $customerSession;
41+
42+
/** @var CartRepositoryInterface */
43+
private $quoteRepository;
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
protected function setUp(): void
49+
{
50+
parent::setUp();
51+
52+
$this->checkoutSession = $this->_objectManager->get(CheckoutSession::class);
53+
$this->orderFactory = $this->_objectManager->get(OrderInterfaceFactory::class);
54+
$this->cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
55+
$this->customerSession = $this->_objectManager->get(Session::class);
56+
$this->quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class);
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
protected function tearDown(): void
63+
{
64+
$createdQuoteId = $this->checkoutSession->getQuoteId();
65+
66+
if ($createdQuoteId !== null) {
67+
try {
68+
$this->quoteRepository->delete($this->quoteRepository->get($createdQuoteId));
69+
} catch (NoSuchEntityException $e) {
70+
//already deleted
71+
}
72+
}
73+
74+
$this->customerSession->setCustomerId(null);
75+
76+
parent::tearDown();
77+
}
78+
79+
/**
80+
* @magentoDbIsolation disabled
81+
*
82+
* @magentoDataFixture Magento/Sales/_files/order_by_guest_with_simple_product.php
83+
*
84+
* @return void
85+
*/
86+
public function testReorderSimpleProduct(): void
87+
{
88+
$orderIncrementId = 'test_order_1';
89+
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
90+
$cookieValue = base64_encode($order->getProtectCode() . ':' . $orderIncrementId);
91+
$this->cookieManager->setPublicCookie(Guest::COOKIE_NAME, $cookieValue);
92+
$this->dispatchReorderRequest();
93+
$this->assertRedirect($this->stringContains('checkout/cart'));
94+
$quoteId = $this->checkoutSession->getQuoteId();
95+
$this->assertNotNull($quoteId);
96+
$quoteItemsCollection = $this->quoteRepository->get((int)$quoteId)->getItemsCollection();
97+
$this->assertCount(1, $quoteItemsCollection);
98+
$this->assertEquals(
99+
$order->getItemsCollection()->getFirstItem()->getSku(),
100+
$quoteItemsCollection->getFirstItem()->getSku()
101+
);
102+
}
103+
104+
/**
105+
* @return void
106+
*/
107+
public function testReorderWithoutParamsAndCookie(): void
108+
{
109+
$this->dispatchReorderRequest();
110+
$this->assertRedirect($this->stringContains('sales/guest/form'));
111+
$this->assertSessionMessages(
112+
$this->containsEqual((string)__('You entered incorrect data. Please try again.')),
113+
MessageInterface::TYPE_ERROR
114+
);
115+
}
116+
117+
/**
118+
* @magentoDataFixture Magento/Customer/_files/customer.php
119+
*
120+
* @return void
121+
*/
122+
public function testReorderGuestOrderByCustomer(): void
123+
{
124+
$this->customerSession->setCustomerId(1);
125+
$this->dispatchReorderRequest();
126+
$this->assertRedirect($this->stringContains('sales/order/history'));
127+
}
128+
129+
/**
130+
* Dispatch reorder request.
131+
*
132+
* @return void
133+
*/
134+
private function dispatchReorderRequest(): void
135+
{
136+
$this->getRequest()->setMethod(Request::METHOD_POST);
137+
$this->dispatch('sales/guest/reorder/');
138+
}
139+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Controller\Order;
9+
10+
use Magento\Checkout\Model\Session as CheckoutSession;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Framework\Escaper;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\Quote\Api\CartRepositoryInterface;
15+
use Magento\Quote\Api\Data\CartInterface;
16+
use Magento\Sales\Api\Data\OrderInterfaceFactory;
17+
use Magento\TestFramework\Request;
18+
use Magento\TestFramework\TestCase\AbstractController;
19+
20+
/**
21+
* Test for reorder controller.
22+
*
23+
* @see \Magento\Sales\Controller\Order\Reorder
24+
* @magentoAppArea frontend
25+
* @magentoDbIsolation enabled
26+
*/
27+
class ReorderTest extends AbstractController
28+
{
29+
/** @var CheckoutSession */
30+
private $checkoutSession;
31+
32+
/** @var OrderInterfaceFactory */
33+
private $orderFactory;
34+
35+
/** @var Session */
36+
private $customerSession;
37+
38+
/** @var CartRepositoryInterface */
39+
private $quoteRepository;
40+
41+
/** @var CartInterface */
42+
private $quote;
43+
44+
/** @var Escaper */
45+
private $escaper;
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function setUp(): void
51+
{
52+
parent::setUp();
53+
54+
$this->checkoutSession = $this->_objectManager->get(CheckoutSession::class);
55+
$this->orderFactory = $this->_objectManager->get(OrderInterfaceFactory::class);
56+
$this->customerSession = $this->_objectManager->get(Session::class);
57+
$this->quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class);
58+
$this->escaper = $this->_objectManager->get(Escaper::class);
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
protected function tearDown(): void
65+
{
66+
if ($this->quote instanceof CartInterface) {
67+
$this->quoteRepository->delete($this->quote);
68+
}
69+
$this->customerSession->setCustomerId(null);
70+
71+
parent::tearDown();
72+
}
73+
74+
/**
75+
* @magentoDataFixture Magento/Sales/_files/customer_order_with_taxable_product.php
76+
*
77+
* @return void
78+
*/
79+
public function testReorder(): void
80+
{
81+
$order = $this->orderFactory->create()->loadByIncrementId('test_order_with_taxable_product');
82+
$this->customerSession->setCustomerId($order->getCustomerId());
83+
$this->dispatchReorderRequest((int)$order->getId());
84+
$this->assertRedirect($this->stringContains('checkout/cart'));
85+
$this->quote = $this->checkoutSession->getQuote();
86+
$quoteItemsCollection = $this->quote->getItemsCollection();
87+
$this->assertCount(1, $quoteItemsCollection);
88+
$this->assertEquals(
89+
$order->getItemsCollection()->getFirstItem()->getSku(),
90+
$quoteItemsCollection->getFirstItem()->getSku()
91+
);
92+
}
93+
94+
/**
95+
* @magentoDataFixture Magento/Sales/_files/customer_order_with_simple_product.php
96+
*
97+
* @return void
98+
*/
99+
public function testReorderProductLowQty(): void
100+
{
101+
$order = $this->orderFactory->create()->loadByIncrementId('55555555');
102+
$this->customerSession->setCustomerId($order->getCustomerId());
103+
$this->dispatchReorderRequest((int)$order->getId());
104+
$origMessage = (string)__('The requested qty is not available');
105+
$message = $this->escaper->escapeHtml(
106+
__('Could not add the product with SKU "%1" to the shopping cart: %2', 'simple-1', $origMessage)
107+
);
108+
$constraint = $this->logicalOr($this->containsEqual($origMessage), $this->containsEqual($message));
109+
$this->assertThat($this->getMessages(MessageInterface::TYPE_ERROR), $constraint);
110+
$this->quote = $this->checkoutSession->getQuote();
111+
}
112+
113+
/**
114+
* @magentoDataFixture Magento/Customer/_files/customer.php
115+
* @magentoDataFixture Magento/Sales/_files/customer_order_with_two_items.php
116+
*
117+
* @return void
118+
*/
119+
public function testReorderByAnotherCustomer(): void
120+
{
121+
$this->customerSession->setCustomerId(1);
122+
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
123+
$this->dispatchReorderRequest((int)$order->getId());
124+
$this->assertRedirect($this->stringContains('sales/order/history'));
125+
}
126+
127+
/**
128+
* Dispatch reorder request.
129+
*
130+
* @param null|int $orderId
131+
* @return void
132+
*/
133+
private function dispatchReorderRequest(?int $orderId = null): void
134+
{
135+
$this->getRequest()->setMethod(Request::METHOD_POST);
136+
$this->getRequest()->setParam('order_id', $orderId);
137+
$this->dispatch('sales/order/reorder/');
138+
}
139+
}

0 commit comments

Comments
 (0)