Skip to content

Commit 97632b3

Browse files
committed
Update the fix for reset password issue when a country is not allowed, update the PHPUnit tests #18170
1 parent 33e42ac commit 97632b3

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\Customer\Model\Customer as CustomerModel;
1919
use Magento\Customer\Model\Customer\CredentialsValidator;
2020
use Magento\Customer\Model\Metadata\Validator;
21+
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
2122
use Magento\Eav\Model\Validator\Attribute\Backend;
2223
use Magento\Framework\Api\ExtensibleDataObjectConverter;
2324
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -45,14 +46,13 @@
4546
use Magento\Framework\Phrase;
4647
use Magento\Framework\Reflection\DataObjectProcessor;
4748
use Magento\Framework\Registry;
49+
use Magento\Framework\Session\SaveHandlerInterface;
50+
use Magento\Framework\Session\SessionManagerInterface;
4851
use Magento\Framework\Stdlib\DateTime;
4952
use Magento\Framework\Stdlib\StringUtils as StringHelper;
5053
use Magento\Store\Model\ScopeInterface;
5154
use Magento\Store\Model\StoreManagerInterface;
5255
use Psr\Log\LoggerInterface as PsrLogger;
53-
use Magento\Framework\Session\SessionManagerInterface;
54-
use Magento\Framework\Session\SaveHandlerInterface;
55-
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
5656

5757
/**
5858
* Handle various customer account actions
@@ -333,6 +333,11 @@ class AccountManagement implements AccountManagementInterface
333333
*/
334334
private $searchCriteriaBuilder;
335335

