Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AuthenticationTest.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class AuthenticationTest extends \PHPUnit\Framework\TestCase
20 {
24  private $backendConfigMock;
25 
29  private $customerRegistryMock;
30 
34  protected $encryptorMock;
35 
39  private $customerRepositoryMock;
40 
44  private $customerSecureMock;
45 
49  private $authentication;
50 
54  private $dateTimeMock;
55 
60 
64  protected $objectManager;
65 
66  protected function setUp()
67  {
68  $this->objectManager = new ObjectManagerHelper($this);
69 
70  $this->backendConfigMock = $this->getMockBuilder(ConfigInterface::class)
71  ->disableOriginalConstructor()
72  ->setMethods(['getValue'])
73  ->getMockForAbstractClass();
74  $this->customerRegistryMock = $this->createPartialMock(
75  CustomerRegistry::class,
76  ['retrieveSecureData', 'retrieve']
77  );
78  $this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->encryptorMock = $this->getMockBuilder(\Magento\Framework\Encryption\EncryptorInterface::class)
82  ->disableOriginalConstructor()
83  ->getMock();
84  $this->dateTimeMock = $this->getMockBuilder(DateTime::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87  $this->dateTimeMock->expects($this->any())
88  ->method('formatDate')
89  ->willReturn('formattedDate');
90  $this->customerSecureMock = $this->createPartialMock(CustomerSecure::class, [
91  'getId',
92  'getPasswordHash',
93  'isCustomerLocked',
94  'getFailuresNum',
95  'getFirstFailure',
96  'getLockExpires',
97  'setFirstFailure',
98  'setFailuresNum',
99  'setLockExpires'
100  ]);
101 
102  $this->customerAuthUpdate = $this->getMockBuilder(\Magento\Customer\Model\CustomerAuthUpdate::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105 
106  $this->authentication = $this->objectManager->getObject(
107  Authentication::class,
108  [
109  'customerRegistry' => $this->customerRegistryMock,
110  'backendConfig' => $this->backendConfigMock,
111  'customerRepository' => $this->customerRepositoryMock,
112  'encryptor' => $this->encryptorMock,
113  'dateTime' => $this->dateTimeMock,
114  ]
115  );
116 
117  $this->objectManager->setBackwardCompatibleProperty(
118  $this->authentication,
119  'customerAuthUpdate',
120  $this->customerAuthUpdate
121  );
122  }
123 
125  {
126  $customerId = 1;
127  $this->backendConfigMock->expects($this->exactly(2))
128  ->method('getValue')
129  ->withConsecutive(
132  )
133  ->willReturnOnConsecutiveCalls(0, 0);
134  $this->customerRegistryMock->expects($this->once())
135  ->method('retrieveSecureData')
136  ->with($customerId)
137  ->willReturn($this->customerSecureMock);
138  $this->authentication->processAuthenticationFailure($customerId);
139  }
140 
154  $failureNum,
155  $firstFailure,
156  $lockExpires,
157  $setFailureNumCallCtr,
158  $setFailureNumValue,
159  $setFirstFailureCallCtr,
160  $setLockExpiresCallCtr,
161  $setLockExpiresValue
162  ) {
163  $customerId = 1;
164  $this->backendConfigMock->expects($this->exactly(2))
165  ->method('getValue')
166  ->withConsecutive(
169  )
170  ->willReturnOnConsecutiveCalls(10, 5);
171 
172  $this->customerRegistryMock->expects($this->once())
173  ->method('retrieveSecureData')
174  ->with($customerId)
175  ->willReturn($this->customerSecureMock);
176  $this->customerAuthUpdate->expects($this->once())
177  ->method('saveAuth')
178  ->with($customerId)
179  ->willReturnSelf();
180 
181  $this->customerSecureMock->expects($this->once())->method('getFailuresNum')->willReturn($failureNum);
182  $this->customerSecureMock->expects($this->once())
183  ->method('getFirstFailure')
184  ->willReturn($firstFailure ? (new \DateTime())->modify($firstFailure)->format('Y-m-d H:i:s') : null);
185  $this->customerSecureMock->expects($this->once())
186  ->method('getLockExpires')
187  ->willReturn($lockExpires ? (new \DateTime())->modify($lockExpires)->format('Y-m-d H:i:s') : null);
188  $this->customerSecureMock->expects($this->exactly($setFirstFailureCallCtr))->method('setFirstFailure');
189  $this->customerSecureMock->expects($this->exactly($setFailureNumCallCtr))
190  ->method('setFailuresNum')
191  ->with($setFailureNumValue);
192  $this->customerSecureMock->expects($this->exactly($setLockExpiresCallCtr))
193  ->method('setLockExpires')
194  ->with($setLockExpiresValue);
195 
196  $this->authentication->processAuthenticationFailure($customerId);
197  }
198 
203  {
204  return [
205  'first attempt' => [0, null, null, 1, 1, 1, 1, null],
206  'not locked' => [3, '-400 second', null, 1, 4, 0, 0, null],
207  'lock expired' => [5, '-400 second', '-100 second', 1, 1, 1, 1, null],
208  'max attempt' => [4, '-400 second', null, 1, 5, 0, 1, 'formattedDate'],
209  ];
210  }
211 
212  public function testUnlock()
213  {
214  $customerId = 1;
215  $this->customerRegistryMock->expects($this->once())
216  ->method('retrieveSecureData')
217  ->with($customerId)
218  ->willReturn($this->customerSecureMock);
219  $this->customerAuthUpdate->expects($this->once())
220  ->method('saveAuth')
221  ->with($customerId)
222  ->willReturnSelf();
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);
226  $this->authentication->unlock($customerId);
227  }
228 
233  {
234  return [[true], [false]];
235  }
236 
240  public function testIsLocked()
241  {
242  $customerId = 7;
243 
244  $customerModelMock = $this->getMockBuilder(\Magento\Customer\Model\Customer::class)
245  ->disableOriginalConstructor()
246  ->getMock();
247  $customerModelMock->expects($this->once())
248  ->method('isCustomerLocked');
249  $this->customerRegistryMock->expects($this->once())
250  ->method('retrieve')
251  ->with($customerId)
252  ->willReturn($customerModelMock);
253 
254  $this->authentication->isLocked($customerId);
255  }
256 
261  public function testAuthenticate($result)
262  {
263  $customerId = 7;
264  $password = '1234567';
265  $hash = '1b2af329dd0';
266 
267  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
268  $this->customerRepositoryMock->expects($this->any())
269  ->method('getById')
270  ->willReturn($customerMock);
271 
272  $this->customerSecureMock->expects($this->any())
273  ->method('getId')
274  ->willReturn($customerId);
275 
276  $this->customerSecureMock->expects($this->once())
277  ->method('getPasswordHash')
278  ->willReturn($hash);
279 
280  $this->customerRegistryMock->expects($this->any())
281  ->method('retrieveSecureData')
282  ->with($customerId)
283  ->willReturn($this->customerSecureMock);
284 
285  $this->encryptorMock->expects($this->once())
286  ->method('validateHash')
287  ->with($password, $hash)
288  ->willReturn($result);
289 
290  if ($result) {
291  $this->assertTrue($this->authentication->authenticate($customerId, $password));
292  } else {
293  $this->backendConfigMock->expects($this->exactly(2))
294  ->method('getValue')
295  ->withConsecutive(
298  )
299  ->willReturnOnConsecutiveCalls(1, 1);
300  $this->customerSecureMock->expects($this->once())
301  ->method('isCustomerLocked')
302  ->willReturn(false);
303 
304  $this->customerRegistryMock->expects($this->once())
305  ->method('retrieve')
306  ->with($customerId)
307  ->willReturn($this->customerSecureMock);
308 
309  $this->customerAuthUpdate->expects($this->once())
310  ->method('saveAuth')
311  ->with($customerId)
312  ->willReturnSelf();
313 
314  $this->expectException(\Magento\Framework\Exception\InvalidEmailOrPasswordException::class);
315  $this->authentication->authenticate($customerId, $password);
316  }
317  }
318 
322  public function validateCustomerPassword()
323  {
324  return [
325  [true],
326  [false],
327  ];
328  }
329 }
testProcessAuthenticationFailureFirstAttempt( $failureNum, $firstFailure, $lockExpires, $setFailureNumCallCtr, $setFailureNumValue, $setFirstFailureCallCtr, $setLockExpiresCallCtr, $setLockExpiresValue)