Skip to content

Commit 907376b

Browse files
author
vpaladiychuk
committed
MAGETWO-35534: Unit Tests code coverage failures - Captcha and View
1 parent f26979e commit 907376b

File tree

4 files changed

+36
-77
lines changed

4 files changed

+36
-77
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,22 @@ 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-
7671
/**
7772
* @param \Magento\Framework\Session\SessionManagerInterface $session
7873
* @param \Magento\Captcha\Helper\Data $captchaData
7974
* @param \Magento\Captcha\Model\Resource\LogFactory $resLogFactory
80-
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateModel
8175
* @param string $formId
8276
*/
8377
public function __construct(
8478
\Magento\Framework\Session\SessionManagerInterface $session,
8579
\Magento\Captcha\Helper\Data $captchaData,
8680
\Magento\Captcha\Model\Resource\LogFactory $resLogFactory,
87-
\Magento\Framework\Stdlib\DateTime\DateTime $dateModel,
8881
$formId
8982
) {
9083
$this->_session = $session;
9184
$this->_captchaData = $captchaData;
9285
$this->_resLogFactory = $resLogFactory;
9386
$this->_formId = $formId;
94-
$this->dateModel = $dateModel;
9587
}
9688

9789
/**
@@ -460,7 +452,7 @@ protected function _getTargetForms()
460452
public function getWord()
461453
{
462454
$sessionData = $this->_session->getData($this->_getFormIdKey(self::SESSION_WORD));
463-
return $this->dateModel->gmtTimestamp() < $sessionData['expires'] ? $sessionData['data'] : null;
455+
return time() < $sessionData['expires'] ? $sessionData['data'] : null;
464456
}
465457

466458
/**
@@ -473,7 +465,7 @@ protected function _setWord($word)
473465
{
474466
$this->_session->setData(
475467
$this->_getFormIdKey(self::SESSION_WORD),
476-
['data' => $word, 'expires' => $this->dateModel->gmtTimestamp() + $this->getTimeout()]
468+
['data' => $word, 'expires' => time() + $this->getTimeout()]
477469
);
478470
$this->_word = $word;
479471
return $this;

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

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class DefaultTest extends \PHPUnit_Framework_TestCase
99
{
1010
/**
11-
* Expiration date
11+
* Expiration frame
1212
*/
13-
const EXPIRATION_TIMESTAMP = 86400;
13+
const EXPIRE_FRAME = 86400;
1414

1515
/**
1616
* Captcha default config data
@@ -79,11 +79,6 @@ class DefaultTest extends \PHPUnit_Framework_TestCase
7979
*/
8080
protected $_resLogFactory;
8181

82-
/**
83-
* @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
84-
*/
85-
protected $dateModel;
86-
8782
/**
8883
* Sets up the fixture, for example, opens a network connection.
8984
* This method is called before a test is executed.
@@ -137,17 +132,11 @@ protected function setUp()
137132
$this->returnValue($this->_getResourceModelStub())
138133
);
139134

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-
]
135+
$this->_object = new \Magento\Captcha\Model\DefaultModel(
136+
$this->session,
137+
$this->_getHelperStub(),
138+
$this->_resLogFactory,
139+
'user_create'
151140
);
152141
}
153142

@@ -202,8 +191,7 @@ public function testIsCorrect()
202191
{
203192
self::$_defaultConfig['case_sensitive'] = '1';
204193
$this->assertFalse($this->_object->isCorrect('abcdef5'));
205-
$sessionData = ['user_create_word' => ['data' => 'AbCdEf5', 'expires' => self::EXPIRATION_TIMESTAMP]];
206-
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::EXPIRATION_TIMESTAMP - 1);
194+
$sessionData = ['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() + self::EXPIRE_FRAME]];
207195
$this->_object->getSession()->setData($sessionData);
208196
self::$_defaultConfig['case_sensitive'] = '0';
209197
$this->assertTrue($this->_object->isCorrect('abcdef5'));
@@ -225,8 +213,16 @@ public function testGetImgSrc()
225213
*/
226214
public function testLogAttempt()
227215
{
228-
$this->_object->logAttempt('admin');
229-
$this->assertEquals($this->_object->getSession()->getData('user_create_show_captcha'), 1);
216+
$captcha = new \Magento\Captcha\Model\DefaultModel(
217+
$this->session,
218+
$this->_getHelperStub(),
219+
$this->_resLogFactory,
220+
'user_create'
221+
);
222+
223+
$captcha->logAttempt('admin');
224+
225+
$this->assertEquals($captcha->getSession()->getData('user_create_show_captcha'), 1);
230226
}
231227

