Skip to content

Commit 79de888

Browse files
authored
Merge pull request #4068 from magento-borg/borg-qwerty-2.2
[borg] Bug fixes
2 parents f2097fa + bf6a8fe commit 79de888

File tree

10 files changed

+341
-108
lines changed

10 files changed

+341
-108
lines changed

app/code/Magento/Captcha/Model/DefaultModel.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Captcha\Model;
79

810
use Magento\Captcha\Helper\Data;
11+
use Magento\Framework\Math\Random;
912

1013
/**
1114
* Implementation of \Zend\Captcha\Image
1215
*
16+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17+
*
1318
* @api
1419
* @since 100.0.2
1520
*/
@@ -78,24 +83,32 @@ class DefaultModel extends \Zend\Captcha\Image implements \Magento\Captcha\Model
7883
*/
7984
protected $session;
8085

86+
/**
87+
* @var Random
88+
*/
89+
private $randomMath;
90+
8191
/**
8292
* @param \Magento\Framework\Session\SessionManagerInterface $session
8393
* @param \Magento\Captcha\Helper\Data $captchaData
8494
* @param ResourceModel\LogFactory $resLogFactory
8595
* @param string $formId
96+
* @param Random $randomMath
8697
* @throws \Zend\Captcha\Exception\ExtensionNotLoadedException
8798
*/
8899
public function __construct(
89100
\Magento\Framework\Session\SessionManagerInterface $session,
90101
\Magento\Captcha\Helper\Data $captchaData,
91102
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
92-
$formId
103+
$formId,
104+
Random $randomMath = null
93105
) {
94106
parent::__construct();
95107
$this->session = $session;
96108
$this->captchaData = $captchaData;
97109
$this->resLogFactory = $resLogFactory;
98110
$this->formId = $formId;
111+
$this->randomMath = $randomMath ?? \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class);
99112
}
100113

101114
/**
@@ -377,23 +390,9 @@ public function setShowCaptchaInSession($value = true)
377390
*/
378391
protected function generateWord()
379392
{
380-
$word = '';
381-
$symbols = $this->getSymbols();
393+
$symbols = (string)$this->captchaData->getConfig('symbols');
382394
$wordLen = $this->getWordLen();
383-
for ($i = 0; $i < $wordLen; $i++) {
384-
$word .= $symbols[array_rand($symbols)];
385-
}
386-
return $word;
387-
}
388-
389-
/**
390-
* Get symbols array to use for word generation
391-
*
392-
* @return array
393-
*/
394-
private function getSymbols()
395-
{
396-
return str_split((string)$this->captchaData->getConfig('symbols'));
395+
return $this->randomMath->getRandomString($wordLen, $symbols);
397396
}
398397

399398
/**

app/code/Magento/Captcha/Test/Unit/Model/DefaultTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Captcha\Test\Unit\Model;
77

8+
use Magento\Framework\Math\Random;
9+
810
/**
911
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1012
*/
@@ -363,4 +365,41 @@ public function isShownToLoggedInUserDataProvider()
363365
[false, 'user_forgotpassword'],
364366
];
365367
}
368+
369+
/**
370+
* @param string $string
371+
* @dataProvider generateWordProvider
372+
* @throws \ReflectionException
373+
*/
374+
public function testGenerateWord($string)
375+
{
376+
$randomMock = $this->createMock(Random::class);
377+
$randomMock->expects($this->once())
378+
->method('getRandomString')
379+
->will($this->returnValue($string));
380+
381+
$captcha = new \Magento\Captcha\Model\DefaultModel(
382+
$this->session,
383+
$this->_getHelperStub(),
384+
$this->_resLogFactory,
385+
'user_create',
386+
$randomMock
387+
);
388+
389+
$method = new \ReflectionMethod($captcha, 'generateWord');
390+
$method->setAccessible(true);
391+
$this->assertEquals($string, $method->invoke($captcha));
392+
}
393+
394+
/**
395+
* @return array
396+
*/
397+
public function generateWordProvider()
398+
{
399+
return [
400+
['ABC123'],
401+
['1234567890'],
402+
['The quick brown fox jumps over the lazy dog.']
403+
];
404+
}
366405
}

app/code/Magento/Catalog/view/frontend/templates/product/view/form.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<input type="hidden" name="product" value="<?= /* @escapeNotVerified */ $_product->getId() ?>" />
2323
<input type="hidden" name="selected_configurable_option" value="" />
2424
<input type="hidden" name="related_product" id="related-products-field" value="" />
25-
<input type="hidden" name="item" value="<?= $block->escapeHtmlAttr($block->getRequest()->getParam('id')) ?>" />
25+
<input type="hidden" name="item" value="<?= $block->escapeJs($block->escapeHtmlAttr($block->getRequest()->getParam('id'))) ?>" />
2626
<?= $block->getBlockHtml('formkey') ?>
2727
<?= $block->getChildHtml('form_top') ?>
2828
<?php if (!$block->hasOptions()):?>

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -741,12 +741,7 @@ protected function _getAllItems()
741741
$itemWeight = $this->_getWeight($itemWeight * $item->getQty());
742742
$maxWeight = $this->_getWeight($this->_maxWeight, true);
743743
if ($itemWeight > $maxWeight) {
744-
$qtyItem = floor($itemWeight / $maxWeight);
745-
$decimalItems[] = ['weight' => $maxWeight, 'qty' => $qtyItem];
746-
$weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight);
747-
if ($weightItem) {
748-
$decimalItems[] = ['weight' => $weightItem, 'qty' => 1];
749-
}
744+
$this->pushDecimalItems($decimalItems, $itemWeight, $maxWeight);
750745
$checkWeight = false;
751746
}
752747
}
@@ -783,6 +778,23 @@ protected function _getAllItems()
783778
return $fullItems;
784779
}
785780

