Skip to content

Commit 1153b09

Browse files
committed
MAGETWO-86306: Update DHL Schema
- Removed restricted fields from shipping label request for country origin from AsiaPacific region
1 parent 8848d94 commit 1153b09

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,9 @@ protected function _doRequest()
14091409
if (!$originRegion) {
14101410
$xml->addChild('RequestedPickupTime', 'N', '');
14111411
}
1412-
$xml->addChild('NewShipper', 'N', '');
1412+
if ($originRegion !== 'AP') {
1413+
$xml->addChild('NewShipper', 'N', '');
1414+
}
14131415
$xml->addChild('LanguageCode', 'EN', '');
14141416
$xml->addChild('PiecesEnabled', 'Y', '');
14151417

@@ -1451,7 +1453,9 @@ protected function _doRequest()
14511453
}
14521454

14531455
$nodeConsignee->addChild('City', $rawRequest->getRecipientAddressCity());
1454-
$nodeConsignee->addChild('Division', $rawRequest->getRecipientAddressStateOrProvinceCode());
1456+
if ($originRegion !== 'AP') {
1457+
$nodeConsignee->addChild('Division', $rawRequest->getRecipientAddressStateOrProvinceCode());
1458+
}
14551459
$nodeConsignee->addChild('PostalCode', $rawRequest->getRecipientAddressPostalCode());
14561460
$nodeConsignee->addChild('CountryCode', $rawRequest->getRecipientAddressCountryCode());
14571461
$nodeConsignee->addChild(
@@ -1500,7 +1504,9 @@ protected function _doRequest()
15001504
$nodeShipper = $xml->addChild('Shipper', '', '');
15011505
$nodeShipper->addChild('ShipperID', (string)$this->getConfigData('account'));
15021506
$nodeShipper->addChild('CompanyName', $rawRequest->getShipperContactCompanyName());
1503-
$nodeShipper->addChild('RegisteredAccount', (string)$this->getConfigData('account'));
1507+
if ($originRegion !== 'AP') {
1508+
$nodeShipper->addChild('RegisteredAccount', (string)$this->getConfigData('account'));
1509+
}
15041510

15051511
$address = $rawRequest->getShipperAddressStreet1() . ' ' . $rawRequest->getShipperAddressStreet2();
15061512
$address = $this->string->split($address, 35, false, true);
@@ -1513,7 +1519,9 @@ protected function _doRequest()
15131519
}
15141520

