Skip to content

[BUGFIX] Made method public so a plugin is possible. #11878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions app/code/Magento/Customer/Model/AccountConfirmation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Model;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Registry;

/**
* Class AccountConfirmation.
* Checks if email confirmation required for customer.
*/
class AccountConfirmation
{
/**
* Configuration path for email confirmation.
*/
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var Registry
*/
private $registry;

/**
* @param ScopeConfigInterface $scopeConfig
* @param Registry $registry
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Registry $registry
) {
$this->scopeConfig = $scopeConfig;
$this->registry = $registry;
}

/**
* Check if accounts confirmation is required.
*
* @param int|null $websiteId
* @param int|null $customerId
* @param string $customerEmail
* @return bool
*/
public function isConfirmationRequired($websiteId, $customerId, $customerEmail): bool
{
if ($this->canSkipConfirmation($customerId, $customerEmail)) {
return false;
}

return (bool)$this->scopeConfig->getValue(
self::XML_PATH_IS_CONFIRM,
ScopeInterface::SCOPE_WEBSITES,
$websiteId
);
}

/**
* Check whether confirmation may be skipped when registering using certain email address.
*
* @param int|null $customerId
* @param string $customerEmail
* @return bool
*/
private function canSkipConfirmation($customerId, $customerEmail): bool
{
if (!$customerId) {
return false;
}

/* If an email was used to start the registration process and it is the same email as the one
used to register, then this can skip confirmation.
*/
$skipConfirmationIfEmail = $this->registry->registry("skip_confirmation_if_email");
if (!$skipConfirmationIfEmail) {
return false;
}

return strtolower($skipConfirmationIfEmail) === strtolower($customerEmail);
}
}
33 changes: 23 additions & 10 deletions app/code/Magento/Customer/Model/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class AccountManagement implements AccountManagementInterface
*/
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';

/**
* @deprecated
* @see AccountConfirmation::XML_PATH_IS_CONFIRM
*/
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';

/**
Expand Down Expand Up @@ -298,6 +302,11 @@ class AccountManagement implements AccountManagementInterface
*/
private $dateTimeFactory;

/**
* @var AccountConfirmation
*/
private $accountConfirmation;

