Skip to content

Commit 79c347f

Browse files
author
vpaladiychuk
committed
Merge branch 'MAGETWO-35534' into S50
2 parents 7ea9e4d + f26979e commit 79c347f

File tree

4 files changed

+81
-35
lines changed

4 files changed

+81
-35
lines changed

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

100644100755
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,30 @@ class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model
6868
*/
6969
protected $_session;
7070

71+
/**
72+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
73+
*/
74+
protected $dateModel;
75+
7176
/**
7277
* @param \Magento\Framework\Session\SessionManagerInterface $session
7378
* @param \Magento\Captcha\Helper\Data $captchaData
7479
* @param \Magento\Captcha\Model\Resource\LogFactory $resLogFactory
80+
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateModel
7581
* @param string $formId
7682
*/
7783
public function __construct(
7884
\Magento\Framework\Session\SessionManagerInterface $session,
7985
\Magento\Captcha\Helper\Data $captchaData,
8086
\Magento\Captcha\Model\Resource\LogFactory $resLogFactory,
87+
\Magento\Framework\Stdlib\DateTime\DateTime $dateModel,
8188
$formId
8289
) {
8390
$this->_session = $session;
8491
$this->_captchaData = $captchaData;
8592
$this->_resLogFactory = $resLogFactory;
8693
$this->_formId = $formId;
94+
$this->dateModel = $dateModel;
8795
}
8896

8997
/**
@@ -452,7 +460,7 @@ protected function _getTargetForms()
452460
public function getWord()
453461
{
454462
$sessionData = $this->_session->getData($this->_getFormIdKey(self::SESSION_WORD));
455-
return time() < $sessionData['expires'] ? $sessionData['data'] : null;
463+
return $this->dateModel->gmtTimestamp() < $sessionData['expires'] ? $sessionData['data'] : null;
456464
}
457465

458466
/**
@@ -465,7 +473,7 @@ protected function _setWord($word)
465473
{
466474
$this->_session->setData(
467475
$this->_getFormIdKey(self::SESSION_WORD),
468-
['data' => $word, 'expires' => time() + $this->getTimeout()]
476+
['data' => $word, 'expires' => $this->dateModel->gmtTimestamp() + $this->getTimeout()]
469477
);
470478
$this->_word = $word;
471479
return $this;

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

100644100755
Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class DefaultTest extends \PHPUnit_Framework_TestCase
99
{
10+
/**
11+
* Expiration date
12+
*/
13+
const EXPIRATION_TIMESTAMP = 86400;
14+
1015
/**
1116
* Captcha default config data
1217
* @var array
@@ -67,13 +72,18 @@ class DefaultTest extends \PHPUnit_Framework_TestCase
6772
/**
6873
* @var \PHPUnit_Framework_MockObject_MockObject
6974
*/
70-
protected $_session;
75+
protected $session;
7176

7277
/**
7378
* @var \PHPUnit_Framework_MockObject_MockObject
7479
*/
7580
protected $_resLogFactory;
7681

82+
/**
83+
* @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
84+
*/
85+
protected $dateModel;
86+
7787
/**
7888
* Sets up the fixture, for example, opens a network connection.
7989
* This method is called before a test is executed.
@@ -127,11 +137,17 @@ protected function setUp()
127137
$this->returnValue($this->_getResourceModelStub())
128138
);
129139

130-
$this->_object = new \Magento\Captcha\Model\DefaultModel(
131-
$this->session,
132-
$this->_getHelperStub(),
133-
$this->_resLogFactory,
134-
'user_create'
140+
$this->dateModel = $this->getMock('Magento\Framework\Stdlib\DateTime\DateTime', [], [], '', false);
141+
142+
$this->_object = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
143+
'Magento\Captcha\Model\DefaultModel',
144+
[
145+
'session' => $this->session,
146+
'captchaData' => $this->_getHelperStub(),
147+
'resLogFactory' => $this->_resLogFactory,
148+
'formId' => 'user_create',
149+
'dateModel' => $this->dateModel
150+
]
135151
);
136152
}
137153

@@ -186,7 +202,8 @@ public function testIsCorrect()
186202
{
187203
self::$_defaultConfig['case_sensitive'] = '1';
188204
$this->assertFalse($this->_object->isCorrect('abcdef5'));
189-
$sessionData = ['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() + 600]];
205+
$sessionData = ['user_create_word' => ['data' => 'AbCdEf5', 'expires' => self::EXPIRATION_TIMESTAMP]];
206+
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::EXPIRATION_TIMESTAMP - 1);
190207
$this->_object->getSession()->setData($sessionData);
191208
self::$_defaultConfig['case_sensitive'] = '0';
192209
$this->assertTrue($this->_object->isCorrect('abcdef5'));
@@ -208,16 +225,8 @@ public function testGetImgSrc()
208225
*/
209226
public function testLogAttempt()
210227
{
211-
$captcha = new \Magento\Captcha\Model\DefaultModel(
212-
$this->session,
213-
$this->_getHelperStub(),
214-
$this->_resLogFactory,
215-
'user_create'
216-
);
217-
218-
$captcha->logAttempt('admin');
219-
220-
$this->assertEquals($captcha->getSession()->getData('user_create_show_captcha'), 1);
228+
$this->_object->logAttempt('admin');
229+
$this->assertEquals($this->_object->getSession()->getData('user_create_show_captcha'), 1);
221230
}
222231

