Skip to content

Commit 2122e54

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MC-6296' into MC-4245
2 parents 9302fc9 + a0ad012 commit 2122e54

File tree

5 files changed

+290
-102
lines changed

5 files changed

+290
-102
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,18 +1000,29 @@ protected function _getQuotesFromServer($request)
10001000
protected function _buildQuotesRequestXml()
10011001
{
10021002
$rawRequest = $this->_rawRequest;
1003-
$xmlStr = '<?xml version = "1.0" encoding = "UTF-8"?>' .
1004-
'<p:DCTRequest xmlns:p="http://www.dhl.com" xmlns:p1="http://www.dhl.com/datatypes" ' .
1005-
'xmlns:p2="http://www.dhl.com/DCTRequestdatatypes" ' .
1003+
1004+
$xmlStr = '<?xml version="1.0" encoding = "UTF-8"?>' .
1005+
'<req:DCTRequest schemaVersion="2.0" ' .
1006+
'xmlns:req="http://www.dhl.com" ' .
10061007
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
1007-
'xsi:schemaLocation="http://www.dhl.com DCT-req.xsd "/>';
1008+
'xsi:schemaLocation="http://www.dhl.com DCT-req_global-2.0.xsd"/>';
1009+
10081010
$xml = $this->_xmlElFactory->create(['data' => $xmlStr]);
10091011
$nodeGetQuote = $xml->addChild('GetQuote', '', '');
10101012
$nodeRequest = $nodeGetQuote->addChild('Request');
10111013

10121014
$nodeServiceHeader = $nodeRequest->addChild('ServiceHeader');
1013-
$nodeServiceHeader->addChild('SiteID', (string)$this->getConfigData('id'));
1014-
$nodeServiceHeader->addChild('Password', (string)$this->getConfigData('password'));
1015+
$nodeServiceHeader->addChild('MessageTime', $this->buildMessageTimestamp());
1016+
$nodeServiceHeader->addChild(
1017+
'MessageReference',
1018+
$this->buildMessageReference(self::SERVICE_PREFIX_QUOTE)
1019+
);
1020+
$nodeServiceHeader->addChild('SiteID', (string) $this->getConfigData('id'));
1021+
$nodeServiceHeader->addChild('Password', (string) $this->getConfigData('password'));
1022+
1023+
$nodeMetaData = $nodeRequest->addChild('MetaData');
1024+
$nodeMetaData->addChild('SoftwareName', $this->buildSoftwareName());
1025+
$nodeMetaData->addChild('SoftwareVersion', $this->buildSoftwareVersion());
10151026

10161027
$nodeFrom = $nodeGetQuote->addChild('From');
10171028
$nodeFrom->addChild('CountryCode', $rawRequest->getOrigCountryId());
@@ -1995,4 +2006,24 @@ private function buildMessageReference(string $servicePrefix): string
19952006

19962007
return str_replace('.', '', uniqid("MAGE_{$servicePrefix}_", true));
19972008
}
2009+
2010+
/**
2011+
* Builds a string to be used as the request SoftwareName.
2012+
*
2013+
* @return string
2014+
*/
2015+
private function buildSoftwareName(): string
2016+
{
2017+
return substr($this->productMetadata->getName(), 0, 30);
2018+
}
2019+
2020+
/**
2021+
* Builds a string to be used as the request SoftwareVersion.
2022+
*
2023+
* @return string
2024+
*/
2025+
private function buildSoftwareVersion(): string
2026+
{
2027+
return substr($this->productMetadata->getVersion(), 0, 10);
2028+
}
19982029
}

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

Lines changed: 141 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,6 @@ protected function setUp()
130130

131131
$this->scope = $this->getMockForAbstractClass(ScopeConfigInterface::class);
132132

133-
$xmlElFactory = $this->getXmlFactory();
134-
$rateFactory = $this->getRateFactory();
135-
$rateMethodFactory = $this->getRateMethodFactory();
136-
$httpClientFactory = $this->getHttpClientFactory();
137-
$configReader = $this->getConfigReader();
138-
$readFactory = $this->getReadFactory();
139-
$storeManager = $this->getStoreManager();
140-
141133
$this->error = $this->getMockBuilder(Error::class)
142134
->setMethods(['setCarrier', 'setCarrierTitle', 'setErrorMessage'])
143135
->getMock();
@@ -148,8 +140,6 @@ protected function setUp()
148140
$this->errorFactory->method('create')
149141
->willReturn($this->error);
150142