/**
* @param CustomerFactory $customerFactory
* @param ManagerInterface $eventManager
Expand All @@ -323,7 +332,8 @@ class AccountManagement implements AccountManagementInterface
* @param ObjectFactory $objectFactory
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
* @param CredentialsValidator|null $credentialsValidator
* @param DateTimeFactory $dateTimeFactory
* @param DateTimeFactory|null $dateTimeFactory
* @param AccountConfirmation|null $accountConfirmation
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -351,7 +361,8 @@ public function __construct(
ObjectFactory $objectFactory,
ExtensibleDataObjectConverter $extensibleDataObjectConverter,
CredentialsValidator $credentialsValidator = null,
DateTimeFactory $dateTimeFactory = null
DateTimeFactory $dateTimeFactory = null,
AccountConfirmation $accountConfirmation = null
) {
$this->customerFactory = $customerFactory;
$this->eventManager = $eventManager;
Expand Down Expand Up @@ -379,6 +390,8 @@ public function __construct(
$this->credentialsValidator =
$credentialsValidator ?: ObjectManager::getInstance()->get(CredentialsValidator::class);
$this->dateTimeFactory = $dateTimeFactory ?: ObjectManager::getInstance()->get(DateTimeFactory::class);
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
->get(AccountConfirmation::class);
}

/**
Expand Down Expand Up @@ -1149,17 +1162,15 @@ protected function sendEmailTemplate(
*
* @param CustomerInterface $customer
* @return bool
* @deprecated
* @see AccountConfirmation::isConfirmationRequired
*/
protected function isConfirmationRequired($customer)
{
if ($this->canSkipConfirmation($customer)) {
return false;
}

return (bool)$this->scopeConfig->getValue(
self::XML_PATH_IS_CONFIRM,
ScopeInterface::SCOPE_WEBSITES,
$customer->getWebsiteId()
return $this->accountConfirmation->isConfirmationRequired(
$customer->getWebsiteId(),
$customer->getId(),
$customer->getEmail()
);
}

Expand All @@ -1168,6 +1179,8 @@ protected function isConfirmationRequired($customer)
*
* @param CustomerInterface $customer
* @return bool
* @deprecated
* @see AccountConfirmation::isConfirmationRequired
*/
protected function canSkipConfirmation($customer)
{
Expand Down
30 changes: 20 additions & 10 deletions app/code/Magento/Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Framework\Indexer\StateInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\ObjectManager;

/**
* Customer model
Expand Down Expand Up @@ -58,6 +59,10 @@ class Customer extends \Magento\Framework\Model\AbstractModel

const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';

/**
* @deprecated
* @see AccountConfirmation::XML_PATH_IS_CONFIRM
*/
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';

const XML_PATH_CONFIRM_EMAIL_TEMPLATE = 'customer/create_account/email_confirmation_template';
Expand Down Expand Up @@ -208,6 +213,11 @@ class Customer extends \Magento\Framework\Model\AbstractModel
*/
protected $indexerRegistry;

/**
* @var AccountConfirmation
*/
private $accountConfirmation;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -229,6 +239,7 @@ class Customer extends \Magento\Framework\Model\AbstractModel
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
* @param array $data
* @param AccountConfirmation|null $accountConfirmation
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -252,7 +263,8 @@ public function __construct(
\Magento\Customer\Api\CustomerMetadataInterface $metadataService,
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
AccountConfirmation $accountConfirmation = null
) {
$this->metadataService = $metadataService;
$this->_scopeConfig = $scopeConfig;
Expand All @@ -269,6 +281,8 @@ public function __construct(
$this->dataObjectProcessor = $dataObjectProcessor;
$this->dataObjectHelper = $dataObjectHelper;
$this->indexerRegistry = $indexerRegistry;
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
->get(AccountConfirmation::class);
parent::__construct(
$context,
$registry,
Expand Down Expand Up @@ -770,20 +784,14 @@ public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeI
* Check if accounts confirmation is required in config
*
* @return bool
* @deprecated
* @see AccountConfirmation::isConfirmationRequired
*/
public function isConfirmationRequired()
{
if ($this->canSkipConfirmation()) {
return false;
}

$websiteId = $this->getWebsiteId() ? $this->getWebsiteId() : null;

return (bool)$this->_scopeConfig->getValue(
self::XML_PATH_IS_CONFIRM,
ScopeInterface::SCOPE_WEBSITES,
$websiteId
);
return $this->accountConfirmation->isConfirmationRequired($websiteId, $this->getId(), $this->getEmail());
}

/**
Expand Down Expand Up @@ -1156,6 +1164,8 @@ public function setIsReadonly($value)
* Check whether confirmation may be skipped when registering using certain email address
*
* @return bool
* @deprecated
* @see AccountConfirmation::isConfirmationRequired
*/
protected function canSkipConfirmation()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Model;

use Magento\Customer\Model\AccountConfirmation;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Registry;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AccountConfirmationTest extends \PHPUnit\Framework\TestCase
{
/**
* @var AccountConfirmation|\PHPUnit_Framework_MockObject_MockObject
*/
private $accountConfirmation;

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfig;

/**
* @var Registry|\PHPUnit_Framework_MockObject_MockObject
*/
private $registry;

protected function setUp()
{
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
$this->registry = $this->createMock(Registry::class);

$this->accountConfirmation = new AccountConfirmation(
$this->scopeConfig,
$this->registry
);
}

/**
* @param $customerId
* @param $customerEmail
* @param $skipConfirmationIfEmail
* @param $isConfirmationEnabled
* @param $expected
* @dataProvider dataProviderIsConfirmationRequired
*/
public function testIsConfirmationRequired(
$customerId,
$customerEmail,
$skipConfirmationIfEmail,
$isConfirmationEnabled,
$expected
) {
$websiteId = 1;

$this->scopeConfig->expects($this->any())
->method('getValue')
->with(
$this->accountConfirmation::XML_PATH_IS_CONFIRM,
ScopeInterface::SCOPE_WEBSITES,
$websiteId
)->willReturn($isConfirmationEnabled);

$this->registry->expects($this->any())
->method('registry')
->with('skip_confirmation_if_email')
->willReturn($skipConfirmationIfEmail);

self::assertEquals(
$expected,
$this->accountConfirmation->isConfirmationRequired($websiteId, $customerId, $customerEmail)
);
}

/**
* @return array
*/
public function dataProviderIsConfirmationRequired()
{
return [
[null, '[email protected]', null, true, true],
[null, '[email protected]', null, false, false],
[1, '[email protected]', '[email protected]', true, false],
[1, '[email protected]', '[email protected]', false, false],
[1, '[email protected]', '[email protected]', true, true],
];
}
}
Loading