Skip to content

Commit

Permalink
Update the fix for reset password issue when a country is not allowed…
Browse files Browse the repository at this point in the history
…, update the PHPUnit tests #18170
  • Loading branch information
dmytro-ch committed Nov 26, 2018
1 parent 33e42ac commit 97632b3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
54 changes: 50 additions & 4 deletions app/code/Magento/Customer/Model/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Customer\Model\Customer as CustomerModel;
use Magento\Customer\Model\Customer\CredentialsValidator;
use Magento\Customer\Model\Metadata\Validator;
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
use Magento\Eav\Model\Validator\Attribute\Backend;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Api\SearchCriteriaBuilder;
Expand Down Expand Up @@ -45,14 +46,13 @@
use Magento\Framework\Phrase;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Framework\Registry;
use Magento\Framework\Session\SaveHandlerInterface;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\StringUtils as StringHelper;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface as PsrLogger;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Session\SaveHandlerInterface;
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;

/**
* Handle various customer account actions
Expand Down Expand Up @@ -333,6 +333,11 @@ class AccountManagement implements AccountManagementInterface
*/
private $searchCriteriaBuilder;

/**
* @var AddressRegistry
*/
private $addressRegistry;

/**
* @param CustomerFactory $customerFactory
* @param ManagerInterface $eventManager
Expand Down Expand Up @@ -364,6 +369,7 @@ class AccountManagement implements AccountManagementInterface
* @param SaveHandlerInterface|null $saveHandler
* @param CollectionFactory|null $visitorCollectionFactory
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
* @param AddressRegistry|null $addressRegistry
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -396,7 +402,8 @@ public function __construct(
SessionManagerInterface $sessionManager = null,
SaveHandlerInterface $saveHandler = null,
CollectionFactory $visitorCollectionFactory = null,
SearchCriteriaBuilder $searchCriteriaBuilder = null
SearchCriteriaBuilder $searchCriteriaBuilder = null,
AddressRegistry $addressRegistry = null
) {
$this->customerFactory = $customerFactory;
$this->eventManager = $eventManager;
Expand Down Expand Up @@ -434,6 +441,8 @@ public function __construct(
?: ObjectManager::getInstance()->get(CollectionFactory::class);
$this->searchCriteriaBuilder = $searchCriteriaBuilder
?: ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
$this->addressRegistry = $addressRegistry
?: ObjectManager::getInstance()->get(AddressRegistry::class);
}

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

// No need to validate customer address while saving customer reset password token
$this->disableAddressValidation($customer);

$newPasswordToken = $this->mathRandom->getUniqueHash();
$this->changeResetPasswordLinkToken($customer, $newPasswordToken);

Expand Down Expand Up @@ -669,6 +681,10 @@ public function resetPassword($email, $resetToken, $newPassword)
} else {
$customer = $this->customerRepository->get($email);
}

// No need to validate customer address while saving customer reset password token
$this->disableAddressValidation($customer);

//Validate Token and new password strength
$this->validateResetPasswordToken($customer->getId(), $resetToken);
$this->credentialsValidator->checkPasswordDifferentFromEmail(
Expand Down Expand Up @@ -921,6 +937,8 @@ public function getDefaultShippingAddress($customerId)
* @param CustomerInterface $customer
* @param string $redirectUrl
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl)
{
Expand Down Expand Up @@ -975,7 +993,10 @@ public function changePasswordById($customerId, $currentPassword, $newPassword)
* @param string $newPassword
* @return bool true on success
* @throws InputException
* @throws InputMismatchException
* @throws InvalidEmailOrPasswordException
* @throws LocalizedException
* @throws NoSuchEntityException
* @throws UserLockedException
*/
private function changePasswordForCustomer($customer, $currentPassword, $newPassword)
Expand Down Expand Up @@ -1190,6 +1211,8 @@ protected function sendNewAccountEmail(
*
* @param CustomerInterface $customer
* @return $this
* @throws LocalizedException
* @throws NoSuchEntityException
* @deprecated 100.1.0
*/
protected function sendPasswordResetNotificationEmail($customer)
Expand Down Expand Up @@ -1252,6 +1275,7 @@ protected function getTemplateTypes()
* @param int|null $storeId
* @param string $email
* @return $this
* @throws MailException
* @deprecated 100.1.0
*/
protected function sendEmailTemplate(
Expand Down Expand Up @@ -1367,6 +1391,9 @@ public function isResetPasswordLinkTokenExpired($rpToken, $rpTokenCreatedAt)
* @param string $passwordLinkToken
* @return bool
* @throws InputException
* @throws InputMismatchException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
{
Expand Down Expand Up @@ -1394,6 +1421,8 @@ public function changeResetPasswordLinkToken($customer, $passwordLinkToken)
*
* @param CustomerInterface $customer
* @return $this
* @throws LocalizedException
* @throws NoSuchEntityException
* @deprecated 100.1.0
*/
public function sendPasswordReminderEmail($customer)
Expand Down Expand Up @@ -1421,6 +1450,8 @@ public function sendPasswordReminderEmail($customer)
*
* @param CustomerInterface $customer
* @return $this
* @throws LocalizedException
* @throws NoSuchEntityException
* @deprecated 100.1.0
*/
public function sendPasswordResetConfirmationEmail($customer)
Expand Down Expand Up @@ -1465,6 +1496,7 @@ protected function getAddressById(CustomerInterface $customer, $addressId)
*
* @param CustomerInterface $customer
* @return Data\CustomerSecure
* @throws NoSuchEntityException
* @deprecated 100.1.0
*/
protected function getFullCustomerObject($customer)
Expand Down Expand Up @@ -1492,6 +1524,20 @@ public function getPasswordHash($password)
return $this->encryptor->getHash($password);
}

/**
* Disable Customer Address Validation
*
* @param CustomerInterface $customer
* @throws NoSuchEntityException
*/
private function disableAddressValidation($customer)
{
foreach ($customer->getAddresses() as $address) {
$addressModel = $this->addressRegistry->retrieve($address->getId());
$addressModel->setShouldIgnoreValidation(true);
}
}

/**
* Get email notification
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace Magento\Customer\Test\Unit\Model;

use Magento\Customer\Model\AccountManagement;
use Magento\Customer\Model\AccountConfirmation;
use Magento\Customer\Model\AccountManagement;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Customer\Model\EmailNotificationInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Intl\DateTimeFactory;
Expand Down Expand Up @@ -147,6 +148,11 @@ class AccountManagementTest extends \PHPUnit\Framework\TestCase
*/
private $addressRegistryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|SearchCriteriaBuilder
*/
private $searchCriteriaBuilderMock;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
Expand Down Expand Up @@ -199,6 +205,7 @@ protected function setUp()

$this->dateTimeFactory = $this->createMock(DateTimeFactory::class);
$this->accountConfirmation = $this->createMock(AccountConfirmation::class);
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);

$this->visitorCollectionFactory = $this->getMockBuilder(
\Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory::class
Expand Down Expand Up @@ -245,6 +252,7 @@ protected function setUp()
'sessionManager' => $this->sessionManager,
'saveHandler' => $this->saveHandler,
'visitorCollectionFactory' => $this->visitorCollectionFactory,
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
'addressRegistry' => $this->addressRegistryMock,
]
);
Expand Down Expand Up @@ -1289,11 +1297,11 @@ public function testInitiatePasswordResetNoTemplate()

