Skip to content

Commit 553c682

Browse files
author
Oleksii Korshenko
authored
MAGETWO-83026: [BUGFIX] Made method public so a plugin is possible. #11878
2 parents 3563c25 + bafa7ea commit 553c682

File tree

8 files changed

+301
-86
lines changed

8 files changed

+301
-86
lines changed
Lines changed: 89 additions & 0 deletions
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+
namespace Magento\Customer\Model;
7+
8+
use Magento\Store\Model\ScopeInterface;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Registry;
11+
12+
/**
13+
* Class AccountConfirmation.
14+
* Checks if email confirmation required for customer.
15+
*/
16+
class AccountConfirmation
17+
{
18+
/**
19+
* Configuration path for email confirmation.
20+
*/
21+
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
22+
23+
/**
24+
* @var ScopeConfigInterface
25+
*/
26+
private $scopeConfig;
27+
28+
/**
29+
* @var Registry
30+
*/
31+
private $registry;
32+
33+
/**
34+
* @param ScopeConfigInterface $scopeConfig
35+
* @param Registry $registry
36+
*/
37+
public function __construct(
38+
ScopeConfigInterface $scopeConfig,
39+
Registry $registry
40+
) {
41+
$this->scopeConfig = $scopeConfig;
42+
$this->registry = $registry;
43+
}
44+
45+
/**
46+
* Check if accounts confirmation is required.
47+
*
48+
* @param int|null $websiteId
49+
* @param int|null $customerId
50+
* @param string $customerEmail
51+
* @return bool
52+
*/
53+
public function isConfirmationRequired($websiteId, $customerId, $customerEmail): bool
54+
{
55+
if ($this->canSkipConfirmation($customerId, $customerEmail)) {
56+
return false;
57+
}
58+
59+
return (bool)$this->scopeConfig->getValue(
60+
self::XML_PATH_IS_CONFIRM,
61+
ScopeInterface::SCOPE_WEBSITES,
62+
$websiteId
63+
);
64+
}
65+
66+
/**
67+
* Check whether confirmation may be skipped when registering using certain email address.
68+
*
69+
* @param int|null $customerId
70+
* @param string $customerEmail
71+
* @return bool
72+
*/
73+
private function canSkipConfirmation($customerId, $customerEmail): bool
74+
{
75+
if (!$customerId) {
76+
return false;
77+
}
78+
79+
/* If an email was used to start the registration process and it is the same email as the one
80+
used to register, then this can skip confirmation.
81+
*/
82+
$skipConfirmationIfEmail = $this->registry->registry("skip_confirmation_if_email");
83+
if (!$skipConfirmationIfEmail) {
84+
return false;
85+
}
86+
87+
return strtolower($skipConfirmationIfEmail) === strtolower($customerEmail);
88+
}
89+
}

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ class AccountManagement implements AccountManagementInterface
8989
*/
9090
const XML_PATH_FORGOT_EMAIL_IDENTITY = 'customer/password/forgot_email_identity';
9191

92+
/**
93+
* @deprecated
94+
* @see AccountConfirmation::XML_PATH_IS_CONFIRM
95+
*/
9296
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
9397

9498
/**
@@ -298,6 +302,11 @@ class AccountManagement implements AccountManagementInterface
298302
*/
299303
private $dateTimeFactory;
300304

