Skip to content

Commit 241450e

Browse files
committed
[Checkout Coverage] Place order for guest
1 parent 2dfe31a commit 241450e

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-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,134 @@
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+
* @param string $maskedQuoteId
102+
* @return string
103+
*/
104+
private function getQuery(string $maskedQuoteId): string
105+
{
106+
return <<<QUERY
107+
mutation {
108+
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
109+
order {
110+
order_id
111+
}
112+
}
113+
}
114+
QUERY;
115+
}
116+
117+
/**
118+
* @inheritdoc
119+
*/
120+
public function tearDown()
121+
{
122+
$this->registry->unregister('isSecureArea');
123+
$this->registry->register('isSecureArea', true);
124+
125+
$orderCollection = $this->orderCollectionFactory->create();
126+
foreach ($orderCollection as $order) {
127+
$this->orderRepository->delete($order);
128+
}
129+
$this->registry->unregister('isSecureArea');
130+
$this->registry->register('isSecureArea', false);
131+
132+
parent::tearDown();
133+
}
134+
}
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)