Skip to content

Commit cee9001

Browse files
ENGCOM-4647: [Test Coverage] 'RemoveCouponFromCart' functionality #497
- Merge Pull Request magento/graphql-ce#497 from magento/graphql-ce:481-remove-coupons-webapi-tests - Merged commits: 1. b93da7a 2. 3d421d4 3. 96543cf 4. 36d8c88 5. d7a5379 6. 9000000 7. a8b4cc9 8. 7677da6 9. 84314af 10. 4ebea96 11. 54355e3 12. 43fad75
2 parents 19c4a50 + 43fad75 commit cee9001

File tree

9 files changed

+371
-85
lines changed

9 files changed

+371
-85
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/AppliedCoupon.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Quote\Api\CouponManagementInterface;
1415

1516
/**
1617
* @inheritdoc
1718
*/
1819
class AppliedCoupon implements ResolverInterface
1920
{
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+
2035
/**
2136
* @inheritdoc
2237
*/
@@ -26,9 +41,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
2641
throw new LocalizedException(__('"model" value should be specified'));
2742
}
2843
$cart = $value['model'];
44+
$cartId = $cart->getId();
2945

30-
$appliedCoupon = $cart->getCouponCode();
31-
46+
$appliedCoupon = $this->couponManagement->get($cartId);
3247
return $appliedCoupon ? ['code' => $appliedCoupon] : null;
3348
}
3449
}

app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveCouponFromCart.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6262
try {
6363
$this->couponManagement->remove($cartId);
6464
} catch (NoSuchEntityException $e) {
65-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
65+
$message = $e->getMessage();
66+
if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) {
67+
$message = 'Cart does not contain products';
68+
}
69+
throw new GraphQlNoSuchEntityException(__($message), $e);
6670
} catch (CouldNotDeleteException $e) {
6771
throw new LocalizedException(__($e->getMessage()), $e);
6872
}

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

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -126,63 +126,6 @@ public function testGuestCustomerAttemptToChangeCustomerCart()
126126
$this->graphQlQuery($query);
127127
}
128128

129-
/**
130-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
131-
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
132-
*/
133-
public function testRemoveCoupon()
134-
{
135-
$couponCode = '2?ds5!2d';
136-
137-
/* Apply coupon to the quote */
138-
$this->quoteResource->load(
139-
$this->quote,
140-
'test_order_with_simple_product_without_address',
141-
'reserved_order_id'
142-
);
143-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
144-
$this->quoteResource->load(
145-
$this->quote,
146-
'test_order_with_simple_product_without_address',
147-
'reserved_order_id'
148-
);
149-
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
150-
$this->graphQlQuery($query);
151-
152-
/* Remove coupon from quote */
153-
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
154-
$response = $this->graphQlQuery($query);
155-
156-
self::assertArrayHasKey('removeCouponFromCart', $response);
157-
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
158-
}
159-
160-
/**
161-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
162-
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
163-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
164-
*/
165-
public function testRemoveCouponFromCustomerCartByGuest()
166-
{
167-
$this->quoteResource->load(
168-
$this->quote,
169-
'test_order_with_simple_product_without_address',
170-
'reserved_order_id'
171-
);
172-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
173-
$this->quoteResource->load(
174-
$this->quote,
175-
'test_order_with_simple_product_without_address',
176-
'reserved_order_id'
177-
);
178-
$this->quote->setCustomerId(1);
179-
$this->quoteResource->save($this->quote);
180-
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
181-
182-
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
183-
$this->graphQlQuery($query);
184-
}
185-
186129
/**
187130
* @param string $maskedQuoteId
188131
* @param string $couponCode
@@ -200,26 +143,6 @@ private function prepareAddCouponRequestQuery(string $maskedQuoteId, string $cou
200143
}
201144
}
202145
}
203-
QUERY;
204-
}
205-
206-
/**
207-
* @param string $maskedQuoteId
208-
* @return string
209-
*/
210-
private function prepareRemoveCouponRequestQuery(string $maskedQuoteId): string
211-
{
212-
return <<<QUERY
213-
mutation {
214-
removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
215-
cart {
216-
applied_coupon {
217-
code
218-
}
219-
}
220-
}
221-
}
222-
223146
QUERY;
224147
}
225148
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
/**
16+
* Check removing of the coupon from customer cart
17+
*/
18+
class RemoveCouponFromCartTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var CustomerTokenServiceInterface
22+
*/
23+
private $customerTokenService;
24+
25+
/**
26+
* @var GetMaskedQuoteIdByReservedOrderId
27+
*/
28+
private $getMaskedQuoteIdByReservedOrderId;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function setUp()
34+
{
35+
$objectManager = Bootstrap::getObjectManager();
36+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
37+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
38+
}
39+
40+
/**
41+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
42+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
43+
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
44+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
45+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
46+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
47+
*/
48+
public function testRemoveCouponFromCart()
49+
{
50+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
51+
52+
$query = $this->getQuery($maskedQuoteId);
53+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
54+
55+
self::assertArrayHasKey('removeCouponFromCart', $response);
56+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
57+
}
58+
59+
/**
60+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
61+
* @expectedException \Exception
62+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
63+
*/
64+
public function testRemoveCouponFromNonExistentCart()
65+
{
66+
$maskedQuoteId = 'non_existent_masked_id';
67+
$query = $this->getQuery($maskedQuoteId);
68+
69+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
70+
}
71+
72+
/**
73+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
74+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
75+
* @expectedException \Exception
76+
* @expectedExceptionMessage Cart does not contain products
77+
*/
78+
public function testRemoveCouponFromEmptyCart()
79+
{
80+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
81+
$query = $this->getQuery($maskedQuoteId);
82+
83+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
84+
}
85+
86+
/**
87+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
88+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
89+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
90+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
91+
*/
92+
public function testRemoveCouponFromCartIfCouponWasNotSet()
93+
{
94+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
95+
96+
$query = $this->getQuery($maskedQuoteId);
97+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
98+
99+
self::assertArrayHasKey('removeCouponFromCart', $response);
100+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
101+
}
102+
103+
/**
104+
* _security
105+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
106+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
107+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
108+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
109+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
110+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
111+
*/
112+
public function testRemoveCouponFromGuestCart()
113+
{
114+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
115+
$query = $this->getQuery($maskedQuoteId);
116+
117+
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
118+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
119+
}
120+
121+
/**
122+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
123+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
124+
* @magentoApiDataFixture Magento/Checkout/_files/discount_10percent_generalusers.php
125+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
126+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
127+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/apply_coupon.php
128+
*/
129+
public function testRemoveCouponFromAnotherCustomerCart()
130+
{
131+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
132+
$query = $this->getQuery($maskedQuoteId);
133+
134+
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
135+
$this->graphQlQuery($query, [], '', $this->getHeaderMap('[email protected]'));
136+
}
137+
138+
/**
139+
* @param string $maskedQuoteId
140+
* @return string
141+
*/
142+
private function getQuery(string $maskedQuoteId): string
143+
{
144+
return <<<QUERY
145+
mutation {
146+
removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
147+
cart {
148+
applied_coupon {
149+
code
150+
}
151+
}
152+
}
153+
}
154+
155+
QUERY;
156+
}
157+
158+
/**
159+
* @param string $username
160+
* @param string $password
161+
* @return array
162+
*/
163+
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
164+
{
165+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
166+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
167+
return $headerMap;
168+
}
169+
}

0 commit comments

Comments
 (0)