Skip to content

Commit b012863

Browse files
committed
Merge remote-tracking branch 'main-ce/develop' into MAGETWO-55692-2
2 parents 9530036 + feeadbe commit b012863

File tree

25 files changed

+1181
-204
lines changed

25 files changed

+1181
-204
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Quantity.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Import\Product\Validator;
77

8-
use Magento\CatalogImportExport\Model\Import\Product\Validator\AbstractImportValidator;
98
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
109

10+
/**
11+
* Class Quantity
12+
*/
1113
class Quantity extends AbstractImportValidator implements RowValidatorInterface
1214
{
1315
/**
@@ -24,7 +26,7 @@ public function init($context)
2426
public function isValid($value)
2527
{
2628
$this->_clearMessages();
27-
if (!empty($value['qty']) && (!is_numeric($value['qty']) || $value['qty'] < 0)) {
29+
if (!empty($value['qty']) && !is_numeric($value['qty'])) {
2830
$this->_addMessages(
2931
[
3032
sprintf(
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Validator;
7+
8+
use Magento\CatalogImportExport\Model\Import\Product;
9+
use Magento\CatalogImportExport\Model\Import\Product\Validator\Quantity;
10+
11+
/**
12+
* Class QuantityTest
13+
*/
14+
class QuantityTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var Quantity
18+
*/
19+
private $quantity;
20+
21+
protected function setUp()
22+
{
23+
$this->quantity = new Quantity();
24+
25+
$contextStub = $this->getMockBuilder(Product::class)
26+
->disableOriginalConstructor()
27+
->getMock();
28+
$contextStub->method('retrieveMessageTemplate')->willReturn(null);
29+
$this->quantity->init($contextStub);
30+
}
31+
32+
/**
33+
* @param bool $expectedResult
34+
* @param array $value
35+
* @dataProvider isValidDataProvider
36+
*/
37+
public function testIsValid($expectedResult, $value)
38+
{
39+
$result = $this->quantity->isValid($value);
40+
$this->assertEquals($expectedResult, $result);
41+
}
42+
43+
/**
44+
* @return array
45+
*/
46+
public function isValidDataProvider()
47+
{
48+
return [
49+
[true, ['qty' => 0]],
50+
[true, ['qty' => 1]],
51+
[true, ['qty' => 5]],
52+
[true, ['qty' => -1]],
53+
[true, ['qty' => -10]],
54+
[true, ['qty' => '']],
55+
[false, ['qty' => 'abc']],
56+
[false, ['qty' => true]],
57+
];
58+
}
59+
}

app/code/Magento/Quote/Model/ShippingMethodManagement.php

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\Quote\Api\Data\EstimateAddressInterface;
1414
use Magento\Quote\Api\ShipmentEstimationInterface;
1515
use Magento\Quote\Model\Quote;
16+
use Magento\Framework\Reflection\DataObjectProcessor;
17+
use Magento\Framework\App\ObjectManager;
18+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1619

1720
/**
1821
* Shipping method read service.
@@ -49,6 +52,16 @@ class ShippingMethodManagement implements
4952
*/
5053
protected $totalsCollector;
5154

