Skip to content

Commit 2aeca2a

Browse files
committed
Merge branch 'ACP2E-174' of https://github.com/magento-l3/magento2ce into PR-2021-20-08
2 parents 2130851 + 582a11e commit 2aeca2a

File tree

2 files changed

+227
-58
lines changed

2 files changed

+227
-58
lines changed

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

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -376,27 +376,6 @@ public function setRequest(RateRequest $request)
376376
$destCountry = self::USA_COUNTRY_ID;
377377
}
378378

379-
//for UPS, puerto rico state for US will assume as puerto rico country
380-
if ($destCountry == self::USA_COUNTRY_ID
381-
&& ($request->getDestPostcode() == '00912'
382-
|| $request->getDestRegionCode() == self::PUERTORICO_COUNTRY_ID)
383-
) {
384-
$destCountry = self::PUERTORICO_COUNTRY_ID;
385-
}
386-
387-
// For UPS, Guam state of the USA will be represented by Guam country
388-
if ($destCountry == self::USA_COUNTRY_ID && $request->getDestRegionCode() == self::GUAM_REGION_CODE) {
389-
$destCountry = self::GUAM_COUNTRY_ID;
390-
}
391-
392-
// For UPS, Las Palmas and Santa Cruz de Tenerife will be represented by Canary Islands country
393-
if ($destCountry === 'ES' &&
394-
($request->getDestRegionCode() === 'Las Palmas'
395-
|| $request->getDestRegionCode() === 'Santa Cruz de Tenerife')
396-
) {
397-
$destCountry = 'IC';
398-
}
399-
400379
$country = $this->_countryFactory->create()->load($destCountry);
401380
$rowRequest->setDestCountry($country->getData('iso2_code') ?: $destCountry);
402381

@@ -406,6 +385,22 @@ public function setRequest(RateRequest $request)
406385
$rowRequest->setDestPostal($request->getDestPostcode());
407386
}
408387

388+
$rowRequest->setOrigCountry(
389+
$this->getNormalizedCountryCode(
390+
$rowRequest->getOrigCountry(),
391+
$rowRequest->getOrigRegionCode(),
392+
$rowRequest->getOrigPostal()
393+
)
394+
);
395+
396+
$rowRequest->setDestCountry(
397+
$this->getNormalizedCountryCode(
398+
$rowRequest->getDestCountry(),
399+
$rowRequest->getDestRegionCode(),
400+
$rowRequest->getDestPostal()
401+
)
402+
);
403+
409404
$weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
410405

411406
$weight = $this->_getCorrectWeight($weight);
@@ -432,6 +427,40 @@ public function setRequest(RateRequest $request)
432427
return $this;
433428
}
434429

