Skip to content

Commit c9bd251

Browse files
MC-33879: Paypal checkout Shipping method not updating correctly
1 parent 90334f8 commit c9bd251

File tree

3 files changed

+137
-18
lines changed

3 files changed

+137
-18
lines changed

app/code/Magento/Braintree/Model/Paypal/Helper/ShippingMethodUpdater.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Quote\Model\Quote;
1111

1212
/**
13-
* Class ShippingMethodUpdater
13+
* Class for updating shipping method in the quote.
1414
*/
1515
class ShippingMethodUpdater extends AbstractHelper
1616
{
@@ -58,6 +58,12 @@ public function execute($shippingMethod, Quote $quote)
5858
$this->disabledQuoteAddressValidation($quote);
5959

6060
$shippingAddress->setShippingMethod($shippingMethod);
61+
$quoteExtensionAttributes = $quote->getExtensionAttributes();
62+
if ($quoteExtensionAttributes && $quoteExtensionAttributes->getShippingAssignments()) {
63+
$quoteExtensionAttributes->getShippingAssignments()[0]
64+
->getShipping()
65+
->setMethod($shippingMethod);
66+
}
6167
$shippingAddress->setCollectShippingRates(true);
6268

6369
$quote->collectTotals();

app/code/Magento/Braintree/Test/Unit/Model/Paypal/Helper/ShippingMethodUpdaterTest.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,55 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Braintree\Test\Unit\Model\Paypal\Helper;
79

810
use Magento\Quote\Model\Quote;
911
use Magento\Quote\Model\Quote\Address;
1012
use Magento\Quote\Api\CartRepositoryInterface;
1113
use Magento\Braintree\Gateway\Config\PayPal\Config;
1214
use Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
1317

1418
/**
15-
* Class ShippingMethodUpdaterTest
16-
*
17-
* @see \Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater
19+
* Test shipping method updater
1820
*/
19-
class ShippingMethodUpdaterTest extends \PHPUnit\Framework\TestCase
21+
class ShippingMethodUpdaterTest extends TestCase
2022
{
2123
const TEST_SHIPPING_METHOD = 'test-shipping-method';
2224

2325
const TEST_EMAIL = 'test@test.loc';
2426

2527
/**
26-
* @var Config|\PHPUnit_Framework_MockObject_MockObject
28+
* @var Config|MockObject
2729
*/
2830
private $configMock;
2931

3032
/**
31-
* @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
33+
* @var CartRepositoryInterface|MockObject
3234
*/
3335
private $quoteRepositoryMock;
3436

3537
/**
36-
* @var Address|\PHPUnit_Framework_MockObject_MockObject
38+
* @var Address|MockObject
3739
*/
3840
private $shippingAddressMock;
3941

42+
/**
43+
* @var Address|MockObject
44+
*/
45+
private $billingAddressMock;
46+
4047
/**
4148
* @var ShippingMethodUpdater
4249
*/
4350
private $shippingMethodUpdater;
4451

52+
/**
53+
* @inheritdoc
54+
*/
4555
protected function setUp()
4656
{
4757
$this->configMock = $this->getMockBuilder(Config::class)
@@ -68,17 +78,22 @@ protected function setUp()
6878
}
6979

7080
/**
81+
* Test execute exception
82+
*
7183
* @expectedException \InvalidArgumentException
7284
* @expectedExceptionMessage The "shippingMethod" field does not exists.
7385
*/
74-
public function testExecuteException()
86+
public function testExecuteException(): void
7587
{
7688
$quoteMock = $this->getQuoteMock();
7789

7890
$this->shippingMethodUpdater->execute('', $quoteMock);
7991
}
8092

81-
public function testExecute()
93+
/**
94+
* Test execute
95+
*/
96+
public function testExecute(): void
8297
{
8398
$quoteMock = $this->getQuoteMock();
8499

@@ -114,9 +129,13 @@ public function testExecute()
114129
}
115130

116131
/**
117-
* @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
132+
* Disable quote address validation
133+
*
134+
* @param MockObject $quoteMock
135+
*
136+
* @return void
118137
*/
119-
private function disabledQuoteAddressValidationStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
138+
private function disabledQuoteAddressValidationStep(MockObject $quoteMock): void
120139
{
121140
$billingAddressMock = $this->getBillingAddressMock($quoteMock);
122141

@@ -139,10 +158,12 @@ private function disabledQuoteAddressValidationStep(\PHPUnit_Framework_MockObjec
139158
}
140159

141160
/**
142-
* @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
143-
* @return Address|\PHPUnit_Framework_MockObject_MockObject
161+
* Get billing address mock object
162+
*
163+
* @param MockObject $quoteMock
164+
* @return Address|MockObject
144165
*/
145-
private function getBillingAddressMock(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
166+
private function getBillingAddressMock(MockObject $quoteMock): MockObject
146167
{
147168
if (!isset($this->billingAddressMock)) {
148169
$this->billingAddressMock = $this->getMockBuilder(Address::class)
@@ -159,17 +180,20 @@ private function getBillingAddressMock(\PHPUnit_Framework_MockObject_MockObject
159180
}
160181

161182
/**
162-
* @return Quote|\PHPUnit_Framework_MockObject_MockObject
183+
* Get quote mock object
184+
*
185+
* @return Quote|MockObject
163186
*/
164-
private function getQuoteMock()
187+
private function getQuoteMock(): MockObject
165188
{
166189
return $this->getMockBuilder(Quote::class)
167190
->setMethods(
168191
[
169192
'collectTotals',
170193
'getBillingAddress',
171194
'getShippingAddress',
172-
'getIsVirtual'
195+
'getIsVirtual',
196+
'getExtensionAttributes'
173197
]
174198
)->disableOriginalConstructor()
175199
->getMock();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
namespace Magento\Braintree\Model\Paypal\Helper;
9+
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\Data\CartInterface;
14+
use Magento\Quote\Model\Quote;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Test shipping method updater
20+
*/
21+
class ShippingMethodUpdaterTest extends TestCase
22+
{
23+
/**
24+
* @var ShippingMethodUpdater
25+
*/
26+
private $shippingMethodUpdater;
27+
28+
/**
29+
* @var ObjectManagerInterface
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$this->objectManager = Bootstrap::getObjectManager();
39+
$this->shippingMethodUpdater = $this->objectManager->get(ShippingMethodUpdater::class);
40+
}
41+
42+
/**
43+
* Tests that shipping method is actually updated in quote.
44+
*
45+
* @return void
46+
* @magentoAppArea frontend
47+
* @magentoConfigFixture default_store carriers/flatrate/active 1
48+
* @magentoConfigFixture default_store carriers/freeshipping/active 1
49+
* @magentoDataFixture Magento/Braintree/Fixtures/paypal_quote.php
50+
*/
51+
public function testExecute(): void
52+
{
53+
$reservedOrderId = 'test01';
54+
/** @var Quote $quote */
55+
$quote = $this->getQuote($reservedOrderId);
56+
57+
$this->assertEquals(
58+
'flatrate_flatrate',
59+
$quote->getShippingAddress()->getShippingMethod()
60+
);
61+
62+
$this->shippingMethodUpdater->execute('freeshipping_freeshipping', $quote);
63+
64+
$this->assertEquals(
65+
'freeshipping_freeshipping',
66+
$quote->getShippingAddress()->getShippingMethod()
67+
);
68+
}
69+
70+
/**
71+
* Gets quote by reserved order ID.
72+
*
73+
* @param string $reservedOrderId
74+
* @return CartInterface
75+
*/
76+
private function getQuote(string $reservedOrderId): CartInterface
77+
{
78+
$searchCriteria = $this->objectManager->get(SearchCriteriaBuilder::class)
79+
->addFilter('reserved_order_id', $reservedOrderId)
80+
->create();
81+
82+
/** @var CartRepositoryInterface $quoteRepository */
83+
$quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
84+
$items = $quoteRepository->getList($searchCriteria)
85+
->getItems();
86+
87+
return array_pop($items);
88+
}
89+
}

0 commit comments

Comments
 (0)