151-
$carrierHelper = $this->getCarrierHelper();
152-
153143
$this->xmlValidator = $this->getMockBuilder(XmlValidator::class)
154144
->disableOriginalConstructor()
155145
->getMock();
@@ -174,17 +164,17 @@ protected function setUp()
174164
Carrier::class,
175165
[
176166
'scopeConfig' => $this->scope,
177-
'xmlSecurity' => new Security(),
178-
'logger' => $this->logger,
179-
'xmlElFactory' => $xmlElFactory,
180-
'rateFactory' => $rateFactory,
181167
'rateErrorFactory' => $this->errorFactory,
182-
'rateMethodFactory' => $rateMethodFactory,
183-
'httpClientFactory' => $httpClientFactory,
184-
'readFactory' => $readFactory,
185-
'storeManager' => $storeManager,
186-
'configReader' => $configReader,
187-
'carrierHelper' => $carrierHelper,
168+
'logger' => $this->logger,
169+
'xmlSecurity' => new Security(),
170+
'xmlElFactory' => $this->getXmlFactory(),
171+
'rateFactory' => $this->getRateFactory(),
172+
'rateMethodFactory' => $this->getRateMethodFactory(),
173+
'carrierHelper' => $this->getCarrierHelper(),
174+
'configReader' => $this->getConfigReader(),
175+
'storeManager' => $this->getStoreManager(),
176+
'readFactory' => $this->getReadFactory(),
177+
'httpClientFactory' => $this->getHttpClientFactory(),
188178
'data' => ['id' => 'dhl', 'store' => '1'],
189179
'xmlValidator' => $this->xmlValidator,
190180
'coreDate' => $this->coreDateMock,
@@ -212,15 +202,15 @@ public function scopeConfigGetValue($path)
212202
'carriers/dhl/content_type' => 'N',
213203
'carriers/dhl/nondoc_methods' => '1,3,4,8,P,Q,E,F,H,J,M,V,Y',
214204
'carriers/dhl/showmethod' => 1,
215-
'carriers/dhl/title' => 'dhl Title',
205+
'carriers/dhl/title' => 'DHL Title',
216206
'carriers/dhl/specificerrmsg' => 'dhl error message',
217207
'carriers/dhl/unit_of_measure' => 'K',
218208
'carriers/dhl/size' => '1',
219209
'carriers/dhl/height' => '1.6',
220210
'carriers/dhl/width' => '1.6',
221211
'carriers/dhl/depth' => '1.6',
222212
'carriers/dhl/debug' => 1,
223-
'shipping/origin/country_id' => 'GB',
213+
'shipping/origin/country_id' => 'GB'
224214
];
225215
return isset($pathMap[$path]) ? $pathMap[$path] : null;
226216
}
@@ -276,22 +266,29 @@ protected function _invokePrepareShippingLabelContent(\SimpleXMLElement $xml)
276266
return $method->invoke($model, $xml);
277267
}
278268

269+
/**
270+
* Tests that valid rates are returned when sending a quotes request.
271+
*/
279272
public function testCollectRates()
280273
{
274+
$requestData = require __DIR__ . '/_files/dhl_quote_request_data.php';
275+
$responseXml = file_get_contents(__DIR__ . '/_files/dhl_quote_response.xml');
276+
281277
$this->scope->method('getValue')
282278
->willReturnCallback([$this, 'scopeConfigGetValue']);
283279

284280
$this->scope->method('isSetFlag')
285281
->willReturn(true);
286282

287283
$this->httpResponse->method('getBody')
288-
->willReturn(file_get_contents(__DIR__ . '/_files/success_dhl_response_rates.xml'));
284+
->willReturn($responseXml);
289285

290-
/** @var RateRequest $request */
291-
$request = $this->objectManager->getObject(
292-
RateRequest::class,
293-
require __DIR__ . '/_files/rates_request_data_dhl.php'
294-
);
286+
$this->coreDateMock->method('date')
287+
->willReturnCallback(function () {
288+
return date(\DATE_RFC3339);
289+
});
290+
291+
$request = $this->objectManager->getObject(RateRequest::class, $requestData);
295292

296293
$reflectionClass = new \ReflectionObject($this->httpClient);
297294
$rawPostData = $reflectionClass->getProperty('raw_post_data');
@@ -301,13 +298,27 @@ public function testCollectRates()
301298
->method('debug')
302299
->with($this->stringContains('<SiteID>****</SiteID><Password>****</Password>'));
303300

304-
self::assertNotEmpty($this->model->collectRates($request)->getAllRates());
305-
self::assertContains('<Weight>18.223</Weight>', $rawPostData->getValue($this->httpClient));
306-
self::assertContains('<Height>0.630</Height>', $rawPostData->getValue($this->httpClient));
307-
self::assertContains('<Width>0.630</Width>', $rawPostData->getValue($this->httpClient));
308-
self::assertContains('<Depth>0.630</Depth>', $rawPostData->getValue($this->httpClient));
301+
$expectedRates = require __DIR__ . '/_files/dhl_quote_response_rates.php';
302+
$actualRates = $this->model->collectRates($request)->getAllRates();
303+
304+
self::assertEquals(count($expectedRates), count($actualRates));
305+
306+
foreach ($actualRates as $i => $actualRate) {
307+
$actualRate = $actualRate->getData();
308+
unset($actualRate['method_title']);
309+
self::assertEquals($expectedRates[$i], $actualRate);
310+
}
311+
312+
$requestXml = $rawPostData->getValue($this->httpClient);
313+
self::assertContains('<Weight>18.223</Weight>', $requestXml);
314+
self::assertContains('<Height>0.630</Height>', $requestXml);
315+
self::assertContains('<Width>0.630</Width>', $requestXml);
316+
self::assertContains('<Depth>0.630</Depth>', $requestXml);
309317
}
310318

