Skip to content

Commit d103a55

Browse files
committed
ACP2E-209: "Table rate" shipping method displays as "Not yet calculated" on checkout
Integration test implemented.
1 parent cfbcf9b commit d103a55

5 files changed

+201
-33
lines changed

dev/tests/integration/testsuite/Magento/Quote/Model/ShippingMethodManagementTest.php

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
use Magento\TestFramework\Helper\Bootstrap;
3333
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
3434
use PHPUnit\Framework\TestCase;
35-
35+
use Magento\Quote\Api\CouponManagementInterface;
3636
/**
3737
* Test for shipping methods management
3838
*
@@ -77,21 +77,22 @@ public function testRateAppliedToShipping(): void
7777
}
7878

7979
/**
80+
* Test table rate amount for the cart that contains some items with free shipping applied.
81+
*
8082
* @magentoConfigFixture current_store carriers/tablerate/active 1
8183
* @magentoConfigFixture current_store carriers/flatrate/active 0
8284
* @magentoConfigFixture current_store carriers/freeshipping/active 0
83-
* @magentoConfigFixture current_store carriers/tablerate/condition_name package_qty
84-
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping_by_cart.php
85-
* @magentoDataFixture Magento/Sales/_files/quote.php
86-
* @magentoDataFixture Magento/OfflineShipping/_files/tablerates.php
85+
* @magentoConfigFixture current_store carriers/tablerate/condition_name package_value_with_discount
86+
* @magentoDataFixture Magento/Catalog/_files/categories.php
87+
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping_by_category.php
88+
* @magentoDataFixture Magento/Sales/_files/quote_with_multiple_products.php
89+
* @magentoDataFixture Magento/OfflineShipping/_files/tablerates_price.php
8790
* @return void
8891
*/
89-
public function testTableRateFreeShipping()
92+
public function testTableRateWithCartRuleForFreeShipping()
9093
{
9194
$objectManager = Bootstrap::getObjectManager();
92-
/** @var Quote $quote */
93-
$quote = $objectManager->get(Quote::class);
94-
$quote->load('test01', 'reserved_order_id');
95+
$quote = $this->getQuote('tableRate');
9596
$cartId = $quote->getId();
9697
if (!$cartId) {
9798
$this->fail('quote fixture failed');
@@ -103,44 +104,40 @@ public function testTableRateFreeShipping()
103104
$quoteIdMask->load($cartId, 'quote_id');
104105
//Use masked cart Id
105106
$cartId = $quoteIdMask->getMaskedId();
106-
$data = [
107-
'data' => [
108-
'country_id' => "US",
109-
'postcode' => null,
110-
'region' => null,
111-
'region_id' => null
112-
]
113-
];
114-
/** @var EstimateAddressInterface $address */
115-
$address = $objectManager->create(EstimateAddressInterface::class, $data);
107+
$addressFactory = $this->objectManager->get(AddressInterfaceFactory::class);
108+
/** @var \Magento\Quote\Api\Data\AddressInterface $address */
109+
$address = $addressFactory->create();
110+
$address->setCountryId('US');
116111
/** @var GuestShippingMethodManagementInterface $shippingEstimation */
117112
$shippingEstimation = $objectManager->get(GuestShippingMethodManagementInterface::class);
118-
$result = $shippingEstimation->estimateByAddress($cartId, $address);
119-
$this->assertNotEmpty($result);
113+
$result = $shippingEstimation->estimateByExtendedAddress($cartId, $address);
114+
$this->assertCount(1, $result);
115+
$rate = reset($result);
120116
$expectedResult = [
121-
'method_code' => 'bestway',
122-
'amount' => 0
117+
'method_code' => 'bestway',
118+
'amount' => 10
123119
];
124-
foreach ($result as $rate) {
125-
$this->assertEquals($expectedResult['amount'], $rate->getAmount());
126-
$this->assertEquals($expectedResult['method_code'], $rate->getMethodCode());
127-
}
120+
$this->assertEquals($expectedResult['method_code'], $rate->getMethodCode());
121+
$this->assertEquals($expectedResult['amount'], $rate->getAmount());
128122
}
129123

130124
/**
131-
* Test table rate amount for the cart that contains some items with free shipping applied.
125+
* Test table rate with zero amount is available for the cart when discount coupon cart price rule to all items
126+
* and freeshipping cart price rule is applied when order subtotal is greater than specified amount.
132127
*
133128
* @magentoConfigFixture current_store carriers/tablerate/active 1
134129
* @magentoConfigFixture current_store carriers/flatrate/active 0
135130
* @magentoConfigFixture current_store carriers/freeshipping/active 0
136-
* @magentoConfigFixture current_store carriers/tablerate/condition_name package_value_with_discount
131+
* @magentoConfigFixture default_store carriers/tablerate/condition_name package_value_with_discount
137132
* @magentoDataFixture Magento/Catalog/_files/categories.php
138-
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping_by_category.php
133+
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_free_shipping_subtotal_equals_more_than_30.php
134+
* @magentoDataFixture Magento/SalesRule/_files/cart_rule_20_percent_off_with_coupon_no_condition.php
139135
* @magentoDataFixture Magento/Sales/_files/quote_with_multiple_products.php
140136
* @magentoDataFixture Magento/OfflineShipping/_files/tablerates_price.php
137+
*
141138
* @return void
142139
*/
143-
public function testTableRateWithCartRuleForFreeShipping()
140+
public function testTableRateWithZeroPriceShownWhenDiscountCouponAndFreeShippingCartRuleApplied()
144141
{
145142
$objectManager = Bootstrap::getObjectManager();
146143
$quote = $this->getQuote('tableRate');
@@ -159,14 +156,18 @@ public function testTableRateWithCartRuleForFreeShipping()
159156
/** @var \Magento\Quote\Api\Data\AddressInterface $address */
160157
$address = $addressFactory->create();
161158
$address->setCountryId('US');
159+
/** @var CouponManagementInterface $couponManagement */
160+
$couponManagement = Bootstrap::getObjectManager()->get(CouponManagementInterface::class);
161+
$couponManagement->set($quote->getId(), '123');
162162
/** @var GuestShippingMethodManagementInterface $shippingEstimation */
163163
$shippingEstimation = $objectManager->get(GuestShippingMethodManagementInterface::class);
164164
$result = $shippingEstimation->estimateByExtendedAddress($cartId, $address);
165165
$this->assertCount(1, $result);
166166
$rate = reset($result);
167+
167168
$expectedResult = [
168-
'method_code' => 'bestway',
169-
'amount' => 10
169+
'method_code' => 'bestway',
170+
'amount' => 0
170171
];
171172
$this->assertEquals($expectedResult['method_code'], $rate->getMethodCode());
172173
$this->assertEquals($expectedResult['amount'], $rate->getAmount());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\SalesRule\Model\Coupon;
9+
use Magento\SalesRule\Model\Rule;
10+
use Magento\SalesRule\Model\RuleFactory;
11+
use Magento\SalesRule\Api\CouponRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\Customer\Model\GroupManagement;
14+
use Magento\Framework\Registry;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
$salesRuleFactory = $objectManager->create(RuleFactory::class);
19+
/** @var Rule $salesRule */
20+
$salesRule = $salesRuleFactory->create();
21+
$salesRule->setData(
22+
[
23+
'name' => 'Rule2',
24+
'is_active' => 1,
25+
'customer_group_ids' => [GroupManagement::NOT_LOGGED_IN_ID],
26+
'coupon_type' => Rule::COUPON_TYPE_SPECIFIC,
27+
'simple_action' => 'by_percent',
28+
'discount_amount' => 20,
29+
'discount_step' => 0,
30+
'stop_rules_processing' => 0,
31+
'website_ids' => [
32+
$objectManager->get(
33+
StoreManagerInterface::class
34+
)->getWebsite()->getId()
35+
]
36+
]
37+
);
38+
$salesRule->save();
39+
40+
// Create specific coupon to cover "20% of product price discount" to cart total
41+
$coupon = $objectManager->create(Coupon::class);
42+
$coupon->setRuleId($salesRule->getId())
43+
->setCode('123')
44+
->setType(0);
45+
46+
/** @var CouponRepositoryInterface $couponRepository */
47+
$couponRepository = $objectManager->get(CouponRepositoryInterface::class);
48+
$couponRepository->save($coupon);
49+
50+
/** @var Registry $registry */
51+
$registry = $objectManager->get(Registry::class);
52+
53+
$registry->unregister('cart_rule_20_percent_off_with_coupon_no_condition');
54+
$registry->register('cart_rule_20_percent_off_with_coupon_no_condition', $salesRule);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Api\SearchCriteriaBuilder;
9+
use Magento\SalesRule\Api\CouponRepositoryInterface;
10+
use Magento\SalesRule\Api\RuleRepositoryInterface;
11+
use Magento\SalesRule\Model\Coupon;
12+
use Magento\SalesRule\Model\Rule;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
$objectManager = Bootstrap::getObjectManager();
16+
17+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
18+
$searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
19+
$searchCriteria = $searchCriteriaBuilder->addFilter('name', 'Rule2')
20+
->create();
21+
22+
/** @var RuleRepositoryInterface $ruleRepository */
23+
$ruleRepository = Bootstrap::getObjectManager()->get(RuleRepositoryInterface::class);
24+
$items = $ruleRepository->getList($searchCriteria)
25+
->getItems();
26+
27+
$salesRule = array_pop($items);
28+
29+
/** @var Rule $salesRule */
30+
if ($salesRule !== null) {
31+
/** @var RuleRepositoryInterface $ruleRepository */
32+
$ruleRepository = $objectManager->get(RuleRepositoryInterface::class);
33+
$ruleRepository->deleteById($salesRule->getRuleId());
34+
}
35+
36+
$coupon = $objectManager->create(Coupon::class);
37+
$coupon->loadByCode('123');
38+
if ($coupon->getCouponId()) {
39+
/** @var CouponRepositoryInterface $couponRepository */
40+
$couponRepository = $objectManager->get(CouponRepositoryInterface::class);
41+
$couponRepository->deleteById($coupon->getCouponId());
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
/** @var Magento\Framework\Registry $registry */
9+
$registry = $objectManager->get(\Magento\Framework\Registry::class);
10+
11+
/** @var \Magento\SalesRule\Model\Rule $salesRule */
12+
$salesRule = $objectManager->create(\Magento\SalesRule\Model\Rule::class);
13+
$salesRule->setData(
14+
[
15+
'name' => 'Rule1',
16+
'is_active' => 1,
17+
'customer_group_ids' => [\Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID],
18+
'coupon_type' => \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON,
19+
'conditions' => [
20+
[
21+
'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
22+
'attribute' => 'base_subtotal',
23+
'operator' => '>=',
24+
'value' => 30
25+
]
26+
],
27+
'simple_action' => 'by_percent',
28+
'discount_amount' => 0,
29+
'stop_rules_processing' => 0,
30+
'simple_free_shipping' => 1,
31+
'website_ids' => [
32+
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
33+
\Magento\Store\Model\StoreManagerInterface::class
34+
)->getWebsite()->getId()
35+
]
36+
]
37+
);
38+
$salesRule->save();
39+
$registry->unregister('Magento/SalesRule/_files/cart_rule_free_shipping_subtotal_equals_more_than_30');
40+
$registry->register('Magento/SalesRule/_files/cart_rule_free_shipping_subtotal_equals_more_than_30_rollback',
41+
$salesRule->getRuleId()
42+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\Registry;
11+
use Magento\SalesRule\Api\RuleRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
14+
$bootstrap = Bootstrap::getObjectManager();
15+
16+
/** @var Registry $registry */
17+
$registry = $bootstrap->get(Registry::class);
18+
19+
/** @var RuleRepositoryInterface $ruleRepository */
20+
$ruleRepository = $bootstrap->get(RuleRepositoryInterface::class);
21+
22+
$ruleId = $registry->registry('Magento/SalesRule/_files/cart_rule_free_shipping_subtotal_equals_more_than_30_rollback');
23+
if ($ruleId) {
24+
try {
25+
$ruleRepository->deleteById($ruleId);
26+
$registry->unregister('Magento/SalesRule/_files/cart_rule_free_shipping_subtotal_equals_more_than_30_rollback');
27+
} catch (NoSuchEntityException $e) {
28+
}
29+
}

0 commit comments

Comments
 (0)