Skip to content

Commit bf6a8fe

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96535' into borg-qwerty-2.2
2 parents 7d57c09 + 064341d commit bf6a8fe

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
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
}

0 commit comments

Comments
 (0)