Skip to content

Commit 6213ff9

Browse files
committed
Merge remote-tracking branch 'origin/MC-29444' into 2.4-develop-pr1
2 parents 2a36016 + e401124 commit 6213ff9

File tree

3 files changed

+151
-35
lines changed

3 files changed

+151
-35
lines changed

app/code/Magento/Ups/Model/Carrier.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
use Zend_Http_Client;
4848

4949
/**
50-
* UPS shipping implementation
50+
* UPS shipping implementation.
51+
*
5152
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
5253
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
5354
*/
@@ -155,7 +156,9 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface
155156
* @inheritdoc
156157
*/
157158
protected $_debugReplacePrivateDataKeys = [
158-
'UserId', 'Password', 'AccessLicenseNumber'
159+
'UserId',
160+
'Password',
161+
'AccessLicenseNumber',
159162
];
160163

161164
/**
@@ -372,9 +375,10 @@ public function setRequest(RateRequest $request)
372375
$destCountry = self::USA_COUNTRY_ID;
373376
}
374377

375-
//for UPS, puero rico state for US will assume as puerto rico country
376-
if ($destCountry == self::USA_COUNTRY_ID && ($request->getDestPostcode() == '00912' ||
377-
$request->getDestRegionCode() == self::PUERTORICO_COUNTRY_ID)
378+
//for UPS, puerto rico state for US will assume as puerto rico country
379+
if ($destCountry == self::USA_COUNTRY_ID
380+
&& ($request->getDestPostcode() == '00912'
381+
|| $request->getDestRegionCode() == self::PUERTORICO_COUNTRY_ID)
378382
) {
379383
$destCountry = self::PUERTORICO_COUNTRY_ID;
380384
}
@@ -1322,20 +1326,28 @@ public function getResponse()
13221326
}
13231327

13241328
/**
1325-
* Get allowed shipping methods
1329+
* Get allowed shipping methods.
13261330
*
13271331
* @return array
13281332
*/
13291333
public function getAllowedMethods()
13301334
{
1331-
$allowed = explode(',', $this->getConfigData('allowed_methods'));
1332-
$arr = [];
1333-
$isByCode = $this->getConfigData('type') == 'UPS_XML';
1334-
foreach ($allowed as $code) {
1335-
$arr[$code] = $isByCode ? $this->getShipmentByCode($code) : $this->configHelper->getCode('method', $code);
1335+
$allowedMethods = explode(',', (string)$this->getConfigData('allowed_methods'));
1336+
$isUpsXml = $this->getConfigData('type') === 'UPS_XML';
1337+
$origin = $this->getConfigData('origin_shipment');
1338+
1339+
$availableByTypeMethods = $isUpsXml
1340+
? $this->configHelper->getCode('originShipment', $origin)
1341+
: $this->configHelper->getCode('method');
1342+
1343+
$methods = [];
1344+
foreach ($availableByTypeMethods as $methodCode => $methodData) {
1345+
if (in_array($methodCode, $allowedMethods)) {
1346+
$methods[$methodCode] = $methodData->getText();
1347+
}
13361348
}
13371349

1338-
return $arr;
1350+
return $methods;
13391351
}
13401352

13411353
/**
@@ -1896,20 +1908,18 @@ public function getContainerTypes(DataObject $params = null)
18961908
];
18971909
}
18981910
$containerTypes = $containerTypes + [
1899-
'03' => __('UPS Tube'),
1900-
'04' => __('PAK'),
1901-
'2a' => __('Small Express Box'),
1902-
'2b' => __('Medium Express Box'),
1903-
'2c' => __('Large Express Box'),
1904-
];
1911+
'03' => __('UPS Tube'),
1912+
'04' => __('PAK'),
1913+
'2a' => __('Small Express Box'),
1914+
'2b' => __('Medium Express Box'),
1915+
'2c' => __('Large Express Box'),
1916+
];
19051917
}
19061918

19071919
return ['00' => __('Customer Packaging')] + $containerTypes;
1908-
} elseif ($countryShipper == self::USA_COUNTRY_ID &&
1909-
$countryRecipient == self::PUERTORICO_COUNTRY_ID &&
1910-
($method == '03' ||
1911-
$method == '02' ||
1912-
$method == '01')
1920+
} elseif ($countryShipper == self::USA_COUNTRY_ID
1921+
&& $countryRecipient == self::PUERTORICO_COUNTRY_ID
1922+
&& in_array($method, ['01', '02', '03'])
19131923
) {
19141924
// Container types should be the same as for domestic
19151925
$params->setCountryRecipient(self::USA_COUNTRY_ID);

app/code/Magento/Ups/Test/Unit/Model/CarrierTest.php

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Ups\Test\Unit\Model;
78

89
use Magento\Directory\Model\Country;
910
use Magento\Directory\Model\CountryFactory;
1011
use Magento\Framework\App\Config\ScopeConfigInterface;
11-
use Magento\Framework\DataObject;
1212
use Magento\Framework\HTTP\ClientFactory;
1313
use Magento\Framework\HTTP\ClientInterface;
1414
use Magento\Framework\Model\AbstractModel;
15+
use Magento\Framework\Phrase;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1617
use Magento\Quote\Model\Quote\Address\RateRequest;
1718
use Magento\Quote\Model\Quote\Address\RateResult\Error;
@@ -20,11 +21,15 @@
2021
use Magento\Shipping\Model\Rate\ResultFactory;
2122
use Magento\Shipping\Model\Simplexml\Element;
2223
use Magento\Shipping\Model\Simplexml\ElementFactory;
24+
use Magento\Store\Model\ScopeInterface;
25+
use Magento\Ups\Helper\Config;
2326
use Magento\Ups\Model\Carrier;
2427
use PHPUnit_Framework_MockObject_MockObject as MockObject;
2528
use Psr\Log\LoggerInterface;
2629

2730
/**
31+
* Unit tests for \Magento\Ups\Model\Carrier class.
32+
*
2833
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2934
*/
3035
class CarrierTest extends \PHPUnit\Framework\TestCase
@@ -92,17 +97,23 @@ class CarrierTest extends \PHPUnit\Framework\TestCase
9297
*/
9398
private $logger;
9499

100+
/**
101+
* @var Config|MockObject
102+
*/
103+
private $configHelper;
104+
105+
/**
106+
* @inheritdoc
107+
*/
95108
protected function setUp()
96109
{
97110
$this->helper = new ObjectManager($this);
98111

99112
$this->scope = $this->getMockBuilder(ScopeConfigInterface::class)
100113
->disableOriginalConstructor()
114+
->setMethods(['getValue', 'isSetFlag'])
101115
->getMock();
102116

103-
$this->scope->method('getValue')
104-
->willReturnCallback([$this, 'scopeConfigGetValue']);
105-
106117
$this->error = $this->getMockBuilder(Error::class)
107118
->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
108119
->getMock();
@@ -143,6 +154,11 @@ protected function setUp()
143154

144155
$this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
145156

157+
$this->configHelper = $this->getMockBuilder(Config::class)
158+
->disableOriginalConstructor()
159+
->setMethods(['getCode'])
160+
->getMock();
161+
146162
$this->model = $this->helper->getObject(
147163
Carrier::class,
148164
[
@@ -153,6 +169,7 @@ protected function setUp()
153169
'xmlElFactory' => $xmlFactory,
154170
'logger' => $this->logger,
155171
'httpClientFactory' => $httpClientFactory,
172+
'configHelper' => $this->configHelper,
156173
]
157174
);
158175
}
@@ -189,14 +206,17 @@ public function scopeConfigGetValue(string $path)
189206
* @param bool $freeShippingEnabled
190207
* @param int $requestSubtotal
191208
* @param int $expectedPrice
209+
* @return void
192210
*/
193211
public function testGetMethodPrice(
194-
$cost,
195-
$shippingMethod,
196-
$freeShippingEnabled,
197-
$requestSubtotal,
198-
$expectedPrice
199-
) {
212+
int $cost,
213+
string $shippingMethod,
214+
bool $freeShippingEnabled,
215+
int $requestSubtotal,
216+
int $expectedPrice
217+
): void {
218+
$this->scope->method('getValue')
219+
->willReturnCallback([$this, 'scopeConfigGetValue']);
200220
$path = 'carriers/' . $this->model->getCarrierCode() . '/';
201221
$this->scope->method('isSetFlag')
202222
->with($path . 'free_shipping_enable')
@@ -244,8 +264,13 @@ public function getMethodPriceProvider()
244264
];
245265
}
246266

