10
10
use Magento \Catalog \Api \CategoryLinkManagementInterface ;
11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
12
use Magento \Catalog \Model \Product ;
13
+ use Magento \Framework \Exception \AuthenticationException ;
14
+ use Magento \GraphQl \GetCustomerAuthenticationHeader ;
13
15
use Magento \SalesRule \Api \RuleRepositoryInterface ;
14
16
use Magento \SalesRule \Model \ResourceModel \Rule \Collection ;
15
17
use Magento \SalesRule \Model \Rule ;
16
18
use Magento \Tax \Model \ClassModel as TaxClassModel ;
17
19
use Magento \Tax \Model \ResourceModel \TaxClass \CollectionFactory as TaxClassCollectionFactory ;
18
20
use Magento \TestFramework \Helper \Bootstrap ;
19
21
use Magento \TestFramework \TestCase \GraphQlAbstract ;
22
+ use Magento \SalesRule \Api \Data \DiscountAppliedToInterface as DiscountAppliedTo ;
20
23
21
24
/**
22
25
* Test cases for applying cart promotions to items in cart
23
26
*/
24
27
class CartPromotionsTest extends GraphQlAbstract
25
28
{
29
+ /** @var GetCustomerAuthenticationHeader */
30
+ private $ customerAuthenticationHeader ;
31
+
26
32
/**
27
33
* @var float
28
34
*/
29
35
private const EPSILON = 0.0000000001 ;
30
36
37
+ protected function setUp ():void
38
+ {
39
+ parent ::setUp ();
40
+ $ this ->customerAuthenticationHeader =
41
+ Bootstrap::getObjectManager ()->get (GetCustomerAuthenticationHeader::class);
42
+ }
43
+
31
44
/**
32
45
* Test adding single cart rule to multiple products in a cart
33
46
*
@@ -188,7 +201,51 @@ public function testCartPromotionsMultipleCartRules()
188
201
]
189
202
);
190
203
}
191
- $ this ->assertEquals ($ response ['cart ' ]['prices ' ]['discounts ' ][0 ]['amount ' ]['value ' ], 24.18 );
204
+ $ this ->assertEquals (21.98 , $ response ['cart ' ]['prices ' ]['discounts ' ][0 ]['amount ' ]['value ' ]);
205
+ $ this ->assertEquals (
206
+ DiscountAppliedTo::APPLIED_TO_ITEM ,
207
+ $ response ['cart ' ]['prices ' ]['discounts ' ][0 ][DiscountAppliedTo::APPLIED_TO ]
208
+ );
209
+ $ this ->assertEquals ($ response ['cart ' ]['prices ' ]['discounts ' ][1 ]['amount ' ]['value ' ], 2.2 );
210
+ $ this ->assertEquals (
211
+ DiscountAppliedTo::APPLIED_TO_ITEM ,
212
+ $ response ['cart ' ]['prices ' ]['discounts ' ][1 ][DiscountAppliedTo::APPLIED_TO ],
213
+ );
214
+ }
215
+
216
+ /**
217
+ * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
218
+ * @magentoApiDataFixture Magento/Sales/_files/quote_with_customer.php
219
+ * @magentoApiDataFixture Magento/SalesRule/_files/cart_rule_10_percent_off_with_discount_on_shipping.php
220
+ * @return void
221
+ * @throws AuthenticationException
222
+ */
223
+ public function testShippingDiscountPresent (): void
224
+ {
225
+ $ skus =['simple1 ' , 'simple2 ' ];
226
+ $ qty = 2 ;
227
+ $ quote = Bootstrap::getObjectManager ()
228
+ ->create (\Magento \Quote \Model \Quote::class)->load ('test01 ' , 'reserved_order_id ' );
229
+ $ cartId = $ quote ->getId ();
230
+
231
+ /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
232
+ $ quoteIdMask = Bootstrap::getObjectManager ()
233
+ ->create (\Magento \Quote \Model \QuoteIdMaskFactory::class)->create ();
234
+ $ quoteIdMask ->load ($ cartId , 'quote_id ' );
235
+ //Use masked cart Id
236
+ $ cartId = $ quoteIdMask ->getMaskedId ();
237
+ $ this ->addMultipleProductsToCustomerCart ($ cartId , $ qty , $ skus [0 ], $ skus [1 ]);
238
+ $ this ->setShippingMethodOnCustomerCart ($ cartId , ['carrier_code ' => 'flatrate ' , 'method_code ' => 'flatrate ' ]);
239
+ $ query = $ this ->getCartItemPricesQuery ($ cartId );
240
+ $ response = $ this ->graphQlMutationForCustomer ($ query );
241
+ $ this ->assertEquals (
242
+ DiscountAppliedTo::APPLIED_TO_ITEM ,
243
+ $ response ['cart ' ]['prices ' ]['discounts ' ][0 ][DiscountAppliedTo::APPLIED_TO ],
244
+ );
245
+ $ this ->assertEquals (
246
+ DiscountAppliedTo::APPLIED_TO_SHIPPING ,
247
+ $ response ['cart ' ]['prices ' ]['discounts ' ][1 ][DiscountAppliedTo::APPLIED_TO ],
248
+ );
192
249
}
193
250
194
251
/**
@@ -499,6 +556,7 @@ private function getCartItemPricesQuery(string $cartId): string
499
556
prices{
500
557
discounts{
501
558
amount{value}
559
+ applied_to
502
560
}
503
561
}
504
562
}
@@ -527,10 +585,11 @@ private function createEmptyCart(): string
527
585
* @param int $sku1
528
586
* @param int $qty
529
587
* @param string $sku2
588
+ * @return string
530
589
*/
531
- private function addMultipleSimpleProductsToCart (string $ cartId , int $ qty , string $ sku1 , string $ sku2 ): void
590
+ private function addSimpleProductsToCartQuery (string $ cartId , int $ qty , string $ sku1 , string $ sku2 ): string
532
591
{
533
- $ query = <<<QUERY
592
+ return <<<QUERY
534
593
mutation {
535
594
addSimpleProductsToCart(input: {
536
595
cart_id: " {$ cartId }",
@@ -559,7 +618,17 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
559
618
}
560
619
}
561
620
QUERY ;
621
+ }
562
622
623
+ /**
624
+ * @param string $cartId
625
+ * @param int $sku1
626
+ * @param int $qty
627
+ * @param string $sku2
628
+ */
629
+ private function addMultipleSimpleProductsToCart (string $ cartId , int $ qty , string $ sku1 , string $ sku2 ): void
630
+ {
631
+ $ query = $ this ->addSimpleProductsToCartQuery ($ cartId , $ qty , $ sku1 , $ sku2 );
563
632
$ response = $ this ->graphQlMutation ($ query );
564
633
565
634
self ::assertArrayHasKey ('cart ' , $ response ['addSimpleProductsToCart ' ]);
@@ -569,6 +638,74 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
569
638
self ::assertEquals ($ sku2 , $ response ['addSimpleProductsToCart ' ]['cart ' ]['items ' ][1 ]['product ' ]['sku ' ]);
570
639
}
571
640
641
+ /**
642
+ * Executes GraphQL mutation for a default customer
643
+ *
644
+ * @param string $query
645
+ * @return array
646
+ * @throws \Magento\Framework\Exception\AuthenticationException
647
+ */
648
+ private function graphQlMutationForCustomer (string $ query ): array
649
+ {
650
+ $ currentEmail = 'customer@example.com ' ;
651
+ $ currentPassword = 'password ' ;
652
+ return $ this ->graphQlMutation (
653
+ $ query ,
654
+ [],
655
+ '' ,
656
+ $ this ->customerAuthenticationHeader ->execute ($ currentEmail , $ currentPassword )
657
+ );
658
+ }
659
+
660
+ /**
661
+ * @param string $cartId
662
+ * @param int $sku1
663
+ * @param int $qty
664
+ * @param string $sku2
665
+ * @throws AuthenticationException
666
+ */
667
+ private function addMultipleProductsToCustomerCart (string $ cartId , int $ qty , string $ sku1 , string $ sku2 ): void
668
+ {
669
+ $ query = $ this ->addSimpleProductsToCartQuery ($ cartId , $ qty , $ sku1 , $ sku2 );
670
+ $ this ->graphQlMutationForCustomer ($ query );
671
+ }
672
+
673
+ /**
674
+ * Set shipping method on cart with GraphQl mutation
675
+ *
676
+ * @param string $cartId
677
+ * @param array $method
678
+ * @return array
679
+ */
680
+ private function setShippingMethodOnCustomerCart (string $ cartId , array $ method ): array
681
+ {
682
+ $ query = <<<QUERY
683
+ mutation {
684
+ setShippingMethodsOnCart(input: {
685
+ cart_id: " {$ cartId }",
686
+ shipping_methods: [
687
+ {
688
+ carrier_code: " {$ method ['carrier_code ' ]}"
689
+ method_code: " {$ method ['method_code ' ]}"
690
+ }
691
+ ]
692
+ }) {
693
+ cart {
694
+ available_payment_methods {
695
+ code
696
+ title
697
+ }
698
+ }
699
+ }
700
+ }
701
+ QUERY ;
702
+
703
+ $ response = $ this ->graphQlMutationForCustomer ($ query );
704
+
705
+ $ availablePaymentMethod = current ($ response ['setShippingMethodsOnCart ' ]['cart ' ]['available_payment_methods ' ]);
706
+ return $ availablePaymentMethod ;
707
+ }
708
+
572
709
/**
573
710
* Set shipping address for the region for which tax rule is set
574
711
*
0 commit comments