Skip to content

Commit 9e90322

Browse files
ENGCOM-6197: [User] Refactor and Unit Test Coverage for class \Magento\User\Model\Backend\Config\ObserverConfig #25327
2 parents 4468cdf + 4b74590 commit 9e90322

File tree

2 files changed

+173
-4
lines changed

2 files changed

+173
-4
lines changed

app/code/Magento/User/Model/Backend/Config/ObserverConfig.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,37 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\User\Model\Backend\Config;
810

911
/**
1012
* User backend observer helper class
13+
*
14+
* Class \Magento\User\Model\Backend\Config\ObserverConfig
1115
*/
1216
class ObserverConfig
1317
{
18+
/**
19+
* Config path for lockout threshold
20+
*/
21+
private const XML_ADMIN_SECURITY_LOCKOUT_THRESHOLD = 'admin/security/lockout_threshold';
22+
23+
/**
24+
* Config path for password change is forced or not
25+
*/
26+
private const XML_ADMIN_SECURITY_PASSWORD_IS_FORCED = 'admin/security/password_is_forced';
27+
28+
/**
29+
* Config path for password lifetime
30+
*/
31+
private const XML_ADMIN_SECURITY_PASSWORD_LIFETIME = 'admin/security/password_lifetime';
32+
33+
/**
34+
* Config path for maximum lockout failures
35+
*/
36+
private const XML_ADMIN_SECURITY_LOCKOUT_FAILURES = 'admin/security/lockout_failures';
37+
1438
/**
1539
* Backend configuration interface
1640
*
@@ -19,6 +43,8 @@ class ObserverConfig
1943
protected $backendConfig;
2044

2145
/**
46+
* Constructor
47+
*
2248
* @param \Magento\Backend\App\ConfigInterface $backendConfig
2349
*/
2450
public function __construct(
@@ -44,11 +70,12 @@ public function _isLatestPasswordExpired($latestPassword)
4470

4571
/**
4672
* Get admin lock threshold from configuration
73+
*
4774
* @return int
4875
*/
4976
public function getAdminLockThreshold()
5077
{
51-
return 60 * (int)$this->backendConfig->getValue('admin/security/lockout_threshold');
78+
return 60 * (int)$this->backendConfig->getValue(self::XML_ADMIN_SECURITY_LOCKOUT_THRESHOLD);
5279
}
5380

5481
/**
@@ -58,7 +85,7 @@ public function getAdminLockThreshold()
5885
*/
5986
public function isPasswordChangeForced()
6087
{
61-
return (bool)(int)$this->backendConfig->getValue('admin/security/password_is_forced');
88+
return (bool)(int)$this->backendConfig->getValue(self::XML_ADMIN_SECURITY_PASSWORD_IS_FORCED);
6289
}
6390

6491
/**
@@ -68,7 +95,7 @@ public function isPasswordChangeForced()
6895
*/
6996
public function getAdminPasswordLifetime()
7097
{
71-
return 86400 * (int)$this->backendConfig->getValue('admin/security/password_lifetime');
98+
return 86400 * (int)$this->backendConfig->getValue(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME);
7299
}
73100

74101
/**
@@ -78,6 +105,6 @@ public function getAdminPasswordLifetime()
78105
*/
79106
public function getMaxFailures()
80107
{
81-
return (int)$this->backendConfig->getValue('admin/security/lockout_failures');
108+
return (int)$this->backendConfig->getValue(self::XML_ADMIN_SECURITY_LOCKOUT_FAILURES);
82109
}
83110
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\User\Test\Unit\Model\Backend\Config;
10+
11+
use Magento\User\Model\Backend\Config\ObserverConfig;
12+
use Magento\Backend\App\ConfigInterface;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
14+
15+
/**
16+
* Unit Test for \Magento\User\Model\Backend\Config\ObserverConfig class
17+
*
18+
* Class \Magento\User\Test\Unit\Model\Backend\Config\ObserverConfigTest
19+
*/
20+
class ObserverConfigTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* Config path for lockout threshold
24+
*/
25+
private const XML_ADMIN_SECURITY_LOCKOUT_THRESHOLD = 'admin/security/lockout_threshold';
26+
27+
/**
28+
* Config path for password change is forced or not
29+
*/
30+
private const XML_ADMIN_SECURITY_PASSWORD_IS_FORCED = 'admin/security/password_is_forced';
31+
32+
/**
33+
* Config path for password lifetime
34+
*/
35+
private const XML_ADMIN_SECURITY_PASSWORD_LIFETIME = 'admin/security/password_lifetime';
36+
37+
/**
38+
* Config path for maximum lockout failures
39+
*/
40+
private const XML_ADMIN_SECURITY_LOCKOUT_FAILURES = 'admin/security/lockout_failures';
41+
42+
/** @var ObserverConfig */
43+
private $model;
44+
45+
/**
46+
* @var \PHPUnit_Framework_MockObject_MockObject|ConfigInterface
47+
*/
48+
private $backendConfigMock;
49+
50+
/**
51+
* Set environment for test
52+
*/
53+
protected function setUp()
54+
{
55+
$this->backendConfigMock = $this->createMock(ConfigInterface::class);
56+
57+
$objectManager = new ObjectManagerHelper($this);
58+
$this->model = $objectManager->getObject(
59+
ObserverConfig::class,
60+
[
61+
'backendConfig' => $this->backendConfigMock
62+
]
63+
);
64+
}
65+
66+
/**
67+
* Test when admin password lifetime = 0 days
68+
*/
69+
public function testIsLatestPasswordExpiredWhenNoAdminLifeTime()
70+
{
71+
$this->backendConfigMock->expects(self::any())->method('getValue')
72+
->with(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME)
73+
->willReturn('0');
74+
$this->assertEquals(false, $this->model->_isLatestPasswordExpired([]));
75+
}
76+
77+
/**
78+
* Test when admin password lifetime = 2 days
79+
*/
80+
public function testIsLatestPasswordExpiredWhenHasAdminLifeTime()
81+
{
82+
$this->backendConfigMock->expects(self::any())->method('getValue')
83+
->with(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME)
84+
->willReturn('2');
85+
$this->assertEquals(true, $this->model->_isLatestPasswordExpired(['last_updated' => 1571428052]));
86+
}
87+
88+
/**
89+
* Test when security lockout threshold = 100 minutes
90+
*/
91+
public function testGetAdminLockThreshold()
92+
{
93+
$this->backendConfigMock->expects(self::any())->method('getValue')
94+
->with(self::XML_ADMIN_SECURITY_LOCKOUT_THRESHOLD)
95+
->willReturn('100');
96+
$this->assertEquals(6000, $this->model->getAdminLockThreshold());
97+
}
98+
99+
/**
100+
* Test when password change force is true
101+
*/
102+
public function testIsPasswordChangeForcedTrue()
103+
{
104+
$this->backendConfigMock->expects(self::any())->method('getValue')
105+
->with(self::XML_ADMIN_SECURITY_PASSWORD_IS_FORCED)
106+
->willReturn('1');
107+
$this->assertEquals(true, $this->model->isPasswordChangeForced());
108+
}
109+
110+
/**
111+
* Test when password change force is false
112+
*/
113+
public function testIsPasswordChangeForcedFalse()
114+
{
115+
$this->backendConfigMock->expects(self::any())->method('getValue')
116+
->with(self::XML_ADMIN_SECURITY_PASSWORD_IS_FORCED)
117+
->willReturn('0');
118+
$this->assertEquals(false, $this->model->isPasswordChangeForced());
119+
}
120+
121+
/**
122+
* Test when admin password lifetime = 2 days
123+
*/
124+
public function testGetAdminPasswordLifetime()
125+
{
126+
$this->backendConfigMock->expects(self::any())->method('getValue')
127+
->with(self::XML_ADMIN_SECURITY_PASSWORD_LIFETIME)
128+
->willReturn('2');
129+
$this->assertEquals(172800, $this->model->getAdminPasswordLifetime());
130+
}
131+
132+
/**
133+
* Test when max failures = 5 (times)
134+
*/
135+
public function testGetMaxFailures()
136+
{
137+
$this->backendConfigMock->expects(self::any())->method('getValue')
138+
->with(self::XML_ADMIN_SECURITY_LOCKOUT_FAILURES)
139+
->willReturn('5');
140+
$this->assertEquals(5, $this->model->getMaxFailures());
141+
}
142+
}

0 commit comments

Comments
 (0)