12
12
use Magento \Customer \Model \AccountManagement ;
13
13
use Magento \Customer \Model \CustomerRegistry ;
14
14
use Magento \Customer \Model \Data \CustomerSecure ;
15
+ use Magento \Framework \Stdlib \DateTime ;
15
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
16
17
17
18
class AuthenticationTest extends \PHPUnit_Framework_TestCase
@@ -46,6 +47,11 @@ class AuthenticationTest extends \PHPUnit_Framework_TestCase
46
47
*/
47
48
private $ authentication ;
48
49
50
+ /**
51
+ * @var DateTime
52
+ */
53
+ private $ dateTimeMock ;
54
+
49
55
protected function setUp ()
50
56
{
51
57
$ this ->backendConfigMock = $ this ->getMockBuilder (ConfigInterface::class)
@@ -65,6 +71,12 @@ protected function setUp()
65
71
$ this ->encryptorMock = $ this ->getMockBuilder (\Magento \Framework \Encryption \EncryptorInterface::class)
66
72
->disableOriginalConstructor ()
67
73
->getMock ();
74
+ $ this ->dateTimeMock = $ this ->getMockBuilder (DateTime::class)
75
+ ->disableOriginalConstructor ()
76
+ ->getMock ();
77
+ $ this ->dateTimeMock ->expects ($ this ->any ())
78
+ ->method ('formatDate ' )
79
+ ->willReturn ('formattedDate ' );
68
80
$ this ->customerSecureMock = $ this ->getMock (
69
81
CustomerSecure::class,
70
82
[
@@ -82,6 +94,7 @@ protected function setUp()
82
94
'' ,
83
95
false
84
96
);
97
+
85
98
$ objectManagerHelper = new ObjectManagerHelper ($ this );
86
99
87
100
$ this ->authentication = $ objectManagerHelper ->getObject (
@@ -91,6 +104,7 @@ protected function setUp()
91
104
'backendConfig ' => $ this ->backendConfigMock ,
92
105
'customerRepository ' => $ this ->customerRepositoryMock ,
93
106
'encryptor ' => $ this ->encryptorMock ,
107
+ 'dateTime ' => $ this ->dateTimeMock ,
94
108
]
95
109
);
96
110
}
@@ -112,42 +126,28 @@ public function testProcessAuthenticationFailureLockingIsDisabled()
112
126
$ this ->authentication ->processAuthenticationFailure ($ customerId );
113
127
}
114
128
115
- public function testProcessAuthenticationFailureFirstAttempt ()
116
- {
117
- $ customerId = 1 ;
118
- $ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
119
- ->method ('getValue ' )
120
- ->withConsecutive (
121
- [\Magento \Customer \Model \Authentication::LOCKOUT_THRESHOLD_PATH ],
122
- [\Magento \Customer \Model \Authentication::MAX_FAILURES_PATH ]
123
- )
124
- ->willReturnOnConsecutiveCalls (10 , 5 );
125
-
126
- $ this ->customerRegistryMock ->expects ($ this ->once ())
127
- ->method ('retrieveSecureData ' )
128
- ->with ($ customerId )
129
- ->willReturn ($ this ->customerSecureMock );
130
- $ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
131
- ->disableOriginalConstructor ()
132
- ->getMock ();
133
- $ this ->customerRepositoryMock ->expects ($ this ->once ())
134
- ->method ('getById ' )
135
- ->with ($ customerId )
136
- ->willReturn ($ customerMock );
137
- $ this ->customerRepositoryMock ->expects ($ this ->once ())
138
- ->method ('save ' )
139
- ->with ($ customerMock );
140
-
141
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (0 );
142
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFirstFailure ' )->willReturn (0 );
143
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFirstFailure ' );
144
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFailuresNum ' );
145
-
146
- $ this ->authentication ->processAuthenticationFailure ($ customerId );
147
- }
148
-
149
- public function testProcessAuthenticationFailureLockExpired ()
150
- {
129
+ /**
130
+ * @param int $failureNum
131
+ * @param string $firstFailure
132
+ * @param string $lockExpires
133
+ * @param int $setFailureNumCallCtr
134
+ * @param int $setFailureNumValue
135
+ * @param int $setFirstFailureCallCtr
136
+ * @param int $setFirstFailureValue
137
+ * @param int $setLockExpiresCallCtr
138
+ * @param int $setLockExpiresValue
139
+ * @dataProvider processAuthenticationFailureDataProvider
140
+ */
141
+ public function testProcessAuthenticationFailureFirstAttempt (
142
+ $ failureNum ,
143
+ $ firstFailure ,
144
+ $ lockExpires ,
145
+ $ setFailureNumCallCtr ,
146
+ $ setFailureNumValue ,
147
+ $ setFirstFailureCallCtr ,
148
+ $ setLockExpiresCallCtr ,
149
+ $ setLockExpiresValue
150
+ ) {
151
151
$ customerId = 1 ;
152
152
$ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
153
153
->method ('getValue ' )
@@ -172,62 +172,32 @@ public function testProcessAuthenticationFailureLockExpired()
172
172
->method ('save ' )
173
173
->with ($ customerMock );
174
174
175
- $ date = new \DateTime ();
176
- $ date ->modify ('-100 second ' );
177
- $ lockExpiresDate = $ date ->format ('Y-m-d H:i:s ' );
178
- $ date ->modify ('-400 second ' );
179
- $ firstFailureDate = $ date ->format ('Y-m-d H:i:s ' );
180
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (5 );
175
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn ($ failureNum );
181
176
$ this ->customerSecureMock ->expects ($ this ->once ())
182
177
->method ('getFirstFailure ' )
183
- ->willReturn ($ firstFailureDate );
178
+ ->willReturn ($ firstFailure ? ( new \ DateTime ())-> modify ( $ firstFailure )-> format ( ' Y-m-d H:i:s ' ) : null );
184
179
$ this ->customerSecureMock ->expects ($ this ->once ())
185
180
->method ('getLockExpires ' )
186
- ->willReturn ($ lockExpiresDate );
187
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFirstFailure ' );
188
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setLockExpires ' )->with (null );
189
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFailuresNum ' )->with (1 );
181
+ ->willReturn ($ lockExpires ? (new \DateTime ())->modify ($ lockExpires )->format ('Y-m-d H:i:s ' ) : null );
182
+ $ this ->customerSecureMock ->expects ($ this ->exactly ($ setFirstFailureCallCtr ))->method ('setFirstFailure ' );
183
+ $ this ->customerSecureMock ->expects ($ this ->exactly ($ setFailureNumCallCtr ))
184
+ ->method ('setFailuresNum ' )
185
+ ->with ($ setFailureNumValue );
186
+ $ this ->customerSecureMock ->expects ($ this ->exactly ($ setLockExpiresCallCtr ))
187
+ ->method ('setLockExpires ' )
188
+ ->with ($ setLockExpiresValue );
190
189
191
190
$ this ->authentication ->processAuthenticationFailure ($ customerId );
192
191
}
193
192
194
- public function testProcessAuthenticationFailureMaxNumberOfAttempts ()
193
+ public function processAuthenticationFailureDataProvider ()
195
194
{
196
- $ customerId = 1 ;
197
- $ date = new \DateTime ();
198
- $ date ->modify ('-500 second ' );
199
- $ formattedDate = $ date ->format ('Y-m-d H:i:s ' );
200
- $ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
201
- ->method ('getValue ' )
202
- ->withConsecutive (
203
- [\Magento \Customer \Model \Authentication::LOCKOUT_THRESHOLD_PATH ],
204
- [\Magento \Customer \Model \Authentication::MAX_FAILURES_PATH ]
205
- )
206
- ->willReturnOnConsecutiveCalls (10 , 5 );
207
-
208
- $ this ->customerRegistryMock ->expects ($ this ->once ())
209
- ->method ('retrieveSecureData ' )
210
- ->with ($ customerId )
211
- ->willReturn ($ this ->customerSecureMock );
212
- $ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
213
- ->disableOriginalConstructor ()
214
- ->getMock ();
215
- $ this ->customerRepositoryMock ->expects ($ this ->once ())
216
- ->method ('getById ' )
217
- ->with ($ customerId )
218
- ->willReturn ($ customerMock );
219
- $ this ->customerRepositoryMock ->expects ($ this ->once ())
220
- ->method ('save ' )
221
- ->with ($ customerMock );
222
-
223
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (4 );
224
- $ this ->customerSecureMock ->expects ($ this ->once ())
225
- ->method ('getFirstFailure ' )
226
- ->willReturn ($ formattedDate );
227
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setLockExpires ' );
228
- $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFailuresNum ' )->with (5 );
229
-
230
- $ this ->authentication ->processAuthenticationFailure ($ customerId );
195
+ return [
196
+ 'first attempt ' => [0 , null , null , 1 , 1 , 1 , 1 , null ],
197
+ 'not locked ' => [3 , '-400 second ' , null , 1 , 4 , 0 , 0 , null ],
198
+ 'lock expired ' => [5 , '-400 second ' , '-100 second ' , 1 , 1 , 1 , 1 , null ],
199
+ 'max attempt ' => [4 , '-400 second ' , null , 1 , 5 , 0 , 1 , 'formattedDate ' ],
200
+ ];
231
201
}
232
202
233
203
public function testUnlock ()
0 commit comments