Skip to content

Commit a4039fe

Browse files
authored
Merge pull request #3893 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 6cc68c6 + 8bf1909 commit a4039fe

File tree

13 files changed

+899
-183
lines changed

13 files changed

+899
-183
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public function execute(string $cartHash, ?int $customerId): Quote
6969
);
7070
}
7171

72+
if (false === (bool)$cart->getIsActive()) {
73+
throw new GraphQlNoSuchEntityException(
74+
__('Current user does not have an active cart.')
75+
);
76+
}
77+
7278
$cartCustomerId = (int)$cart->getCustomerId();
7379

7480
/* Guest cart, allow operations */
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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\OfflineShipping;
9+
10+
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\QuoteFactory;
12+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
13+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Test for setting offline shipping methods on cart
19+
*/
20+
class SetOfflineShippingMethodsOnCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var QuoteFactory
24+
*/
25+
private $quoteFactory;
26+
27+
/**
28+
* @var CustomerTokenServiceInterface
29+
*/
30+
private $customerTokenService;
31+
32+
/**
33+
* @var QuoteResource
34+
*/
35+
private $quoteResource;
36+
37+
/**
38+
* @var QuoteIdToMaskedQuoteIdInterface
39+
*/
40+
private $quoteIdToMaskedId;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$objectManager = Bootstrap::getObjectManager();
48+
$this->quoteResource = $objectManager->get(QuoteResource::class);
49+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
50+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
51+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
52+
}
53+
54+
/**
55+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
56+
* @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php
57+
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
58+
*
59+
* @param string $carrierCode
60+
* @param string $methodCode
61+
* @param float $amount
62+
* @param string $label
63+
* @dataProvider offlineShippingMethodDataProvider
64+
*/
65+
public function testSetOfflineShippingMethod(string $carrierCode, string $methodCode, float $amount, string $label)
66+
{
67+
$quote = $this->quoteFactory->create();
68+
$this->quoteResource->load(
69+
$quote,
70+
'test_order_1',
71+
'reserved_order_id'
72+
);
73+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
74+
$shippingAddressId = (int)$quote->getShippingAddress()->getId();
75+
76+
$query = $this->getQuery(
77+
$maskedQuoteId,
78+
$shippingAddressId,
79+
$carrierCode,
80+
$methodCode
81+
);
82+
83+
$response = $this->sendRequestWithToken($query);
84+
85+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
86+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $carrierCode);
87+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $methodCode);
88+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $amount);
89+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $label);
90+
}
91+
92+
/**
93+
* @return array
94+
*/
95+
public function offlineShippingMethodDataProvider()
96+
{
97+
return [
98+
'flatrate_flatrate' => ['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'],
99+
'tablerate_bestway' => ['tablerate', 'bestway', 10, 'Best Way - Table Rate'],
100+
'freeshipping_freeshipping' => ['freeshipping', 'freeshipping', 0, 'Free Shipping - Free'],
101+
];
102+
}
103+
104+
/**
105+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
106+
* @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php
107+
*/
108+
public function testSetShippingMethodTwiceInOneRequest()
109+
{
110+
$quote = $this->quoteFactory->create();
111+
$this->quoteResource->load(
112+
$quote,
113+
'test_order_1',
114+
'reserved_order_id'
115+
);
116+
$shippingAddress = $quote->getShippingAddress();
117+
$shippingAddressId = $shippingAddress->getId();
118+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
119+
120+
$query = <<<QUERY
121+
mutation {
122+
setShippingMethodsOnCart(input: {
123+
cart_id: "$maskedQuoteId"
124+
shipping_methods: [
125+
{
126+
cart_address_id: $shippingAddressId
127+
carrier_code: "flatrate"
128+
method_code: "flatrate"
129+
}
130+
{
131+
cart_address_id: $shippingAddressId
132+
carrier_code: "freeshipping"
133+
method_code: "freeshipping"
134+
}
135+
]
136+
}) {
137+
cart {
138+
shipping_addresses {
139+
selected_shipping_method {
140+
carrier_code
141+
method_code
142+
label
143+
amount
144+
}
145+
}
146+
}
147+
}
148+
}
149+
QUERY;
150+
self::expectExceptionMessage('You cannot specify multiple shipping methods.');
151+
$this->sendRequestWithToken($query);
152+
}
153+
154+
/**
155+
* Generates query for setting the specified shipping method on cart
156+
*
157+
* @param int $shippingAddressId
158+
* @param string $maskedQuoteId
159+
* @param string $carrierCode
160+
* @param string $methodCode
161+
* @return string
162+
*/
163+
private function getQuery(
164+
string $maskedQuoteId,
165+
int $shippingAddressId,
166+
string $carrierCode,
167+
string $methodCode
168+
): string {
169+
return <<<QUERY
170+
mutation {
171+
setShippingMethodsOnCart(input: {
172+
cart_id: "$maskedQuoteId"
173+
shipping_methods: [
174+
{
175+
cart_address_id: $shippingAddressId
176+
carrier_code: "$carrierCode"
177+
method_code: "$methodCode"
178+
}
179+
]
180+
}) {
181+
cart {
182+
shipping_addresses {
183+
selected_shipping_method {
184+
carrier_code
185+
method_code
186+
label
187+
amount
188+
}
189+
}
190+
}
191+
}
192+
}
193+
QUERY;
194+
}
195+
196+
/**
197+
* Sends a GraphQL request with using a bearer token
198+
*
199+
* @param string $query
200+
* @return array
201+
* @throws \Magento\Framework\Exception\AuthenticationException
202+
*/
203+
private function sendRequestWithToken(string $query): array
204+
{
205+
$customerToken = $this->customerTokenService->createCustomerAccessToken('[email protected]', 'password');
206+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
207+
208+
return $this->graphQlQuery($query, [], '', $headerMap);
209+
}
210+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,43 @@ public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int
105105
}
106106
QUERY;
107107
}
108+
109+
/**
110+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
111+
* @expectedException \Exception
112+
* @expectedExceptionMessage Could not find a cart with ID "wrong_cart_hash"
113+
*/
114+
public function testAddProductWithWrongCartHash()
115+
{
116+
$sku = 'simple';
117+
$qty = 1;
118+
119+
$maskedQuoteId = 'wrong_cart_hash';
120+
121+
$query = <<<QUERY
122+
mutation {
123+
addSimpleProductsToCart(
124+
input: {
125+
cart_id: "{$maskedQuoteId}"
126+
cartItems: [
127+
{
128+
data: {
129+
qty: $qty
130+
sku: "$sku"
131+
}
132+
}
133+
]
134+
}
135+
) {
136+
cart {
137+
items {
138+
qty
139+
}
140+
}
141+
}
142+
}
143+
QUERY;
144+
145+
$this->graphQlQuery($query);
146+
}
108147
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@ public function testGetNonExistentCart()
114114
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
115115
}
116116

117+
/**
118+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
119+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
120+
* @expectedException \Exception
121+
* @expectedExceptionMessage Current user does not have an active cart.
122+
*/
123+
public function testGetInactiveCart()
124+
{
125+
$quote = $this->quoteFactory->create();
126+
$this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id');
127+
$quote->setCustomerId(1);
128+
$quote->setIsActive(false);
129+
$this->quoteResource->save($quote);
130+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
131+
132+
$query = $this->getCartQuery($maskedQuoteId);
133+
134+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
135+
}
136+
117137
/**
118138
* @param string $maskedQuoteId
119139
* @return string

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ public function testPaymentMethodOnNonExistentCart()
171171
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
172172
}
173173

174+
/**
175+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_payment_saved.php
176+
*/
177+
public function testReSetPayment()
178+
{
179+
/** @var \Magento\Quote\Model\Quote $quote */
180+
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_1_with_payment');
181+
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
182+
$query = $this->prepareMutationQuery($maskedQuoteId, $methodCode);
183+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
184+
185+
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
186+
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
187+
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
188+
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
189+
}
190+
174191
/**
175192
* @param string $maskedQuoteId
176193
* @param string $methodCode

0 commit comments

Comments
 (0)