319+
/**
320+
* Tests that an error is returned when attempting to collect rates for an inactive shipping method.
321+
*/
311322
public function testCollectRatesErrorMessage()
312323
{
313324
$this->scope->method('getValue')
@@ -325,16 +336,6 @@ public function testCollectRatesErrorMessage()
325336
$this->assertSame($this->error, $this->model->collectRates($request));
326337
}
327338

328-
public function testCollectRatesFail()
329-
{
330-
$this->scope->expects($this->once())->method('isSetFlag')->willReturn(true);
331-
332-
$request = new RateRequest();
333-
$request->setPackageWeight(1);
334-
335-
$this->assertFalse(false, $this->model->collectRates($request));
336-
}
337-
338339
/**
339340
* Test request to shipment sends valid xml values.
340341
*
@@ -520,34 +521,104 @@ public function dhlProductsDataProvider() : array
520521
}
521522

522523
/**
523-
* Tests that the built message reference string is of the appropriate format.
524+
* Tests that the built MessageReference string is of the appropriate format.
525+
*
526+
* @dataProvider buildMessageReferenceDataProvider
527+
* @param $servicePrefix
528+
* @throws \ReflectionException
529+
*/
530+
public function testBuildMessageReference($servicePrefix)
531+
{
532+
$method = new \ReflectionMethod($this->model, 'buildMessageReference');
533+
$method->setAccessible(true);
534+
535+
$messageReference = $method->invoke($this->model, $servicePrefix);
536+
$this->assertGreaterThanOrEqual(28, strlen($messageReference));
537+
$this->assertLessThanOrEqual(32, strlen($messageReference));
538+
}
539+
540+
/**
541+
* @return array
542+
*/
543+
public function buildMessageReferenceDataProvider()
544+
{
545+
return [
546+
'quote_prefix' => ['QUOT'],
547+
'shipval_prefix' => ['SHIP'],
548+
'tracking_prefix' => ['TRCK']
549+
];
550+
}
551+
552+
/**
553+
* Tests that an exception is thrown when an invalid service prefix is provided.
524554
*
525555
* @expectedException \Magento\Framework\Exception\LocalizedException
526556
* @expectedExceptionMessage Invalid service prefix
527-
* @throws \ReflectionException
528557
*/
529-
public function testBuildMessageReference()
558+
public function testBuildMessageReferenceInvalidPrefix()
530559
{
531560
$method = new \ReflectionMethod($this->model, 'buildMessageReference');
532561
$method->setAccessible(true);
533562

534-
$constPrefixQuote = new \ReflectionClassConstant($this->model, 'SERVICE_PREFIX_QUOTE');
535-
$constPrefixShipval = new \ReflectionClassConstant($this->model, 'SERVICE_PREFIX_SHIPVAL');
536-
$constPrefixTracking = new \ReflectionClassConstant($this->model, 'SERVICE_PREFIX_TRACKING');
563+
$method->invoke($this->model, 'INVALID');
564+
}
537565