55+
/**
56+
* @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
57+
*/
58+
private $dataProcessor;
59+
60+
/**
61+
* @var AddressInterfaceFactory $addressFactory
62+
*/
63+
private $addressFactory;
64+
5265
/**
5366
* Constructs a shipping method read service object.
5467
*
@@ -189,13 +202,7 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
189202
return [];
190203
}
191204

192-
return $this->getEstimatedRates(
193-
$quote,
194-
$address->getCountryId(),
195-
$address->getPostcode(),
196-
$address->getRegionId(),
197-
$address->getRegion()
198-
);
205+
return $this->getShippingMethods($quote, $address);
199206
}
200207

201208
/**
@@ -210,7 +217,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
210217
if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
211218
return [];
212219
}
213-
return $this->getShippingMethods($quote, $address->getData());
220+
return $this->getShippingMethods($quote, $address);
214221
}
215222

216223
/**
@@ -227,13 +234,7 @@ public function estimateByAddressId($cartId, $addressId)
227234
}
228235
$address = $this->addressRepository->getById($addressId);
229236

230-
return $this->getEstimatedRates(
231-
$quote,
232-
$address->getCountryId(),
233-
$address->getPostcode(),
234-
$address->getRegionId(),
235-
$address->getRegion()
236-
);
237+
return $this->getShippingMethods($quote, $address);
237238
}
238239

239240
/**
@@ -244,30 +245,39 @@ public function estimateByAddressId($cartId, $addressId)
244245
* @param string $postcode
245246
* @param int $regionId
246247
* @param string $region
248+
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
247249
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
250+
* @deprecated
248251
*/
249-
protected function getEstimatedRates(\Magento\Quote\Model\Quote $quote, $country, $postcode, $regionId, $region)
250-
{
251-
$data = [
252-
EstimateAddressInterface::KEY_COUNTRY_ID => $country,
253-
EstimateAddressInterface::KEY_POSTCODE => $postcode,
254-
EstimateAddressInterface::KEY_REGION_ID => $regionId,
255-
EstimateAddressInterface::KEY_REGION => $region
256-
];
257-
return $this->getShippingMethods($quote, $data);
252+
protected function getEstimatedRates(
253+
\Magento\Quote\Model\Quote $quote,
254+
$country,
255+
$postcode,
256+
$regionId,
257+
$region,
258+
$address = null
259+
) {
260+
if (!$address) {
261+
$address = $this->getAddressFactory()->create()
262+
->setCountryId($country)
263+
->setPostcode($postcode)
264+
->setRegionId($regionId)
265+
->setRegion($region);
266+
}
267+
return $this->getShippingMethods($quote, $address);
258268
}
259269

260270
/**
261271
* Get list of available shipping methods
262272
* @param \Magento\Quote\Model\Quote $quote
263-
* @param array $addressData
273+
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
264274
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
265275
*/
266-
private function getShippingMethods(Quote $quote, array $addressData)
276+
private function getShippingMethods(Quote $quote, $address)
267277
{
268278
$output = [];
269279
$shippingAddress = $quote->getShippingAddress();
270-
$shippingAddress->addData($addressData);
280+
$shippingAddress->addData($this->extractAddressData($address));
271281
$shippingAddress->setCollectShippingRates(true);
272282

273283
$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
@@ -279,4 +289,51 @@ private function getShippingMethods(Quote $quote, array $addressData)
279289
}
280290
return $output;
281291
}
292+
293+
/**
294+
* Get transform address interface into Array
295+
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
296+
* @return array
297+
*/
298+
private function extractAddressData($address)
299+
{
300+
$className = \Magento\Customer\Api\Data\AddressInterface::class;
301+
if ($address instanceof \Magento\Quote\Api\Data\AddressInterface) {
302+
$className = \Magento\Quote\Api\Data\AddressInterface::class;
303+
} elseif ($address instanceof EstimateAddressInterface) {
304+
$className = EstimateAddressInterface::class;
305+
}
306+
return $this->getDataObjectProcessor()->buildOutputDataArray(
307+
$address,
308+
$className
309+
);
310+
}
311+
312+
/**
313+
* Gets the data object processor
314+
* @return \Magento\Framework\Reflection\DataObjectProcessor
315+
* @deprecated
316+
*/
317+
private function getDataObjectProcessor()
318+
{
319+
if ($this->dataProcessor === null) {
320+
$this->dataProcessor = ObjectManager::getInstance()
321+
->get(DataObjectProcessor::class);
322+
}
323+
return $this->dataProcessor;
324+
}
325+
326+
/**
327+
* Gets the address factory
328+
* @return AddressInterfaceFactory
329+
* @deprecated
330+
*/
331+
private function getAddressFactory()
332+
{
333+
if ($this->addressFactory === null) {
334+
$this->addressFactory = ObjectManager::getInstance()
335+
->get(AddressInterfaceFactory::class);
336+
}
337+
return $this->addressFactory;
338+
}
282339
}

0 commit comments

Comments
 (0)