Skip to content

Commit 777203c

Browse files
ENGCOM-8388: ISSUE-27954: Forgot password save user only one column #27972
- Merge Pull Request #27972 from sheepfy/magento2:issue-27954/forgot-password - Merged commits: 1. 29621ba 2. bc52285 3. 4f8f9f2 4. 09964ae 5. b7d5fa5
2 parents 632a7c6 + b7d5fa5 commit 777203c

File tree

4 files changed

+209
-12
lines changed

4 files changed

+209
-12
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\Customer\Model\ForgotPasswordToken;
99

1010
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
@@ -25,15 +27,11 @@ class ConfirmCustomerByToken
2527
private $customerRepository;
2628

2729
/**
28-
* ConfirmByToken constructor.
29-
*
3030
* @param GetCustomerByToken $getByToken
3131
* @param CustomerRepositoryInterface $customerRepository
3232
*/
33-
public function __construct(
34-
GetCustomerByToken $getByToken,
35-
CustomerRepositoryInterface $customerRepository
36-
) {
33+
public function __construct(GetCustomerByToken $getByToken, CustomerRepositoryInterface $customerRepository)
34+
{
3735
$this->getByToken = $getByToken;
3836
$this->customerRepository = $customerRepository;
3937
}
@@ -42,17 +40,29 @@ public function __construct(
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->customerRepository->save(
54-
$customer->setConfirmation(null)
55-
);
50+
$this->resetConfirmation($customer);
5651
}
5752
}
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+
}
5868
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(
5454
* @throws NoSuchEntityException
5555
* @throws \Magento\Framework\Exception\LocalizedException
5656
*/
57-
public function execute(string $resetPasswordToken):CustomerInterface
57+
public function execute(string $resetPasswordToken): CustomerInterface
5858
{
5959
$this->searchCriteriaBuilder->addFilter(
6060
'rp_token',
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Test\Unit\Model\ForgotPasswordToken;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken;
13+
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Test for \Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken.
19+
*/
20+
class ConfirmCustomerByTokenTest extends TestCase
21+
{
22+
private const STUB_RESET_PASSWORD_TOKEN = 'resetPassword';
23+
24+
/**
25+
* @var ConfirmCustomerByToken;
26+
*/
27+
private $model;
28+
29+
/**
30+
* @var CustomerInterface|MockObject
31+
*/
32+
private $customerMock;
33+
34+
/**
35+
* @var CustomerRepositoryInterface|MockObject
36+
*/
37+
private $customerRepositoryMock;
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
protected function setUp(): void
43+
{
44+
$this->customerMock = $this->getMockBuilder(CustomerInterface::class)
45+
->disableOriginalConstructor()
46+
->addMethods(['setData'])
47+
->getMockForAbstractClass();
48+
49+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
50+
51+
$getCustomerByTokenMock = $this->createMock(GetCustomerByToken::class);
52+
$getCustomerByTokenMock->method('execute')->willReturn($this->customerMock);
53+
54+
$this->model = new ConfirmCustomerByToken($getCustomerByTokenMock, $this->customerRepositoryMock);
55+
}
56+
57+
/**
58+
* Confirm customer with confirmation
59+
*
60+
* @return void
61+
*/
62+
public function testExecuteWithConfirmation(): void
63+
{
64+
$this->customerMock->expects($this->once())
65+
->method('getConfirmation')
66+
->willReturn('GWz2ik7Kts517MXAgrm4DzfcxKayGCm4');
67+
$this->customerMock->expects($this->once())
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);
76+
77+
$this->model->execute(self::STUB_RESET_PASSWORD_TOKEN);
78+
}
79+
80+
/**
81+
* Confirm customer without confirmation
82+
*
83+
* @return void
84+
*/
85+
public function testExecuteWithoutConfirmation(): void
86+
{
87+
$this->customerMock->expects($this->once())
88+
->method('getConfirmation')
89+
->willReturn(null);
90+
$this->customerRepositoryMock->expects($this->never())
91+
->method('save');
92+
93+
$this->model->execute(self::STUB_RESET_PASSWORD_TOKEN);
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\ForgotPasswordToken;
9+
10+
use Magento\Customer\Model\Customer;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Test for \Magento\Customer\Model\ForgotPasswordToken\ConfirmCustomerByToken.
19+
*/
20+
class ConfirmCustomerByTokenTest extends TestCase
21+
{
22+
private const STUB_CUSTOMER_RESET_TOKEN = 'token12345';
23+
24+
/**
25+
* @var ObjectManagerInterface
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var ConfirmCustomerByToken
31+
*/
32+
private $confirmCustomerByToken;
33+
34+
/**
35+
* @var AdapterInterface
36+
*/
37+
private $connection;
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
protected function setUp(): void
43+
{
44+
$this->objectManager = Bootstrap::getObjectManager();
45+
46+
$resource = $this->objectManager->get(ResourceConnection::class);
47+
$this->connection = $resource->getConnection();
48+
49+
$this->confirmCustomerByToken = $this->objectManager->get(ConfirmCustomerByToken::class);
50+
}
51+
52+
/**
53+
* Customer address shouldn't validate during confirm customer by token
54+
*
55+
* @magentoDataFixture Magento/Customer/_files/customer.php
56+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
57+
*
58+
* @return void
59+
*/
60+
public function testExecuteWithInvalidAddress(): void
61+
{
62+
$id = 1;
63+
64+
$customerModel = $this->objectManager->create(Customer::class);
65+
$customerModel->load($id);
66+
$customerModel->setRpToken(self::STUB_CUSTOMER_RESET_TOKEN);
67+
$customerModel->setRpTokenCreatedAt(date('Y-m-d H:i:s'));
68+
$customerModel->setConfirmation($customerModel->getRandomConfirmationKey());
69+
$customerModel->save();
70+
71+
//make city address invalid
72+
$this->makeCityInvalid($id);
73+
74+
$this->confirmCustomerByToken->execute(self::STUB_CUSTOMER_RESET_TOKEN);
75+
$this->assertNull($customerModel->load($id)->getConfirmation());
76+
}
77+
78+
/**
79+
* Set city invalid for customer address
80+
*
81+
* @param int $id
82+
* @return void
83+
*/
84+
private function makeCityInvalid(int $id): void
85+
{
86+
$this->connection->update(
87+
$this->connection->getTableName('customer_address_entity'),
88+
['city' => ''],
89+
$this->connection->quoteInto('entity_id = ?', $id)
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)