305+
/**
306+
* @var AccountConfirmation
307+
*/
308+
private $accountConfirmation;
309+
301310
/**
302311
* @param CustomerFactory $customerFactory
303312
* @param ManagerInterface $eventManager
@@ -323,7 +332,8 @@ class AccountManagement implements AccountManagementInterface
323332
* @param ObjectFactory $objectFactory
324333
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
325334
* @param CredentialsValidator|null $credentialsValidator
326-
* @param DateTimeFactory $dateTimeFactory
335+
* @param DateTimeFactory|null $dateTimeFactory
336+
* @param AccountConfirmation|null $accountConfirmation
327337
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
328338
*/
329339
public function __construct(
@@ -351,7 +361,8 @@ public function __construct(
351361
ObjectFactory $objectFactory,
352362
ExtensibleDataObjectConverter $extensibleDataObjectConverter,
353363
CredentialsValidator $credentialsValidator = null,
354-
DateTimeFactory $dateTimeFactory = null
364+
DateTimeFactory $dateTimeFactory = null,
365+
AccountConfirmation $accountConfirmation = null
355366
) {
356367
$this->customerFactory = $customerFactory;
357368
$this->eventManager = $eventManager;
@@ -379,6 +390,8 @@ public function __construct(
379390
$this->credentialsValidator =
380391
$credentialsValidator ?: ObjectManager::getInstance()->get(CredentialsValidator::class);
381392
$this->dateTimeFactory = $dateTimeFactory ?: ObjectManager::getInstance()->get(DateTimeFactory::class);
393+
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
394+
->get(AccountConfirmation::class);
382395
}
383396

384397
/**
@@ -1149,17 +1162,15 @@ protected function sendEmailTemplate(
11491162
*
11501163
* @param CustomerInterface $customer
11511164
* @return bool
1165+
* @deprecated
1166+
* @see AccountConfirmation::isConfirmationRequired
11521167
*/
11531168
protected function isConfirmationRequired($customer)
11541169
{
1155-
if ($this->canSkipConfirmation($customer)) {
1156-
return false;
1157-
}
1158-
1159-
return (bool)$this->scopeConfig->getValue(
1160-
self::XML_PATH_IS_CONFIRM,
1161-
ScopeInterface::SCOPE_WEBSITES,
1162-
$customer->getWebsiteId()
1170+
return $this->accountConfirmation->isConfirmationRequired(
1171+
$customer->getWebsiteId(),
1172+
$customer->getId(),
1173+
$customer->getEmail()
11631174
);
11641175
}
11651176

@@ -1168,6 +1179,8 @@ protected function isConfirmationRequired($customer)
11681179
*
11691180
* @param CustomerInterface $customer
11701181
* @return bool
1182+
* @deprecated
1183+
* @see AccountConfirmation::isConfirmationRequired
11711184
*/
11721185
protected function canSkipConfirmation($customer)
11731186
{

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\Framework\Indexer\StateInterface;
1919
use Magento\Framework\Reflection\DataObjectProcessor;
2020
use Magento\Store\Model\ScopeInterface;
21+
use Magento\Framework\App\ObjectManager;
2122

2223
/**
2324
* Customer model
@@ -58,6 +59,10 @@ class Customer extends \Magento\Framework\Model\AbstractModel
5859

5960
const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';
6061

62+
/**
63+
* @deprecated
64+
* @see AccountConfirmation::XML_PATH_IS_CONFIRM
65+
*/
6166
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
6267

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

216+
/**
217+
* @var AccountConfirmation
218+
*/
219+
private $accountConfirmation;
220+
211221
/**
212222
* @param \Magento\Framework\Model\Context $context
213223
* @param \Magento\Framework\Registry $registry
@@ -229,6 +239,7 @@ class Customer extends \Magento\Framework\Model\AbstractModel
229239
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
230240
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
231241
* @param array $data
242+
* @param AccountConfirmation|null $accountConfirmation
232243
*
233244
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
234245
*/
@@ -252,7 +263,8 @@ public function __construct(
252263
\Magento\Customer\Api\CustomerMetadataInterface $metadataService,
253264
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
254265
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
255-
array $data = []
266+
array $data = [],
267+
AccountConfirmation $accountConfirmation = null
256268
) {
257269
$this->metadataService = $metadataService;
258270
$this->_scopeConfig = $scopeConfig;
@@ -269,6 +281,8 @@ public function __construct(
269281
$this->dataObjectProcessor = $dataObjectProcessor;
270282
$this->dataObjectHelper = $dataObjectHelper;
271283
$this->indexerRegistry = $indexerRegistry;
284+
$this->accountConfirmation = $accountConfirmation ?: ObjectManager::getInstance()
285+
->get(AccountConfirmation::class);
272286
parent::__construct(
273287
$context,
274288
$registry,
@@ -770,20 +784,14 @@ public function sendNewAccountEmail($type = 'registered', $backUrl = '', $storeI
770784
* Check if accounts confirmation is required in config
771785
*
772786
* @return bool
787+
* @deprecated
788+
* @see AccountConfirmation::isConfirmationRequired
773789
*/
774790
public function isConfirmationRequired()
775791
{
776-
if ($this->canSkipConfirmation()) {
777-
return false;
778-
}
779-
780792
$websiteId = $this->getWebsiteId() ? $this->getWebsiteId() : null;
781793

782-
return (bool)$this->_scopeConfig->getValue(
783-
self::XML_PATH_IS_CONFIRM,
784-
ScopeInterface::SCOPE_WEBSITES,
785-
$websiteId
786-
);
794+
return $this->accountConfirmation->isConfirmationRequired($websiteId, $this->getId(), $this->getEmail());
787795
}
788796

789797
/**
@@ -1156,6 +1164,8 @@ public function setIsReadonly($value)
11561164
* Check whether confirmation may be skipped when registering using certain email address
11571165
*
11581166
* @return bool
1167+
* @deprecated
1168+
* @see AccountConfirmation::isConfirmationRequired
11591169
*/
11601170
protected function canSkipConfirmation()
11611171
{
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Model;
7+
8+
use Magento\Customer\Model\AccountConfirmation;
9+
use Magento\Store\Model\ScopeInterface;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Registry;
12+
13+
/**
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15+
*/
16+
class AccountConfirmationTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var AccountConfirmation|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $accountConfirmation;
22+
23+
/**
24+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $scopeConfig;
27+
28+
/**
29+
* @var Registry|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $registry;
32+
33+
protected function setUp()
34+
{
35+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
36+
$this->registry = $this->createMock(Registry::class);
37+
38+
$this->accountConfirmation = new AccountConfirmation(
39+
$this->scopeConfig,
40+
$this->registry
41+
);
42+
}
43+
44+
/**
45+
* @param $customerId
46+
* @param $customerEmail
47+
* @param $skipConfirmationIfEmail
48+
* @param $isConfirmationEnabled
49+
* @param $expected
50+
* @dataProvider dataProviderIsConfirmationRequired
51+
*/
52+
public function testIsConfirmationRequired(
53+
$customerId,
54+
$customerEmail,
55+
$skipConfirmationIfEmail,
56+
$isConfirmationEnabled,
57+
$expected
58+
) {
59+
$websiteId = 1;
60+
61+
$this->scopeConfig->expects($this->any())
62+
->method('getValue')
63+
->with(
64+
$this->accountConfirmation::XML_PATH_IS_CONFIRM,
65+
ScopeInterface::SCOPE_WEBSITES,
66+
$websiteId
67+
)->willReturn($isConfirmationEnabled);
68+
69+
$this->registry->expects($this->any())
70+
->method('registry')
71+
->with('skip_confirmation_if_email')
72+
->willReturn($skipConfirmationIfEmail);
73+
74+
self::assertEquals(
75+
$expected,
76+
$this->accountConfirmation->isConfirmationRequired($websiteId, $customerId, $customerEmail)
77+
);
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function dataProviderIsConfirmationRequired()
84+
{
85+
return [
86+
[null, '[email protected]', null, true, true],
87+
[null, '[email protected]', null, false, false],
88+
[1, '[email protected]', '[email protected]', true, false],
89+
[1, '[email protected]', '[email protected]', false, false],
90+
[1, '[email protected]', '[email protected]', true, true],
91+
];
92+
}
93+
}

0 commit comments

Comments
 (0)