Skip to content

Commit 355b916

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MAGETWO-98068' into 2.1.18-develop-pr
2 parents 5626885 + 39bcac5 commit 355b916

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Framework\App\ObjectManager;
1919

2020
/**
21+
* Class ShippingInformationManagement
22+
*
2123
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2224
*/
2325
class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInformationManagementInterface
@@ -149,31 +151,32 @@ public function saveAddressInformation(
149151
$cartId,
150152
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
151153
) {
152-
$address = $addressInformation->getShippingAddress();
153-
$billingAddress = $addressInformation->getBillingAddress();
154-
$carrierCode = $addressInformation->getShippingCarrierCode();
155-
$methodCode = $addressInformation->getShippingMethodCode();
156-
157-
if (!$address->getCustomerAddressId()) {
158-
$address->setCustomerAddressId(null);
159-
}
160-
161-
if (!$address->getCountryId()) {
162-
throw new StateException(__('Shipping address is not set'));
163-
}
164-
165154
/** @var \Magento\Quote\Model\Quote $quote */
166155
$quote = $this->quoteRepository->getActive($cartId);
167-
$address->setLimitCarrier($carrierCode);
168-
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
169156
$this->validateQuote($quote);
170-
$quote->setIsMultiShipping(false);
171157

172-
if ($billingAddress) {
173-
$quote->setBillingAddress($billingAddress);
158+
$address = $addressInformation->getShippingAddress();
159+
if (!$address || !$address->getCountryId()) {
160+
throw new StateException(__('Shipping address is not set'));
161+
}
162+
if (!$address->getCustomerAddressId()) {
163+
$address->setCustomerAddressId(null);
174164
}
175165

176166
try {
167+
$billingAddress = $addressInformation->getBillingAddress();
168+
if ($billingAddress) {
169+
$this->addressValidator->validateForCart($quote, $billingAddress);
170+
$quote->setBillingAddress($billingAddress);
171+
}
172+
173+
$this->addressValidator->validateForCart($quote, $address);
174+
$carrierCode = $addressInformation->getShippingCarrierCode();
175+
$address->setLimitCarrier($carrierCode);
176+
$methodCode = $addressInformation->getShippingMethodCode();
177+
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
178+
$quote->setIsMultiShipping(false);
179+
177180
$this->quoteRepository->save($quote);
178181
} catch (\Exception $e) {
179182
$this->logger->critical($e);
@@ -211,6 +214,8 @@ protected function validateQuote(\Magento\Quote\Model\Quote $quote)
211214
}
212215

213216
/**
217+
* Prepare shipping assignment.
218+
*
214219
* @param CartInterface $quote
215220
* @param AddressInterface $address
216221
* @param string $method

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

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Checkout\Test\Unit\Model;
77

8+
use Magento\Quote\Model\QuoteAddressValidator;
9+
810
/**
911
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1012
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -149,6 +151,7 @@ protected function setUp()
149151
$this->getMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create'], [], '', false);
150152
$this->shippingFactoryMock =
151153
$this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false);
154+
$this->addressValidatorMock = $this->getMock(QuoteAddressValidator::class, [], [], '', false);
152155

153156
$this->model = $this->objectManager->getObject(
154157
\Magento\Checkout\Model\ShippingInformationManagement::class,
@@ -157,6 +160,7 @@ protected function setUp()
157160
'paymentDetailsFactory' => $this->paymentDetailsFactoryMock,
158161
'cartTotalsRepository' => $this->cartTotalsRepositoryMock,
159162
'quoteRepository' => $this->quoteRepositoryMock,
163+
'addressValidator' => $this->addressValidatorMock,
160164
]
161165
);
162166
$this->objectManager->setBackwardCompatibleProperty(
@@ -183,22 +187,8 @@ protected function setUp()
183187
public function testSaveAddressInformationIfCartIsEmpty()
184188
{
185189
$cartId = 100;
186-
$carrierCode = 'carrier_code';
187-
$shippingMethod = 'shipping_method';
188190
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
189191

190-
$billingAddress = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
191-
$addressInformationMock->expects($this->once())
192-
->method('getShippingAddress')
193-
->willReturn($this->shippingAddressMock);
194-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
195-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
196-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
197-
198-
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
199-
200-
$this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
201-
202192
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
203193
$this->quoteRepositoryMock->expects($this->once())
204194
->method('getActive')
@@ -271,21 +261,19 @@ private function setShippingAssignmentsMocks($shippingMethod)
271261
public function testSaveAddressInformationIfShippingAddressNotSet()
272262
{
273263
$cartId = 100;
274-
$carrierCode = 'carrier_code';
275-
$shippingMethod = 'shipping_method';
276264
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
277-
278265
$addressInformationMock->expects($this->once())
279266
->method('getShippingAddress')
280267
->willReturn($this->shippingAddressMock);
281-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
282-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
283-
284-
$billingAddress = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
285-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
286268

287269
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
288270

271+
$this->quoteRepositoryMock->expects($this->once())
272+
->method('getActive')
273+
->with($cartId)
274+
->willReturn($this->quoteMock);
275+
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
276+
289277
$this->model->saveAddressInformation($cartId, $addressInformationMock);
290278
}
291279

@@ -300,6 +288,9 @@ public function testSaveAddressInformationIfCanNotSaveQuote()
300288
$shippingMethod = 'shipping_method';
301289
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
302290

291+
$this->addressValidatorMock->expects($this->exactly(2))
292+
->method('validateForCart');
293+
303294
$this->quoteRepositoryMock->expects($this->once())
304295
->method('getActive')
305296
->with($cartId)
@@ -341,6 +332,9 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid()
341332
$shippingMethod = 'shipping_method';
342333
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
343334

335+
$this->addressValidatorMock->expects($this->exactly(2))
336+
->method('validateForCart');
337+
344338
$this->quoteRepositoryMock->expects($this->once())
345339
->method('getActive')
346340
->with($cartId)
@@ -382,6 +376,9 @@ public function testSaveAddressInformation()
382376
$shippingMethod = 'shipping_method';
383377
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
384378

379+
$this->addressValidatorMock->expects($this->exactly(2))
380+
->method('validateForCart');
381+
385382
$this->quoteRepositoryMock->expects($this->once())
386383
->method('getActive')
387384
->with($cartId)

0 commit comments

Comments
 (0)