Skip to content

Commit b7d5fa5

Browse files
fix confirm customer by token
1 parent 09964ae commit b7d5fa5

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Magento\Customer\Model\ForgotPasswordToken;
99

10-
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\Exception\LocalizedException;
1113

1214
/**
1315
* Confirm customer by reset password token
@@ -20,37 +22,47 @@ class ConfirmCustomerByToken
2022
private $getByToken;
2123

2224
/**
23-
* @var CustomerResource
25+
* @var CustomerRepositoryInterface
2426
*/
25-
private $customerResource;
27+
private $customerRepository;
2628

2729
/**
28-
* ConfirmByToken constructor.
29-
*
3030
* @param GetCustomerByToken $getByToken
31-
* @param CustomerResource $customerResource
31+
* @param CustomerRepositoryInterface $customerRepository
3232
*/
33-
public function __construct(
34-
GetCustomerByToken $getByToken,
35-
CustomerResource $customerResource
36-
) {
33+
public function __construct(GetCustomerByToken $getByToken, CustomerRepositoryInterface $customerRepository)
34+
{
3735
$this->getByToken = $getByToken;
38-
$this->customerResource = $customerResource;
36+
$this->customerRepository = $customerRepository;
3937
}
4038

4139
/**
4240
* Confirm customer account my rp_token
4341
*
4442
* @param string $resetPasswordToken
45-
*
4643
* @return void
47-
* @throws \Magento\Framework\Exception\LocalizedException
44+
* @throws LocalizedException
4845
*/
4946
public function execute(string $resetPasswordToken): void
5047
{
5148
$customer = $this->getByToken->execute($resetPasswordToken);
5249
if ($customer->getConfirmation()) {
53-
$this->customerResource->updateColumn($customer->getId(), 'confirmation', null);
50+
$this->resetConfirmation($customer);
5451
}
5552
}
53+
54+
/**
55+
* Reset customer confirmation
56+
*
57+
* @param CustomerInterface $customer
58+
* @return void
59+
*/
60+
private function resetConfirmation(CustomerInterface $customer): void
61+
{
62+
// skip unnecessary address and customer validation
63+
$customer->setData('ignore_validation_flag', true);
64+
$customer->setConfirmation(null);
65+
66+
$this->customerRepository->save($customer);
67+
}
5668
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,4 @@ public function changeResetPasswordLinkToken(\Magento\Customer\Model\Customer $c
403403
}
404404
return $this;
405405
}
406-
407-
/**
408-
* @param int $customerId
409-
* @param string $column
410-
* @param string $value
411-
*/
412-
public function updateColumn($customerId, $column, $value)
413-
{
414-
$this->getConnection()->update(
415-
$this->getTable('customer_entity'),
416-
[$column => $value],
417-
[$this->getEntityIdField() . ' = ?' => $customerId]
418-
);
419-
420-
return $this;
421-
}
422406
}

app/code/Magento/Customer/Test/Unit/Model/ForgotPasswordToken/ConfirmCustomerByTokenTest.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
namespace Magento\Customer\Test\Unit\Model\ForgotPasswordToken;
99

10+
use Magento\Customer\Api\CustomerRepositoryInterface;
1011
use Magento\Customer\Api\Data\CustomerInterface;
11-
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
12-
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
13-
use Magento\Customer\Model\ResourceModel\Customer;
1412
use Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken;
13+
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
1514
use PHPUnit\Framework\MockObject\MockObject;
1615
use PHPUnit\Framework\TestCase;
1716

@@ -33,22 +32,26 @@ class ConfirmCustomerByTokenTest extends TestCase
3332
private $customerMock;
3433

3534
/**
36-
* @var CustomerResource|MockObject
35+
* @var CustomerRepositoryInterface|MockObject
3736
*/
38-
private $customerResourceMock;
37+
private $customerRepositoryMock;
3938

4039
/**
4140
* @inheritDoc
4241
*/
4342
protected function setUp(): void
4443
{
45-
$this->customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
46-
$this->customerResourceMock = $this->createMock(CustomerResource::class);
44+
$this->customerMock = $this->getMockBuilder(CustomerInterface::class)
45+
->disableOriginalConstructor()
46+
->addMethods(['setData'])
47+
->getMockForAbstractClass();
48+
49+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
4750

4851
$getCustomerByTokenMock = $this->createMock(GetCustomerByToken::class);
4952
$getCustomerByTokenMock->method('execute')->willReturn($this->customerMock);
5053

51-
$this->model = new ConfirmCustomerByToken($getCustomerByTokenMock, $this->customerResourceMock);
54+
$this->model = new ConfirmCustomerByToken($getCustomerByTokenMock, $this->customerRepositoryMock);
5255
}
5356

5457
/**
@@ -58,17 +61,18 @@ protected function setUp(): void
5861
*/
5962
public function testExecuteWithConfirmation(): void
6063
{
61-
$customerId = 777;
62-
6364
$this->customerMock->expects($this->once())
6465
->method('getConfirmation')
6566
->willReturn('GWz2ik7Kts517MXAgrm4DzfcxKayGCm4');
6667
$this->customerMock->expects($this->once())
67-
->method('getId')
68-
->willReturn($customerId);
69-
$this->customerResourceMock->expects($this->once())
70-
->method('updateColumn')
71-
->with($customerId, 'confirmation', null);
68+
->method('setData')
69+
->with('ignore_validation_flag', true);
70+
$this->customerMock->expects($this->once())
71+
->method('setConfirmation')
72+
->with(null);
73+
$this->customerRepositoryMock->expects($this->once())
74+
->method('save')
75+
->with($this->customerMock);
7276

7377
$this->model->execute(self::STUB_RESET_PASSWORD_TOKEN);
7478
}
@@ -83,8 +87,8 @@ public function testExecuteWithoutConfirmation(): void
8387
$this->customerMock->expects($this->once())
8488
->method('getConfirmation')
8589
->willReturn(null);
86-
$this->customerResourceMock->expects($this->never())
87-
->method('updateColumn');
90+
$this->customerRepositoryMock->expects($this->never())
91+
->method('save');
8892

8993
$this->model->execute(self::STUB_RESET_PASSWORD_TOKEN);
9094
}

0 commit comments

Comments
 (0)