538-
$msgRefQuote = $method->invoke($this->model, $constPrefixQuote->getValue());
539-
self::assertGreaterThanOrEqual(28, strlen($msgRefQuote));
540-
self::assertLessThanOrEqual(32, strlen($msgRefQuote));
566+
/**
567+
* Tests that the built software name string is of the appropriate format.
568+
*
569+
* @dataProvider buildSoftwareNameDataProvider
570+
* @param $productName
571+
* @throws \ReflectionException
572+
*/
573+
public function testBuildSoftwareName($productName)
574+
{
575+
$method = new \ReflectionMethod($this->model, 'buildSoftwareName');
576+
$method->setAccessible(true);
541577

542-
$msgRefShip = $method->invoke($this->model, $constPrefixShipval->getValue());
543-
self::assertGreaterThanOrEqual(28, strlen($msgRefShip));
544-
self::assertLessThanOrEqual(32, strlen($msgRefShip));
578+
$this->productMetadataMock->method('getName')->willReturn($productName);
545579

546-
$msgRefTrack = $method->invoke($this->model, $constPrefixTracking->getValue());
547-
self::assertGreaterThanOrEqual(28, strlen($msgRefTrack));
548-
self::assertLessThanOrEqual(32, strlen($msgRefTrack));
580+
$softwareName = $method->invoke($this->model);
581+
$this->assertLessThanOrEqual(30, strlen($softwareName));
582+
}
549583

550-
$method->invoke($this->model, 'TEST');
584+
/**
585+
* @return array
586+
*/
587+
public function buildSoftwareNameDataProvider()
588+
{
589+
return [
590+
'valid_length' => ['Magento'],
591+
'exceeds_length' => ['Product_Name_Longer_Than_30_Char']
592+
];
593+
}
594+
595+
/**
596+
* Tests that the built software version string is of the appropriate format.
597+
*
598+
* @dataProvider buildSoftwareVersionProvider
599+
* @param $productVersion
600+
* @throws \ReflectionException
601+
*/
602+
public function testBuildSoftwareVersion($productVersion)
603+
{
604+
$method = new \ReflectionMethod($this->model, 'buildSoftwareVersion');
605+
$method->setAccessible(true);
606+
607+
$this->productMetadataMock->method('getVersion')->willReturn($productVersion);
608+
609+
$softwareVersion = $method->invoke($this->model);
610+
$this->assertLessThanOrEqual(10, strlen($softwareVersion));
611+
}
612+
613+
/**
614+
* @return array
615+
*/
616+
public function buildSoftwareVersionProvider()
617+
{
618+
return [
619+
'valid_length' => ['2.3.1'],
620+
'exceeds_length' => ['dev-MC-1000']
621+
];
551622
}
552623

553624
/**
@@ -608,14 +679,18 @@ private function getRateMethodFactory(): MockObject
608679
->disableOriginalConstructor()
609680
->setMethods(['create'])
610681
->getMock();
611-
$rateMethod = $this->getMockBuilder(Method::class)
612-
->disableOriginalConstructor()
613-
->setMethods(['setPrice'])
614-
->getMock();
615-
$rateMethod->method('setPrice')
616-
->willReturnSelf();
682+
617683
$rateMethodFactory->method('create')
618-
->willReturn($rateMethod);
684+
->willReturnCallback(function () {
685+
$rateMethod = $this->getMockBuilder(Method::class)
686+
->disableOriginalConstructor()
687+
->setMethods(['setPrice'])
688+
->getMock();
689+
$rateMethod->method('setPrice')
690+
->willReturnSelf();
691+
692+
return $rateMethod;
693+
});
619694

620695
return $rateMethodFactory;
621696
}

app/code/Magento/Dhl/Test/Unit/Model/_files/rates_request_data_dhl.php renamed to app/code/Magento/Dhl/Test/Unit/Model/_files/dhl_quote_request_data.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
'limit_carrier' => null,
2525
'base_subtotal_incl_tax' => '5',
2626
'orig_country_id' => 'US',
27-
'country_id' => 'US',
28-
'region_id' => '12',
29-
'city' => 'Fremont',
30-
'postcode' => '94538',
27+
'orig_region_id' => '12',
28+
'orig_city' => 'Fremont',
29+
'orig_postcode' => '94538',
3130
'dhl_id' => 'MAGEN_8501',
3231
'dhl_password' => 'QR2GO1U74X',
3332
'dhl_account' => '799909537',

0 commit comments

Comments
 (0)