Skip to content

Commit 58338f0

Browse files
authored
ENGCOM-4746: [Checkout Coverage] Place order for guest #571
2 parents e8b43c1 + 2ff3247 commit 58338f0

File tree

3 files changed

+304
-0
lines changed

3 files changed

+304
-0
lines changed

Diff for: app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6565

6666
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6767

68+
if ($context->getUserId() === 0) {
69+
if (!$cart->getCustomerEmail()) {
70+
throw new GraphQlInputException(__("Guest email for cart is missing. Please enter"));
71+
}
72+
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
73+
}
74+
6875
try {
6976
$orderId = $this->cartManagement->placeOrder($cart->getId());
7077
$order = $this->orderRepository->get($orderId);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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\GraphQl\Quote\Guest;
9+
10+
use Magento\Framework\Registry;
11+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\Sales\Api\OrderRepositoryInterface;
13+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Test for placing an order for guest
19+
*/
20+
class PlaceOrderTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var GetMaskedQuoteIdByReservedOrderId
24+
*/
25+
private $getMaskedQuoteIdByReservedOrderId;
26+
27+
/**
28+
* @var CollectionFactory
29+
*/
30+
private $orderCollectionFactory;
31+
32+
/**
33+
* @var OrderRepositoryInterface
34+
*/
35+
private $orderRepository;
36+
37+
/**
38+
* @var Registry
39+
*/
40+
private $registry;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$objectManager = Bootstrap::getObjectManager();
48+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
49+
$this->orderCollectionFactory = $objectManager->get(CollectionFactory::class);
50+
$this->orderRepository = $objectManager->get(OrderRepositoryInterface::class);
51+
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
52+
}
53+
54+
/**
55+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
56+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
57+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
58+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
59+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
60+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
61+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
62+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
63+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
64+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
65+
*/
66+
public function testPlaceOrder()
67+
{
68+
$reservedOrderId = 'test_quote';
69+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
70+
71+
$query = $this->getQuery($maskedQuoteId);
72+
$response = $this->graphQlQuery($query);
73+
74+
self::assertArrayHasKey('placeOrder', $response);
75+
self::assertArrayHasKey('order_id', $response['placeOrder']['order']);
76+
self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_id']);
77+
}
78+
79+
/**
80+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
81+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
82+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
83+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
84+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
85+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
87+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
89+
*/
90+
public function testPlaceOrderWithNoEmail()
91+
{
92+
$reservedOrderId = 'test_quote';
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
94+
$query = $this->getQuery($maskedQuoteId);
95+
96+
self::expectExceptionMessage("Guest email for cart is missing. Please enter");
97+
$this->graphQlQuery($query);
98+
}
99+
100+
/**
101+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
102+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
103+
*/
104+
public function testPlaceOrderWithNoItemsInCart()
105+
{
106+
$reservedOrderId = 'test_quote';
107+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
108+
$query = $this->getQuery($maskedQuoteId);
109+
110+
self::expectExceptionMessage(
111+
'Unable to place order: A server error stopped your order from being placed. ' .
112+
'Please try to place your order again'
113+
);
114+
$this->graphQlQuery($query);
115+
}
116+
117+
/**
118+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
119+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
120+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
121+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
122+
*/
123+
public function testPlaceOrderWithNoShippingAddress()
124+
{
125+
$reservedOrderId = 'test_quote';
126+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
127+
$query = $this->getQuery($maskedQuoteId);
128+
129+
self::expectExceptionMessage(
130+
'Unable to place order: Some addresses can\'t be used due to the configurations for specific countries'
131+
);
132+
$this->graphQlQuery($query);
133+
}
134+
135+
/**
136+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
137+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
138+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
139+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
140+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
141+
*/
142+
public function testPlaceOrderWithNoShippingMethod()
143+
{
144+
$reservedOrderId = 'test_quote';
145+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
146+
$query = $this->getQuery($maskedQuoteId);
147+
148+
self::expectExceptionMessage(
149+
'Unable to place order: The shipping method is missing. Select the shipping method and try again'
150+
);
151+
$this->graphQlQuery($query);
152+
}
153+
154+
/**
155+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
156+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
157+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
158+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
159+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
160+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
161+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
162+
*/
163+
public function testPlaceOrderWithNoBillingAddress()
164+
{
165+
$reservedOrderId = 'test_quote';
166+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
167+
$query = $this->getQuery($maskedQuoteId);
168+
169+
self::expectExceptionMessageRegExp(
170+
'/Unable to place order: Please check the billing address information*/'
171+
);
172+
$this->graphQlQuery($query);
173+
}
174+
175+
/**
176+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
177+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
178+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
179+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
180+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
181+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
182+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
183+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
184+
*/
185+
public function testPlaceOrderWithNoPaymentMethod()
186+
{
187+
$reservedOrderId = 'test_quote';
188+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
189+
$query = $this->getQuery($maskedQuoteId);
190+
191+
self::expectExceptionMessage('Unable to place order: Enter a valid payment method and try again');
192+
$this->graphQlQuery($query);
193+
}
194+
195+
/**
196+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
197+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
198+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
199+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
200+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
201+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
202+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
203+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
204+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php
205+
*/
206+
public function testPlaceOrderWithOutOfStockProduct()
207+
{
208+
$reservedOrderId = 'test_quote';
209+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
210+
$query = $this->getQuery($maskedQuoteId);
211+
212+
self::expectExceptionMessage('Unable to place order: Some of the products are out of stock');
213+
$this->graphQlQuery($query);
214+
}
215+
216+
/**
217+
* _security
218+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
219+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
220+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
221+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
222+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
223+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
224+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
225+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
226+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
227+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
228+
*/
229+
public function testPlaceOrderOfCustomerCart()
230+
{
231+
$reservedOrderId = 'test_quote';
232+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
233+
$query = $this->getQuery($maskedQuoteId);
234+
235+
self::expectExceptionMessageRegExp('/The current user cannot perform operations on cart*/');
236+
$this->graphQlQuery($query);
237+
}
238+
239+
/**
240+
* @param string $maskedQuoteId
241+
* @return string
242+
*/
243+
private function getQuery(string $maskedQuoteId): string
244+
{
245+
return <<<QUERY
246+
mutation {
247+
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
248+
order {
249+
order_id
250+
}
251+
}
252+
}
253+
QUERY;
254+
}
255+
256+
/**
257+
* @inheritdoc
258+
*/
259+
public function tearDown()
260+
{
261+
$this->registry->unregister('isSecureArea');
262+
$this->registry->register('isSecureArea', true);
263+
264+
$orderCollection = $this->orderCollectionFactory->create();
265+
foreach ($orderCollection as $order) {
266+
$this->orderRepository->delete($order);
267+
}
268+
$this->registry->unregister('isSecureArea');
269+
$this->registry->register('isSecureArea', false);
270+
271+
parent::tearDown();
272+
}
273+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Quote\Model\QuoteFactory;
10+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
/** @var QuoteFactory $quoteFactory */
14+
$quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class);
15+
/** @var CartRepositoryInterface $cartRepository */
16+
$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
17+
/** @var QuoteResource $quoteResource */
18+
$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class);
19+
20+
$quote = $quoteFactory->create();
21+
$quoteResource->load($quote, 'test_quote', 'reserved_order_id');
22+
23+
$quote->setCustomerEmail('[email protected]');
24+
$cartRepository->save($quote);

0 commit comments

Comments
 (0)