11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
12
use Magento \Catalog \Model \Product ;
13
13
use Magento \Catalog \Model \Product \Visibility ;
14
- use Magento \SalesRule \Api \RuleRepositoryInterface ;
15
14
use Magento \SalesRule \Model \ResourceModel \Rule \Collection ;
16
15
use Magento \SalesRule \Model \Rule ;
17
16
use Magento \TestFramework \Helper \Bootstrap ;
18
17
use Magento \TestFramework \TestCase \GraphQlAbstract ;
18
+ use Magento \Tax \Model \ClassModel as TaxClassModel ;
19
+ use Magento \Tax \Model \ResourceModel \TaxClass \CollectionFactory as TaxClassCollectionFactory ;
19
20
20
21
/**
21
22
* Test cases for applying cart promotions to items in cart
@@ -56,14 +57,26 @@ public function testCartPromotionSingleCartRule()
56
57
foreach ($ ruleCollection as $ rule ) {
57
58
$ ruleLabels = $ rule ->getStoreLabels ();
58
59
}
59
-
60
60
$ qty = 2 ;
61
61
$ cartId = $ this ->createEmptyCart ();
62
62
$ this ->addMultipleSimpleProductsToCart ($ cartId , $ qty , $ skus [0 ], $ skus [1 ]);
63
63
$ query = $ this ->getCartItemPricesQuery ($ cartId );
64
64
$ response = $ this ->graphQlMutation ($ query );
65
65
$ this ->assertCount (2 , $ response ['cart ' ]['items ' ]);
66
66
//validating the line item prices, quantity and discount
67
+ $ this ->assertLineItemDiscountPrices ($ response , $ productsInCart , $ qty , $ ruleLabels );
68
+ }
69
+
70
+ /**
71
+ * Assert the row total discounts and individual discount break down and cart rule labels
72
+ *
73
+ * @param $response
74
+ * @param $productsInCart
75
+ * @param $qty
76
+ * @param $ruleLabels
77
+ */
78
+ private function assertLineItemDiscountPrices ($ response , $ productsInCart , $ qty , $ ruleLabels )
79
+ {
67
80
$ productsInResponse = array_map (null , $ response ['cart ' ]['items ' ], $ productsInCart );
68
81
$ count = count ($ productsInCart );
69
82
for ($ itemIndex = 0 ; $ itemIndex < $ count ; $ itemIndex ++) {
@@ -80,54 +93,17 @@ public function testCartPromotionSingleCartRule()
80
93
0 =>[
81
94
'amount ' =>
82
95
['value ' => $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 ],
83
- 'label ' => $ ruleLabels [ 0 ]
96
+ 'label ' => ' TestRule_Label '
84
97
]
85
98
]
86
99
],
87
100
]
88
101
);
89
102
}
90
-
91
- /** @var Collection $ruleCollection */
92
- // $ruleCollection = $objectManager->get(Collection::class);
93
- /** @var RuleRepositoryInterface $ruleRepository */
94
- // $ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
95
-
96
- /** @var Rule $rule */
97
- // foreach ($ruleCollection as $rule) {
98
- // $ruleName = $rule->getName();
99
- // if($ruleName === '50% Off on Large Orders'){
100
- // $ruleId = $rule->getRuleId();
101
- /** @var \Magento\SalesRule\Model\Data\Rule $salesRule */
102
- // $salesRule = $ruleRepository->getById($ruleId);
103
- // $salesRule->setStoreLabels(['store_labels' => 'Test Label']);
104
-
105
- // $salesRule->setStoreLabels([
106
- // 'store_labels' => [
107
- // [
108
- // 'store_id' => 0,
109
- // 'store_label' => 'TestRule_Label',
110
- // ]
111
- // ]
112
- //
113
- // ]
114
- // );
115
- // $ruleRepository->save($salesRule);
116
- // $salesRule->save();
117
- /** @var Rule $salesRule */
118
- // $salesRule = $objectManager->get(Rule::class);
119
- // $salesRule->setData
120
- // ([
121
- // 'store_labels' => [0 => '50% discount for products in category']
122
- // ]
123
- // );
124
- // $salesRule->save();
125
-
126
-
127
103
}
128
104
129
105
/**
130
- * Test adding multiple cart rules to multiple products in a cart
106
+ * Test applying multiple cart rules to multiple products in a cart
131
107
*
132
108
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
133
109
* @magentoApiDataFixture Magento/SalesRule/_files/rules_category.php
@@ -174,15 +150,22 @@ public function testCartPromotionsMultipleCartRules()
174
150
for ($ itemIndex = 0 ; $ itemIndex < $ count ; $ itemIndex ++) {
175
151
$ this ->assertNotEmpty ($ productsInResponse [$ itemIndex ]);
176
152
$ lineItemDiscount = $ productsInResponse [$ itemIndex ][0 ]['prices ' ]['discounts ' ];
177
- $ expectedTotalDiscountValue = ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 )+($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 *0.1 );
178
- $ this ->assertEquals ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 , current ($ lineItemDiscount )['amount ' ]['value ' ]);
153
+ $ expectedTotalDiscountValue = ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 ) +
154
+ ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 *0.1 );
155
+ $ this ->assertEquals (
156
+ $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 ,
157
+ current ($ lineItemDiscount )['amount ' ]['value ' ]
158
+ );
179
159
$ this ->assertEquals ('TestRule_Label ' , current ($ lineItemDiscount )['label ' ]);
180
160
181
161
$ lineItemDiscountValue = next ($ lineItemDiscount )['amount ' ]['value ' ];
182
- $ this ->assertEquals (round ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 )*0.1 , $ lineItemDiscountValue );
162
+ $ this ->assertEquals (
163
+ round ($ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *0.5 )*0.1 ,
164
+ $ lineItemDiscountValue
165
+ );
183
166
$ this ->assertEquals ('10% off with two items_Label ' , end ($ lineItemDiscount )['label ' ]);
184
- $ actualTotalDiscountValue = $ lineItemDiscount [0 ]['amount ' ]['value ' ] + $ lineItemDiscount [1 ]['amount ' ]['value ' ];
185
- $ this ->assertEquals (round ($ expectedTotalDiscountValue ,2 ), $ actualTotalDiscountValue );
167
+ $ actualTotalDiscountValue = $ lineItemDiscount [0 ]['amount ' ]['value ' ]+ $ lineItemDiscount [1 ]['amount ' ]['value ' ];
168
+ $ this ->assertEquals (round ($ expectedTotalDiscountValue , 2 ), $ actualTotalDiscountValue );
186
169
187
170
//removing the elements from the response so that the rest of the response values can be compared
188
171
unset($ productsInResponse [$ itemIndex ][0 ]['prices ' ]['discounts ' ]);
@@ -200,6 +183,90 @@ public function testCartPromotionsMultipleCartRules()
200
183
}
201
184
}
202
185
186
+ /**
187
+ * Test applying single cart rules to multiple products in a cart with tax settings
188
+ * Tax settings are : Including and Excluding tax for Price Display and Shopping cart display settings
189
+ * Discount on Prices Includes Tax
190
+ * Tax rate = 7.5%
191
+ * Cart rule to apply 50% for products assigned to a specific category
192
+ *
193
+ * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
194
+ * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
195
+ * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_calculation_price_and_cart_display_settings.php
196
+ * @magentoApiDataFixture Magento/SalesRule/_files/rules_category.php
197
+ *
198
+ */
199
+ public function testCartPromotionsSingleCartRulesWithTaxes ()
200
+ {
201
+ $ objectManager = Bootstrap::getObjectManager ();
202
+ /** @var ProductRepositoryInterface $productRepository */
203
+ $ productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
204
+ /** @var Product $prod2 */
205
+ $ prod1 = $ productRepository ->get ('simple1 ' );
206
+ $ prod2 = $ productRepository ->get ('simple2 ' );
207
+ $ productsInCart = [$ prod1 , $ prod2 ];
208
+ $ skus =['simple1 ' , 'simple2 ' ];
209
+
210
+ /** @var TaxClassCollectionFactory $taxClassCollectionFactory */
211
+ $ taxClassCollectionFactory = $ objectManager ->get (TaxClassCollectionFactory::class);
212
+ $ taxClassCollection = $ taxClassCollectionFactory ->create ();
213
+
214
+ /** @var TaxClassModel $taxClass */
215
+ $ taxClassCollection ->addFieldToFilter ('class_type ' , TaxClassModel::TAX_CLASS_TYPE_PRODUCT );
216
+ $ taxClass = $ taxClassCollection ->getFirstItem ();
217
+ foreach ($ productsInCart as $ product ) {
218
+ $ product ->setCustomAttribute ('tax_class_id ' , $ taxClass ->getClassId ());
219
+ $ productRepository ->save ($ product );
220
+ }
221
+ $ categoryId = 66 ;
222
+ /** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */
223
+ $ categoryLinkManagement = $ objectManager ->create (CategoryLinkManagementInterface::class);
224
+ foreach ($ skus as $ sku ) {
225
+ $ categoryLinkManagement ->assignProductToCategories (
226
+ $ sku ,
227
+ [$ categoryId ]
228
+ );
229
+ }
230
+ $ qty = 1 ;
231
+ $ cartId = $ this ->createEmptyCart ();
232
+ $ this ->addMultipleSimpleProductsToCart ($ cartId , $ qty , $ skus [0 ], $ skus [1 ]);
233
+ $ this ->setShippingAddressOnCart ($ cartId );
234
+ $ query = $ this ->getCartItemPricesQuery ($ cartId );
235
+ $ response = $ this ->graphQlMutation ($ query );
236
+ $ this ->assertCount (2 , $ response ['cart ' ]['items ' ]);
237
+ $ productsInResponse = array_map (null , $ response ['cart ' ]['items ' ], $ productsInCart );
238
+ $ count = count ($ productsInCart );
239
+ for ($ itemIndex = 0 ; $ itemIndex < $ count ; $ itemIndex ++) {
240
+ $ this ->assertNotEmpty ($ productsInResponse [$ itemIndex ]);
241
+ $ rowTotalIncludingTax = round (
242
+ $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty +
243
+ $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty *.075 ,
244
+ 2
245
+ );
246
+ $ this ->assertResponseFields (
247
+ $ productsInResponse [$ itemIndex ][0 ],
248
+ [
249
+ 'quantity ' => $ qty ,
250
+ 'prices ' => [
251
+ // row_total is the line item price without the tax
252
+ 'row_total ' => ['value ' => $ productsInCart [$ itemIndex ]->getSpecialPrice ()*$ qty ],
253
+ // row_total including tax is the price + price * tax rate
254
+ 'row_total_including_tax ' => ['value ' => $ rowTotalIncludingTax ],
255
+ // discount from cart rule after tax is applied : 50% of row_total_including_tax
256
+ 'discount ' => ['value ' => round ($ rowTotalIncludingTax /2 , 2 )],
257
+ 'discounts ' => [
258
+ 0 =>[
259
+ 'amount ' =>
260
+ ['value ' => round ($ rowTotalIncludingTax /2 , 2 )],
261
+ 'label ' => 'TestRule_Label '
262
+ ]
263
+ ]
264
+ ],
265
+ ]
266
+ );
267
+ }
268
+ }
269
+
203
270
/**
204
271
* @param string $cartId
205
272
* @return string
@@ -292,4 +359,55 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
292
359
self ::assertEquals ($ qty , $ response ['addSimpleProductsToCart ' ]['cart ' ]['items ' ][1 ]['quantity ' ]);
293
360
self ::assertEquals ($ sku2 , $ response ['addSimpleProductsToCart ' ]['cart ' ]['items ' ][1 ]['product ' ]['sku ' ]);
294
361
}
362
+
363
+ /**
364
+ * Set shipping address for the region for which tax rule is set
365
+ *
366
+ * @param string $cartId
367
+ * @return void
368
+ */
369
+ private function setShippingAddressOnCart (string $ cartId ) :void
370
+ {
371
+ $ query = <<<QUERY
372
+ mutation {
373
+ setShippingAddressesOnCart(
374
+ input: {
375
+ cart_id: " $ cartId"
376
+ shipping_addresses: [
377
+ {
378
+ address: {
379
+ firstname: "John"
380
+ lastname: "Doe"
381
+ company: "Magento"
382
+ street: ["test street 1", "test street 2"]
383
+ city: "Montgomery"
384
+ region: "AL"
385
+ postcode: "36043"
386
+ country_code: "US"
387
+ telephone: "88776655"
388
+ save_in_address_book: false
389
+ }
390
+ }
391
+ ]
392
+ }
393
+ ) {
394
+ cart {
395
+ shipping_addresses {
396
+ city
397
+ region{label}
398
+ }
399
+ }
400
+ }
401
+ }
402
+ QUERY ;
403
+ $ response = $ this ->graphQlMutation ($ query );
404
+ self ::assertEquals (
405
+ 'Montgomery ' ,
406
+ $ response ['setShippingAddressesOnCart ' ]['cart ' ]['shipping_addresses ' ][0 ]['city ' ]
407
+ );
408
+ self ::assertEquals (
409
+ 'Alabama ' ,
410
+ $ response ['setShippingAddressesOnCart ' ]['cart ' ]['shipping_addresses ' ][0 ]['region ' ]['label ' ]
411
+ );
412
+ }
295
413
}
0 commit comments