Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 8df5651

Browse files
authored
ENGCOM-4078: Query for retrieving shopping cart information #266
2 parents cd50535 + a5ac789 commit 8df5651

File tree

5 files changed

+250
-5
lines changed

5 files changed

+250
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public function execute(Quote $cart): array
4040
];
4141
}
4242

43+
$appliedCoupon = $cart->getCouponCode();
44+
4345
return [
4446
'items' => $items,
47+
'applied_coupon' => $appliedCoupon ? ['code' => $appliedCoupon] : null
4548
];
4649
}
4750
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
15+
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromCart;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class Cart implements ResolverInterface
21+
{
22+
/**
23+
* @var ExtractDataFromCart
24+
*/
25+
private $extractDataFromCart;
26+
27+
/**
28+
* @var GetCartForUser
29+
*/
30+
private $getCartForUser;
31+
32+
/**
33+
* @param GetCartForUser $getCartForUser
34+
* @param ExtractDataFromCart $extractDataFromCart
35+
*/
36+
public function __construct(
37+
GetCartForUser $getCartForUser,
38+
ExtractDataFromCart $extractDataFromCart
39+
) {
40+
$this->getCartForUser = $getCartForUser;
41+
$this->extractDataFromCart = $extractDataFromCart;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
48+
{
49+
if (!isset($args['cart_id'])) {
50+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
51+
}
52+
$maskedCartId = $args['cart_id'];
53+
54+
$currentUserId = $context->getUserId();
55+
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId);
56+
57+
$data = array_merge(
58+
[
59+
'cart_id' => $maskedCartId,
60+
'model' => $cart
61+
],
62+
$this->extractDataFromCart->execute($cart)
63+
);
64+
65+
return $data;
66+
}
67+
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See COPYING.txt for license details.
33

44
type Query {
5-
getAvailableShippingMethodsOnCart(input: AvailableShippingMethodsOnCartInput): AvailableShippingMethodsOnCartOutput @doc(description:"Returns available shipping methods for cart by address/address_id")
5+
Cart(cart_id: String!): Cart @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart") @doc(description:"Returns information about shopping cart")
66
}
77

88
type Mutation {
@@ -84,10 +84,6 @@ input AvailableShippingMethodsOnCartInput {
8484
address: CartAddressInput
8585
}
8686

87-
type AvailableShippingMethodsOnCartOutput {
88-
available_shipping_methods: [CheckoutShippingMethod]
89-
}
90-
9187
input ApplyCouponToCartInput {
9288
cart_id: String!
9389
coupon_code: String!
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\Quote;
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 getting cart information
19+
*/
20+
class GetCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var CustomerTokenServiceInterface
24+
*/
25+
private $customerTokenService;
26+
27+
/**
28+
* @var QuoteResource
29+
*/
30+
private $quoteResource;
31+
32+
/**
33+
* @var Quote
34+
*/
35+
private $quote;
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->create(QuoteResource::class);
49+
$this->quote = $objectManager->create(Quote::class);
50+
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
51+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
52+
}
53+
54+
/**
55+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
56+
*/
57+
public function testGetOwnCartForRegisteredCustomer()
58+
{
59+
$reservedOrderId = 'test_order_item_with_items';
60+
$this->quoteResource->load(
61+
$this->quote,
62+
$reservedOrderId,
63+
'reserved_order_id'
64+
);
65+
66+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
67+
$query = $this->prepareGetCartQuery($maskedQuoteId);
68+
69+
$response = $this->sendRequestWithToken($query);
70+
71+
self::assertArrayHasKey('Cart', $response);
72+
self::assertNotEmpty($response['Cart']['items']);
73+
self::assertNotEmpty($response['Cart']['addresses']);
74+
}
75+
76+
/**
77+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
78+
*/
79+
public function testGetCartFromAnotherCustomer()
80+
{
81+
$reservedOrderId = 'test_order_item_with_items';
82+
$this->quoteResource->load(
83+
$this->quote,
84+
$reservedOrderId,
85+
'reserved_order_id'
86+
);
87+
88+
89+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
90+
$query = $this->prepareGetCartQuery($maskedQuoteId);
91+
92+
self::expectExceptionMessage("The current user cannot perform operations on cart \"$maskedQuoteId\"");
93+
94+
$this->graphQlQuery($query);
95+
}
96+
97+
/**
98+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
99+
*/
100+
public function testGetCartForGuest()
101+
{
102+
$reservedOrderId = 'test_order_1';
103+
$this->quoteResource->load(
104+
$this->quote,
105+
$reservedOrderId,
106+
'reserved_order_id'
107+
);
108+
109+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
110+
$query = $this->prepareGetCartQuery($maskedQuoteId);
111+
112+
$response = $this->graphQlQuery($query);
113+
114+
self::assertArrayHasKey('Cart', $response);
115+
}
116+
117+
public function testGetNonExistentCart()
118+
{
119+
$maskedQuoteId = 'non_existent_masked_id';
120+
$query = $this->prepareGetCartQuery($maskedQuoteId);
121+
122+
self::expectExceptionMessage("Could not find a cart with ID \"$maskedQuoteId\"");
123+
124+
$this->graphQlQuery($query);
125+
}
126+
127+
/**
128+
* Generates query for setting the specified shipping method on cart
129+
*
130+
* @param string $maskedQuoteId
131+
* @return string
132+
*/
133+
private function prepareGetCartQuery(
134+
string $maskedQuoteId
135+
) : string {
136+
return <<<QUERY
137+
{
138+
Cart(cart_id: "$maskedQuoteId") {
139+
applied_coupon {
140+
code
141+
}
142+
items {
143+
id
144+
}
145+
addresses {
146+
firstname,
147+
lastname,
148+
address_type
149+
}
150+
}
151+
}
152+
153+
QUERY;
154+
}
155+
156+
/**
157+
* Sends a GraphQL request with using a bearer token
158+
*
159+
* @param string $query
160+
* @return array
161+
* @throws \Magento\Framework\Exception\AuthenticationException
162+
*/
163+
private function sendRequestWithToken(string $query): array
164+
{
165+
166+
$customerToken = $this->customerTokenService->createCustomerAccessToken('[email protected]', 'password');
167+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
168+
169+
return $this->graphQlQuery($query, [], '', $headerMap);
170+
}
171+
}

dev/tests/integration/testsuite/Magento/Checkout/_files/active_quote.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@
99
->setIsMultiShipping(false)
1010
->setReservedOrderId('test_order_1')
1111
->save();
12+
13+
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
14+
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
15+
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
16+
->create();
17+
$quoteIdMask->setQuoteId($quote->getId());
18+
$quoteIdMask->setDataChanges(true);
19+
$quoteIdMask->save();

0 commit comments

Comments
 (0)