Skip to content

Commit addca69

Browse files
author
Spandana Chittimala
committed
Merge remote-tracking branch 'tango/MAGETWO-99493' into PR-05-29-2019
2 parents 1cc5d0b + d9d5a11 commit addca69

File tree

3 files changed

+145
-16
lines changed

3 files changed

+145
-16
lines changed

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,74 @@
66

77
namespace Magento\Customer\Model;
88

9+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
913
/**
1014
* Customer Authentication update model.
1115
*/
1216
class CustomerAuthUpdate
1317
{
1418
/**
15-
* @var \Magento\Customer\Model\CustomerRegistry
19+
* @var CustomerRegistry
1620
*/
1721
protected $customerRegistry;
1822

1923
/**
20-
* @var \Magento\Customer\Model\ResourceModel\Customer
24+
* @var CustomerResourceModel
2125
*/
2226
protected $customerResourceModel;
2327

2428
/**
25-
* @param \Magento\Customer\Model\CustomerRegistry $customerRegistry
26-
* @param \Magento\Customer\Model\ResourceModel\Customer $customerResourceModel
29+
* @var Customer
30+
*/
31+
private $customerModel;
32+
33+
/**
34+
* @param CustomerRegistry $customerRegistry
35+
* @param CustomerResourceModel $customerResourceModel
36+
* @param Customer|null $customerModel
2737
*/
2838
public function __construct(
29-
\Magento\Customer\Model\CustomerRegistry $customerRegistry,
30-
\Magento\Customer\Model\ResourceModel\Customer $customerResourceModel
39+
CustomerRegistry $customerRegistry,
40+
CustomerResourceModel $customerResourceModel,
41+
Customer $customerModel = null
3142
) {
3243
$this->customerRegistry = $customerRegistry;
3344
$this->customerResourceModel = $customerResourceModel;
45+
$this->customerModel = $customerModel ?: ObjectManager::getInstance()->get(Customer::class);
3446
}
3547

3648
/**
3749
* Reset Authentication data for customer.
3850
*
3951
* @param int $customerId
4052
* @return $this
53+
* @throws NoSuchEntityException
4154
*/
4255
public function saveAuth($customerId)
4356
{
4457
$customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
4558

59+
$this->customerResourceModel->load($this->customerModel, $customerId);
60+
$currentLockExpiresVal = $this->customerModel->getData('lock_expires');
61+
$newLockExpiresVal = $customerSecure->getData('lock_expires');
62+
4663
$this->customerResourceModel->getConnection()->update(
4764
$this->customerResourceModel->getTable('customer_entity'),
4865
[
4966
'failures_num' => $customerSecure->getData('failures_num'),
5067
'first_failure' => $customerSecure->getData('first_failure'),
51-
'lock_expires' => $customerSecure->getData('lock_expires'),
68+
'lock_expires' => $newLockExpiresVal,
5269
],
5370
$this->customerResourceModel->getConnection()->quoteInto('entity_id = ?', $customerId)
5471
);
5572

73+
if ($currentLockExpiresVal !== $newLockExpiresVal) {
74+
$this->customerModel->reindex();
75+
}
76+
5677
return $this;
5778
}
5879
}

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
*/
66
namespace Magento\Customer\Test\Unit\Model;
77

8+
use Magento\Customer\Model\Customer as CustomerModel;
89
use Magento\Customer\Model\CustomerAuthUpdate;
10+
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Customer\Model\Data\CustomerSecure;
12+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
13+
use Magento\Framework\DB\Adapter\AdapterInterface;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
916

1017
/**
1118
* Class CustomerAuthUpdateTest
@@ -18,17 +25,22 @@ class CustomerAuthUpdateTest extends \PHPUnit\Framework\TestCase
1825
protected $model;
1926

2027
/**
21-
* @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
28+
* @var CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
2229
*/
2330
protected $customerRegistry;
2431

2532
/**
26-
* @var \Magento\Customer\Model\ResourceModel\Customer|\PHPUnit_Framework_MockObject_MockObject
33+
* @var CustomerResourceModel|\PHPUnit_Framework_MockObject_MockObject
2734
*/
2835
protected $customerResourceModel;
2936

3037
/**
31-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
38+
* @var CustomerModel|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
protected $customerModel;
41+
42+
/**
43+
* @var ObjectManager
3244
*/
3345
protected $objectManager;
3446

