Skip to content

Commit 531aa7e

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96534' into borg-qwerty-2.1
2 parents f2cf72c + 3ba2370 commit 531aa7e

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*/
66
namespace Magento\Captcha\Model;
77

8+
use Magento\Framework\Math\Random;
9+
810
/**
911
* Implementation of \Zend_Captcha
1012
*
13+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
14+
*
1115
* @author Magento Core Team <core@magentocommerce.com>
1216
*/
1317
class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model\CaptchaInterface
@@ -68,22 +72,30 @@ class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model
6872
*/
6973
protected $_session;
7074

75+
/**
76+
* @var Random
77+
*/
78+
private $randomMath;
79+
7180
/**
7281
* @param \Magento\Framework\Session\SessionManagerInterface $session
7382
* @param \Magento\Captcha\Helper\Data $captchaData
7483
* @param \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory
7584
* @param string $formId
85+
* @param Random $randomMath
7686
*/
7787
public function __construct(
7888
\Magento\Framework\Session\SessionManagerInterface $session,
7989
\Magento\Captcha\Helper\Data $captchaData,
8090
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
81-
$formId
91+
$formId,
92+
Random $randomMath = null
8293
) {
8394
$this->_session = $session;
8495
$this->_captchaData = $captchaData;
8596
$this->_resLogFactory = $resLogFactory;
8697
$this->_formId = $formId;
98+
$this->randomMath = $randomMath ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class);
8799
}
88100

89101
/**
@@ -361,13 +373,9 @@ public function setShowCaptchaInSession($value = true)
361373
*/
362374
protected function _generateWord()
363375
{
364-
$word = '';
365-
$symbols = $this->_getSymbols();
376+
$symbols = (string)$this->_captchaData->getConfig('symbols');
366377
$wordLen = $this->_getWordLen();
367-
for ($i = 0; $i < $wordLen; $i++) {
368-
$word .= $symbols[array_rand($symbols)];
369-
}
370-
return $word;
378+
return $this->randomMath->getRandomString($wordLen, $symbols);
371379
}
372380

373381
/**

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,41 @@ public function isShownToLoggedInUserDataProvider()
376376
[false, 'guest_checkout']
377377
];
378378
}
379+
380+
/**
381+
* @param string $string
382+
* @dataProvider generateWordProvider
383+
* @throws \ReflectionException
384+
*/
385+
public function testGenerateWord($string)
386+
{
387+
$randomMock = $this->getMock('Magento\Framework\Math\Random');
388+
$randomMock->expects($this->once())
389+
->method('getRandomString')
390+
->will($this->returnValue($string));
391+
392+
$captcha = new \Magento\Captcha\Model\DefaultModel(
393+
$this->session,
394+
$this->_getHelperStub(),
395+
$this->_resLogFactory,
396+
'user_create',
397+
$randomMock
398+
);
399+
400+
$method = new \ReflectionMethod($captcha, '_generateWord');
401+
$method->setAccessible(true);
402+
$this->assertEquals($string, $method->invoke($captcha));
403+
}
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+
}
379416
}

0 commit comments

Comments
 (0)