Skip to content

Commit 805df5a

Browse files
committed
Merge remote-tracking branch 'origin/ENGCOM-5290-magento-graphql-ce-701' into graphql-develop-prs
# Conflicts: # app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php
2 parents 35e1c77 + 58d864d commit 805df5a

16 files changed

+520
-14
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Checkout\Helper\Data as CheckoutHelper;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
12+
use Magento\Quote\Model\Quote;
13+
14+
/**
15+
* Checks if guest checkout is allowed.
16+
*/
17+
class CheckCartCheckoutAllowance
18+
{
19+
/**
20+
* @var CheckoutHelper
21+
*/
22+
private $checkoutHelper;
23+
24+
/**
25+
* @param CheckoutHelper $checkoutHelper
26+
*/
27+
public function __construct(
28+
CheckoutHelper $checkoutHelper
29+
) {
30+
$this->checkoutHelper = $checkoutHelper;
31+
}
32+
33+
/**
34+
* Check if User is allowed to checkout
35+
*
36+
* @param Quote $quote
37+
* @return void
38+
* @throws GraphQlAuthorizationException
39+
*/
40+
public function execute(Quote $quote): void
41+
{
42+
if (false === $quote->getCustomerIsGuest()) {
43+
return;
44+
}
45+
46+
$isAllowedGuestCheckout = (bool)$this->checkoutHelper->isAllowedGuestCheckout($quote);
47+
if (false === $isAllowedGuestCheckout) {
48+
throw new GraphQlAuthorizationException(
49+
__(
50+
'Guest checkout is not allowed. ' .
51+
'Register a customer account or login with existing one.'
52+
)
53+
);
54+
}
55+
}
56+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Quote\Api\CartManagementInterface;
1818
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1919
use Magento\Sales\Api\OrderRepositoryInterface;
20+
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
2021

2122
/**
2223
* @inheritdoc
@@ -38,19 +39,27 @@ class PlaceOrder implements ResolverInterface
3839
*/
3940
private $orderRepository;
4041

42+
/**
43+
* @var CheckCartCheckoutAllowance
44+
*/
45+
private $checkCartCheckoutAllowance;
46+
4147
/**
4248
* @param GetCartForUser $getCartForUser
4349
* @param CartManagementInterface $cartManagement
4450
* @param OrderRepositoryInterface $orderRepository
51+
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
4552
*/
4653
public function __construct(
4754
GetCartForUser $getCartForUser,
4855
CartManagementInterface $cartManagement,
49-
OrderRepositoryInterface $orderRepository
56+
OrderRepositoryInterface $orderRepository,
57+
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
5058
) {
5159
$this->getCartForUser = $getCartForUser;
5260
$this->cartManagement = $cartManagement;
5361
$this->orderRepository = $orderRepository;
62+
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
5463
}
5564

5665
/**
@@ -64,6 +73,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6473
$maskedCartId = $args['input']['cart_id'];
6574

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

6878
if ((int)$context->getUserId() === 0) {
6979
if (!$cart->getCustomerEmail()) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1515
use Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart as SetBillingAddressOnCartModel;
16+
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
1617

1718
/**
1819
* Mutation resolver for setting billing address for shopping cart
@@ -29,16 +30,24 @@ class SetBillingAddressOnCart implements ResolverInterface
2930
*/
3031
private $setBillingAddressOnCart;
3132

33+
/**
34+
* @var CheckCartCheckoutAllowance
35+
*/
36+
private $checkCartCheckoutAllowance;
37+
3238
/**
3339
* @param GetCartForUser $getCartForUser
3440
* @param SetBillingAddressOnCartModel $setBillingAddressOnCart
41+
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3542
*/
3643
public function __construct(
3744
GetCartForUser $getCartForUser,
38-
SetBillingAddressOnCartModel $setBillingAddressOnCart
45+
SetBillingAddressOnCartModel $setBillingAddressOnCart,
46+
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3947
) {
4048
$this->getCartForUser = $getCartForUser;
4149
$this->setBillingAddressOnCart = $setBillingAddressOnCart;
50+
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
4251
}
4352

4453
/**
@@ -57,6 +66,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5766
$billingAddress = $args['input']['billing_address'];
5867

5968
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
69+
$this->checkCartCheckoutAllowance->execute($cart);
6070
$this->setBillingAddressOnCart->execute($context, $cart, $billingAddress);
6171

6272
return [

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
1717
use Magento\Quote\Api\CartRepositoryInterface;
1818
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19+
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
1920

2021
/**
2122
* @inheritdoc
@@ -37,19 +38,27 @@ class SetGuestEmailOnCart implements ResolverInterface
3738
*/
3839
private $emailValidator;
3940

41+
/**
42+
* @var CheckCartCheckoutAllowance
43+
*/
44+
private $checkCartCheckoutAllowance;
45+
4046
/**
4147
* @param GetCartForUser $getCartForUser
4248
* @param CartRepositoryInterface $cartRepository
4349
* @param EmailAddressValidator $emailValidator
50+
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
4451
*/
4552
public function __construct(
4653
GetCartForUser $getCartForUser,
4754
CartRepositoryInterface $cartRepository,
48-
EmailAddressValidator $emailValidator
55+
EmailAddressValidator $emailValidator,
56+
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
4957
) {
5058
$this->getCartForUser = $getCartForUser;
5159
$this->cartRepository = $cartRepository;
5260
$this->emailValidator = $emailValidator;
61+
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
5362
}
5463

5564
/**
@@ -78,6 +87,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7887
}
7988

8089
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId);
90+
$this->checkCartCheckoutAllowance->execute($cart);
8191
$cart->setCustomerEmail($email);
8292

8393
try {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
1415
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1516
use Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart as SetPaymentMethodOnCartModel;
1617

@@ -29,16 +30,24 @@ class SetPaymentMethodOnCart implements ResolverInterface
2930
*/
3031
private $setPaymentMethodOnCart;
3132

33+
/**
34+
* @var CheckCartCheckoutAllowance
35+
*/
36+
private $checkCartCheckoutAllowance;
37+
3238
/**
3339
* @param GetCartForUser $getCartForUser
3440
* @param SetPaymentMethodOnCartModel $setPaymentMethodOnCart
41+
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3542
*/
3643
public function __construct(
3744
GetCartForUser $getCartForUser,
38-
SetPaymentMethodOnCartModel $setPaymentMethodOnCart
45+
SetPaymentMethodOnCartModel $setPaymentMethodOnCart,
46+
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3947
) {
4048
$this->getCartForUser = $getCartForUser;
4149
$this->setPaymentMethodOnCart = $setPaymentMethodOnCart;
50+
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
4251
}
4352

4453
/**
@@ -57,6 +66,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5766
$paymentData = $args['input']['payment_method'];
5867

5968
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
69+
$this->checkCartCheckoutAllowance->execute($cart);
6070
$this->setPaymentMethodOnCart->execute($cart, $paymentData);
6171

6272
return [

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1515
use Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface;
16+
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
1617

1718
/**
1819
* Mutation resolver for setting shipping addresses for shopping cart
@@ -29,16 +30,24 @@ class SetShippingAddressesOnCart implements ResolverInterface
2930
*/
3031
private $setShippingAddressesOnCart;
3132

33+
/**
34+
* @var CheckCartCheckoutAllowance
35+
*/
36+
private $checkCartCheckoutAllowance;
37+
3238
/**
3339
* @param GetCartForUser $getCartForUser
3440
* @param SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
41+
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3542
*/
3643
public function __construct(
3744
GetCartForUser $getCartForUser,
38-
SetShippingAddressesOnCartInterface $setShippingAddressesOnCart
45+
SetShippingAddressesOnCartInterface $setShippingAddressesOnCart,
46+
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3947
) {
4048
$this->getCartForUser = $getCartForUser;
4149
$this->setShippingAddressesOnCart = $setShippingAddressesOnCart;
50+
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
4251
}
4352

4453
/**
@@ -57,6 +66,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5766
$shippingAddresses = $args['input']['shipping_addresses'];
5867

5968
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
69+
$this->checkCartCheckoutAllowance->execute($cart);
6070
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
6171

6272
return [

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1515
use Magento\QuoteGraphQl\Model\Cart\SetShippingMethodsOnCartInterface;
16+
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
1617

1718
/**
1819
* Mutation resolver for setting shipping methods for shopping cart
@@ -29,16 +30,24 @@ class SetShippingMethodsOnCart implements ResolverInterface
2930
*/
3031
private $setShippingMethodsOnCart;
3132

33+
/**
34+
* @var CheckCartCheckoutAllowance
35+
*/
36+
private $checkCartCheckoutAllowance;
37+
3238
/**
3339
* @param GetCartForUser $getCartForUser
3440
* @param SetShippingMethodsOnCartInterface $setShippingMethodsOnCart
41+
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3542
*/
3643
public function __construct(
3744
GetCartForUser $getCartForUser,
38-
SetShippingMethodsOnCartInterface $setShippingMethodsOnCart
45+
SetShippingMethodsOnCartInterface $setShippingMethodsOnCart,
46+
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
3947
) {
4048
$this->getCartForUser = $getCartForUser;
4149
$this->setShippingMethodsOnCart = $setShippingMethodsOnCart;
50+
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
4251
}
4352

4453
/**
@@ -57,6 +66,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5766
$shippingMethods = $args['input']['shipping_methods'];
5867

5968
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
69+
$this->checkCartCheckoutAllowance->execute($cart);
6070
$this->setShippingMethodsOnCart->execute($context, $cart, $shippingMethods);
6171

6272
return [

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/ApplyCouponToCartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
239239
'Required parameter "cart_id" is missing'
240240
],
241241
'missed_coupon_code' => [
242-
'cart_id: "test"',
242+
'cart_id: "test_quote"',
243243
'Required parameter "coupon_code" is missing'
244244
],
245245
];

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
128128
'Required parameter "cart_id" is missing.'
129129
],
130130
'missed_cart_item_id' => [
131-
'cart_id: "test"',
131+
'cart_id: "test_quote"',
132132
'Required parameter "cart_item_id" is missing.'
133133
],
134134
];

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetPaymentMethodOnCartTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ public function testSetPaymentMethodToAnotherCustomerCart()
187187
*/
188188
public function testSetPaymentMethodWithoutRequiredParameters(string $input, string $message)
189189
{
190+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
191+
$input = str_replace('cart_id_value', $maskedQuoteId, $input);
192+
190193
$query = <<<QUERY
191194
mutation {
192195
setPaymentMethodOnCart(
@@ -235,11 +238,11 @@ public function dataProviderSetPaymentMethodWithoutRequiredParameters(): array
235238
'Required parameter "cart_id" is missing.'
236239
],
237240
'missed_payment_method' => [
238-
'cart_id: "test"',
241+
'cart_id: "cart_id_value"',
239242
'Required parameter "code" for "payment_method" is missing.'
240243
],
241244
'missed_payment_method_code' => [
242-
'cart_id: "test", payment_method: {code: ""}',
245+
'cart_id: "cart_id_value", payment_method: {code: ""}',
243246
'Required parameter "code" for "payment_method" is missing.'
244247
],
245248
];

0 commit comments

Comments
 (0)