223232
/**
@@ -226,9 +235,7 @@ public function testLogAttempt()
226235
public function testGetWord()
227236
{
228237
$this->assertEquals($this->_object->getWord(), 'AbCdEf5');
229-
$this->_object->getSession()->setData(
230-
['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() - 360]]
231-
);
238+
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::EXPIRATION_TIMESTAMP + 1);
232239
$this->assertNull($this->_object->getWord());
233240
}
234241

@@ -251,7 +258,7 @@ protected function _getSessionStub()
251258
);
252259
$session->expects($this->any())->method('isLoggedIn')->will($this->returnValue(false));
253260

254-
$session->setData(['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() + 600]]);
261+
$session->setData(['user_create_word' => ['data' => 'AbCdEf5', 'expires' => self::EXPIRATION_TIMESTAMP]]);
255262
return $session;
256263
}
257264

@@ -350,11 +357,14 @@ protected function _getStoreStub()
350357
*/
351358
public function testIsShownToLoggedInUser($expectedResult, $formId)
352359
{
353-
$captcha = new \Magento\Captcha\Model\DefaultModel(
354-
$this->session,
355-
$this->_getHelperStub(),
356-
$this->_resLogFactory,
357-
$formId
360+
$captcha = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
361+
'Magento\Captcha\Model\DefaultModel',
362+
[
363+
'session' => $this->session,
364+
'captchaData' => $this->_getHelperStub(),
365+
'resLogFactory' => $this->_resLogFactory,
366+
'formId' => $formId
367+
]
358368
);
359369
$this->assertEquals($expectedResult, $captcha->isShownToLoggedInUser());
360370
}

lib/internal/Magento/Framework/App/Test/Unit/View/Deployment/VersionTest.php

100644100755
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
class VersionTest extends \PHPUnit_Framework_TestCase
1212
{
13+
/**
14+
* Current timestamp for test
15+
*/
16+
const CURRENT_TIMESTAMP = 360;
17+
1318
/**
1419
* @var Version
1520
*/
@@ -25,21 +30,35 @@ class VersionTest extends \PHPUnit_Framework_TestCase
2530
*/
2631
private $versionStorage;
2732

33+
/**
34+
* @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $dateModel;
37+
2838
protected function setUp()
2939
{
3040
$this->appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
3141
$this->versionStorage = $this->getMock('Magento\Framework\App\View\Deployment\Version\StorageInterface');
32-
$this->object = new Version($this->appState, $this->versionStorage);
42+
$this->dateModel = $this->getMock('Magento\Framework\Stdlib\DateTime\DateTime', [], [], '', false);
43+
$this->object = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
44+
'Magento\Framework\App\View\Deployment\Version',
45+
[
46+
'appState' => $this->appState,
47+
'versionStorage' => $this->versionStorage,
48+
'dateModel' => $this->dateModel
49+
]
50+
);
3351
}
3452

3553
public function testGetValueDeveloperMode()
3654
{
55+
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::CURRENT_TIMESTAMP);
3756
$this->appState
3857
->expects($this->once())
3958
->method('getMode')
4059
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
4160
$this->versionStorage->expects($this->never())->method($this->anything());
42-
$this->assertEquals(time(), $this->object->getValue(), '', 5);
61+
$this->assertEquals(self::CURRENT_TIMESTAMP, $this->object->getValue());
4362
$this->object->getValue(); // Ensure computation occurs only once and result is cached in memory
4463
}
4564

@@ -79,8 +98,9 @@ public function testGetValueDefaultModeSaving()
7998
->expects($this->once())
8099
->method('load')
81100
->will($this->throwException($storageException));
82-
$this->versionStorage->expects($this->once())->method('save')->with($this->equalTo(time(), 5));
83-
$this->assertEquals(time(), $this->object->getValue());
101+
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::CURRENT_TIMESTAMP);
102+
$this->versionStorage->expects($this->once())->method('save')->with(self::CURRENT_TIMESTAMP);
103+
$this->assertEquals(self::CURRENT_TIMESTAMP, $this->object->getValue());
84104
$this->object->getValue(); // Ensure caching in memory
85105
}
86106
}

lib/internal/Magento/Framework/App/View/Deployment/Version.php

100644100755
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@ class Version
2626
*/
2727
private $cachedValue;
2828

29+
/**
30+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
31+
*/
32+
protected $dateModel;
33+
2934
/**
3035
* @param \Magento\Framework\App\State $appState
3136
* @param Version\StorageInterface $versionStorage
37+
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateModel
3238
*/
3339
public function __construct(
3440
\Magento\Framework\App\State $appState,
35-
\Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage
41+
\Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage,
42+
\Magento\Framework\Stdlib\DateTime\DateTime $dateModel
3643
) {
3744
$this->appState = $appState;
3845
$this->versionStorage = $versionStorage;
46+
$this->dateModel = $dateModel;
3947
}
4048

4149
/**
@@ -64,13 +72,13 @@ protected function readValue($appMode)
6472
try {
6573
$result = $this->versionStorage->load();
6674
} catch (\UnexpectedValueException $e) {
67-
$result = (new \DateTime())->getTimestamp();
75+
$result = $this->dateModel->gmtTimestamp();
6876
$this->versionStorage->save($result);
6977
}
7078
break;
7179

7280
case \Magento\Framework\App\State::MODE_DEVELOPER:
73-
$result = (new \DateTime())->getTimestamp();
81+
$result = $this->dateModel->gmtTimestamp();
7482
break;
7583

7684
default:

0 commit comments

Comments
 (0)