Skip to content

Commit 3d72853

Browse files
author
Prabhu Ram
committed
MC-19254: Implement the schema changes
- Added resolver for applied_coupons
1 parent d91a70f commit 3d72853

File tree

3 files changed

+245
-1
lines changed

3 files changed

+245
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Exception\LocalizedException;
11+
use Magento\Quote\Api\CouponManagementInterface;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
class AppliedCoupons implements ResolverInterface
20+
{
21+
/**
22+
* @var CouponManagementInterface
23+
*/
24+
private $couponManagement;
25+
26+
/**
27+
* @param CouponManagementInterface $couponManagement
28+
*/
29+
public function __construct(
30+
CouponManagementInterface $couponManagement
31+
) {
32+
$this->couponManagement = $couponManagement;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
39+
{
40+
if (!isset($value['model'])) {
41+
throw new LocalizedException(__('"model" value should be specified'));
42+
}
43+
$cart = $value['model'];
44+
$cartId = $cart->getId();
45+
$appliedCoupons = [];
46+
$appliedCoupon = $this->couponManagement->get($cartId);
47+
if ($appliedCoupon) {
48+
$appliedCoupons[] = [ 'code' => $appliedCoupon ];
49+
}
50+
return !empty($appliedCoupons) ? $appliedCoupons : null;
51+
}
52+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ type PlaceOrderOutput {
193193
type Cart {
194194
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
195195
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon") @doc(description:"An array of coupons that have been applied to the cart") @deprecated(reason: "Use applied_coupons instead ")
196-
applied_coupons: [AppliedCoupon]
196+
applied_coupons: [AppliedCoupon] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupons") @doc(description:"An array of `AppliedCoupon` objects. Each object contains the `code` text attribute, which specifies the coupon code")
197197
email: String @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartEmail")
198198
shipping_addresses: [ShippingCartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
199199
billing_address: BillingCartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
/**
15+
* Test Apply Coupons to Cart functionality for guest
16+
*/
17+
class ApplyCouponsToCartTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetMaskedQuoteIdByReservedOrderId
21+
*/
22+
private $getMaskedQuoteIdByReservedOrderId;
23+
24+
protected function setUp()
25+
{
26+
$objectManager = Bootstrap::getObjectManager();
27+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
28+
}
29+
30+
/**
31+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
32+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
33+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
34+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
35+
*/
36+
public function testApplyCouponsToCart()
37+
{
38+
$couponCode = '2?ds5!2d';
39+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
40+
$query = $this->getQuery($maskedQuoteId, $couponCode);
41+
$response = $this->graphQlMutation($query);
42+
43+
self::assertArrayHasKey('applyCouponToCart', $response);
44+
self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupons'][0]['code']);
45+
}
46+
47+
/**
48+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
49+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
50+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
51+
* @expectedException \Exception
52+
* @expectedExceptionMessage Cart does not contain products.
53+
*/
54+
public function testApplyCouponsToCartWithoutItems()
55+
{
56+
$couponCode = '2?ds5!2d';
57+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
58+
$query = $this->getQuery($maskedQuoteId, $couponCode);
59+
60+
$this->graphQlMutation($query);
61+
}
62+
63+
/**
64+
* _security
65+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
66+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
67+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
68+
* @expectedException \Exception
69+
*/
70+
public function testApplyCouponsToCustomerCart()
71+
{
72+
$couponCode = '2?ds5!2d';
73+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
74+
$query = $this->getQuery($maskedQuoteId, $couponCode);
75+
76+
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
77+
$this->graphQlMutation($query);
78+
}
79+
80+
/**
81+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
82+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
83+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
84+
* @expectedException \Exception
85+
* @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
86+
*/
87+
public function testApplyNonExistentCouponToCart()
88+
{
89+
$couponCode = 'non_existent_coupon_code';
90+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
91+
$query = $this->getQuery($maskedQuoteId, $couponCode);
92+
93+
$this->graphQlMutation($query);
94+
}
95+
96+
/**
97+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
98+
* @expectedException \Exception
99+
*/
100+
public function testApplyCouponsToNonExistentCart()
101+
{
102+
$couponCode = '2?ds5!2d';
103+
$maskedQuoteId = 'non_existent_masked_id';
104+
$query = $this->getQuery($maskedQuoteId, $couponCode);
105+
106+
self::expectExceptionMessage('Could not find a cart with ID "' . $maskedQuoteId . '"');
107+
$this->graphQlMutation($query);
108+
}
109+
110+
/**
111+
* Products in cart don't fit to the coupon
112+
*
113+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
114+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
115+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
116+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
117+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/restrict_coupon_usage_for_simple_product.php
118+
* @expectedException \Exception
119+
* @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
120+
*/
121+
public function testApplyCouponsWhichIsNotApplicable()
122+
{
123+
$couponCode = '2?ds5!2d';
124+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
125+
$query = $this->getQuery($maskedQuoteId, $couponCode);
126+
127+
$this->graphQlMutation($query);
128+
}
129+
130+
/**
131+
* @param string $input
132+
* @param string $message
133+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
134+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
135+
* @dataProvider dataProviderUpdateWithMissedRequiredParameters
136+
* @expectedException \Exception
137+
*/
138+
public function testApplyCouponsWithMissedRequiredParameters(string $input, string $message)
139+
{
140+
$query = <<<QUERY
141+
mutation {
142+
applyCouponToCart(input: {{$input}}) {
143+
cart {
144+
applied_coupons {
145+
code
146+
}
147+
}
148+
}
149+
}
150+
QUERY;
151+
152+
$this->expectExceptionMessage($message);
153+
$this->graphQlMutation($query);
154+
}
155+
156+
/**
157+
* @return array
158+
*/
159+
public function dataProviderUpdateWithMissedRequiredParameters(): array
160+
{
161+
return [
162+
'missed_cart_id' => [
163+
'coupon_code: "test"',
164+
'Required parameter "cart_id" is missing'
165+
],
166+
'missed_coupon_code' => [
167+
'cart_id: "test_quote"',
168+
'Required parameter "coupon_code" is missing'
169+
],
170+
];
171+
}
172+
173+
/**
174+
* @param string $maskedQuoteId
175+
* @param string $couponCode
176+
* @return string
177+
*/
178+
private function getQuery(string $maskedQuoteId, string $couponCode): string
179+
{
180+
return <<<QUERY
181+
mutation {
182+
applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) {
183+
cart {
184+
applied_coupons {
185+
code
186+
}
187+
}
188+
}
189+
}
190+
QUERY;
191+
}
192+
}

0 commit comments

Comments
 (0)