336+
/**
337+
* @var AddressRegistry
338+
*/
339+
private $addressRegistry;
340+
336341
/**
337342
* @param CustomerFactory $customerFactory
338343
* @param ManagerInterface $eventManager
@@ -364,6 +369,7 @@ class AccountManagement implements AccountManagementInterface
364369
* @param SaveHandlerInterface|null $saveHandler
365370
* @param CollectionFactory|null $visitorCollectionFactory
366371
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
372+
* @param AddressRegistry|null $addressRegistry
367373
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
368374
*/
369375
public function __construct(
@@ -396,7 +402,8 @@ public function __construct(
396402
SessionManagerInterface $sessionManager = null,
397403
SaveHandlerInterface $saveHandler = null,
398404
CollectionFactory $visitorCollectionFactory = null,
399-
SearchCriteriaBuilder $searchCriteriaBuilder = null
405+
SearchCriteriaBuilder $searchCriteriaBuilder = null,
406+
AddressRegistry $addressRegistry = null
400407
) {
401408
$this->customerFactory = $customerFactory;
402409
$this->eventManager = $eventManager;
@@ -434,6 +441,8 @@ public function __construct(
434441
?: ObjectManager::getInstance()->get(CollectionFactory::class);
435442
$this->searchCriteriaBuilder = $searchCriteriaBuilder
436443
?: ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
444+
$this->addressRegistry = $addressRegistry
445+
?: ObjectManager::getInstance()->get(AddressRegistry::class);
437446
}
438447

439448
/**
@@ -579,6 +588,9 @@ public function initiatePasswordReset($email, $template, $websiteId = null)
579588
// load customer by email
580589
$customer = $this->customerRepository->get($email, $websiteId);
581590

591+
// No need to validate customer address while saving customer reset password token
592+
$this->disableAddressValidation($customer);
593+
582594
$newPasswordToken = $this->mathRandom->getUniqueHash();
583595
$this->changeResetPasswordLinkToken($customer, $newPasswordToken);
584596

@@ -669,6 +681,10 @@ public function resetPassword($email, $resetToken, $newPassword)
669681
} else {
670682
$customer = $this->customerRepository->get($email);
671683
}
684+
685+
// No need to validate customer address while saving customer reset password token
686+
$this->disableAddressValidation($customer);
687+
672688
//Validate Token and new password strength
673689
$this->validateResetPasswordToken($customer->getId(), $resetToken);
674690
$this->credentialsValidator->checkPasswordDifferentFromEmail(
@@ -921,6 +937,8 @@ public function getDefaultShippingAddress($customerId)
921937
* @param CustomerInterface $customer
922938
* @param string $redirectUrl
923939
* @return void
940+
* @throws LocalizedException
941+
* @throws NoSuchEntityException
924942
*/
925943
protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl)
926944
{
@@ -975,7 +993,10 @@ public function changePasswordById($customerId, $currentPassword, $newPassword)
975993
* @param string $newPassword
976994
* @return bool true on success
977995
* @throws InputException
996+
* @throws InputMismatchException
978997
* @throws InvalidEmailOrPasswordException
998+
* @throws LocalizedException
999+
* @throws NoSuchEntityException
9791000
* @throws UserLockedException
9801001
*/
9811002
private function changePasswordForCustomer($customer, $currentPassword, $newPassword)
@@ -1190,6 +1211,8 @@ protected function sendNewAccountEmail(
11901211
*
11911212
* @param CustomerInterface $customer
11921213
* @return $this
1214+
* @throws LocalizedException
1215+
* @throws NoSuchEntityException
11931216
* @deprecated 100.1.0
11941217
*/
11951218
protected function sendPasswordResetNotificationEmail($customer)
@@ -1252,6 +1275,7 @@ protected function getTemplateTypes()
12521275
* @param int|null $storeId
12531276
* @param string $email
12541277
* @return $this
1278+
* @throws MailException
12551279
* @deprecated 100.1.0
12561280
*/
12571281
protected function sendEmailTemplate(
@@ -1367,6 +1391,9 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)
13671391
* @param string $passwordLinkToken
13681392
* @return bool
13691393
* @throws InputException
1394+
* @throws InputMismatchException
1395+
* @throws LocalizedException
1396+
* @throws NoSuchEntityException
13701397
*/
13711398
public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13721399
{
@@ -1394,6 +1421,8 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
13941421
*
13951422
* @param CustomerInterface $customer
13961423
* @return $this
1424+
* @throws LocalizedException
1425+
* @throws NoSuchEntityException
13971426
* @deprecated 100.1.0
13981427
*/
13991428
public function sendPasswordReminderEmail($customer)
@@ -1421,6 +1450,8 @@ public function sendPasswordReminderEmail($customer)
14211450
*
14221451
* @param CustomerInterface $customer
14231452
* @return $this
1453+
* @throws LocalizedException
1454+
* @throws NoSuchEntityException
14241455
* @deprecated 100.1.0
14251456
*/
14261457
public function sendPasswordResetConfirmationEmail($customer)
@@ -1465,6 +1496,7 @@ protected function getAddressById(CustomerInterface $customer, $addressId)
14651496
*
14661497
* @param CustomerInterface $customer
14671498
* @return Data\CustomerSecure
1499+
* @throws NoSuchEntityException
14681500
* @deprecated 100.1.0
14691501
*/
14701502
protected function getFullCustomerObject($customer)
@@ -1492,6 +1524,20 @@ public function getPasswordHash($password)
14921524
return $this->encryptor->getHash($password);
14931525
}
14941526

1527+
/**
1528+
* Disable Customer Address Validation
1529+
*
1530+
* @param CustomerInterface $customer
1531+
* @throws NoSuchEntityException
1532+
*/
1533+
private function disableAddressValidation($customer)
1534+
{
1535+
foreach ($customer->getAddresses() as $address) {
1536+
$addressModel = $this->addressRegistry->retrieve($address->getId());
1537+
$addressModel->setShouldIgnoreValidation(true);
1538+
}
1539+
}
1540+
14951541
/**
14961542
* Get email notification
14971543
*

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
namespace Magento\Customer\Test\Unit\Model;
88

9-
use Magento\Customer\Model\AccountManagement;
109
use Magento\Customer\Model\AccountConfirmation;
10+
use Magento\Customer\Model\AccountManagement;
1111
use Magento\Customer\Model\AuthenticationInterface;
1212
use Magento\Customer\Model\EmailNotificationInterface;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
1314
use Magento\Framework\App\Area;
1415
use Magento\Framework\Exception\NoSuchEntityException;
1516
use Magento\Framework\Intl\DateTimeFactory;
@@ -147,6 +148,11 @@ class AccountManagementTest extends \PHPUnit\Framework\TestCase
147148
*/
148149
private $addressRegistryMock;
149150

151+
/**
152+
* @var \PHPUnit_Framework_MockObject_MockObject|SearchCriteriaBuilder
153+
*/
154+
private $searchCriteriaBuilderMock;
155+
150156
/**
151157
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
152158
*/
@@ -199,6 +205,7 @@ protected function setUp()
199205

200206
$this->dateTimeFactory = $this->createMock(DateTimeFactory::class);
201207
$this->accountConfirmation = $this->createMock(AccountConfirmation::class);
208+
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
202209

203210
$this->visitorCollectionFactory = $this->getMockBuilder(
204211
\Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
@@ -245,6 +252,7 @@ protected function setUp()
245252
'sessionManager' => $this->sessionManager,
246253
'saveHandler' => $this->saveHandler,
247254
'visitorCollectionFactory' => $this->visitorCollectionFactory,
255+
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
248256
'addressRegistry' => $this->addressRegistryMock,
249257
]
250258
);
@@ -1289,11 +1297,11 @@ public function testInitiatePasswordResetNoTemplate()
12891297

12901298
/**
12911299
* @expectedException \Magento\Framework\Exception\InputException
1292-
* @expectedExceptionMessage Invalid value of "" provided for the customerId field
1300+
* @expectedExceptionMessage Invalid value of "0" provided for the customerId field
12931301
*/
12941302
public function testValidateResetPasswordTokenBadCustomerId()
12951303
{
1296-
$this->accountManagement->validateResetPasswordLinkToken(null, '');
1304+
$this->accountManagement->validateResetPasswordLinkToken(0, '');
12971305
}
12981306

12991307
/**
@@ -1436,6 +1444,7 @@ private function reInitModel()
14361444
'encryptor' => $this->encryptor,
14371445
'dataProcessor' => $this->dataObjectProcessor,
14381446
'storeManager' => $this->storeManager,
1447+
'addressRegistry' => $this->addressRegistryMock,
14391448
'transportBuilder' => $this->transportBuilder,
14401449
]
14411450
);
@@ -1548,12 +1557,34 @@ public function testResetPassword()
15481557
{
15491558
$customerEmail = 'customer@example.com';
15501559
$customerId = '1';
1560+
$addressId = 5;
15511561
$resetToken = 'newStringToken';
15521562
$newPassword = 'new_password';
15531563

15541564
$this->reInitModel();
1565+
/** @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $addressModel */
1566+
$addressModel = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->disableOriginalConstructor()
1567+
->setMethods(['setShouldIgnoreValidation'])->getMock();
1568+
1569+
/** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
1570+
$address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
1571+
$address->expects($this->any())
1572+
->method('getId')
1573+
->willReturn($addressId);
1574+
1575+
/** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
15551576
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)->getMock();
15561577
$customer->expects($this->any())->method('getId')->willReturn($customerId);
1578+
$customer->expects($this->any())
1579+
->method('getAddresses')
1580+
->willReturn([$address]);
1581+
$this->addressRegistryMock->expects($this->once())
1582+
->method('retrieve')
1583+
->with($addressId)
1584+
->willReturn($addressModel);
1585+
$addressModel->expects($this->once())
1586+
->method('setShouldIgnoreValidation')
1587+
->with(true);
15571588
$this->customerRepository->expects($this->atLeastOnce())->method('get')->with($customerEmail)
15581589
->willReturn($customer);
15591590
$this->customer->expects($this->atLeastOnce())->method('getResetPasswordLinkExpirationPeriod')

0 commit comments

Comments
 (0)