Skip to content

Commit ba81563

Browse files
committed
ACP2E-3004: Reordering customer order via guest order form results an empty cart
- with test
1 parent b7de65c commit ba81563

File tree

2 files changed

+366
-0
lines changed

2 files changed

+366
-0
lines changed

app/code/Magento/Sales/Model/Reorder/Reorder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232

3333
/**
3434
* Allows customer quickly to reorder previously added products and put them to the Cart
35+
*
3536
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
3638
*/
3739
class Reorder
3840
{
Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Sales\Test\Unit\Model\Reorder;
20+
21+
use Magento\Catalog\Api\Data\ProductInterface;
22+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
23+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
24+
use Magento\Customer\Model\Session as CustomerSession;
25+
use Magento\Framework\DataObject;
26+
use Magento\Framework\Exception\AlreadyExistsException;
27+
use Magento\Framework\Exception\CouldNotSaveException;
28+
use Magento\Framework\Exception\InputException;
29+
use Magento\Framework\Exception\LocalizedException;
30+
use Magento\Framework\Exception\NoSuchEntityException;
31+
use Magento\Quote\Api\CartRepositoryInterface;
32+
use Magento\Quote\Model\Cart\CustomerCartResolver;
33+
use Magento\Quote\Model\GuestCart\GuestCartResolver;
34+
use Magento\Quote\Model\Quote;
35+
use Magento\Sales\Helper\Reorder as ReorderHelper;
36+
use Magento\Sales\Model\Order;
37+
use Magento\Sales\Model\Order\Item;
38+
use Magento\Sales\Model\OrderFactory;
39+
use Magento\Sales\Model\Reorder\OrderInfoBuyRequestGetter;
40+
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
41+
use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory;
42+
use Magento\Sales\Model\Reorder\Reorder;
43+
use Magento\Store\Api\Data\StoreInterface;
44+
use Magento\Store\Model\StoreManagerInterface;
45+
use PHPUnit\Framework\MockObject\MockObject;
46+
use PHPUnit\Framework\TestCase;
47+
use Psr\Log\LoggerInterface;
48+
49+
/**
50+
* Test case for reorder test
51+
*
52+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
53+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
54+
*/
55+
class ReorderTest extends TestCase
56+
{
57+
/**
58+
* @var OrderFactory|MockObject
59+
*/
60+
private $orderFactory;
61+
62+
/**
63+
* @var Order|MockObject
64+
*/
65+
private $order;
66+
67+
/**
68+
* @var CartRepositoryInterface|MockObject
69+
*/
70+
private $cartRepository;
71+
72+
/**
73+
* @var Quote|MockObject
74+
*/
75+
private $cart;
76+
77+
/**
78+
* @var ReorderHelper|MockObject
79+
*/
80+
private $reorderHelper;
81+
82+
/**
83+
* @var LoggerInterface|MockObject
84+
*/
85+
private $loggerMock;
86+
87+
/**
88+
* @var CustomerCartResolver|MockObject
89+
*/
90+
private $customerCartProvider;
91+
92+
/**
93+
* @var GuestCartResolver|MockObject
94+
*/
95+
private $guestCartResolver;
96+
97+
/**
98+
* @var ProductCollectionFactory|MockObject
99+
*/
100+
private $productCollectionFactory;
101+
102+
/**
103+
* @var OrderInfoBuyRequestGetter|MockObject
104+
*/
105+
private $orderInfoBuyRequestGetter;
106+
107+
/**
108+
* @var CustomerSession|MockObject
109+
*/
110+
private $customerSession;
111+
112+
/**
113+
* @var ItemCollection|MockObject
114+
*/
115+
private $itemCollection;
116+
117+
/**
118+
* @var StoreManagerInterface|MockObject
119+
*/
120+
private $storeManager;
121+
122+
/**
123+
* @var StoreInterface|MockObject
124+
*/
125+
private $store;
126+
127+
/**
128+
* @var Reorder
129+
*/
130+
private $reorder;
131+
132+
/**
133+
* @inheritDoc
134+
*/
135+
protected function setUp(): void
136+
{
137+
$this->orderFactory = $this->getMockBuilder(OrderFactory::class)
138+
->disableOriginalConstructor()
139+
->onlyMethods(['create'])
140+
->getMockForAbstractClass();
141+
$this->order = $this->createPartialMock(
142+
Order::class,
143+
[
144+
'loadByIncrementIdAndStoreId',
145+
'getId',
146+
'getCustomerId',
147+
'getItemsCollection',
148+
'getStore'
149+
]
150+
);
151+
$this->cartRepository = $this->getMockBuilder(CartRepositoryInterface::class)
152+
->disableOriginalConstructor()
153+
->getMockForAbstractClass();
154+
$this->cart = $this->createPartialMock(
155+
Quote::class,
156+
[]
157+
);
158+
$this->reorderHelper = $this->createPartialMock(
159+
ReorderHelper::class,
160+
['isAllowed']
161+
);
162+
$this->customerCartProvider = $this->createPartialMock(
163+
CustomerCartResolver::class,
164+
['resolve']
165+
);
166+
$this->guestCartResolver = $this->createPartialMock(
167+
GuestCartResolver::class,
168+
['resolve']
169+
);
170+
$this->productCollectionFactory = $this->getMockBuilder(ProductCollectionFactory::class)
171+
->disableOriginalConstructor()
172+
->onlyMethods(['create'])
173+
->getMockForAbstractClass();
174+
$this->itemCollection = $this->getMockBuilder(ItemCollection::class)
175+
->disableOriginalConstructor()
176+
->getMock();
177+
$this->orderInfoBuyRequestGetter = $this->createPartialMock(
178+
OrderInfoBuyRequestGetter::class,
179+
['getInfoBuyRequest']
180+
);
181+
$this->customerSession = $this->createPartialMock(
182+
CustomerSession::class,
183+
['isLoggedIn']
184+
);
185+
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
186+
->onlyMethods(['getStore'])
187+
->disableOriginalConstructor()
188+
->getMockForAbstractClass();
189+
$this->store = $this->getMockBuilder(StoreInterface::class)
190+
->disableOriginalConstructor()
191+
->getMockForAbstractClass();
192+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
193+
$this->reorder = new Reorder(
194+
$this->orderFactory,
195+
$this->customerCartProvider,
196+
$this->guestCartResolver,
197+
$this->cartRepository,
198+
$this->reorderHelper,
199+
$this->loggerMock,
200+
$this->productCollectionFactory,
201+
$this->orderInfoBuyRequestGetter,
202+
$this->storeManager,
203+
false,
204+
$this->customerSession
205+
);
206+
}
207+
208+
/**
209+
* Test case for execute reorder
210+
*
211+
* @param string $orderNumber
212+
* @param int $orderId
213+
* @param string $storeId
214+
* @param int $customerId
215+
* @param bool $customerIsLoggedIn
216+
* @param bool $isReorderAllowed
217+
* @return void
218+
* @throws AlreadyExistsException
219+
* @throws CouldNotSaveException
220+
* @throws InputException
221+
* @throws LocalizedException
222+
* @throws NoSuchEntityException
223+
* @dataProvider dataProvider
224+
*/
225+
public function testExecuteReorder(
226+
string $orderNumber,
227+
int $orderId,
228+
string $storeId,
229+
int $customerId,
230+
bool $customerIsLoggedIn,
231+
bool $isReorderAllowed,
232+
): void {
233+
$item1 = $this->createPartialMock(
234+
Item::class,
235+
['getParentItem', 'getProductId', 'getId']
236+
);
237+
$item1->expects($this->any())
238+
->method('getParentItem')
239+
->willReturn(null);
240+
$item1->expects($this->any())
241+
->method('getProductId')
242+
->willReturn(1);
243+
$item1->expects($this->any())
244+
->method('getId')
245+
->willReturn(5);
246+
$item2 = $this->createPartialMock(
247+
Item::class,
248+
['getParentItem', 'getProductId', 'getId']
249+
);
250+
$item2->expects($this->any())
251+
->method('getParentItem')
252+
->willReturn(null);
253+
$item2->expects($this->any())
254+
->method('getProductId')
255+
->willReturn(2);
256+
$item2->expects($this->any())
257+
->method('getId')
258+
->willReturn(5);
259+
$collection = $this->createMock(ItemCollection::class);
260+
$collection->expects($this->any())->method('getIterator')
261+
->willReturn(new \ArrayIterator([$item1, $item2]));
262+
$this->order->expects($this->once())
263+
->method('getItemsCollection')
264+
->willReturn($collection);
265+
$productCollection = $this->getMockBuilder(Collection::class)
266+
->onlyMethods(
267+
[
268+
'getItems',
269+
'addIdFilter',
270+
'addStoreFilter',
271+
'addAttributeToSelect',
272+
'joinAttribute',
273+
'addOptionsToResult',
274+
'getIterator',
275+
'setStore'
276+
]
277+
)
278+
->addMethods(['getStore'])
279+
->disableOriginalConstructor()
280+
->getMock();
281+
$productCollection->expects($this->any())->method('setStore')->willReturnSelf();
282+
$productCollection->expects($this->any())->method('addIdFilter')->willReturnSelf();
283+
$productCollection->expects($this->any())->method('addStoreFilter')->willReturnSelf();
284+
$productCollection->expects($this->once())->method('addAttributeToSelect')->willReturnSelf();
285+
$productCollection->expects($this->any())->method('joinAttribute')->willReturnSelf();
286+
$productCollection->expects($this->once())->method('addOptionsToResult')->willReturnSelf();
287+
$this->productCollectionFactory->expects($this->once())
288+
->method('create')
289+
->willReturn($productCollection);
290+
// $product1 = $this->getMockForAbstractClass(ProductInterface::class);
291+
// $product2 = $this->getMockForAbstractClass(ProductInterface::class);
292+
// $productCollection->expects($this->once())->method('getItems')->willReturn([$product1, $product2]);
293+
$productCollection->expects($this->once())->method('getItems')->willReturn([]);
294+
$this->orderFactory->expects($this->once())
295+
->method('create')
296+
->willReturn($this->order);
297+
$this->order->expects($this->once())
298+
->method('loadByIncrementIdAndStoreId')
299+
->with($orderNumber, $storeId)
300+
->willReturnSelf();
301+
$this->order->expects($this->once())
302+
->method('getId')
303+
->willReturn($orderId);
304+
$this->order->expects($this->once())
305+
->method('getCustomerId')
306+
->willReturn($customerId);
307+
$this->order->expects($this->once())
308+
->method('getStore')
309+
->willReturn($this->store);
310+
$this->customerSession->expects($this->once())
311+
->method('isLoggedIn')
312+
->willReturn($customerIsLoggedIn);
313+
$this->guestCartResolver->expects($this->any())
314+
->method('resolve')
315+
->willReturn($this->cart);
316+
$this->customerCartProvider->expects($this->any())
317+
->method('resolve')
318+
->with($customerId)
319+
->willReturn($this->cart);
320+
$this->reorderHelper->expects($this->any())
321+
->method('isAllowed')
322+
->with($this->store)
323+
->willReturn($isReorderAllowed);
324+
$this->storeManager->expects($this->once())
325+
->method('getStore')
326+
->willReturn($this->store);
327+
$this->store->expects($this->once())
328+
->method('getId')
329+
->willReturn($storeId);
330+
$this->cartRepository->expects($this->once())
331+
->method('save')
332+
->with($this->cart)
333+
->willReturnSelf();
334+
// $infoBuyRequest = new DataObject(['options' => [
335+
// [
336+
// 'option_id' => 1,
337+
// 'option_value' => 2
338+
// ]
339+
// ]]);
340+
// $this->orderInfoBuyRequestGetter->expects($this->once())
341+
// ->method('getInfoBuyRequest')
342+
// ->willReturn($infoBuyRequest);
343+
$savedCart = $this->getMockBuilder(\Magento\Quote\Api\Data\CartInterface::class)
344+
->disableOriginalConstructor()
345+
->addMethods(['setHasError'])
346+
->getMockForAbstractClass();
347+
$this->cartRepository->expects($this->once())
348+
->method('get')
349+
->willReturn($savedCart);
350+
351+
$output = $this->reorder->execute($orderNumber, $storeId);
352+
$this->assertNotEmpty($output);
353+
}
354+
355+
/**
356+
* @return array
357+
*/
358+
public function dataProvider()
359+
{
360+
return [
361+
'test case 1' => ['000001', 1, '1', 1, true, true],
362+
];
363+
}
364+
}

0 commit comments

Comments
 (0)