Skip to content

Commit 5d95e73

Browse files
committed
magento/graphql-ce#679: Add support of "Allow Guest Checkout" option
1 parent d3a92fb commit 5d95e73

File tree

11 files changed

+352
-220
lines changed

11 files changed

+352
-220
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/CheckCartCheckoutAllowance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function execute(Quote $quote): void
4343
return;
4444
}
4545

46-
$isAllowedGuestCheckout = $this->checkoutHelper->isAllowedGuestCheckout($quote);
46+
$isAllowedGuestCheckout = (bool)$this->checkoutHelper->isAllowedGuestCheckout($quote);
4747
if (false === $isAllowedGuestCheckout) {
4848
throw new GraphQlAuthorizationException(
4949
__(

app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7373
$maskedCartId = $args['input']['cart_id'];
7474

7575
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
76+
$this->checkCartCheckoutAllowance->execute($cart);
7677

7778
if ($context->getUserId() === 0) {
7879
if (!$cart->getCustomerEmail()) {
@@ -83,7 +84,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8384

8485
try {
8586
$orderId = $this->cartManagement->placeOrder($cart->getId());
86-
$this->checkCartCheckoutAllowance->execute($cart);
8787
$order = $this->orderRepository->get($orderId);
8888

8989
return [

app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8282
}
8383
$maskedCartId = $args['input']['cart_id'];
8484

85+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
86+
$this->checkCartCheckoutAllowance->execute($cart);
87+
8588
if (!isset($args['input']['payment_method']['code']) || empty($args['input']['payment_method']['code'])) {
8689
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
8790
}
@@ -90,8 +93,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9093
$poNumber = $args['input']['payment_method']['purchase_order_number'] ?? null;
9194
$additionalData = $this->additionalDataProviderPool->getData($paymentMethodCode, $args) ?? [];
9295

93-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
94-
$this->checkCartCheckoutAllowance->execute($cart);
9596
$payment = $this->paymentFactory->create(
9697
[
9798
'data' => [
Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
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 Exception;
11+
use Magento\Framework\Registry;
12+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
13+
use Magento\OfflinePayments\Model\Checkmo;
14+
use Magento\Quote\Api\GuestCartRepositoryInterface;
15+
use Magento\Sales\Api\OrderRepositoryInterface;
16+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\TestFramework\TestCase\GraphQlAbstract;
19+
20+
/**
21+
* Test for placing an order for guest
22+
*/
23+
class PlaceOrderTest extends GraphQlAbstract
24+
{
25+
/**
26+
* @var GetMaskedQuoteIdByReservedOrderId
27+
*/
28+
private $getMaskedQuoteIdByReservedOrderId;
29+
30+
/**
31+
* @var CollectionFactory
32+
*/
33+
private $orderCollectionFactory;
34+
35+
/**
36+
* @var OrderRepositoryInterface
37+
*/
38+
private $orderRepository;
39+
40+
/**
41+
* @var Registry
42+
*/
43+
private $registry;
44+
45+
/**
46+
* @var GuestCartRepositoryInterface
47+
*/
48+
private $guestCartRepository;
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
protected function setUp()
54+
{
55+
$objectManager = Bootstrap::getObjectManager();
56+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
57+
$this->orderCollectionFactory = $objectManager->get(CollectionFactory::class);
58+
$this->orderRepository = $objectManager->get(OrderRepositoryInterface::class);
59+
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
60+
$this->guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class);
61+
}
62+
63+
/**
64+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
65+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
66+
*/
67+
public function testCreateEmptyCartIfGuestCheckoutIsDisabled()
68+
{
69+
$query = <<<QUERY
70+
mutation {
71+
createEmptyCart
72+
}
73+
QUERY;
74+
$response = $this->graphQlMutation($query);
75+
76+
self::assertArrayHasKey('createEmptyCart', $response);
77+
self::assertNotEmpty($response['createEmptyCart']);
78+
79+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
80+
81+
self::assertNotNull($guestCart->getId());
82+
self::assertNull($guestCart->getCustomer()->getId());
83+
self::assertEquals('default', $guestCart->getStore()->getCode());
84+
}
85+
86+
/**
87+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
89+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
90+
*
91+
* @expectedException \Exception
92+
* @expectedExceptionMessage Guest checkout is not allowed. Register a customer account or login with existing one.
93+
*/
94+
public function testSetBillingAddressToGuestCustomerCart()
95+
{
96+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
97+
98+
$query = <<<QUERY
99+
mutation {
100+
setBillingAddressOnCart(
101+
input: {
102+
cart_id: "$maskedQuoteId"
103+
billing_address: {
104+
address: {
105+
firstname: "test firstname"
106+
lastname: "test lastname"
107+
company: "test company"
108+
street: ["test street 1", "test street 2"]
109+
city: "test city"
110+
region: "test region"
111+
postcode: "887766"
112+
country_code: "US"
113+
telephone: "88776655"
114+
save_in_address_book: false
115+
}
116+
}
117+
}
118+
) {
119+
cart {
120+
billing_address {
121+
city
122+
}
123+
}
124+
}
125+
}
126+
QUERY;
127+
128+
$this->graphQlMutation($query);
129+
}
130+
131+
/**
132+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
133+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
134+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
135+
*
136+
* @expectedException \Exception
137+
* @expectedExceptionMessage Guest checkout is not allowed. Register a customer account or login with existing one.
138+
*/
139+
public function testSetGuestEmailOnCartWithGuestCheckoutDisabled()
140+
{
141+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
142+
$email = '[email protected]';
143+
144+
$query = <<<QUERY
145+
mutation {
146+
setGuestEmailOnCart(input: {
147+
cart_id: "$maskedQuoteId"
148+
email: "$email"
149+
}) {
150+
cart {
151+
email
152+
}
153+
}
154+
}
155+
QUERY;
156+
$this->graphQlMutation($query);
157+
}
158+
159+
/**
160+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
161+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
162+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
163+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
164+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
165+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
166+
*
167+
* @expectedException \Exception
168+
* @expectedExceptionMessage Guest checkout is not allowed. Register a customer account or login with existing one.
169+
*/
170+
public function testSetPaymentOnCartWithGuestCheckoutDisabled()
171+
{
172+
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
173+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
174+
175+
$query = <<<QUERY
176+
mutation {
177+
setPaymentMethodOnCart(input: {
178+
cart_id: "{$maskedQuoteId}",
179+
payment_method: {
180+
code: "{$methodCode}"
181+
}
182+
}) {
183+
cart {
184+
selected_payment_method {
185+
code
186+
}
187+
}
188+
}
189+
}
190+
QUERY;
191+
$response = $this->graphQlMutation($query);
192+
193+
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
194+
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
195+
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
196+
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
197+
}
198+
199+
/**
200+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
201+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
202+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
203+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
204+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
205+
*
206+
* @expectedException \Exception
207+
* @expectedExceptionMessage Guest checkout is not allowed. Register a customer account or login with existing one.
208+
*/
209+
public function testSetNewShippingAddressOnCartWithGuestCheckoutDisabled()
210+
{
211+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
212+
213+
$query = <<<QUERY
214+
mutation {
215+
setShippingAddressesOnCart(
216+
input: {
217+
cart_id: "$maskedQuoteId"
218+
shipping_addresses: [
219+
{
220+
address: {
221+
firstname: "test firstname"
222+
lastname: "test lastname"
223+
company: "test company"
224+
street: ["test street 1", "test street 2"]
225+
city: "test city"
226+
region: "test region"
227+
postcode: "887766"
228+
country_code: "US"
229+
telephone: "88776655"
230+
save_in_address_book: false
231+
}
232+
}
233+
]
234+
}
235+
) {
236+
cart {
237+
shipping_addresses {
238+
firstname
239+
}
240+
}
241+
}
242+
}
243+
QUERY;
244+
$this->graphQlMutation($query);
245+
}
246+
247+
/**
248+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
249+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
250+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
251+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
252+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
253+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
254+
*
255+
* @expectedException \Exception
256+
* @expectedExceptionMessage Guest checkout is not allowed. Register a customer account or login with existing one.
257+
*/
258+
public function testSetShippingMethodOnCartWithGuestCheckoutDisabled()
259+
{
260+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
261+
$carrierCode = 'flatrate';
262+
$methodCode = 'flatrate';
263+
264+
$query = <<<QUERY
265+
mutation {
266+
setShippingMethodsOnCart(input:
267+
{
268+
cart_id: "$maskedQuoteId",
269+
shipping_methods: [{
270+
carrier_code: "$carrierCode"
271+
method_code: "$methodCode"
272+
}]
273+
}) {
274+
cart {
275+
shipping_addresses {
276+
selected_shipping_method {
277+
carrier_code
278+
}
279+
}
280+
}
281+
}
282+
}
283+
QUERY;
284+
$this->graphQlMutation($query);
285+
}
286+
287+
/**
288+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
289+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
290+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
291+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
292+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
293+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
294+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
295+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
296+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
297+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
298+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_guest_checkout.php
299+
* @magentoConfigFixture default_store checkout/options/guest_checkout 0
300+
*
301+
* @expectedException \Exception
302+
* @expectedExceptionMessage Guest checkout is not allowed. Register a customer account or login with existing one.
303+
*/
304+
public function testPlaceOrderWithGuestCheckoutDisabled()
305+
{
306+
$reservedOrderId = 'test_quote';
307+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
308+
309+
$query = $this->getQuery($maskedQuoteId);
310+
$this->graphQlMutation($query);
311+
}
312+
313+
/**
314+
* @param string $maskedQuoteId
315+
* @return string
316+
*/
317+
private function getQuery(string $maskedQuoteId): string
318+
{
319+
return <<<QUERY
320+
mutation {
321+
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
322+
order {
323+
order_id
324+
}
325+
}
326+
}
327+
QUERY;
328+
}
329+
330+
/**
331+
* @inheritdoc
332+
*/
333+
public function tearDown()
334+
{
335+
$this->registry->unregister('isSecureArea');
336+
$this->registry->register('isSecureArea', true);
337+
338+
$orderCollection = $this->orderCollectionFactory->create();
339+
foreach ($orderCollection as $order) {
340+
$this->orderRepository->delete($order);
341+
}
342+
$this->registry->unregister('isSecureArea');
343+
$this->registry->register('isSecureArea', false);
344+
345+
parent::tearDown();
346+
}
347+
}

0 commit comments

Comments
 (0)