/**
* @expectedException \Magento\Framework\Exception\InputException
* @expectedExceptionMessage Invalid value of "" provided for the customerId field
* @expectedExceptionMessage Invalid value of "0" provided for the customerId field
*/
public function testValidateResetPasswordTokenBadCustomerId()
{
$this->accountManagement->validateResetPasswordLinkToken(null, '');
$this->accountManagement->validateResetPasswordLinkToken(0, '');
}

/**
Expand Down Expand Up @@ -1436,6 +1444,7 @@ private function reInitModel()
'encryptor' => $this->encryptor,
'dataProcessor' => $this->dataObjectProcessor,
'storeManager' => $this->storeManager,
'addressRegistry' => $this->addressRegistryMock,
'transportBuilder' => $this->transportBuilder,
]
);
Expand Down Expand Up @@ -1548,12 +1557,34 @@ public function testResetPassword()
{
$customerEmail = 'customer@example.com';
$customerId = '1';
$addressId = 5;
$resetToken = 'newStringToken';
$newPassword = 'new_password';

$this->reInitModel();
/** @var \Magento\Customer\Model\Address|\PHPUnit_Framework_MockObject_MockObject $addressModel */
$addressModel = $this->getMockBuilder(\Magento\Customer\Model\Address::class)->disableOriginalConstructor()
->setMethods(['setShouldIgnoreValidation'])->getMock();

/** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
$address = $this->createMock(\Magento\Customer\Api\Data\AddressInterface::class);
$address->expects($this->any())
->method('getId')
->willReturn($addressId);

/** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)->getMock();
$customer->expects($this->any())->method('getId')->willReturn($customerId);
$customer->expects($this->any())
->method('getAddresses')
->willReturn([$address]);
$this->addressRegistryMock->expects($this->once())
->method('retrieve')
->with($addressId)
->willReturn($addressModel);
$addressModel->expects($this->once())
->method('setShouldIgnoreValidation')
->with(true);
$this->customerRepository->expects($this->atLeastOnce())->method('get')->with($customerEmail)
->willReturn($customer);
$this->customer->expects($this->atLeastOnce())->method('getResetPasswordLinkExpirationPeriod')
Expand Down

0 comments on commit 97632b3

Please sign in to comment.