5
5
*/
6
6
namespace Magento \Customer \Test \Unit \Model ;
7
7
8
+ use Magento \Backend \App \ConfigInterface ;
8
9
use Magento \Customer \Api \CustomerRepositoryInterface ;
9
10
use Magento \Customer \Api \Data \CustomerInterface ;
10
11
use Magento \Customer \Model \Authentication ;
11
12
use Magento \Customer \Model \AccountManagement ;
13
+ use Magento \Customer \Model \CustomerRegistry ;
14
+ use Magento \Customer \Model \Data \CustomerSecure ;
12
15
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
13
16
14
17
class AuthenticationTest extends \PHPUnit_Framework_TestCase
15
18
{
16
19
/**
17
- * Backend configuration interface
18
- *
19
- * @var \Magento\Backend\App\ConfigInterface
20
+ * @var \Magento\Backend\App\ConfigInterface | \PHPUnit_Framework_MockObject_MockObject
20
21
*/
21
22
private $ backendConfigMock ;
22
23
23
24
/**
24
- * @var \Magento\Customer\Model\CustomerRegistry
25
+ * @var \Magento\Customer\Model\CustomerRegistry | \PHPUnit_Framework_MockObject_MockObject
25
26
*/
26
27
private $ customerRegistryMock ;
27
28
28
29
/**
29
- * @var \Magento\Framework\Encryption\EncryptorInterface
30
+ * @var \Magento\Framework\Encryption\EncryptorInterface | \PHPUnit_Framework_MockObject_MockObject
30
31
*/
31
32
protected $ encryptorMock ;
32
33
33
34
/**
34
- * @var CustomerRepositoryInterface
35
+ * @var CustomerRepositoryInterface | \PHPUnit_Framework_MockObject_MockObject
35
36
*/
36
37
private $ customerRepositoryMock ;
37
38
38
39
/**
39
- * @var \Magento\Customer\Model\Data\CustomerSecure
40
+ * @var \Magento\Customer\Model\Data\CustomerSecure | \PHPUnit_Framework_MockObject_MockObject
40
41
*/
41
- private $ customerSecure ;
42
+ private $ customerSecureMock ;
42
43
43
44
/**
44
45
* @var \Magento\Customer\Model\Authentication
@@ -47,12 +48,12 @@ class AuthenticationTest extends \PHPUnit_Framework_TestCase
47
48
48
49
protected function setUp ()
49
50
{
50
- $ this ->backendConfigMock = $ this ->getMockBuilder (' Magento\Backend\App\ ConfigInterface' )
51
+ $ this ->backendConfigMock = $ this ->getMockBuilder (ConfigInterface::class )
51
52
->disableOriginalConstructor ()
52
53
->setMethods (['getValue ' ])
53
54
->getMockForAbstractClass ();
54
55
$ this ->customerRegistryMock = $ this ->getMock (
55
- ' Magento\Customer\Model\ CustomerRegistry' ,
56
+ CustomerRegistry::class ,
56
57
['retrieveSecureData ' , 'retrieve ' ],
57
58
[],
58
59
'' ,
@@ -64,14 +65,15 @@ protected function setUp()
64
65
$ this ->encryptorMock = $ this ->getMockBuilder (\Magento \Framework \Encryption \EncryptorInterface::class)
65
66
->disableOriginalConstructor ()
66
67
->getMock ();
67
- $ this ->customerSecure = $ this ->getMock (
68
- ' Magento\Customer\Model\Data\ CustomerSecure' ,
68
+ $ this ->customerSecureMock = $ this ->getMock (
69
+ CustomerSecure::class ,
69
70
[
70
71
'getId ' ,
71
72
'getPasswordHash ' ,
72
73
'isCustomerLocked ' ,
73
74
'getFailuresNum ' ,
74
75
'getFirstFailure ' ,
76
+ 'getLockExpires ' ,
75
77
'setFirstFailure ' ,
76
78
'setFailuresNum ' ,
77
79
'setLockExpires '
@@ -93,10 +95,7 @@ protected function setUp()
93
95
);
94
96
}
95
97
96
- /**
97
- * @return void
98
- */
99
- public function testLockingIsDisabled ()
98
+ public function testProcessAuthenticationFailureLockingIsDisabled ()
100
99
{
101
100
$ customerId = 1 ;
102
101
$ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
@@ -109,14 +108,11 @@ public function testLockingIsDisabled()
109
108
$ this ->customerRegistryMock ->expects ($ this ->once ())
110
109
->method ('retrieveSecureData ' )
111
110
->with ($ customerId )
112
- ->willReturn ($ this ->customerSecure );
111
+ ->willReturn ($ this ->customerSecureMock );
113
112
$ this ->authentication ->processAuthenticationFailure ($ customerId );
114
113
}
115
114
116
- /**
117
- * @return void
118
- */
119
- public function testCustomerFailedFirstAttempt ()
115
+ public function testProcessAuthenticationFailureFirstAttempt ()
120
116
{
121
117
$ customerId = 1 ;
122
118
$ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
@@ -130,7 +126,7 @@ public function testCustomerFailedFirstAttempt()
130
126
$ this ->customerRegistryMock ->expects ($ this ->once ())
131
127
->method ('retrieveSecureData ' )
132
128
->with ($ customerId )
133
- ->willReturn ($ this ->customerSecure );
129
+ ->willReturn ($ this ->customerSecureMock );
134
130
$ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
135
131
->disableOriginalConstructor ()
136
132
->getMock ();
@@ -142,18 +138,60 @@ public function testCustomerFailedFirstAttempt()
142
138
->method ('save ' )
143
139
->with ($ customerMock );
144
140
145
- $ this ->customerSecure ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (0 );
146
- $ this ->customerSecure ->expects ($ this ->once ())->method ('getFirstFailure ' )->willReturn (0 );
147
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setFirstFailure ' );
148
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setFailuresNum ' );
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 ' );
149
145
150
146
$ this ->authentication ->processAuthenticationFailure ($ customerId );
151
147
}
152
148
153
- /**
154
- * @return void
155
- */
156
- public function testCustomerHasFailedMaxNumberOfAttempts ()
149
+ public function testProcessAuthenticationFailureLockExpired ()
150
+ {
151
+ $ customerId = 1 ;
152
+ $ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
153
+ ->method ('getValue ' )
154
+ ->withConsecutive (
155
+ [\Magento \Customer \Model \Authentication::LOCKOUT_THRESHOLD_PATH ],
156
+ [\Magento \Customer \Model \Authentication::MAX_FAILURES_PATH ]
157
+ )
158
+ ->willReturnOnConsecutiveCalls (10 , 5 );
159
+
160
+ $ this ->customerRegistryMock ->expects ($ this ->once ())
161
+ ->method ('retrieveSecureData ' )
162
+ ->with ($ customerId )
163
+ ->willReturn ($ this ->customerSecureMock );
164
+ $ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
165
+ ->disableOriginalConstructor ()
166
+ ->getMock ();
167
+ $ this ->customerRepositoryMock ->expects ($ this ->once ())
168
+ ->method ('getById ' )
169
+ ->with ($ customerId )
170
+ ->willReturn ($ customerMock );
171
+ $ this ->customerRepositoryMock ->expects ($ this ->once ())
172
+ ->method ('save ' )
173
+ ->with ($ customerMock );
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 );
181
+ $ this ->customerSecureMock ->expects ($ this ->once ())
182
+ ->method ('getFirstFailure ' )
183
+ ->willReturn ($ firstFailureDate );
184
+ $ this ->customerSecureMock ->expects ($ this ->once ())
185
+ ->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 );
190
+
191
+ $ this ->authentication ->processAuthenticationFailure ($ customerId );
192
+ }
193
+
194
+ public function testProcessAuthenticationFailureMaxNumberOfAttempts ()
157
195
{
158
196
$ customerId = 1 ;
159
197
$ date = new \DateTime ();
@@ -170,7 +208,7 @@ public function testCustomerHasFailedMaxNumberOfAttempts()
170
208
$ this ->customerRegistryMock ->expects ($ this ->once ())
171
209
->method ('retrieveSecureData ' )
172
210
->with ($ customerId )
173
- ->willReturn ($ this ->customerSecure );
211
+ ->willReturn ($ this ->customerSecureMock );
174
212
$ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
175
213
->disableOriginalConstructor ()
176
214
->getMock ();
@@ -182,26 +220,23 @@ public function testCustomerHasFailedMaxNumberOfAttempts()
182
220
->method ('save ' )
183
221
->with ($ customerMock );
184
222
185
- $ this ->customerSecure ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (5 );
186
- $ this ->customerSecure ->expects ($ this ->once ())
223
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (4 );
224
+ $ this ->customerSecureMock ->expects ($ this ->once ())
187
225
->method ('getFirstFailure ' )
188
226
->willReturn ($ formattedDate );
189
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setLockExpires ' );
190
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setFailuresNum ' );
227
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setLockExpires ' );
228
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFailuresNum ' )-> with ( 5 );
191
229
192
230
$ this ->authentication ->processAuthenticationFailure ($ customerId );
193
231
}
194
232
195
- /**
196
- * @return void
197
- */
198
- public function testProcessUnlockData ()
233
+ public function testUnlock ()
199
234
{
200
235
$ customerId = 1 ;
201
236
$ this ->customerRegistryMock ->expects ($ this ->once ())
202
237
->method ('retrieveSecureData ' )
203
238
->with ($ customerId )
204
- ->willReturn ($ this ->customerSecure );
239
+ ->willReturn ($ this ->customerSecureMock );
205
240
$ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
206
241
->disableOriginalConstructor ()
207
242
->getMock ();
@@ -212,9 +247,9 @@ public function testProcessUnlockData()
212
247
$ this ->customerRepositoryMock ->expects ($ this ->once ())
213
248
->method ('save ' )
214
249
->with ($ customerMock );
215
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setFailuresNum ' )->with (0 );
216
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setFirstFailure ' )->with (null );
217
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setLockExpires ' )->with (null );
250
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFailuresNum ' )->with (0 );
251
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFirstFailure ' )->with (null );
252
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setLockExpires ' )->with (null );
218
253
$ this ->authentication ->unlock ($ customerId );
219
254
}
220
255
@@ -229,7 +264,7 @@ public function validatePasswordAndLockStatusDataProvider()
229
264
/**
230
265
* @return void
231
266
*/
232
- public function testCheckIfLocked ()
267
+ public function testIsLocked ()
233
268
{
234
269
$ customerId = 7 ;
235
270
@@ -250,7 +285,7 @@ public function testCheckIfLocked()
250
285
* @param bool $result
251
286
* @dataProvider validateCustomerPassword
252
287
*/
253
- public function testValidateCustomerPassword ($ result )
288
+ public function testAuthenticate ($ result )
254
289
{
255
290
$ customerId = 7 ;
256
291
$ password = '1234567 ' ;
@@ -267,18 +302,18 @@ public function testValidateCustomerPassword($result)
267
302
->method ('getById ' )
268
303
->willReturn ($ customerMock );
269
304
270
- $ this ->customerSecure ->expects ($ this ->any ())
305
+ $ this ->customerSecureMock ->expects ($ this ->any ())
271
306
->method ('getId ' )
272
307
->willReturn ($ customerId );
273
308
274
- $ this ->customerSecure ->expects ($ this ->once ())
309
+ $ this ->customerSecureMock ->expects ($ this ->once ())
275
310
->method ('getPasswordHash ' )
276
311
->willReturn ($ hash );
277
312
278
313
$ this ->customerRegistryMock ->expects ($ this ->any ())
279
314
->method ('retrieveSecureData ' )
280
315
->with ($ customerId )
281
- ->willReturn ($ this ->customerSecure );
316
+ ->willReturn ($ this ->customerSecureMock );
282
317
283
318
$ this ->encryptorMock ->expects ($ this ->once ())
284
319
->method ('validateHash ' )
@@ -295,14 +330,14 @@ public function testValidateCustomerPassword($result)
295
330
[\Magento \Customer \Model \Authentication::MAX_FAILURES_PATH ]
296
331
)
297
332
->willReturnOnConsecutiveCalls (1 , 1 );
298
- $ this ->customerSecure ->expects ($ this ->once ())
333
+ $ this ->customerSecureMock ->expects ($ this ->once ())
299
334
->method ('isCustomerLocked ' )
300
335
->willReturn (false );
301
336
302
337
$ this ->customerRegistryMock ->expects ($ this ->once ())
303
338
->method ('retrieve ' )
304
339
->with ($ customerId )
305
- ->willReturn ($ this ->customerSecure );
340
+ ->willReturn ($ this ->customerSecureMock );
306
341
307
342
$ this ->customerRepositoryMock ->expects ($ this ->once ())
308
343
->method ('save ' )
0 commit comments