@@ -37,32 +49,36 @@ class CustomerAuthUpdateTest extends \PHPUnit\Framework\TestCase
3749
*/
3850
protected function setUp()
3951
{
40-
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
52+
$this->objectManager = new ObjectManager($this);
4153

4254
$this->customerRegistry =
43-
$this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
55+
$this->createMock(CustomerRegistry::class);
4456
$this->customerResourceModel =
45-
$this->createMock(\Magento\Customer\Model\ResourceModel\Customer::class);
57+
$this->createMock(CustomerResourceModel::class);
58+
$this->customerModel =
59+
$this->createMock(CustomerModel::class);
4660

4761
$this->model = $this->objectManager->getObject(
48-
\Magento\Customer\Model\CustomerAuthUpdate::class,
62+
CustomerAuthUpdate::class,
4963
[
5064
'customerRegistry' => $this->customerRegistry,
5165
'customerResourceModel' => $this->customerResourceModel,
66+
'customerModel' => $this->customerModel
5267
]
5368
);
5469
}
5570

5671
/**
5772
* test SaveAuth
73+
* @throws NoSuchEntityException
5874
*/
5975
public function testSaveAuth()
6076
{
6177
$customerId = 1;
6278

63-
$customerSecureMock = $this->createMock(\Magento\Customer\Model\Data\CustomerSecure::class);
79+
$customerSecureMock = $this->createMock(CustomerSecure::class);
6480

65-
$dbAdapter = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
81+
$dbAdapter = $this->createMock(AdapterInterface::class);
6682

6783
$this->customerRegistry->expects($this->once())
6884
->method('retrieveSecureData')
@@ -98,6 +114,9 @@ public function testSaveAuth()
98114
$customerId
99115
);
100116

117+
$this->customerModel->expects($this->once())
118+
->method('reindex');
119+
101120
$this->model->saveAuth($customerId);
102121
}
103122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\ResourceModel\Grid;
8+
9+
use Magento\Customer\Api\AccountManagementInterface;
10+
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\Indexer\TestCase;
15+
use Magento\Tests\NamingConvention\true\mixed;
16+
17+
/**
18+
* Test if customer account lock on too many failed authentication attempts triggers customer grid reindex
19+
*
20+
* @SuppressWarnings(PHPMD)
21+
*/
22+
class CollectionReindexOnAccountLockTest extends TestCase
23+
{
24+
/**
25+
* Trigger customer account lock by making 10 failed authentication attempts
26+
*/
27+
private function lockCustomerAccountWithInvalidAuthentications()
28+
{
29+
/** @var AccountManagementInterface */
30+
$accountManagement = Bootstrap::getObjectManager()->create(AccountManagementInterface::class);
31+
32+
for ($i = 0; $i < 10; $i++) {
33+
try {
34+
$accountManagement->authenticate('[email protected]', 'wrongPassword');
35+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
36+
} catch (InvalidEmailOrPasswordException $e) {
37+
}
38+
}
39+
}
40+
41+
/**
42+
* @return mixed
43+
* @throws NoSuchEntityException
44+
*/
45+
private function getCustomerLockExpire(): ?string
46+
{
47+
/** @var CustomerRegistry $customerRegistry */
48+
$customerRegistry = Bootstrap::getObjectManager()->create(CustomerRegistry::class);
49+
$customerModel = $customerRegistry->retrieve(1);
50+
$this->assertNotEmpty($customerModel);
51+
52+
return $customerModel->getData('lock_expires');
53+
}
54+
55+
/**
56+
* @return mixed
57+
*/
58+
private function getCustomerGridLockExpire(): ?string
59+
{
60+
/** @var Collection */
61+
$gridCustomerCollection = Bootstrap::getObjectManager()->create(Collection::class);
62+
$gridCustomerItem = $gridCustomerCollection->getItemById(1);
63+
$this->assertNotEmpty($gridCustomerItem);
64+
65+
return $gridCustomerItem->getData('lock_expires');
66+
}
67+
68+
/**
69+
* Test if customer account lock on too many failed authentication attempts triggers customer grid reindex
70+
*
71+
* @magentoDataFixture Magento/Customer/_files/customer.php
72+
* @magentoAppIsolation enabled
73+
* @magentoDbIsolation disabled
74+
*/
75+
public function testCustomerAccountReindexOnLock()
76+
{
77+
$this->assertSame(
78+
$this->getCustomerGridLockExpire(),
79+
$this->getCustomerLockExpire()
80+
);
81+
82+
$this->lockCustomerAccountWithInvalidAuthentications();
83+
84+
$this->assertSame(
85+
$this->getCustomerGridLockExpire(),
86+
$this->getCustomerLockExpire()
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)