24 private $backendConfigMock;
29 private $customerRegistryMock;
39 private $customerRepositoryMock;
44 private $customerSecureMock;
49 private $authentication;
54 private $dateTimeMock;
68 $this->objectManager =
new ObjectManagerHelper($this);
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']
78 $this->customerRepositoryMock = $this->getMockBuilder(CustomerRepositoryInterface::class)
79 ->disableOriginalConstructor()
81 $this->encryptorMock = $this->getMockBuilder(\
Magento\Framework\Encryption\EncryptorInterface::class)
82 ->disableOriginalConstructor()
84 $this->dateTimeMock = $this->getMockBuilder(DateTime::class)
85 ->disableOriginalConstructor()
87 $this->dateTimeMock->expects($this->any())
88 ->method(
'formatDate')
89 ->willReturn(
'formattedDate');
90 $this->customerSecureMock = $this->createPartialMock(CustomerSecure::class, [
102 $this->customerAuthUpdate = $this->getMockBuilder(\
Magento\
Customer\Model\CustomerAuthUpdate::class)
103 ->disableOriginalConstructor()
106 $this->authentication = $this->objectManager->getObject(
107 Authentication::class,
109 'customerRegistry' => $this->customerRegistryMock,
110 'backendConfig' => $this->backendConfigMock,
111 'customerRepository' => $this->customerRepositoryMock,
112 'encryptor' => $this->encryptorMock,
113 'dateTime' => $this->dateTimeMock,
117 $this->objectManager->setBackwardCompatibleProperty(
118 $this->authentication,
119 'customerAuthUpdate',
120 $this->customerAuthUpdate
127 $this->backendConfigMock->expects($this->exactly(2))
133 ->willReturnOnConsecutiveCalls(0, 0);
134 $this->customerRegistryMock->expects($this->once())
135 ->method(
'retrieveSecureData')
137 ->willReturn($this->customerSecureMock);
138 $this->authentication->processAuthenticationFailure(
$customerId);
157 $setFailureNumCallCtr,
159 $setFirstFailureCallCtr,
160 $setLockExpiresCallCtr,
164 $this->backendConfigMock->expects($this->exactly(2))
170 ->willReturnOnConsecutiveCalls(10, 5);
172 $this->customerRegistryMock->expects($this->once())
173 ->method(
'retrieveSecureData')
175 ->willReturn($this->customerSecureMock);
176 $this->customerAuthUpdate->expects($this->once())
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);
196 $this->authentication->processAuthenticationFailure(
$customerId);
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'],
215 $this->customerRegistryMock->expects($this->once())
216 ->method(
'retrieveSecureData')
218 ->willReturn($this->customerSecureMock);
219 $this->customerAuthUpdate->expects($this->once())
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);
234 return [[
true], [
false]];
244 $customerModelMock = $this->getMockBuilder(\
Magento\
Customer\Model\Customer::class)
245 ->disableOriginalConstructor()
247 $customerModelMock->expects($this->once())
248 ->method(
'isCustomerLocked');
249 $this->customerRegistryMock->expects($this->once())
252 ->willReturn($customerModelMock);
264 $password =
'1234567';
265 $hash =
'1b2af329dd0';
267 $customerMock = $this->createMock(\
Magento\
Customer\Api\Data\CustomerInterface::class);
268 $this->customerRepositoryMock->expects($this->any())
270 ->willReturn($customerMock);
272 $this->customerSecureMock->expects($this->any())
276 $this->customerSecureMock->expects($this->once())
277 ->method(
'getPasswordHash')
280 $this->customerRegistryMock->expects($this->any())
281 ->method(
'retrieveSecureData')
283 ->willReturn($this->customerSecureMock);
285 $this->encryptorMock->expects($this->once())
286 ->method(
'validateHash')
287 ->with($password, $hash)
291 $this->assertTrue($this->authentication->authenticate(
$customerId, $password));
293 $this->backendConfigMock->expects($this->exactly(2))
299 ->willReturnOnConsecutiveCalls(1, 1);
300 $this->customerSecureMock->expects($this->once())
301 ->method(
'isCustomerLocked')
304 $this->customerRegistryMock->expects($this->once())
307 ->willReturn($this->customerSecureMock);
309 $this->customerAuthUpdate->expects($this->once())
314 $this->expectException(\
Magento\Framework\Exception\InvalidEmailOrPasswordException::class);
315 $this->authentication->authenticate(
$customerId, $password);
validateCustomerPassword()
const LOCKOUT_THRESHOLD_PATH
validatePasswordAndLockStatusDataProvider()
testAuthenticate($result)
testProcessAuthenticationFailureFirstAttempt( $failureNum, $firstFailure, $lockExpires, $setFailureNumCallCtr, $setFailureNumValue, $setFirstFailureCallCtr, $setLockExpiresCallCtr, $setLockExpiresValue)
processAuthenticationFailureDataProvider()
testProcessAuthenticationFailureLockingIsDisabled()