Skip to content

Commit 419106a

Browse files
authored
ENGCOM-4612: Add API Test For Selected Shipping Method functionality #491
2 parents a9a72b6 + 261a7e2 commit 419106a

18 files changed

+893
-118
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
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
class GetSelectedShippingMethodTest extends GraphQlAbstract
16+
{
17+
/**
18+
* @var CustomerTokenServiceInterface
19+
*/
20+
private $customerTokenService;
21+
22+
/**
23+
* @var GetMaskedQuoteIdByReservedOrderId
24+
*/
25+
private $getMaskedQuoteIdByReservedOrderId;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function setUp()
31+
{
32+
$objectManager = Bootstrap::getObjectManager();
33+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
34+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
35+
}
36+
37+
/**
38+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
39+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
40+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
41+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
42+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
43+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
44+
*/
45+
public function testGetSelectedShippingMethod()
46+
{
47+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
48+
49+
$query = $this->getQuery($maskedQuoteId);
50+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
51+
52+
self::assertArrayHasKey('cart', $response);
53+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
54+
self::assertCount(1, $response['cart']['shipping_addresses']);
55+
56+
$shippingAddress = current($response['cart']['shipping_addresses']);
57+
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
58+
59+
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
60+
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
61+
62+
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
63+
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
64+
}
65+
66+
/**
67+
* _security
68+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
69+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
70+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
71+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
72+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
73+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
74+
*/
75+
public function testGetSelectedShippingMethodFromGuestCart()
76+
{
77+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
78+
$query = $this->getQuery($maskedQuoteId);
79+
80+
$this->expectExceptionMessage(
81+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
82+
);
83+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
84+
}
85+
86+
/**
87+
* _security
88+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
89+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
90+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
91+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
92+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
93+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
94+
*/
95+
public function testGetSelectedShippingMethodFromAnotherCustomerCart()
96+
{
97+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
98+
$query = $this->getQuery($maskedQuoteId);
99+
100+
$this->expectExceptionMessage(
101+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
102+
);
103+
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
104+
}
105+
106+
/**
107+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
108+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
109+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
110+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
111+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
112+
*/
113+
public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet()
114+
{
115+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
116+
$query = $this->getQuery($maskedQuoteId);
117+
118+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
119+
120+
self::assertArrayHasKey('cart', $response);
121+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
122+
self::assertCount(1, $response['cart']['shipping_addresses']);
123+
124+
$shippingAddress = current($response['cart']['shipping_addresses']);
125+
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
126+
127+
self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']);
128+
self::assertNull($shippingAddress['selected_shipping_method']['method_code']);
129+
self::assertNull($shippingAddress['selected_shipping_method']['label']);
130+
self::assertNull($shippingAddress['selected_shipping_method']['amount']);
131+
}
132+
133+
/**
134+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
135+
*
136+
* @expectedException \Exception
137+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
138+
*/
139+
public function testGetGetSelectedShippingMethodOfNonExistentCart()
140+
{
141+
$maskedQuoteId = 'non_existent_masked_id';
142+
$query = $this->getQuery($maskedQuoteId);
143+
144+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
145+
}
146+
147+
/**
148+
* @param string $username
149+
* @param string $password
150+
* @return array
151+
*/
152+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
153+
{
154+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
155+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
156+
return $headerMap;
157+
}
158+
159+
/**
160+
* @param string $maskedQuoteId
161+
* @return string
162+
*/
163+
private function getQuery(string $maskedQuoteId): string
164+
{
165+
return <<<QUERY
166+
{
167+
cart(cart_id: "$maskedQuoteId") {
168+
shipping_addresses {
169+
selected_shipping_method {
170+
carrier_code
171+
method_code
172+
label
173+
amount
174+
}
175+
}
176+
}
177+
}
178+
QUERY;
179+
}
180+
}

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
}

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+
}

0 commit comments

Comments
 (0)