Skip to content

Commit 2d563e4

Browse files
ENGCOM-4615: magento/graphql-ce#542: Replace deprecated Magento/Checkout/_files/quote_with_address_saved.php fixture in SetOfflineShippingMethodsOnCartTest #543
- Merge Pull Request magento/graphql-ce#543 from magento/graphql-ce:542-new-fixture-for-SetOfflineShippingMethodsOnCartTest - Merged commits: 1. 4f19d03 2. 43f5bce 3. 4bdc30e 4. f9ecace 5. f01dc00 6. 16ce046 7. c2f15b3 8. 14d5c3d 9. 2643424 10. 7b80d61 11. cf4eb72 12. 829a39b 13. 9a0ecef 14. 1093d8b
2 parents a9a72b6 + 1093d8b commit 2d563e4

19 files changed

+877
-328
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
11-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1211
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1312
use Magento\Quote\Api\Data\AddressInterface;
1413
use Magento\Quote\Api\Data\AddressInterfaceFactory;
14+
use Magento\Quote\Api\Data\CartInterface;
1515
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource;
1616

1717
/**
@@ -44,14 +44,14 @@ public function __construct(
4444
/**
4545
* Get quote address
4646
*
47+
* @param CartInterface $cart
4748
* @param int $quoteAddressId
4849
* @param int|null $customerId
4950
* @return AddressInterface
50-
* @throws GraphQlInputException
51-
* @throws GraphQlNoSuchEntityException
5251
* @throws GraphQlAuthorizationException
52+
* @throws GraphQlNoSuchEntityException
5353
*/
54-
public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
54+
public function execute(CartInterface $cart, int $quoteAddressId, ?int $customerId): AddressInterface
5555
{
5656
$quoteAddress = $this->quoteAddressFactory->create();
5757

@@ -62,14 +62,15 @@ public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
6262
);
6363
}
6464