430+
/**
431+
* Return country code according to UPS
432+
*
433+
* @param string $countryCode
434+
* @param string $regionCode
435+
* @param string $postCode
436+
* @return string
437+
*/
438+
private function getNormalizedCountryCode($countryCode, $regionCode, $postCode)
439+
{
440+
//for UPS, puerto rico state for US will assume as puerto rico country
441+
if ($countryCode == self::USA_COUNTRY_ID
442+
&& ($postCode == '00912'
443+
|| $regionCode == self::PUERTORICO_COUNTRY_ID)
444+
) {
445+
$countryCode = self::PUERTORICO_COUNTRY_ID;
446+
}
447+
448+
// For UPS, Guam state of the USA will be represented by Guam country
449+
if ($countryCode == self::USA_COUNTRY_ID && $regionCode == self::GUAM_REGION_CODE) {
450+
$countryCode = self::GUAM_COUNTRY_ID;
451+
}
452+
453+
// For UPS, Las Palmas and Santa Cruz de Tenerife will be represented by Canary Islands country
454+
if ($countryCode === 'ES' &&
455+
($regionCode === 'Las Palmas'
456+
|| $regionCode === 'Santa Cruz de Tenerife')
457+
) {
458+
$countryCode = 'IC';
459+
}
460+
461+
return $countryCode;
462+
}
463+
435464
/**
436465
* Get correct weight
437466
*
@@ -1679,6 +1708,22 @@ public function getShipAcceptUrl()
16791708
*/
16801709
private function requestQuotes(DataObject $request): array
16811710
{
1711+
$request->setShipperAddressCountryCode(
1712+
$this->getNormalizedCountryCode(
1713+
$request->getShipperAddressCountryCode(),
1714+
$request->getShipperAddressStateOrProvinceCode(),
1715+
$request->getShipperAddressPostalCode(),
1716+
)
1717+
);
1718+
1719+
$request->setRecipientAddressCountryCode(
1720+
$this->getNormalizedCountryCode(
1721+
$request->getRecipientAddressCountryCode(),
1722+
$request->getRecipientAddressStateOrProvinceCode(),
1723+
$request->getRecipientAddressPostalCode(),
1724+
)
1725+
);
1726+
16821727
/** @var HttpResponseDeferredInterface[] $quotesRequests */
16831728
//Getting quotes
16841729
$this->_prepareShipmentRequest($request);

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

Lines changed: 161 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\App\Config\ScopeConfigInterface;
1313
use Magento\Framework\HTTP\ClientFactory;
1414
use Magento\Framework\HTTP\ClientInterface;
15-
use Magento\Framework\Model\AbstractModel;
1615
use Magento\Framework\Phrase;
1716
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1817
use Magento\Quote\Model\Quote\Address\RateRequest;
@@ -33,6 +32,7 @@
3332
* Unit tests for \Magento\Ups\Model\Carrier class.
3433
*
3534
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
3636
*/
3737
class CarrierTest extends TestCase
3838
{
@@ -79,11 +79,6 @@ class CarrierTest extends TestCase
7979
*/
8080
private $country;
8181

82-
/**
83-
* @var AbstractModel
84-
*/
85-
private $abstractModel;
86-
8782
/**
8883
* @var Result
8984
*/
@@ -135,13 +130,8 @@ protected function setUp(): void
135130
->onlyMethods(['load', 'getData'])
136131
->getMock();
137132

138-
$this->abstractModel = $this->getMockBuilder(AbstractModel::class)
139-
->disableOriginalConstructor()
140-
->onlyMethods(['getData'])
141-
->getMock();
142-
143133
$this->country->method('load')
144-
->willReturn($this->abstractModel);
134+
->willReturnCallback([$this, 'getCountryById']);
145135

146136
$this->countryFactory = $this->getMockBuilder(CountryFactory::class)
147137
->disableOriginalConstructor()
@@ -363,51 +353,185 @@ public function logDataProvider(): array
363353
}
364354

365355
/**
366-
* @param string $countryCode
367-
* @param string $foundCountryCode
356+
* @param array $requestData
357+
* @param array $rawRequestData
368358
*
369359
* @return void
370360
* @dataProvider countryDataProvider
371361
*/
372-
public function testSetRequest($countryCode, $foundCountryCode): void
362+
public function testSetRequest(array $requestData, array $rawRequestData): void
373363
{
374364
/** @var RateRequest $request */
375365
$request = $this->helper->getObject(RateRequest::class);
376-
$request->setData(
377-
[
378-
'orig_country' => 'USA',
379-
'orig_region_code' => 'CA',
380-
'orig_post_code' => 90230,
381-
'orig_city' => 'Culver City',
382-
'dest_country_id' => $countryCode
383-
]
384-
);
385-
386-
$this->country
387-
->method('load')
388-
->withConsecutive([], [$countryCode])
389-
->willReturnSelf();
390-
391-
$this->country->method('getData')
392-
->with('iso2_code')
393-
->willReturn($foundCountryCode);
394-
366+
$request->setData($requestData);
395367
$this->model->setRequest($request);
368+
$property = new \ReflectionProperty($this->model, '_rawRequest');
369+
$property->setAccessible(true);
370+
$rawRequest = $property->getValue($this->model);
371+
$this->assertEquals($rawRequestData, array_intersect_key($rawRequest->getData(), $rawRequestData));
396372
}
397373

398374
/**
399-
* Get list of country variations.
375+
* Get list of request variations for setRequest.
400376
*
401377
* @return array
402378
*/
403379
public function countryDataProvider(): array
404380
{
405381
return [
406-
['countryCode' => 'PR', 'foundCountryCode' => null],
407-
['countryCode' => 'US', 'foundCountryCode' => 'US']
382+
[
383+
[
384+
'orig_region_code' => 'CA',
385+
'orig_postcode' => '90230',
386+
'orig_country' => 'US',
387+
'dest_region_code' => 'NY',
388+
'dest_postcode' => '11236',
389+
'dest_country_id' => 'US',
390+
],
391+
[
392+
'orig_region_code' => 'CA',
393+
'orig_postal' => '90230',
394+
'orig_country' => 'US',
395+
'dest_region_code' => 'NY',
396+
'dest_postal' => '11236',
397+
'dest_country' => 'US',
398+
]
399+
],
400+
[
401+
[
402+
'orig_region_code' => 'CA',
403+
'orig_postcode' => '90230',
404+
'orig_country' => 'US',
405+
'dest_region_code' => 'PR',
406+
'dest_postcode' => '00968',
407+
'dest_country_id' => 'US',
408+
],
409+
[
410+
'orig_region_code' => 'CA',
411+
'orig_postal' => '90230',
412+
'orig_country' => 'US',
413+
'dest_region_code' => 'PR',
414+
'dest_postal' => '00968',
415+
'dest_country' => 'PR',
416+
]
417+
],
418+
[
419+
[
420+
'orig_region_code' => 'PR',
421+
'orig_postcode' => '00968',
422+
'orig_country' => 'US',
423+
'dest_region_code' => 'CA',
424+
'dest_postcode' => '90230',
425+
'dest_country_id' => 'US',
426+
],
427+
[
428+
'orig_region_code' => 'PR',
429+
'orig_postal' => '00968',
430+
'orig_country' => 'PR',
431+
'dest_region_code' => 'CA',
432+
'dest_postal' => '90230',
433+
'dest_country' => 'US',
434+
]
435+
],
408436
];
409437
}
410438

439+
/**
440+
* @param array $requestData
441+
* @param array $expectedRequestData
442+
* @dataProvider requestToShipmentDataProvider
443+
*/
444+
public function testRequestToShipment(array $requestData, array $expectedRequestData): void
445+
{
446+
/** @var \Magento\Shipping\Model\Shipment\Request $request */
447+
$request = $this->helper->getObject(\Magento\Shipping\Model\Shipment\Request::class);
448+
$request->setData($requestData);
449+
$request->setPackages([['items' => []]]);
450+
$this->model->requestToShipment($request);
451+
$this->assertEquals($expectedRequestData, array_intersect_key($request->getData(), $expectedRequestData));
452+
}
453+
454+
/**
455+
* Get list of request variations for requestToShipment.
456+
*
457+
* @return array
458+
*/
459+
public function requestToShipmentDataProvider(): array
460+
{
461+
return [
462+
[
463+
[
464+
'recipient_address_state_or_province_code' => 'CA',
465+
'recipient_address_postal_code' => '90230',
466+
'recipient_address_country_code' => 'US',
467+
'shipper_address_state_or_province_code' => 'NY',
468+
'shipper_address_postal_code' => '11236',
469+
'shipper_address_country_code' => 'US',
470+
],
471+
[
472+
'recipient_address_state_or_province_code' => 'CA',
473+
'recipient_address_postal_code' => '90230',
474+
'recipient_address_country_code' => 'US',
475+
'shipper_address_state_or_province_code' => 'NY',
476+
'shipper_address_postal_code' => '11236',
477+
'shipper_address_country_code' => 'US',
478+
]
479+
],
480+
[
481+
[
482+
'recipient_address_state_or_province_code' => 'CA',
483+
'recipient_address_postal_code' => '90230',
484+
'recipient_address_country_code' => 'US',
485+
'shipper_address_state_or_province_code' => 'PR',
486+
'shipper_address_postal_code' => '00968',
487+
'shipper_address_country_code' => 'US',
488+
],
489+
[
490+
'recipient_address_state_or_province_code' => 'CA',
491+
'recipient_address_postal_code' => '90230',
492+
'recipient_address_country_code' => 'US',
493+
'shipper_address_state_or_province_code' => 'PR',
494+
'shipper_address_postal_code' => '00968',
495+
'shipper_address_country_code' => 'PR',
496+
]
497+
],
498+
[
499+
[
500+
'recipient_address_state_or_province_code' => 'PR',
501+
'recipient_address_postal_code' => '00968',
502+
'recipient_address_country_code' => 'US',
503+
'shipper_address_state_or_province_code' => 'CA',
504+
'shipper_address_postal_code' => '90230',
505+
'shipper_address_country_code' => 'US',
506+
],
507+
[
508+
'recipient_address_state_or_province_code' => 'PR',
509+
'recipient_address_postal_code' => '00968',
510+
'recipient_address_country_code' => 'PR',
511+
'shipper_address_state_or_province_code' => 'CA',
512+
'shipper_address_postal_code' => '90230',
513+
'shipper_address_country_code' => 'US',
514+
]
515+
],
516+
];
517+
}
518+
519+
/**
520+
* @param string|null $id
521+
* @return Country
522+
*/
523+
public function getCountryById(?string $id): Country
524+
{
525+
$countries = [
526+
'US' => 'US'
527+
];
528+
$countryMock = $this->getMockBuilder(Country::class)
529+
->disableOriginalConstructor()
530+
->getMockForAbstractClass();
531+
$countryMock->setData('iso2_code', $countries[$id] ?? null);
532+
return $countryMock;
533+
}
534+
411535
/**
412536
* @param string $carrierType
413537
* @param string $methodType

0 commit comments

Comments
 (0)