781+
/**
782+
* Pushes items into array that are decimal places on item weight
783+
*
784+
* @param array $decimalItems
785+
* @param float $itemWeight
786+
* @param float $maxWeight
787+
*/
788+
private function pushDecimalItems(array &$decimalItems, float $itemWeight, float $maxWeight): void
789+
{
790+
$qtyItem = floor($itemWeight / $maxWeight);
791+
$decimalItems[] = ['weight' => $maxWeight, 'qty' => $qtyItem];
792+
$weightItem = $this->mathDivision->getExactDivision($itemWeight, $maxWeight);
793+
if ($weightItem) {
794+
$decimalItems[] = ['weight' => $weightItem, 'qty' => 1];
795+
}
796+
}
797+
786798
/**
787799
* Make pieces
788800
*
@@ -968,7 +980,7 @@ protected function _getQuotes()
968980
protected function _getQuotesFromServer($request)
969981
{
970982
$client = $this->_httpClientFactory->create();
971-
$client->setUri((string)$this->getConfigData('gateway_url'));
983+
$client->setUri($this->getGatewayURL());
972984
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
973985
$client->setRawData(utf8_encode($request));
974986

@@ -1557,7 +1569,7 @@ protected function _doRequest()
15571569
try {
15581570
/** @var \Magento\Framework\HTTP\ZendClient $client */
15591571
$client = $this->_httpClientFactory->create();
1560-
$client->setUri((string)$this->getConfigData('gateway_url'));
1572+
$client->setUri($this->getGatewayURL());
15611573
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
15621574
$client->setRawData($request);
15631575
$responseBody = $client->request(\Magento\Framework\HTTP\ZendClient::POST)->getBody();
@@ -1751,7 +1763,7 @@ protected function _getXMLTracking($trackings)
17511763
try {
17521764
/** @var \Magento\Framework\HTTP\ZendClient $client */
17531765
$client = $this->_httpClientFactory->create();
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();
@@ -1950,6 +1962,7 @@ protected function _prepareShippingLabelContent(\SimpleXMLElement $xml)
19501962
}
19511963
$result->setTrackingNumber((string)$xml->AirwayBillNumber);
19521964
$labelContent = (string)$xml->LabelImage->OutputImage;
1965+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
19531966
$result->setShippingLabelContent(base64_decode($labelContent));
19541967
} catch (\Exception $e) {
19551968
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
@@ -1959,6 +1972,8 @@ protected function _prepareShippingLabelContent(\SimpleXMLElement $xml)
19591972
}
19601973

19611974
/**
1975+
* Verify if the shipment is dutiable
1976+
*
19621977
* @param string $origCountryId
19631978
* @param string $destCountryId
19641979
*
@@ -1970,4 +1985,18 @@ protected function isDutiable($origCountryId, $destCountryId)
19701985

19711986
return !$this->_isDomestic;
19721987
}
1988+
1989+
/**
1990+
* Get the gateway URL
1991+
*
1992+
* @return string
1993+
*/
1994+
private function getGatewayURL(): string
1995+
{
1996+
if ($this->getConfigData('sandbox_mode')) {
1997+
return (string)$this->getConfigData('sandbox_url');
1998+
} else {
1999+
return (string)$this->getConfigData('gateway_url');
2000+
}
2001+
}
19732002
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,50 @@ public function dhlProductsDataProvider() : array
545545
];
546546
}
547547

548+
/**
549+
* Tests if the DHL client returns the appropriate API URL.
550+
*
551+
* @dataProvider getGatewayURLProvider
552+
* @param $sandboxMode
553+
* @param $expectedURL
554+
* @throws \ReflectionException
555+
*/
556+
public function testGetGatewayURL($sandboxMode, $expectedURL)
557+
{
558+
$scopeConfigValueMap = [
559+
['carriers/dhl/gateway_url', 'store', null, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
560+
['carriers/dhl/sandbox_url', 'store', null, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet'],
561+
['carriers/dhl/sandbox_mode', 'store', null, $sandboxMode]
562+
];
563+
564+
$this->scope->method('getValue')
565+
->willReturnMap($scopeConfigValueMap);
566+
567+
$this->model = $this->objectManager->getObject(
568+
Carrier::class,
569+
[
570+
'scopeConfig' => $this->scope
571+
]
572+
);
573+
574+
$method = new \ReflectionMethod($this->model, 'getGatewayURL');
575+
$method->setAccessible(true);
576+
$this->assertEquals($expectedURL, $method->invoke($this->model));
577+
}
578+
579+
/**
580+
* Data provider for testGetGatewayURL
581+
*
582+
* @return array
583+
*/
584+
public function getGatewayURLProvider()
585+
{
586+
return [
587+
'standard_url' => [0, 'https://xmlpi-ea.dhl.com/XMLShippingServlet'],
588+
'sandbox_url' => [1, 'https://xmlpitest-ea.dhl.com/XMLShippingServlet']
589+
];
590+
}
591+
548592
/**
549593
* Creates mock for XML factory.
550594
*

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>
@@ -145,6 +142,10 @@
145142
<label>Debug</label>
146143
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
147144
</field>
145+
<field id="sandbox_mode" translate="label" type="select" sortOrder="1960" showInDefault="1" showInWebsite="1" showInStore="0">
146+
<label>Sandbox Mode</label>
147+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
148+
</field>
148149
</group>
149150
</section>
150151
</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)