Skip to content

Commit 53db612

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98341' into borg-qwerty-2.1
2 parents 531aa7e + bc50d91 commit 53db612

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -730,12 +730,7 @@ protected function _getAllItems()
730730
$itemWeight = $this->_getWeight($itemWeight * $item->getQty());
731731
$maxWeight = $this->_getWeight($this->_maxWeight, true);
732732
if ($itemWeight > $maxWeight) {
733-
$qtyItem = floor($itemWeight / $maxWeight);
734-
$decimalItems[] = ['weight' => $maxWeight, 'qty' => $qtyItem];
735-
$weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight);
736-
if ($weightItem) {
737-
$decimalItems[] = ['weight' => $weightItem, 'qty' => 1];
738-
}
733+
$this->pushDecimalItems($decimalItems, $itemWeight, $maxWeight);
739734
$checkWeight = false;
740735
}
741736
}
@@ -772,6 +767,23 @@ protected function _getAllItems()
772767
return $fullItems;
773768
}
774769

770+
/**
771+
* Pushes items into array that are decimal places on item weight
772+
*
773+
* @param array $decimalItems
774+
* @param float $itemWeight
775+
* @param float $maxWeight
776+
*/
777+
private function pushDecimalItems(array &$decimalItems, float $itemWeight, float $maxWeight): void
778+
{
779+
$qtyItem = floor($itemWeight / $maxWeight);
780+
$decimalItems[] = ['weight' => $maxWeight, 'qty' => $qtyItem];
781+
$weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight);
782+
if ($weightItem) {
783+
$decimalItems[] = ['weight' => $weightItem, 'qty' => 1];
784+
}
785+
}
786+
775787
/**
776788
* Make pieces
777789
*
@@ -957,7 +969,7 @@ protected function _getQuotes()
957969
protected function _getQuotesFromServer($request)
958970
{
959971
$client = $this->_httpClientFactory->create();
960-
$client->setUri((string)$this->getConfigData('gateway_url'));
972+
$client->setUri($this->getGatewayURL());
961973
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
962974
$client->setRawData(utf8_encode($request));
963975

@@ -1558,7 +1570,7 @@ protected function _doRequest()
15581570
$debugData = ['request' => $request];
15591571
try {
15601572
$client = $this->_httpClientFactory->create();
1561-
$client->setUri((string)$this->getConfigData('gateway_url'));
1573+
$client->setUri($this->getGatewayURL());
15621574
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
15631575
$client->setRawData($request);
15641576
$responseBody = $client->request(\Magento\Framework\HTTP\ZendClient::POST)->getBody();
@@ -1751,7 +1763,7 @@ protected function _getXMLTracking($trackings)
17511763
$debugData = ['request' => $request];
17521764
try {
17531765
$client = new \Magento\Framework\HTTP\ZendClient();
1754-
$client->setUri((string)$this->getConfigData('gateway_url'));
1766+
$client->setUri($this->getGatewayURL());
17551767
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
17561768
$client->setRawData($request);
17571769
$responseBody = $client->request(\Magento\Framework\HTTP\ZendClient::POST)->getBody();
@@ -1959,6 +1971,8 @@ protected function _prepareShippingLabelContent(\SimpleXMLElement $xml)
19591971
}
19601972

19611973
/**
1974+
* Verify if the shipment is dutiable
1975+
*
19621976
* @param string $origCountryId
19631977
* @param string $destCountryId
19641978
*
@@ -1972,4 +1986,18 @@ protected function isDutiable($origCountryId, $destCountryId)
19721986
self::DHL_CONTENT_TYPE_NON_DOC == $this->getConfigData('content_type')
19731987
&& !$this->_isDomestic;
19741988
}
1989+
1990+
/**
1991+
* Get the gateway URL
1992+
*
1993+
* @return string
1994+
*/
1995+
private function getGatewayURL()
1996+
{
1997+
if ($this->getConfigData('sandbox_mode')) {
1998+
return (string)$this->getConfigData('sandbox_url');
1999+
} else {
2000+
return (string)$this->getConfigData('gateway_url');
2001+
}
2002+
}
19752003
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Quote\Model\Quote\Address\RateRequest;
99
use Magento\Framework\Xml\Security;
10+
use Magento\Dhl\Model\Carrier;
1011

1112
class CarrierTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -305,4 +306,52 @@ public function testCollectRatesFail()
305306

306307
$this->assertFalse(false, $this->_model->collectRates($request));
307308
}
309+
310+
/**
311+
* Tests if the DHL client returns the appropriate API URL.
312+
*
313+
* @dataProvider getGatewayURLProvider
314+
* @param $sandboxMode
315+
* @param $expectedURL
316+
* @throws \ReflectionException
317+
*/
318+
public function testGetGatewayURL($sandboxMode, $expectedURL)
319+
{
320+
$scope = $this->getMockBuilder(
321+
'\Magento\Framework\App\Config\ScopeConfigInterface'
322+
)->disableOriginalConstructor()->getMock();
323+
324+
$scopeConfigValueMap = [
325+
['carriers/dhl/gateway_url', 'store', null, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
326+
['carriers/dhl/sandbox_url', 'store', null, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet'],
327+
['carriers/dhl/sandbox_mode', 'store', null, $sandboxMode]
328+
];
329+
330+
$scope->method('getValue')
331+
->willReturnMap($scopeConfigValueMap);
332+
333+
$this->model = $this->_helper->getObject(
334+
Carrier::class,
335+
[
336+
'scopeConfig' => $scope
337+
]
338+
);
339+
340+
$method = new \ReflectionMethod($this->model, 'getGatewayURL');
341+
$method->setAccessible(true);
342+
$this->assertEquals($expectedURL, $method->invoke($this->model));
343+
}
344+
345+
/**
346+
* Data provider for testGetGatewayURL
347+
*
348+
* @return array
349+
*/
350+
public function getGatewayURLProvider()
351+
{
352+
return [
353+
'standard_url' => [0, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
354+
'sandbox_url' => [1, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet']
355+
];
356+
}
308357
}

app/code/Magento/Dhl/etc/adminhtml/system.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
<label>Enabled for Checkout</label>
1515
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
1616
</field>
17-
<field id="gateway_url" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
18-
<label>Gateway URL</label>
19-
</field>
2017
<field id="title" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
2118
<label>Title</label>
2219
</field>
@@ -150,6 +147,10 @@
150147
<label>Debug</label>
151148
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
152149
</field>
150+
<field id="sandbox_mode" translate="label" type="select" sortOrder="1960" showInDefault="1" showInWebsite="1" showInStore="0">
151+
<label>Sandbox Mode</label>
152+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
153+
</field>
153154
</group>
154155
</section>
155156
</system>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<doc_methods>2,5,6,7,9,B,C,D,U,K,L,G,W,I,N,O,R,S,T,X</doc_methods>
2626
<free_method>G</free_method>
2727
<gateway_url>https://xmlpi-ea.dhl.com/XMLShippingServlet</gateway_url>
28+
<sandbox_url>https://xmlpitest-ea.dhl.com/XMLShippingServlet</sandbox_url>
2829
<id backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
2930
<password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
3031
<content_type>N</content_type>

0 commit comments

Comments
 (0)