8
8
namespace Magento \GraphQl \Quote \Customer ;
9
9
10
10
use Magento \Framework \Exception \AuthenticationException ;
11
+ use Magento \Framework \Exception \LocalizedException ;
12
+ use Magento \Framework \Exception \NoSuchEntityException ;
11
13
use Magento \Integration \Api \CustomerTokenServiceInterface ;
12
14
use Magento \Quote \Model \Quote ;
13
15
use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
14
16
use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
17
+ use Magento \SalesRule \Api \CouponRepositoryInterface ;
18
+ use Magento \SalesRule \Model \Coupon ;
15
19
use Magento \TestFramework \Helper \Bootstrap ;
16
20
use Magento \TestFramework \TestCase \GraphQlAbstract ;
17
21
@@ -40,13 +44,25 @@ class RemoveCouponFromCartTest extends GraphQlAbstract
40
44
*/
41
45
private $ customerTokenService ;
42
46
47
+ /**
48
+ * @var CouponRepositoryInterface
49
+ */
50
+ private $ couponRepository ;
51
+
52
+ /**
53
+ * @var Coupon
54
+ */
55
+ private $ coupon ;
56
+
43
57
protected function setUp ()
44
58
{
45
59
$ objectManager = Bootstrap::getObjectManager ();
46
60
$ this ->quoteResource = $ objectManager ->create (QuoteResource::class);
47
61
$ this ->quote = $ objectManager ->create (Quote::class);
48
62
$ this ->quoteIdToMaskedId = $ objectManager ->create (QuoteIdToMaskedQuoteIdInterface::class);
49
63
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
64
+ $ this ->couponRepository = $ objectManager ->get (CouponRepositoryInterface::class);
65
+ $ this ->coupon = $ objectManager ->create (Coupon::class);
50
66
}
51
67
52
68
/**
@@ -81,16 +97,119 @@ public function testRemoveCouponFromCart()
81
97
self ::assertNull ($ response ['removeCouponFromCart ' ]['cart ' ]['applied_coupon ' ]['code ' ]);
82
98
}
83
99
100
+ /**
101
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
102
+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
103
+ * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
104
+ */
105
+ public function testRemoveCouponFromCartTwice ()
106
+ {
107
+ $ couponCode = '2?ds5!2d ' ;
108
+
109
+ /* Apply coupon to the customer quote */
110
+ $ this ->quoteResource ->load (
111
+ $ this ->quote ,
112
+ 'test_order_with_simple_product_without_address ' ,
113
+ 'reserved_order_id '
114
+ );
115
+ $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
116
+
117
+ $ this ->quote ->setCustomerId (1 );
118
+ $ this ->quoteResource ->save ($ this ->quote );
119
+
120
+ $ queryHeaders = $ this ->prepareAuthorizationHeaders ('customer@example.com ' , 'password ' );
121
+ $ query = $ this ->prepareAddCouponRequestQuery ($ maskedQuoteId , $ couponCode );
122
+ $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
123
+
124
+ /* Remove coupon from the quote */
125
+ $ query = $ this ->prepareRemoveCouponRequestQuery ($ maskedQuoteId );
126
+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
127
+
128
+ self ::assertArrayHasKey ('removeCouponFromCart ' , $ response );
129
+ self ::assertNull ($ response ['removeCouponFromCart ' ]['cart ' ]['applied_coupon ' ]['code ' ]);
130
+
131
+ /* Remove coupon from the quote the second time */
132
+ $ query = $ this ->prepareRemoveCouponRequestQuery ($ maskedQuoteId );
133
+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
134
+
135
+ self ::assertArrayHasKey ('removeCouponFromCart ' , $ response );
136
+ self ::assertNull ($ response ['removeCouponFromCart ' ]['cart ' ]['applied_coupon ' ]['code ' ]);
137
+ }
138
+
139
+ /**
140
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
141
+ * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
142
+ */
143
+ public function testRemoveCouponFromEmptyCart ()
144
+ {
145
+ /* Assign the empty quote to the customer */
146
+ $ this ->quoteResource ->load (
147
+ $ this ->quote ,
148
+ 'test_order_1 ' ,
149
+ 'reserved_order_id '
150
+ );
151
+ $ quoteId = (int )$ this ->quote ->getId ();
152
+ $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ($ quoteId );
153
+
154
+ $ this ->quote ->setCustomerId (1 );
155
+ $ this ->quoteResource ->save ($ this ->quote );
156
+
157
+ /* Remove coupon from the empty quote */
158
+ $ query = $ this ->prepareRemoveCouponRequestQuery ($ maskedQuoteId );
159
+ $ queryHeaders = $ this ->prepareAuthorizationHeaders ('customer@example.com ' , 'password ' );
160
+
161
+ $ this ->expectExceptionMessage ("The \"$ quoteId \" Cart doesn't contain products " );
162
+ $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
163
+ }
164
+
165
+ /**
166
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
167
+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
168
+ * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
169
+ */
170
+ public function testRemoveCouponFromCartWithoutItems ()
171
+ {
172
+ $ couponCode = '2?ds5!2d ' ;
173
+
174
+ /* Assign the quote to the customer */
175
+ $ this ->quoteResource ->load (
176
+ $ this ->quote ,
177
+ 'test_order_with_simple_product_without_address ' ,
178
+ 'reserved_order_id '
179
+ );
180
+
181
+ $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
182
+
183
+ $ this ->quote ->setCustomerId (1 );
184
+ $ this ->quoteResource ->save ($ this ->quote );
185
+
186
+ /* Apply coupon to the customer quote */
187
+ $ queryHeaders = $ this ->prepareAuthorizationHeaders ('customer@example.com ' , 'password ' );
188
+ $ query = $ this ->prepareAddCouponRequestQuery ($ maskedQuoteId , $ couponCode );
189
+ $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
190
+
191
+ /* Clear the quote */
192
+ $ this ->quote ->removeAllItems ();
193
+ $ this ->quoteResource ->save ($ this ->quote );
194
+
195
+ /* Remove coupon from the customer quote */
196
+ $ query = $ this ->prepareRemoveCouponRequestQuery ($ maskedQuoteId );
197
+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
198
+
199
+ self ::assertArrayHasKey ('removeCouponFromCart ' , $ response );
200
+ self ::assertNull ($ response ['removeCouponFromCart ' ]['cart ' ]['applied_coupon ' ]['code ' ]);
201
+ }
202
+
84
203
/**
85
204
* @magentoApiDataFixture Magento/Customer/_files/two_customers.php
86
205
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
87
206
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
88
207
*/
89
- public function testRemoveCouponFromAonotherCustomerCart ()
208
+ public function testRemoveCouponFromAnotherCustomerCart ()
90
209
{
91
210
$ couponCode = '2?ds5!2d ' ;
92
211
93
- /* Apply coupon to the first customer quote */
212
+ /* Assign the quote to the customer */
94
213
$ this ->quoteResource ->load (
95
214
$ this ->quote ,
96
215
'test_order_with_simple_product_without_address ' ,
@@ -101,6 +220,7 @@ public function testRemoveCouponFromAonotherCustomerCart()
101
220
$ this ->quote ->setCustomerId (1 );
102
221
$ this ->quoteResource ->save ($ this ->quote );
103
222
223
+ /* Apply coupon to the first customer quote */
104
224
$ query = $ this ->prepareAddCouponRequestQuery ($ maskedQuoteId , $ couponCode );
105
225
$ queryHeaders = $ this ->prepareAuthorizationHeaders ('customer@example.com ' , 'password ' );
106
226
$ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
@@ -141,6 +261,60 @@ public function testRemoveCouponFromGuestCart()
141
261
$ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
142
262
}
143
263
264
+ /**
265
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
266
+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
267
+ * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
268
+ */
269
+ public function testRemoveNonExistentCouponFromCart ()
270
+ {
271
+ $ couponCode = '2?ds5!2d ' ;
272
+
273
+ /* Assign the quote to the customer */
274
+ $ this ->quoteResource ->load (
275
+ $ this ->quote ,
276
+ 'test_order_with_simple_product_without_address ' ,
277
+ 'reserved_order_id '
278
+ );
279
+ $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
280
+
281
+ $ this ->quote ->setCustomerId (1 );
282
+ $ this ->quoteResource ->save ($ this ->quote );
283
+
284
+ /* Apply coupon to the customer quote */
285
+ $ query = $ this ->prepareAddCouponRequestQuery ($ maskedQuoteId , $ couponCode );
286
+ $ queryHeaders = $ this ->prepareAuthorizationHeaders ('customer@example.com ' , 'password ' );
287
+ $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
288
+
289
+ /* Remove the coupon */
290
+ $ this ->removeCoupon ($ couponCode );
291
+
292
+ /* Remove the non-existent coupon from the quote */
293
+ $ query = $ this ->prepareRemoveCouponRequestQuery ($ maskedQuoteId );
294
+
295
+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ queryHeaders );
296
+
297
+ self ::assertArrayHasKey ('removeCouponFromCart ' , $ response );
298
+ self ::assertNull ($ response ['removeCouponFromCart ' ]['cart ' ]['applied_coupon ' ]['code ' ]);
299
+ }
300
+
301
+ /**
302
+ * Remove the given coupon code from the database
303
+ *
304
+ * @param string $couponCode
305
+ * @throws LocalizedException
306
+ * @throws NoSuchEntityException
307
+ */
308
+ private function removeCoupon (string $ couponCode ): void
309
+ {
310
+ $ this ->coupon ->loadByCode ($ couponCode );
311
+ $ couponId = $ this ->coupon ->getCouponId ();
312
+
313
+ if ($ couponId ) {
314
+ $ this ->couponRepository ->deleteById ($ couponId );
315
+ }
316
+ }
317
+
144
318
/**
145
319
* Retrieve customer authorization headers
146
320
*
0 commit comments