Skip to content

Commit 21c9b53

Browse files
authored
ENGCOM-8154: Fix for issue 25147 - Totals information management module - only set carrier/method code if set in address #25510
2 parents b7452e9 + f4a82bf commit 21c9b53

File tree

2 files changed

+143
-5
lines changed

2 files changed

+143
-5
lines changed

app/code/Magento/Checkout/Model/TotalsInformationManagement.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Checkout\Model;
77

88
/**
9-
* Class TotalsInformationManagement
9+
* Class for management of totals information.
1010
*/
1111
class TotalsInformationManagement implements \Magento\Checkout\Api\TotalsInformationManagementInterface
1212
{
@@ -38,7 +38,7 @@ public function __construct(
3838
}
3939

4040
/**
41-
* {@inheritDoc}
41+
* @inheritDoc
4242
*/
4343
public function calculate(
4444
$cartId,
@@ -52,16 +52,20 @@ public function calculate(
5252
$quote->setBillingAddress($addressInformation->getAddress());
5353
} else {
5454
$quote->setShippingAddress($addressInformation->getAddress());
55-
$quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod(
56-
$addressInformation->getShippingCarrierCode() . '_' . $addressInformation->getShippingMethodCode()
57-
);
55+
if ($addressInformation->getShippingCarrierCode() && $addressInformation->getShippingMethodCode()) {
56+
$quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod(
57+
$addressInformation->getShippingCarrierCode().'_'.$addressInformation->getShippingMethodCode()
58+
);
59+
}
5860
}
5961
$quote->collectTotals();
6062

6163
return $this->cartTotalRepository->get($cartId);
6264
}
6365

6466
/**
67+
* Check if quote have items.
68+
*
6569
* @param \Magento\Quote\Model\Quote $quote
6670
* @throws \Magento\Framework\Exception\LocalizedException
6771
* @return void
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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\Checkout\Test\Unit\Model;
9+
10+
use Magento\Checkout\Api\Data\TotalsInformationInterface;
11+
use Magento\Checkout\Model\TotalsInformationManagement;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Api\CartTotalRepositoryInterface;
15+
use Magento\Quote\Model\Quote\Address;
16+
17+
class TotalsInformationManagementTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var ObjectManager
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @var CartRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
26+
*/
27+
private $cartRepositoryMock;
28+
29+
/**
30+
* @var CartTotalRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
31+
*/
32+
private $cartTotalRepositoryMock;
33+
34+
/**
35+
* @var TotalsInformationManagement
36+
*/
37+
private $totalsInformationManagement;
38+
39+
protected function setUp(): void
40+
{
41+
$this->objectManager = new ObjectManager($this);
42+
$this->cartRepositoryMock = $this->createMock(
43+
CartRepositoryInterface::class
44+
);
45+
$this->cartTotalRepositoryMock = $this->createMock(
46+
CartTotalRepositoryInterface::class
47+
);
48+
49+
$this->totalsInformationManagement = $this->objectManager->getObject(
50+
TotalsInformationManagement::class,
51+
[
52+
'cartRepository' => $this->cartRepositoryMock,
53+
'cartTotalRepository' => $this->cartTotalRepositoryMock,
54+
]
55+
);
56+
}
57+
58+
/**
59+
* Test for \Magento\Checkout\Model\TotalsInformationManagement::calculate.
60+
*
61+
* @param string|null $carrierCode
62+
* @param string|null $carrierMethod
63+
* @param int $methodSetCount
64+
* @dataProvider dataProviderCalculate
65+
*/
66+
public function testCalculate(?string $carrierCode, ?string $carrierMethod, int $methodSetCount)
67+
{
68+
$cartId = 1;
69+
$cartMock = $this->createMock(
70+
\Magento\Quote\Model\Quote::class
71+
);
72+
$cartMock->expects($this->once())->method('getItemsCount')->willReturn(1);
73+
$cartMock->expects($this->once())->method('getIsVirtual')->willReturn(false);
74+
$this->cartRepositoryMock->expects($this->once())->method('get')->with($cartId)->willReturn($cartMock);
75+
$this->cartTotalRepositoryMock->expects($this->once())->method('get')->with($cartId);
76+
77+
$addressInformationMock = $this->createMock(
78+
TotalsInformationInterface::class
79+
);
80+
$addressMock = $this->getMockBuilder(Address::class)
81+
->addMethods(
82+
[
83+
'setShippingMethod',
84+
'setCollectShippingRates',
85+
]
86+
)
87+
->disableOriginalConstructor()
88+
->getMock();
89+
90+
$addressInformationMock->expects($this->once())->method('getAddress')->willReturn($addressMock);
91+
$addressInformationMock->expects($this->any())->method('getShippingCarrierCode')->willReturn($carrierCode);
92+
$addressInformationMock->expects($this->any())->method('getShippingMethodCode')->willReturn($carrierMethod);
93+
$cartMock->expects($this->once())->method('setShippingAddress')->with($addressMock);
94+
$cartMock->expects($this->exactly($methodSetCount))->method('getShippingAddress')->willReturn($addressMock);
95+
$addressMock->expects($this->exactly($methodSetCount))
96+
->method('setCollectShippingRates')->with(true)->willReturn($addressMock);
97+
$addressMock->expects($this->exactly($methodSetCount))
98+
->method('setShippingMethod')->with($carrierCode . '_' . $carrierMethod);
99+
$cartMock->expects($this->once())->method('collectTotals');
100+
101+
$this->totalsInformationManagement->calculate($cartId, $addressInformationMock);
102+
}
103+
104+
/**
105+
* Data provider for testCalculate.
106+
*
107+
* @return array
108+
*/
109+
public function dataProviderCalculate(): array
110+
{
111+
return [
112+
[
113+
null,
114+
null,
115+
0
116+
],
117+
[
118+
null,
119+
'carrier_method',
120+
0
121+
],
122+
[
123+
'carrier_code',
124+
null,
125+
0
126+
],
127+
[
128+
'carrier_code',
129+
'carrier_method',
130+
1
131+
]
132+
];
133+
}
134+
}

0 commit comments

Comments
 (0)