15151521
$nodeShipper->addChild('City', $rawRequest->getShipperAddressCity());
1516-
$nodeShipper->addChild('Division', $rawRequest->getShipperAddressStateOrProvinceCode());
1522+
if ($originRegion !== 'AP') {
1523+
$nodeShipper->addChild('Division', $rawRequest->getShipperAddressStateOrProvinceCode());
1524+
}
15171525
$nodeShipper->addChild('PostalCode', $rawRequest->getShipperAddressPostalCode());
15181526
$nodeShipper->addChild('CountryCode', $rawRequest->getShipperAddressCountryCode());
15191527
$nodeShipper->addChild(

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

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Dhl\Test\Unit\Model;
77

8+
use Magento\Dhl\Model\Carrier;
89
use Magento\Framework\App\Config\ScopeConfigInterface;
910
use Magento\Framework\HTTP\ZendClient;
1011
use Magento\Framework\HTTP\ZendClientFactory;
@@ -13,6 +14,7 @@
1314
use Magento\Quote\Model\Quote\Address\RateRequest;
1415
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1516
use Magento\Dhl\Model\Validator\XmlValidator;
17+
use Magento\Store\Model\ScopeInterface;
1618

1719
/**
1820
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -30,7 +32,7 @@ class CarrierTest extends \PHPUnit\Framework\TestCase
3032
private $httpResponse;
3133

3234
/**
33-
* @var \Magento\Dhl\Model\Carrier
35+
* @var Carrier
3436
*/
3537
private $model;
3638

@@ -92,8 +94,6 @@ protected function setUp()
9294
->getMock();
9395

9496
$this->scope = $this->getMockForAbstractClass(ScopeConfigInterface::class);
95-
$this->scope->method('getValue')
96-
->willReturnCallback([$this, 'scopeConfigGetValue']);
9797

9898
// xml element factory
9999
$xmlElFactory = $this->getMockBuilder(
@@ -219,7 +219,7 @@ function ($data) {
219219
->getMock();
220220

221221
$this->model = $this->objectManager->getObject(
222-
\Magento\Dhl\Model\Carrier::class,
222+
Carrier::class,
223223
[
224224
'scopeConfig' => $this->scope,
225225
'xmlSecurity' => new Security(),
@@ -314,14 +314,17 @@ public function prepareShippingLabelContentExceptionDataProvider()
314314
*/
315315
protected function _invokePrepareShippingLabelContent(\SimpleXMLElement $xml)
316316
{
317-
$model = $this->objectManager->getObject(\Magento\Dhl\Model\Carrier::class);
317+
$model = $this->objectManager->getObject(Carrier::class);
318318
$method = new \ReflectionMethod($model, '_prepareShippingLabelContent');
319319
$method->setAccessible(true);
320320
return $method->invoke($model, $xml);
321321
}
322322

323323
public function testCollectRates()
324324
{
325+
$this->scope->method('getValue')
326+
->willReturnCallback([$this, 'scopeConfigGetValue']);
327+
325328
$this->scope->method('isSetFlag')
326329
->willReturn(true);
327330

@@ -348,6 +351,9 @@ public function testCollectRates()
348351

349352
public function testCollectRatesErrorMessage()
350353
{
354+
$this->scope->method('getValue')
355+
->willReturnCallback([$this, 'scopeConfigGetValue']);
356+
351357
$this->scope->expects($this->once())->method('isSetFlag')->willReturn(false);
352358

353359
$this->error->expects($this->once())->method('setCarrier')->with('dhl');
@@ -375,6 +381,9 @@ public function testCollectRatesFail()
375381
*/
376382
public function testRequestToShipment()
377383
{
384+
$this->scope->method('getValue')
385+
->willReturnCallback([$this, 'scopeConfigGetValue']);
386+
378387
$this->httpResponse->method('getBody')
379388
->willReturn(utf8_encode(file_get_contents(__DIR__ . '/_files/response_shipping_label.xml')));
380389
$this->xmlValidator->expects($this->any())->method('validate');
@@ -389,7 +398,7 @@ public function testRequestToShipment()
389398
'weight_units' => 'POUND',
390399
'weight' => '0.454000000001',
391400
'customs_value' => '10.00',
392-
'container' => \Magento\Dhl\Model\Carrier::DHL_CONTENT_TYPE_NON_DOC,
401+
'container' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
393402
],
394403
'items' => [
395404
'item1' => [
@@ -432,20 +441,73 @@ public function testRequestToShipment()
432441
}
433442

434443
/**
435-
* Data provider to testRequestToShipment
436-
*
437-
* @return array
444+
* Test that shipping label request for origin country from AP region doesn't contain restricted fields.
438445
*/
439-
public function requestToShipmentDataProvider()
446+
public function testShippingLabelRequestForAsiaPacificRegion()
440447
{
441-
return [
442-
[
443-
'GB'
444-
],
445-
[
446-
null
448+
$this->scope->method('getValue')
449+
->willReturnMap(
450+
[
451+
['shipping/origin/country_id', ScopeInterface::SCOPE_STORE, null, 'SG'],
452+
['carriers/dhl/gateway_url', ScopeInterface::SCOPE_STORE, null, 'https://xmlpi-ea.dhl.com'],
453+
]
454+
);
455+
456+
$this->httpResponse->method('getBody')
457+
->willReturn(utf8_encode(file_get_contents(__DIR__ . '/_files/response_shipping_label.xml')));
458+
459+
$packages = [
460+
'package' => [
461+
'params' => [
462+
'width' => '1',
463+
'length' => '1',
464+
'height' => '1',
465+
'dimension_units' => 'INCH',
466+
'weight_units' => 'POUND',
467+
'weight' => '0.45',
468+
'customs_value' => '10.00',
469+
'container' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
470+
],
471+
'items' => [
472+
'item1' => [
473+
'name' => 'item_name',
474+
],
475+
],
447476
]
448477
];
478+
479+
$this->request->method('getPackages')->willReturn($packages);
480+
$this->request->method('getOrigCountryId')->willReturn('SG');
481+
$this->request->method('setPackages')->willReturnSelf();
482+
$this->request->method('setPackageWeight')->willReturnSelf();
483+
$this->request->method('setPackageValue')->willReturnSelf();
484+
$this->request->method('setValueWithDiscount')->willReturnSelf();
485+
$this->request->method('setPackageCustomsValue')->willReturnSelf();
486+
487+
$result = $this->model->requestToShipment($this->request);
488+
489+
$reflectionClass = new \ReflectionObject($this->httpClient);
490+
$rawPostData = $reflectionClass->getProperty('raw_post_data');
491+
$rawPostData->setAccessible(true);
492+
493+
$this->assertNotNull($result);
494+
$requestXml = $rawPostData->getValue($this->httpClient);
495+
496+
$this->assertNotContains(
497+
'NewShipper',
498+
$requestXml,
499+
'NewShipper is restricted field for AP region'
500+
);
501+
$this->assertNotContains(
502+
'Division',
503+
$requestXml,
504+
'Division is restricted field for AP region'
505+
);
506+
$this->assertNotContains(
507+
'RegisteredAccount',
508+
$requestXml,
509+
'RegisteredAccount is restricted field for AP region'
510+
);
449511
}
450512

451513
/**
@@ -466,7 +528,7 @@ public function dhlProductsDataProvider() : array
466528
{
467529
return [
468530
'doc' => [
469-
'docType' => \Magento\Dhl\Model\Carrier::DHL_CONTENT_TYPE_DOC,
531+
'docType' => Carrier::DHL_CONTENT_TYPE_DOC,
470532
'products' => [
471533
'2' => 'Easy shop',
472534
'5' => 'Sprintline',
@@ -491,7 +553,7 @@ public function dhlProductsDataProvider() : array
491553
]
492554
],
493555
'non-doc' => [
494-
'docType' => \Magento\Dhl\Model\Carrier::DHL_CONTENT_TYPE_NON_DOC,
556+
'docType' => Carrier::DHL_CONTENT_TYPE_NON_DOC,
495557
'products' => [
496558
'1' => 'Domestic express 12:00',
497559
'3' => 'Easy shop',

0 commit comments

Comments
 (0)