65-
$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();
66-
67-
/* Guest cart, allow operations */
68-
if (!$quoteAddressCustomerId && null === $customerId) {
69-
return $quoteAddress;
65+
// TODO: GetQuoteAddress::execute should depend only on AddressInterface contract
66+
// https://github.com/magento/graphql-ce/issues/550
67+
if ($quoteAddress->getQuoteId() !== $cart->getId()) {
68+
throw new GraphQlNoSuchEntityException(
69+
__('Cart does not contain address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
70+
);
7071
}
7172

72-
if ($quoteAddressCustomerId !== $customerId) {
73+
if ((int)$quoteAddress->getCustomerId() !== (int)$customerId) {
7374
throw new GraphQlAuthorizationException(
7475
__(
7576
'The current user cannot use cart address with ID "%cart_address_id"',

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
6969
}
7070
$methodCode = $shippingMethodInput['method_code'];
7171

72-
$quoteAddress = $this->getQuoteAddress->execute($cartAddressId, $context->getUserId());
73-
72+
$quoteAddress = $this->getQuoteAddress->execute($cart, $cartAddressId, $context->getUserId());
7473
$this->assignShippingMethodToCart->execute($cart, $quoteAddress, $carrierCode, $methodCode);
7574
}
7675
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,17 +429,27 @@ public function testSetBillingAddressIfCustomerIsNotOwnerOfAddress()
429429

430430
/**
431431
* @magentoApiDataFixture Magento/Customer/_files/customer.php
432+
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
432433
* @expectedException \Exception
433434
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
434435
*/
435436
public function testSetBillingAddressOnNonExistentCart()
436437
{
437438
$maskedQuoteId = 'non_existent_masked_id';
438439
$query = <<<QUERY
439-
{
440-
cart(cart_id: "$maskedQuoteId") {
441-
items {
442-
id
440+
mutation {
441+
setBillingAddressOnCart(
442+
input: {
443+
cart_id: "$maskedQuoteId"
444+
billing_address: {
445+
customer_address_id: 1
446+
}
447+
}
448+
) {
449+
cart {
450+
billing_address {
451+
city
452+
}
443453
}
444454
}
445455
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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\Customer;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\GraphQl\Quote\GetQuoteShippingAddressIdByReservedQuoteId;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test for setting offline shipping methods on cart
18+
*/
19+
class SetOfflineShippingMethodsOnCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var GetMaskedQuoteIdByReservedOrderId
23+
*/
24+
private $getMaskedQuoteIdByReservedOrderId;
25+
26+
/**
27+
* @var GetQuoteShippingAddressIdByReservedQuoteId
28+
*/
29+
private $getQuoteShippingAddressIdByReservedQuoteId;
30+
31+
/**
32+
* @var CustomerTokenServiceInterface
33+
*/
34+
private $customerTokenService;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
$objectManager = Bootstrap::getObjectManager();
42+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
43+
$this->getQuoteShippingAddressIdByReservedQuoteId = $objectManager->get(
44+
GetQuoteShippingAddressIdByReservedQuoteId::class
45+
);
46+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
47+
}
48+
49+
/**
50+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
51+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
52+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
53+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
54+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
55+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
56+
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
57+
*
58+
* @param string $carrierCode
59+
* @param string $methodCode
60+
* @param float $amount
61+
* @param string $label
62+
* @dataProvider offlineShippingMethodDataProvider
63+
*/
64+
public function testSetOfflineShippingMethod(string $carrierCode, string $methodCode, float $amount, string $label)
65+
{
66+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
67+
$quoteAddressId = $this->getQuoteShippingAddressIdByReservedQuoteId->execute('test_quote');
68+
69+
$query = $this->getQuery(
70+
$maskedQuoteId,
71+
$methodCode,
72+
$carrierCode,
73+
$quoteAddressId
74+
);
75+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
76+
77+
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
78+
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
79+
self::assertArrayHasKey('shipping_addresses', $response['setShippingMethodsOnCart']['cart']);
80+
self::assertCount(1, $response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
81+
82+
$shippingAddress = current($response['setShippingMethodsOnCart']['cart']['shipping_addresses']);
83+
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
84+
85+
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
86+
self::assertEquals($carrierCode, $shippingAddress['selected_shipping_method']['carrier_code']);
87+
88+
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
89+
self::assertEquals($methodCode, $shippingAddress['selected_shipping_method']['method_code']);
90+
91+
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
92+
self::assertEquals($amount, $shippingAddress['selected_shipping_method']['amount']);
93+
94+
self::assertArrayHasKey('label', $shippingAddress['selected_shipping_method']);
95+
self::assertEquals($label, $shippingAddress['selected_shipping_method']['label']);
96+
}
97+
98+
/**
99+
* @return array
100+
*/
101+
public function offlineShippingMethodDataProvider(): array
102+
{
103+
return [
104+
'flatrate_flatrate' => ['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'],
105+
'tablerate_bestway' => ['tablerate', 'bestway', 10, 'Best Way - Table Rate'],
106+
'freeshipping_freeshipping' => ['freeshipping', 'freeshipping', 0, 'Free Shipping - Free'],
107+
];
108+
}
109+
110+
/**
111+
* @param string $maskedQuoteId
112+
* @param string $shippingMethodCode
113+
* @param string $shippingCarrierCode
114+
* @param int $shippingAddressId
115+
* @return string
116+
*/
117+
private function getQuery(
118+
string $maskedQuoteId,
119+
string $shippingMethodCode,
120+
string $shippingCarrierCode,
121+
int $shippingAddressId
122+
): string {
123+
return <<<QUERY
124+
mutation {
125+
setShippingMethodsOnCart(input:
126+
{
127+
cart_id: "$maskedQuoteId",
128+
shipping_methods: [{
129+
cart_address_id: $shippingAddressId
130+
carrier_code: "$shippingCarrierCode"
131+
method_code: "$shippingMethodCode"
132+
}]
133+
}) {
134+
cart {
135+
shipping_addresses {
136+
selected_shipping_method {
137+
carrier_code
138+
method_code
139+
amount
140+
label
141+
}
142+
}
143+
}
144+
}
145+
}
146+
QUERY;
147+
}
148+
149+
/**
150+
* @param string $username
151+
* @param string $password
152+
* @return array
153+
*/
154+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
155+
{
156+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
157+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
158+
return $headerMap;
159+
}
160+
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,9 @@ public function testSetNonExistentPaymentMethod()
126126
public function testSetPaymentOnNonExistentCart()
127127
{
128128
$maskedQuoteId = 'non_existent_masked_id';
129-
$query = <<<QUERY
130-
{
131-
cart(cart_id: "$maskedQuoteId") {
132-
items {
133-
id
134-
}
135-
}
136-
}
137-
QUERY;
129+
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
130+
131+
$query = $this->getQuery($maskedQuoteId, $methodCode);
138132
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
139133
}
140134

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\GraphQl\Quote;
99

10+
use Magento\Framework\Exception\NoSuchEntityException;
1011
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
1112
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1213
use Magento\Quote\Model\QuoteFactory;
@@ -51,7 +52,7 @@ public function __construct(
5152
*
5253
* @param string $reversedOrderId
5354
* @return string
54-
* @throws \Magento\Framework\Exception\NoSuchEntityException
55+
* @throws NoSuchEntityException
5556
*/
5657
public function execute(string $reversedOrderId): string
5758
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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;
9+
10+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
11+
use Magento\Quote\Model\QuoteFactory;
12+
13+
/**
14+
* Get quote shipping address id by reserved order id
15+
*/
16+
class GetQuoteShippingAddressIdByReservedQuoteId
17+
{
18+
/**
19+
* @var QuoteFactory
20+
*/
21+
private $quoteFactory;
22+
23+
/**
24+
* @var QuoteResource
25+
*/
26+
private $quoteResource;
27+
28+
/**
29+
* @param QuoteFactory $quoteFactory
30+
* @param QuoteResource $quoteResource
31+
*/
32+
public function __construct(
33+
QuoteFactory $quoteFactory,
34+
QuoteResource $quoteResource
35+
) {
36+
$this->quoteFactory = $quoteFactory;
37+
$this->quoteResource = $quoteResource;
38+
}
39+
40+
/**
41+
* Get quote shipping address id by reserved order id
42+
*
43+
* @param string $reversedOrderId
44+
* @return int
45+
*/
46+
public function execute(string $reversedOrderId): int
47+
{
48+
$quote = $this->quoteFactory->create();
49+
$this->quoteResource->load($quote, $reversedOrderId, 'reserved_order_id');
50+
51+
return (int)$quote->getShippingAddress()->getId();
52+
}
53+
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,30 @@ public function testSetBillingAddressOnNonExistentCart()
248248
{
249249
$maskedQuoteId = 'non_existent_masked_id';
250250
$query = <<<QUERY
251-
{
252-
cart(cart_id: "$maskedQuoteId") {
253-
items {
254-
id
251+
mutation {
252+
setBillingAddressOnCart(
253+
input: {
254+
cart_id: "$maskedQuoteId"
255+
billing_address: {
256+
address: {
257+
firstname: "test firstname"
258+
lastname: "test lastname"
259+
company: "test company"
260+
street: ["test street 1", "test street 2"]
261+
city: "test city"
262+
region: "test region"
263+
postcode: "887766"
264+
country_code: "US"
265+
telephone: "88776655"
266+
save_in_address_book: false
267+
}
268+
}
269+
}
270+
) {
271+
cart {
272+
billing_address {
273+
city
274+
}
255275
}
256276
}
257277
}
@@ -289,7 +309,6 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
289309
}
290310
}
291311
QUERY;
292-
293312
$this->expectExceptionMessage($message);
294313
$this->graphQlQuery($query);
295314
}

0 commit comments

Comments
 (0)