247-
public function testCollectRatesErrorMessage()
267+
/**
268+
* @return void
269+
*/
270+
public function testCollectRatesErrorMessage(): void
248271
{
272+
$this->scope->method('getValue')
273+
->willReturnCallback([$this, 'scopeConfigGetValue']);
249274
$this->scope->method('isSetFlag')
250275
->willReturn(false);
251276

@@ -373,6 +398,87 @@ public function countryDataProvider()
373398
];
374399
}
375400

401+
/**
402+
* @dataProvider allowedMethodsDataProvider
403+
* @param string $carrierType
404+
* @param string $methodType
405+
* @param string $methodCode
406+
* @param string $methodTitle
407+
* @param string $allowedMethods
408+
* @param array $expectedMethods
409+
* @return void
410+
*/
411+
public function testGetAllowedMethods(
412+
string $carrierType,
413+
string $methodType,
414+
string $methodCode,
415+
string $methodTitle,
416+
string $allowedMethods,
417+
array $expectedMethods
418+
): void {
419+
$this->scope->method('getValue')
420+
->willReturnMap(
421+
[
422+
[
423+
'carriers/ups/allowed_methods',
424+
ScopeInterface::SCOPE_STORE,
425+
null,
426+
$allowedMethods,
427+
],
428+
[
429+
'carriers/ups/type',
430+
ScopeInterface::SCOPE_STORE,
431+
null,
432+
$carrierType,
433+
],
434+
[
435+
'carriers/ups/origin_shipment',
436+
ScopeInterface::SCOPE_STORE,
437+
null,
438+
'Shipments Originating in United States',
439+
],
440+
]
441+
);
442+
$this->configHelper->method('getCode')
443+
->with($methodType)
444+
->willReturn([$methodCode => new Phrase($methodTitle)]);
445+
$actualMethods = $this->model->getAllowedMethods();
446+
$this->assertEquals($expectedMethods, $actualMethods);
447+
}
448+
449+
/**
450+
* @return array
451+
*/
452+
public function allowedMethodsDataProvider(): array
453+
{
454+
return [
455+
[
456+
'UPS',
457+
'method',
458+
'1DM',
459+
'Next Day Air Early AM',
460+
'',
461+
[],
462+
],
463+
[
464+
'UPS',
465+
'method',
466+
'1DM',
467+
'Next Day Air Early AM',
468+
'1DM,1DML,1DA',
469+
['1DM' => 'Next Day Air Early AM'],
470+
],
471+
[
472+
'UPS_XML',
473+
'originShipment',
474+
'01',
475+
'UPS Next Day Air',
476+
'01,02,03',
477+
['01' => 'UPS Next Day Air'],
478+
],
479+
];
480+
}
481+
376482
/**
377483
* Creates mock for XML factory.
378484
*

app/code/Magento/Usps/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<usps>
1212
<active>0</active>
1313
<sallowspecific>0</sallowspecific>
14-
<allowed_methods>0_FCLE,0_FCL,0_FCP,1,2,3,4,6,7,13,16,17,22,23,25,27,28,33,34,35,36,37,42,43,53,55,56,57,61,INT_1,INT_2,INT_4,INT_6,INT_7,INT_8,INT_9,INT_10,INT_11,INT_12,INT_13,INT_14,INT_15,INT_16,INT_20,INT_26</allowed_methods>
14+
<allowed_methods>0_FCLE,0_FCL,0_FCP,1,2,3,4,6,7,13,16,17,22,23,25,27,28,33,34,35,36,37,42,43,53,57,61,INT_1,INT_2,INT_4,INT_6,INT_7,INT_8,INT_9,INT_10,INT_11,INT_12,INT_13,INT_14,INT_15,INT_16,INT_20</allowed_methods>
1515
<container>VARIABLE</container>
1616
<cutoff_cost />
1717
<free_method />

0 commit comments

Comments
 (0)