232228
/**
@@ -235,7 +231,9 @@ public function testLogAttempt()
235231
public function testGetWord()
236232
{
237233
$this->assertEquals($this->_object->getWord(), 'AbCdEf5');
238-
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::EXPIRATION_TIMESTAMP + 1);
234+
$this->_object->getSession()->setData(
235+
['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() - 360]]
236+
);
239237
$this->assertNull($this->_object->getWord());
240238
}
241239

@@ -258,7 +256,7 @@ protected function _getSessionStub()
258256
);
259257
$session->expects($this->any())->method('isLoggedIn')->will($this->returnValue(false));
260258

261-
$session->setData(['user_create_word' => ['data' => 'AbCdEf5', 'expires' => self::EXPIRATION_TIMESTAMP]]);
259+
$session->setData(['user_create_word' => ['data' => 'AbCdEf5', 'expires' => time() + self::EXPIRE_FRAME]]);
262260
return $session;
263261
}
264262

@@ -357,14 +355,11 @@ protected function _getStoreStub()
357355
*/
358356
public function testIsShownToLoggedInUser($expectedResult, $formId)
359357
{
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-
]
358+
$captcha = new \Magento\Captcha\Model\DefaultModel(
359+
$this->session,
360+
$this->_getHelperStub(),
361+
$this->_resLogFactory,
362+
$formId
368363
);
369364
$this->assertEquals($expectedResult, $captcha->isShownToLoggedInUser());
370365
}

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

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

1212
class VersionTest extends \PHPUnit_Framework_TestCase
1313
{
14-
/**
15-
* Current timestamp for test
16-
*/
17-
const CURRENT_TIMESTAMP = 360;
18-
1914
/**
2015
* @var Version
2116
*/
@@ -31,35 +26,21 @@ class VersionTest extends \PHPUnit_Framework_TestCase
3126
*/
3227
private $versionStorage;
3328

34-
/**
35-
* @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
36-
*/
37-
protected $dateModel;
38-
3929
protected function setUp()
4030
{
4131
$this->appState = $this->getMock('Magento\Framework\App\State', [], [], '', false);
4232
$this->versionStorage = $this->getMock('Magento\Framework\App\View\Deployment\Version\StorageInterface');
43-
$this->dateModel = $this->getMock('Magento\Framework\Stdlib\DateTime\DateTime', [], [], '', false);
44-
$this->object = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
45-
'Magento\Framework\App\View\Deployment\Version',
46-
[
47-
'appState' => $this->appState,
48-
'versionStorage' => $this->versionStorage,
49-
'dateModel' => $this->dateModel
50-
]
51-
);
33+
$this->object = new Version($this->appState, $this->versionStorage);
5234
}
5335

5436
public function testGetValueDeveloperMode()
5537
{
56-
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::CURRENT_TIMESTAMP);
5738
$this->appState
5839
->expects($this->once())
5940
->method('getMode')
6041
->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
6142
$this->versionStorage->expects($this->never())->method($this->anything());
62-
$this->assertEquals(self::CURRENT_TIMESTAMP, $this->object->getValue());
43+
$this->assertInternalType('integer', $this->object->getValue());
6344
$this->object->getValue(); // Ensure computation occurs only once and result is cached in memory
6445
}
6546

@@ -99,9 +80,8 @@ public function testGetValueDefaultModeSaving()
9980
->expects($this->once())
10081
->method('load')
10182
->will($this->throwException($storageException));
102-
$this->dateModel->expects($this->once())->method('gmtTimestamp')->willReturn(self::CURRENT_TIMESTAMP);
103-
$this->versionStorage->expects($this->once())->method('save')->with(self::CURRENT_TIMESTAMP);
104-
$this->assertEquals(self::CURRENT_TIMESTAMP, $this->object->getValue());
83+
$this->versionStorage->expects($this->once())->method('save')->with($this->equalTo(time(), 5));
84+
$this->assertEquals(time(), $this->object->getValue());
10585
$this->object->getValue(); // Ensure caching in memory
10686
}
10787
}

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

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

29-
/**
30-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
31-
*/
32-
protected $dateModel;
33-
3429
/**
3530
* @param \Magento\Framework\App\State $appState
3631
* @param Version\StorageInterface $versionStorage
37-
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateModel
3832
*/
3933
public function __construct(
4034
\Magento\Framework\App\State $appState,
41-
\Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage,
42-
\Magento\Framework\Stdlib\DateTime\DateTime $dateModel
35+
\Magento\Framework\App\View\Deployment\Version\StorageInterface $versionStorage
4336
) {
4437
$this->appState = $appState;
4538
$this->versionStorage = $versionStorage;
46-
$this->dateModel = $dateModel;
4739
}
4840

4941
/**
@@ -72,13 +64,13 @@ protected function readValue($appMode)
7264
try {
7365
$result = $this->versionStorage->load();
7466
} catch (\UnexpectedValueException $e) {
75-
$result = $this->dateModel->gmtTimestamp();
67+
$result = (new \DateTime())->getTimestamp();
7668
$this->versionStorage->save($result);
7769
}
7870
break;
7971

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

8476
default:

0 commit comments

Comments
 (0)