Skip to content

Commit df70c99

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MC-13810' into 2.3-qwerty-pr
2 parents 1549426 + 1eb4619 commit df70c99

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
5353

5454
/**
5555
* @var QuoteAddressValidator
56-
* @deprecated 100.2.0
5756
*/
5857
protected $addressValidator;
5958

@@ -152,35 +151,36 @@ public function saveAddressInformation(
152151
$cartId,
153152
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
154153
) {
155-
$address = $addressInformation->getShippingAddress();
156-
$billingAddress = $addressInformation->getBillingAddress();
157-
$carrierCode = $addressInformation->getShippingCarrierCode();
158-
$methodCode = $addressInformation->getShippingMethodCode();
154+
/** @var \Magento\Quote\Model\Quote $quote */
155+
$quote = $this->quoteRepository->getActive($cartId);
156+
$this->validateQuote($quote);
159157

158+
$address = $addressInformation->getShippingAddress();
159+
if (!$address || !$address->getCountryId()) {
160+
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
161+
}
160162
if (!$address->getCustomerAddressId()) {
161163
$address->setCustomerAddressId(null);
162164
}
163165

164-
if ($billingAddress && !$billingAddress->getCustomerAddressId()) {
165-
$billingAddress->setCustomerAddressId(null);
166-
}
167-
168-
if (!$address->getCountryId()) {
169-
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
170-
}
166+
try {
167+
$billingAddress = $addressInformation->getBillingAddress();
168+
if ($billingAddress) {
169+
if (!$billingAddress->getCustomerAddressId()) {
170+
$billingAddress->setCustomerAddressId(null);
171+
}
172+
$this->addressValidator->validateForCart($quote, $billingAddress);
173+
$quote->setBillingAddress($billingAddress);
174+
}
171175

172-
/** @var \Magento\Quote\Model\Quote $quote */
173-
$quote = $this->quoteRepository->getActive($cartId);
174-
$address->setLimitCarrier($carrierCode);
175-
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
176-
$this->validateQuote($quote);
177-
$quote->setIsMultiShipping(false);
176+
$this->addressValidator->validateForCart($quote, $address);
177+
$carrierCode = $addressInformation->getShippingCarrierCode();
178+
$address->setLimitCarrier($carrierCode);
179+
$methodCode = $addressInformation->getShippingMethodCode();
180+
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
178181

179-
if ($billingAddress) {
180-
$quote->setBillingAddress($billingAddress);
181-
}
182+
$quote->setIsMultiShipping(false);
182183

183-
try {
184184
$this->quoteRepository->save($quote);
185185
} catch (\Exception $e) {
186186
$this->logger->critical($e);

app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
8282
*/
8383
private $shippingMock;
8484

85+
/**
86+
* @var \PHPUnit_Framework_MockObject_MockObject
87+
*/
88+
private $addressValidatorMock;
89+
8590
protected function setUp()
8691
{
8792
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -141,6 +146,9 @@ protected function setUp()
141146
$this->createPartialMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create']);
142147
$this->shippingFactoryMock =
143148
$this->createPartialMock(\Magento\Quote\Model\ShippingFactory::class, ['create']);
149+
$this->addressValidatorMock = $this->createMock(
150+
\Magento\Quote\Model\QuoteAddressValidator::class
151+
);
144152

145153
$this->model = $this->objectManager->getObject(
146154
\Magento\Checkout\Model\ShippingInformationManagement::class,
@@ -151,7 +159,8 @@ protected function setUp()
151159
'quoteRepository' => $this->quoteRepositoryMock,
152160
'shippingAssignmentFactory' => $this->shippingAssignmentFactoryMock,
153161
'cartExtensionFactory' => $this->cartExtensionFactoryMock,
154-
'shippingFactory' => $this->shippingFactoryMock
162+
'shippingFactory' => $this->shippingFactoryMock,
163+
'addressValidator' => $this->addressValidatorMock,
155164
]
156165
);
157166
}
@@ -163,22 +172,8 @@ protected function setUp()
163172
public function testSaveAddressInformationIfCartIsEmpty()
164173
{
165174
$cartId = 100;
166-
$carrierCode = 'carrier_code';
167-
$shippingMethod = 'shipping_method';
168175
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
169176

170-
$billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
171-
$addressInformationMock->expects($this->once())
172-
->method('getShippingAddress')
173-
->willReturn($this->shippingAddressMock);
174-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
175-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
176-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
177-
178-
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
179-
180-
$this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
181-
182177
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
183178
$this->quoteRepositoryMock->expects($this->once())
184179
->method('getActive')
@@ -244,21 +239,19 @@ private function setShippingAssignmentsMocks($shippingMethod)
244239
public function testSaveAddressInformationIfShippingAddressNotSet()
245240
{
246241
$cartId = 100;
247-
$carrierCode = 'carrier_code';
248-
$shippingMethod = 'shipping_method';
249242
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
250-
251243
$addressInformationMock->expects($this->once())
252244
->method('getShippingAddress')
253245
->willReturn($this->shippingAddressMock);
254-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
255-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
256-
257-
$billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
258-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
259246

260247
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
261248

249+
$this->quoteRepositoryMock->expects($this->once())
250+
->method('getActive')
251+
->with($cartId)
252+
->willReturn($this->quoteMock);
253+
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
254+
262255
$this->model->saveAddressInformation($cartId, $addressInformationMock);
263256
}
264257

@@ -273,6 +266,9 @@ public function testSaveAddressInformationIfCanNotSaveQuote()
273266
$shippingMethod = 'shipping_method';
274267
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
275268

269+
$this->addressValidatorMock->expects($this->exactly(2))
270+
->method('validateForCart');
271+
276272
$this->quoteRepositoryMock->expects($this->once())
277273
->method('getActive')
278274
->with($cartId)
@@ -314,6 +310,9 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid()
314310
$shippingMethod = 'shipping_method';
315311
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
316312

313+
$this->addressValidatorMock->expects($this->exactly(2))
314+
->method('validateForCart');
315+
317316
$this->quoteRepositoryMock->expects($this->once())
318317
->method('getActive')
319318
->with($cartId)
@@ -355,6 +354,9 @@ public function testSaveAddressInformation()
355354
$shippingMethod = 'shipping_method';
356355
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
357356

357+
$this->addressValidatorMock->expects($this->exactly(2))
358+
->method('validateForCart');
359+
358360
$this->quoteRepositoryMock->expects($this->once())
359361
->method('getActive')
360362
->with($cartId)

0 commit comments

Comments
 (0)