Skip to content

Commit 1a8fbf2

Browse files
author
Mark Berube
committed
MC-5881: Changing random string generation
1 parent a040e79 commit 1a8fbf2

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

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

Lines changed: 17 additions & 18 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
*/
@@ -83,24 +88,32 @@ class DefaultModel extends \Zend\Captcha\Image implements \Magento\Captcha\Model
8388
*/
8489
private $words;
8590

91+
/**
92+
* @var Random
93+
*/
94+
private $randomMath;
95+
8696
/**
8797
* @param \Magento\Framework\Session\SessionManagerInterface $session
8898
* @param \Magento\Captcha\Helper\Data $captchaData
8999
* @param ResourceModel\LogFactory $resLogFactory
90100
* @param string $formId
101+
* @param Random $randomMath
91102
* @throws \Zend\Captcha\Exception\ExtensionNotLoadedException
92103
*/
93104
public function __construct(
94105
\Magento\Framework\Session\SessionManagerInterface $session,
95106
\Magento\Captcha\Helper\Data $captchaData,
96107
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
97-
$formId
108+
$formId,
109+
Random $randomMath = null
98110
) {
99111
parent::__construct();
100112
$this->session = $session;
101113
$this->captchaData = $captchaData;
102114
$this->resLogFactory = $resLogFactory;
103115
$this->formId = $formId;
116+
$this->randomMath = $randomMath ?? \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class);
104117
}
105118

106119
/**
@@ -382,23 +395,9 @@ public function setShowCaptchaInSession($value = true)
382395
*/
383396
protected function generateWord()
384397
{
385-
$word = '';
386-
$symbols = $this->getSymbols();
398+
$symbols = (string)$this->captchaData->getConfig('symbols');
387399
$wordLen = $this->getWordLen();
388-
for ($i = 0; $i < $wordLen; $i++) {
389-
$word .= $symbols[array_rand($symbols)];
390-
}
391-
return $word;
392-
}
393-
394-
/**
395-
* Get symbols array to use for word generation
396-
*
397-
* @return array
398-
*/
399-
private function getSymbols()
400-
{
401-
return str_split((string)$this->captchaData->getConfig('symbols'));
400+
return $this->randomMath->getRandomString($wordLen, $symbols);
402401
}
403402

404403
/**
@@ -562,7 +561,7 @@ protected function randomSize()
562561
*/
563562
protected function gc()
564563
{
565-
//do nothing
564+
return; // required for static testing to pass
566565
}
567566

568567
/**

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
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\Test\Unit\Model;
79

10+
use Magento\Framework\Math\Random;
11+
812
/**
913
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1014
*/
@@ -375,4 +379,38 @@ public function isShownToLoggedInUserDataProvider()
375379
[false, 'user_forgotpassword']
376380
];
377381
}
382+
383+
/**
384+
* @param string $string
385+
* @dataProvider generateWordProvider
386+
* @throws \ReflectionException
387+
*/
388+
public function testGenerateWord($string)
389+
{
390+
$randomMock = $this->createMock(Random::class);
391+
$randomMock->expects($this->once())
392+
->method('getRandomString')
393+
->will($this->returnValue($string));
394+
$captcha = new \Magento\Captcha\Model\DefaultModel(
395+
$this->session,
396+
$this->_getHelperStub(),
397+
$this->_resLogFactory,
398+
'user_create',
399+
$randomMock
400+
);
401+
$method = new \ReflectionMethod($captcha, 'generateWord');
402+
$method->setAccessible(true);
403+
$this->assertEquals($string, $method->invoke($captcha));
404+
}
405+
/**
406+
* @return array
407+
*/
408+
public function generateWordProvider()
409+
{
410+
return [
411+
['ABC123'],
412+
['1234567890'],
413+
['The quick brown fox jumps over the lazy dog.']
414+
];
415+
}
378416
}

0 commit comments

Comments
 (0)