Skip to content

Commit 997944e

Browse files
author
Anna Bukatar
committed
ACP2E-2044: Shipping Discount label is hardcoded
1 parent 42881d0 commit 997944e

File tree

4 files changed

+151
-13
lines changed

4 files changed

+151
-13
lines changed

app/code/Magento/SalesRule/Model/Data/DiscountData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
/**
1515
* Discount Data Model
1616
*/
17-
class DiscountData extends \Magento\Framework\Api\AbstractExtensibleObject
18-
implements DiscountDataInterface, DiscountAppliedToInterface
17+
class DiscountData extends \Magento\Framework\Api\AbstractExtensibleObject implements
18+
DiscountDataInterface,
19+
DiscountAppliedToInterface
1920
{
2021

2122
public const AMOUNT = 'amount';

app/code/Magento/SalesRule/Model/RulesApplier.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ public function addDiscountDescription($address, $rule)
165165
/**
166166
* Retrieve rule label
167167
*
168-
* @param $address
169-
* @param $rule
168+
* @param Address $address
169+
* @param Rule $rule
170170
* @return string
171171
*/
172-
private function getRuleLabel($address, $rule)
172+
private function getRuleLabel(Address $address, Rule $rule): string
173173
{
174174
$ruleLabel = $rule->getStoreLabel($address->getQuote()->getStore());
175175
$label = '';
@@ -190,12 +190,12 @@ private function getRuleLabel($address, $rule)
190190
/**
191191
* Add rule shipping discount description label to address object
192192
*
193-
* @param $address
194-
* @param $rule
195-
* @param $discount
193+
* @param Address $address
194+
* @param Rule $rule
195+
* @param array $discount
196196
* @return void
197197
*/
198-
public function addShippingDiscountDescription($address, $rule, $discount)
198+
public function addShippingDiscountDescription(Address $address, Rule $rule, array $discount): void
199199
{
200200
$addressDiscounts = $address->getExtensionAttributes()->getDiscounts();
201201
$ruleLabel = $this->getRuleLabel($address, $rule);

app/code/Magento/SalesRule/Test/Unit/Model/ValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function setUp(): void
110110
$this->helper = new ObjectManager($this);
111111
$this->rulesApplier = $this->createPartialMock(
112112
RulesApplier::class,
113-
['setAppliedRuleIds', 'applyRules', 'addDiscountDescription']
113+
['setAppliedRuleIds', 'applyRules', 'addDiscountDescription', 'addShippingDiscountDescription']
114114
);
115115

116116
$this->addressMock = $this->getMockBuilder(Address::class)

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

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,37 @@
1010
use Magento\Catalog\Api\CategoryLinkManagementInterface;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\Catalog\Model\Product;
13+
use Magento\Framework\Exception\AuthenticationException;
14+
use Magento\GraphQl\GetCustomerAuthenticationHeader;
1315
use Magento\SalesRule\Api\RuleRepositoryInterface;
1416
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
1517
use Magento\SalesRule\Model\Rule;
1618
use Magento\Tax\Model\ClassModel as TaxClassModel;
1719
use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
1820
use Magento\TestFramework\Helper\Bootstrap;
1921
use 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
*/
2427
class 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
534593
mutation {
535594
addSimpleProductsToCart(input: {
536595
cart_id: "{$cartId}",
@@ -559,7 +618,17 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
559618
}
560619
}
561620
QUERY;
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 = '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+
572709
/**
573710
* Set shipping address for the region for which tax rule is set
574711
*

0 commit comments

Comments
 (0)