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 ;
15
+ use Magento \Framework \Stdlib \DateTime ;
12
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
13
17
18
+ /**
19
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
+ */
14
21
class AuthenticationTest extends \PHPUnit_Framework_TestCase
15
22
{
16
23
/**
17
- * Backend configuration interface
18
- *
19
- * @var \Magento\Backend\App\ConfigInterface
24
+ * @var \Magento\Backend\App\ConfigInterface | \PHPUnit_Framework_MockObject_MockObject
20
25
*/
21
26
private $ backendConfigMock ;
22
27
23
28
/**
24
- * @var \Magento\Customer\Model\CustomerRegistry
29
+ * @var \Magento\Customer\Model\CustomerRegistry | \PHPUnit_Framework_MockObject_MockObject
25
30
*/
26
31
private $ customerRegistryMock ;
27
32
28
33
/**
29
- * @var \Magento\Framework\Encryption\EncryptorInterface
34
+ * @var \Magento\Framework\Encryption\EncryptorInterface | \PHPUnit_Framework_MockObject_MockObject
30
35
*/
31
36
protected $ encryptorMock ;
32
37
33
38
/**
34
- * @var CustomerRepositoryInterface
39
+ * @var CustomerRepositoryInterface | \PHPUnit_Framework_MockObject_MockObject
35
40
*/
36
41
private $ customerRepositoryMock ;
37
42
38
43
/**
39
- * @var \Magento\Customer\Model\Data\CustomerSecure
44
+ * @var \Magento\Customer\Model\Data\CustomerSecure | \PHPUnit_Framework_MockObject_MockObject
40
45
*/
41
- private $ customerSecure ;
46
+ private $ customerSecureMock ;
42
47
43
48
/**
44
49
* @var \Magento\Customer\Model\Authentication
45
50
*/
46
51
private $ authentication ;
47
52
53
+ /**
54
+ * @var DateTime
55
+ */
56
+ private $ dateTimeMock ;
57
+
48
58
protected function setUp ()
49
59
{
50
- $ this ->backendConfigMock = $ this ->getMockBuilder (' Magento\Backend\App\ ConfigInterface' )
60
+ $ this ->backendConfigMock = $ this ->getMockBuilder (ConfigInterface::class )
51
61
->disableOriginalConstructor ()
52
62
->setMethods (['getValue ' ])
53
63
->getMockForAbstractClass ();
54
64
$ this ->customerRegistryMock = $ this ->getMock (
55
- ' Magento\Customer\Model\ CustomerRegistry' ,
65
+ CustomerRegistry::class ,
56
66
['retrieveSecureData ' , 'retrieve ' ],
57
67
[],
58
68
'' ,
@@ -64,14 +74,21 @@ protected function setUp()
64
74
$ this ->encryptorMock = $ this ->getMockBuilder (\Magento \Framework \Encryption \EncryptorInterface::class)
65
75
->disableOriginalConstructor ()
66
76
->getMock ();
67
- $ this ->customerSecure = $ this ->getMock (
68
- 'Magento\Customer\Model\Data\CustomerSecure ' ,
77
+ $ this ->dateTimeMock = $ this ->getMockBuilder (DateTime::class)
78
+ ->disableOriginalConstructor ()
79
+ ->getMock ();
80
+ $ this ->dateTimeMock ->expects ($ this ->any ())
81
+ ->method ('formatDate ' )
82
+ ->willReturn ('formattedDate ' );
83
+ $ this ->customerSecureMock = $ this ->getMock (
84
+ CustomerSecure::class,
69
85
[
70
86
'getId ' ,
71
87
'getPasswordHash ' ,
72
88
'isCustomerLocked ' ,
73
89
'getFailuresNum ' ,
74
90
'getFirstFailure ' ,
91
+ 'getLockExpires ' ,
75
92
'setFirstFailure ' ,
76
93
'setFailuresNum ' ,
77
94
'setLockExpires '
@@ -80,6 +97,7 @@ protected function setUp()
80
97
'' ,
81
98
false
82
99
);
100
+
83
101
$ objectManagerHelper = new ObjectManagerHelper ($ this );
84
102
85
103
$ this ->authentication = $ objectManagerHelper ->getObject (
@@ -89,14 +107,12 @@ protected function setUp()
89
107
'backendConfig ' => $ this ->backendConfigMock ,
90
108
'customerRepository ' => $ this ->customerRepositoryMock ,
91
109
'encryptor ' => $ this ->encryptorMock ,
110
+ 'dateTime ' => $ this ->dateTimeMock ,
92
111
]
93
112
);
94
113
}
95
114
96
- /**
97
- * @return void
98
- */
99
- public function testLockingIsDisabled ()
115
+ public function testProcessAuthenticationFailureLockingIsDisabled ()
100
116
{
101
117
$ customerId = 1 ;
102
118
$ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
@@ -109,15 +125,32 @@ public function testLockingIsDisabled()
109
125
$ this ->customerRegistryMock ->expects ($ this ->once ())
110
126
->method ('retrieveSecureData ' )
111
127
->with ($ customerId )
112
- ->willReturn ($ this ->customerSecure );
128
+ ->willReturn ($ this ->customerSecureMock );
113
129
$ this ->authentication ->processAuthenticationFailure ($ customerId );
114
130
}
115
131
116
132
/**
117
- * @return void
133
+ * @param int $failureNum
134
+ * @param string $firstFailure
135
+ * @param string $lockExpires
136
+ * @param int $setFailureNumCallCtr
137
+ * @param int $setFailureNumValue
138
+ * @param int $setFirstFailureCallCtr
139
+ * @param int $setFirstFailureValue
140
+ * @param int $setLockExpiresCallCtr
141
+ * @param int $setLockExpiresValue
142
+ * @dataProvider processAuthenticationFailureDataProvider
118
143
*/
119
- public function testCustomerFailedFirstAttempt ()
120
- {
144
+ public function testProcessAuthenticationFailureFirstAttempt (
145
+ $ failureNum ,
146
+ $ firstFailure ,
147
+ $ lockExpires ,
148
+ $ setFailureNumCallCtr ,
149
+ $ setFailureNumValue ,
150
+ $ setFirstFailureCallCtr ,
151
+ $ setLockExpiresCallCtr ,
152
+ $ setLockExpiresValue
153
+ ) {
121
154
$ customerId = 1 ;
122
155
$ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
123
156
->method ('getValue ' )
@@ -130,7 +163,7 @@ public function testCustomerFailedFirstAttempt()
130
163
$ this ->customerRegistryMock ->expects ($ this ->once ())
131
164
->method ('retrieveSecureData ' )
132
165
->with ($ customerId )
133
- ->willReturn ($ this ->customerSecure );
166
+ ->willReturn ($ this ->customerSecureMock );
134
167
$ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
135
168
->disableOriginalConstructor ()
136
169
->getMock ();
@@ -142,66 +175,41 @@ public function testCustomerFailedFirstAttempt()
142
175
->method ('save ' )
143
176
->with ($ customerMock );
144
177
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 ' );
178
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn ($ failureNum );
179
+ $ this ->customerSecureMock ->expects ($ this ->once ())
180
+ ->method ('getFirstFailure ' )
181
+ ->willReturn ($ firstFailure ? (new \DateTime ())->modify ($ firstFailure )->format ('Y-m-d H:i:s ' ) : null );
182
+ $ this ->customerSecureMock ->expects ($ this ->once ())
183
+ ->method ('getLockExpires ' )
184
+ ->willReturn ($ lockExpires ? (new \DateTime ())->modify ($ lockExpires )->format ('Y-m-d H:i:s ' ) : null );
185
+ $ this ->customerSecureMock ->expects ($ this ->exactly ($ setFirstFailureCallCtr ))->method ('setFirstFailure ' );
186
+ $ this ->customerSecureMock ->expects ($ this ->exactly ($ setFailureNumCallCtr ))
187
+ ->method ('setFailuresNum ' )
188
+ ->with ($ setFailureNumValue );
189
+ $ this ->customerSecureMock ->expects ($ this ->exactly ($ setLockExpiresCallCtr ))
190
+ ->method ('setLockExpires ' )
191
+ ->with ($ setLockExpiresValue );
149
192
150
193
$ this ->authentication ->processAuthenticationFailure ($ customerId );
151
194
}
152
195
153
- /**
154
- * @return void
155
- */
156
- public function testCustomerHasFailedMaxNumberOfAttempts ()
196
+ public function processAuthenticationFailureDataProvider ()
157
197
{
158
- $ customerId = 1 ;
159
- $ date = new \DateTime ();
160
- $ date ->modify ('-500 second ' );
161
- $ formattedDate = $ date ->format ('Y-m-d H:i:s ' );
162
- $ this ->backendConfigMock ->expects ($ this ->exactly (2 ))
163
- ->method ('getValue ' )
164
- ->withConsecutive (
165
- [\Magento \Customer \Model \Authentication::LOCKOUT_THRESHOLD_PATH ],
166
- [\Magento \Customer \Model \Authentication::MAX_FAILURES_PATH ]
167
- )
168
- ->willReturnOnConsecutiveCalls (10 , 5 );
169
-
170
- $ this ->customerRegistryMock ->expects ($ this ->once ())
171
- ->method ('retrieveSecureData ' )
172
- ->with ($ customerId )
173
- ->willReturn ($ this ->customerSecure );
174
- $ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
175
- ->disableOriginalConstructor ()
176
- ->getMock ();
177
- $ this ->customerRepositoryMock ->expects ($ this ->once ())
178
- ->method ('getById ' )
179
- ->with ($ customerId )
180
- ->willReturn ($ customerMock );
181
- $ this ->customerRepositoryMock ->expects ($ this ->once ())
182
- ->method ('save ' )
183
- ->with ($ customerMock );
184
-
185
- $ this ->customerSecure ->expects ($ this ->once ())->method ('getFailuresNum ' )->willReturn (5 );
186
- $ this ->customerSecure ->expects ($ this ->once ())
187
- ->method ('getFirstFailure ' )
188
- ->willReturn ($ formattedDate );
189
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setLockExpires ' );
190
- $ this ->customerSecure ->expects ($ this ->once ())->method ('setFailuresNum ' );
191
-
192
- $ this ->authentication ->processAuthenticationFailure ($ customerId );
198
+ return [
199
+ 'first attempt ' => [0 , null , null , 1 , 1 , 1 , 1 , null ],
200
+ 'not locked ' => [3 , '-400 second ' , null , 1 , 4 , 0 , 0 , null ],
201
+ 'lock expired ' => [5 , '-400 second ' , '-100 second ' , 1 , 1 , 1 , 1 , null ],
202
+ 'max attempt ' => [4 , '-400 second ' , null , 1 , 5 , 0 , 1 , 'formattedDate ' ],
203
+ ];
193
204
}
194
205
195
- /**
196
- * @return void
197
- */
198
- public function testProcessUnlockData ()
206
+ public function testUnlock ()
199
207
{
200
208
$ customerId = 1 ;
201
209
$ this ->customerRegistryMock ->expects ($ this ->once ())
202
210
->method ('retrieveSecureData ' )
203
211
->with ($ customerId )
204
- ->willReturn ($ this ->customerSecure );
212
+ ->willReturn ($ this ->customerSecureMock );
205
213
$ customerMock = $ this ->getMockBuilder (CustomerInterface::class)
206
214
->disableOriginalConstructor ()
207
215
->getMock ();
@@ -212,9 +220,9 @@ public function testProcessUnlockData()
212
220
$ this ->customerRepositoryMock ->expects ($ this ->once ())
213
221
->method ('save ' )
214
222
->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 );
223
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFailuresNum ' )->with (0 );
224
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setFirstFailure ' )->with (null );
225
+ $ this ->customerSecureMock ->expects ($ this ->once ())->method ('setLockExpires ' )->with (null );
218
226
$ this ->authentication ->unlock ($ customerId );
219
227
}
220
228
@@ -229,7 +237,7 @@ public function validatePasswordAndLockStatusDataProvider()
229
237
/**
230
238
* @return void
231
239
*/
232
- public function testCheckIfLocked ()
240
+ public function testIsLocked ()
233
241
{
234
242
$ customerId = 7 ;
235
243
@@ -250,7 +258,7 @@ public function testCheckIfLocked()
250
258
* @param bool $result
251
259
* @dataProvider validateCustomerPassword
252
260
*/
253
- public function testValidateCustomerPassword ($ result )
261
+ public function testAuthenticate ($ result )
254
262
{
255
263
$ customerId = 7 ;
256
264
$ password = '1234567 ' ;
@@ -267,18 +275,18 @@ public function testValidateCustomerPassword($result)
267
275
->method ('getById ' )
268
276
->willReturn ($ customerMock );
269
277
270
- $ this ->customerSecure ->expects ($ this ->any ())
278
+ $ this ->customerSecureMock ->expects ($ this ->any ())
271
279
->method ('getId ' )
272
280
->willReturn ($ customerId );
273
281
274
- $ this ->customerSecure ->expects ($ this ->once ())
282
+ $ this ->customerSecureMock ->expects ($ this ->once ())
275
283
->method ('getPasswordHash ' )
276
284
->willReturn ($ hash );
277
285
278
286
$ this ->customerRegistryMock ->expects ($ this ->any ())
279
287
->method ('retrieveSecureData ' )
280
288
->with ($ customerId )
281
- ->willReturn ($ this ->customerSecure );
289
+ ->willReturn ($ this ->customerSecureMock );
282
290
283
291
$ this ->encryptorMock ->expects ($ this ->once ())
284
292
->method ('validateHash ' )
@@ -295,14 +303,14 @@ public function testValidateCustomerPassword($result)
295
303
[\Magento \Customer \Model \Authentication::MAX_FAILURES_PATH ]
296
304
)
297
305
->willReturnOnConsecutiveCalls (1 , 1 );
298
- $ this ->customerSecure ->expects ($ this ->once ())
306
+ $ this ->customerSecureMock ->expects ($ this ->once ())
299
307
->method ('isCustomerLocked ' )
300
308
->willReturn (false );
301
309
302
310
$ this ->customerRegistryMock ->expects ($ this ->once ())
303
311
->method ('retrieve ' )
304
312
->with ($ customerId )
305
- ->willReturn ($ this ->customerSecure );
313
+ ->willReturn ($ this ->customerSecureMock );
306
314
307
315
$ this ->customerRepositoryMock ->expects ($ this ->once ())
308
316
->method ('save ' )
0 commit comments