1010use Magento \Catalog \Api \CategoryLinkManagementInterface ;
1111use Magento \Catalog \Api \ProductRepositoryInterface ;
1212use Magento \Catalog \Model \Product ;
13+ use Magento \Framework \Exception \AuthenticationException ;
14+ use Magento \GraphQl \GetCustomerAuthenticationHeader ;
1315use Magento \SalesRule \Api \RuleRepositoryInterface ;
1416use Magento \SalesRule \Model \ResourceModel \Rule \Collection ;
1517use Magento \SalesRule \Model \Rule ;
1618use Magento \Tax \Model \ClassModel as TaxClassModel ;
1719use Magento \Tax \Model \ResourceModel \TaxClass \CollectionFactory as TaxClassCollectionFactory ;
1820use Magento \TestFramework \Helper \Bootstrap ;
1921use Magento \TestFramework \TestCase \GraphQlAbstract ;
22+ use Magento \SalesRule \Api \Data \DiscountAppliedToInterface as DiscountAppliedTo ;
2023
2124/**
2225 * Test cases for applying cart promotions to items in cart
2326 */
2427class CartPromotionsTest extends GraphQlAbstract
2528{
29+ /** @var GetCustomerAuthenticationHeader */
30+ private $ customerAuthenticationHeader ;
31+
2632 /**
2733 * @var float
2834 */
2935 private const EPSILON = 0.0000000001 ;
3036
37+ protected function setUp ():void
38+ {
39+ parent ::setUp ();
40+ $ this ->customerAuthenticationHeader =
41+ Bootstrap::getObjectManager ()->get (GetCustomerAuthenticationHeader::class);
42+ }
43+
3144 /**
3245 * Test adding single cart rule to multiple products in a cart
3346 *
@@ -188,7 +201,51 @@ public function testCartPromotionsMultipleCartRules()
188201 ]
189202 );
190203 }
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+ );
192249 }
193250
194251 /**
@@ -499,6 +556,7 @@ private function getCartItemPricesQuery(string $cartId): string
499556 prices{
500557 discounts{
501558 amount{value}
559+ applied_to
502560 }
503561 }
504562 }
@@ -527,10 +585,11 @@ private function createEmptyCart(): string
527585 * @param int $sku1
528586 * @param int $qty
529587 * @param string $sku2
588+ * @return string
530589 */
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
532591 {
533- $ query = <<<QUERY
592+ return <<<QUERY
534593mutation {
535594 addSimpleProductsToCart(input: {
536595 cart_id: " {$ cartId }",
@@ -559,7 +618,17 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
559618 }
560619}
561620QUERY ;
621+ }
562622
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 );
563632 $ response = $ this ->graphQlMutation ($ query );
564633
565634 self ::assertArrayHasKey ('cart ' , $ response ['addSimpleProductsToCart ' ]);
@@ -569,6 +638,74 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
569638 self ::assertEquals ($ sku2 , $ response ['addSimpleProductsToCart ' ]['cart ' ]['items ' ][1 ]['product ' ]['sku ' ]);
570639 }
571640
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 =
'[email protected] ' ;
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+
572709 /**
573710 * Set shipping address for the region for which tax rule is set
574711 *
0 commit comments