Skip to content

Commit 92143fe

Browse files
author
Oleksii Korshenko
authored
Merge pull request #248 from magento-fearless-kiwis/FearlessKiwis-MAGETWO-56126-Login-failed-after-new-custom-attribute-was-added
Fixed Issue: - MAGETWO-56126: Login failed after new custom attribute was added
2 parents 5f1cfd1 + 61ad1ba commit 92143fe

File tree

4 files changed

+233
-25
lines changed

4 files changed

+233
-25
lines changed

app/code/Magento/Customer/Model/Authentication.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
namespace Magento\Customer\Model;
77

88
use Magento\Customer\Api\CustomerRepositoryInterface;
9+
use Magento\Customer\Model\ResourceModel\CustomerRepository;
10+
use Magento\Customer\Model\CustomerAuthUpdate;
911
use Magento\Backend\App\ConfigInterface;
1012
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
1113
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
1214
use Magento\Framework\Exception\State\UserLockedException;
1315

16+
/**
17+
* Class Authentication
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
*/
1420
class Authentication implements AuthenticationInterface
1521
{
1622
/**
@@ -50,6 +56,11 @@ class Authentication implements AuthenticationInterface
5056
*/
5157
protected $customerRepository;
5258

59+
/**
60+
* @var CustomerAuthUpdate
61+
*/
62+
private $customerAuthUpdate;
63+
5364
/**
5465
* @param CustomerRepositoryInterface $customerRepository
5566
* @param CustomerRegistry $customerRegistry
@@ -105,7 +116,7 @@ public function processAuthenticationFailure($customerId)
105116
}
106117

107118
$customerSecure->setFailuresNum($failuresNum);
108-
$this->customerRepository->save($this->customerRepository->getById($customerId));
119+
$this->getCustomerAuthUpdate()->saveAuth($customerId);
109120
}
110121

111122
/**
@@ -117,7 +128,7 @@ public function unlock($customerId)
117128
$customerSecure->setFailuresNum(0);
118129
$customerSecure->setFirstFailure(null);
119130
$customerSecure->setLockExpires(null);
120-
$this->customerRepository->save($this->customerRepository->getById($customerId));
131+
$this->getCustomerAuthUpdate()->saveAuth($customerId);
121132
}
122133

123134
/**
@@ -165,4 +176,19 @@ public function authenticate($customerId, $password)
165176
}
166177
return true;
167178
}
179+
180+
/**
181+
* Get customer authentication update model
182+
*
183+
* @return \Magento\Customer\Model\CustomerAuthUpdate
184+
* @deprecated
185+
*/
186+
private function getCustomerAuthUpdate()
187+
{
188+
if ($this->customerAuthUpdate === null) {
189+
$this->customerAuthUpdate =
190+
\Magento\Framework\App\ObjectManager::getInstance()->get(CustomerAuthUpdate::class);
191+
}
192+
return $this->customerAuthUpdate;
193+
}
168194
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model;
8+
9+
/**
10+
* Customer Authentication update model.
11+
*/
12+
class CustomerAuthUpdate
13+
{
14+
/**
15+
* @var \Magento\Customer\Model\CustomerRegistry
16+
*/
17+
protected $customerRegistry;
18+
19+
/**
20+
* @var \Magento\Customer\Model\ResourceModel\Customer
21+
*/
22+
protected $customerResourceModel;
23+
24+
/**
25+
* @param \Magento\Customer\Model\CustomerRegistry $customerRegistry
26+
* @param \Magento\Customer\Model\ResourceModel\Customer $customerResourceModel
27+
*/
28+
public function __construct(
29+
\Magento\Customer\Model\CustomerRegistry $customerRegistry,
30+
\Magento\Customer\Model\ResourceModel\Customer $customerResourceModel
31+
) {
32+
$this->customerRegistry = $customerRegistry;
33+
$this->customerResourceModel = $customerResourceModel;
34+
}
35+
36+
/**
37+
* Reset Authentication data for customer.
38+
*
39+
* @param int $customerId
40+
* @return $this
41+
*/
42+
public function saveAuth($customerId)
43+
{
44+
$customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
45+
46+
$this->customerResourceModel->getConnection()->update(
47+
$this->customerResourceModel->getTable('customer_entity'),
48+
[
49+
'failures_num' => $customerSecure->getData('failures_num'),
50+
'first_failure' => $customerSecure->getData('first_failure'),
51+
'lock_expires' => $customerSecure->getData('lock_expires'),
52+
],
53+
$this->customerResourceModel->getConnection()->quoteInto('entity_id = ?', $customerId)
54+
);
55+
56+
return $this;
57+
}
58+
}

app/code/Magento/Customer/Test/Unit/Model/AuthenticationTest.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,20 @@ class AuthenticationTest extends \PHPUnit_Framework_TestCase
5555
*/
5656
private $dateTimeMock;
5757

58+
/**
59+
* @var \Magento\Customer\Model\CustomerAuthUpdate | \PHPUnit_Framework_MockObject_MockObject
60+
*/
61+
protected $customerAuthUpdate;
62+
63+
/**
64+
* @var ObjectManagerHelper
65+
*/
66+
protected $objectManager;
67+
5868
protected function setUp()
5969
{
70+
$this->objectManager = new ObjectManagerHelper($this);
71+
6072
$this->backendConfigMock = $this->getMockBuilder(ConfigInterface::class)
6173
->disableOriginalConstructor()
6274
->setMethods(['getValue'])
@@ -98,9 +110,11 @@ protected function setUp()
98110
false
99111
);
100112

101-
$objectManagerHelper = new ObjectManagerHelper($this);
113+
$this->customerAuthUpdate = $this->getMockBuilder(\Magento\Customer\Model\CustomerAuthUpdate::class)
114+
->disableOriginalConstructor()
115+
->getMock();
102116

103-
$this->authentication = $objectManagerHelper->getObject(
117+
$this->authentication = $this->objectManager->getObject(
104118
Authentication::class,
105119
[
106120
'customerRegistry' => $this->customerRegistryMock,
@@ -110,6 +124,12 @@ protected function setUp()
110124
'dateTime' => $this->dateTimeMock,
111125
]
112126
);
127+
128+
$this->objectManager->setBackwardCompatibleProperty(
129+
$this->authentication,
130+
'customerAuthUpdate',
131+
$this->customerAuthUpdate
132+
);
113133
}
114134

115135
public function testProcessAuthenticationFailureLockingIsDisabled()
@@ -164,16 +184,10 @@ public function testProcessAuthenticationFailureFirstAttempt(
164184
->method('retrieveSecureData')
165185
->with($customerId)
166186
->willReturn($this->customerSecureMock);
167-
$customerMock = $this->getMockBuilder(CustomerInterface::class)
168-
->disableOriginalConstructor()
169-
->getMock();
170-
$this->customerRepositoryMock->expects($this->once())
171-
->method('getById')
187+
$this->customerAuthUpdate->expects($this->once())
188+
->method('saveAuth')
172189
->with($customerId)
173-
->willReturn($customerMock);
174-
$this->customerRepositoryMock->expects($this->once())
175-
->method('save')
176-
->with($customerMock);
190+
->willReturnSelf();
177191

178192
$this->customerSecureMock->expects($this->once())->method('getFailuresNum')->willReturn($failureNum);
179193
$this->customerSecureMock->expects($this->once())
@@ -210,16 +224,10 @@ public function testUnlock()
210224
->method('retrieveSecureData')
211225
->with($customerId)
212226
->willReturn($this->customerSecureMock);
213-
$customerMock = $this->getMockBuilder(CustomerInterface::class)
214-
->disableOriginalConstructor()
215-
->getMock();
216-
$this->customerRepositoryMock->expects($this->once())
217-
->method('getById')
227+
$this->customerAuthUpdate->expects($this->once())
228+
->method('saveAuth')
218229
->with($customerId)
219-
->willReturn($customerMock);
220-
$this->customerRepositoryMock->expects($this->once())
221-
->method('save')
222-
->with($customerMock);
230+
->willReturnSelf();
223231
$this->customerSecureMock->expects($this->once())->method('setFailuresNum')->with(0);
224232
$this->customerSecureMock->expects($this->once())->method('setFirstFailure')->with(null);
225233
$this->customerSecureMock->expects($this->once())->method('setLockExpires')->with(null);
@@ -312,9 +320,10 @@ public function testAuthenticate($result)
312320
->with($customerId)
313321
->willReturn($this->customerSecureMock);
314322

315-
$this->customerRepositoryMock->expects($this->once())
316-
->method('save')
317-
->willReturn($customerMock);
323+
$this->customerAuthUpdate->expects($this->once())
324+
->method('saveAuth')
325+
->with($customerId)
326+
->willReturnSelf();
318327

319328
$this->setExpectedException(\Magento\Framework\Exception\InvalidEmailOrPasswordException::class);
320329
$this->authentication->authenticate($customerId, $password);
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Model;
7+
8+
use Magento\Customer\Model\CustomerAuthUpdate;
9+
10+
/**
11+
* Class CustomerAuthUpdateTest
12+
*/
13+
class CustomerAuthUpdateTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var CustomerAuthUpdate
17+
*/
18+
protected $model;
19+
20+
/**
21+
* @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $customerRegistry;
24+
25+
/**
26+
* @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $customerResourceModel;
29+
30+
/**
31+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
32+
*/
33+
protected $objectManager;
34+
35+
/**
36+
* Setup the test
37+
*/
38+
protected function setUp()
39+
{
40+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
41+
42+
$this->customerRegistry =
43+
$this->getMock(\Magento\Customer\Model\CustomerRegistry::class, [], [], '', false);
44+
$this->customerResourceModel =
45+
$this->getMock(\Magento\Customer\Model\ResourceModel\Customer::class, [], [], '', false);
46+
47+
$this->model = $this->objectManager->getObject(
48+
\Magento\Customer\Model\CustomerAuthUpdate::class,
49+
[
50+
'customerRegistry' => $this->customerRegistry,
51+
'customerResourceModel' => $this->customerResourceModel,
52+
]
53+
);
54+
}
55+
56+
/**
57+
* test SaveAuth
58+
*/
59+
public function testSaveAuth()
60+
{
61+
$customerId = 1;
62+
63+
$customerSecureMock = $this->getMock(
64+
\Magento\Customer\Model\Data\CustomerSecure::class,
65+
[],
66+
[],
67+
'',
68+
false
69+
);
70+
71+
$dbAdapter = $this->getMock(
72+
\Magento\Framework\DB\Adapter\AdapterInterface::class,
73+
[],
74+
[],
75+
'',
76+
false
77+
);
78+
79+
$this->customerRegistry->expects($this->once())
80+
->method('retrieveSecureData')
81+
->willReturn($customerSecureMock);
82+
83+
$customerSecureMock->expects($this->exactly(3))
84+
->method('getData')
85+
->willReturn(1);
86+
87+
$this->customerResourceModel->expects($this->any())
88+
->method('getConnection')
89+
->willReturn($dbAdapter);
90+
91+
$this->customerResourceModel->expects($this->any())
92+
->method('getTable')
93+
->willReturn('customer_entity');
94+
95+
$dbAdapter->expects($this->any())
96+
->method('update')
97+
->with(
98+
'customer_entity',
99+
[
100+
'failures_num' => 1,
101+
'first_failure' => 1,
102+
'lock_expires' => 1
103+
]
104+
);
105+
106+
$dbAdapter->expects($this->any())
107+
->method('quoteInto')
108+
->with(
109+
'entity_id = ?',
110+
$customerId
111+
);
112+
113+
$this->model->saveAuth($customerId);
114+
}
115+
}

0 commit comments

Comments
 (0)