Skip to content

Commit 8d893d5

Browse files
ENGCOM-7527: #26288 Throw an exception when a customer does not exist requests password reset #27269
2 parents 761b54a + ebede20 commit 8d893d5

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function execute(string $resetPasswordToken):CustomerInterface
7373
}
7474
if ($found->getTotalCount() === 0) {
7575
//Customer with such token not found.
76-
new NoSuchEntityException(
76+
throw new NoSuchEntityException(
7777
new Phrase(
7878
'No such entity with rp_token = %value',
7979
[
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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\Api\Data\CustomerSearchResultsInterface;
13+
use Magento\Customer\Model\ForgotPasswordToken\GetCustomerByToken;
14+
use Magento\Framework\Api\SearchCriteria;
15+
use Magento\Framework\Api\SearchCriteriaBuilder;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\Exception\State\ExpiredException;
18+
use Magento\Framework\Phrase;
19+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class GetCustomerByTokenTest extends TestCase
24+
{
25+
private const RESET_PASSWORD = 'resetPassword';
26+
27+
/**
28+
* @var SearchCriteriaBuilder|MockObject
29+
*/
30+
private $searchCriteriaBuilderMock;
31+
32+
/**
33+
* @var SearchCriteria|MockObject
34+
*/
35+
private $searchCriteriaMock;
36+
37+
/**
38+
* @var CustomerRepositoryInterface|MockObject
39+
*/
40+
private $customerRepositoryMock;
41+
42+
/**
43+
* @var CustomerSearchResultsInterface|MockObject
44+
*/
45+
private $searchResultMock;
46+
47+
/**
48+
* @var CustomerInterface|MockObject
49+
*/
50+
private $customerMock;
51+
52+
/**
53+
* @var GetCustomerByToken;
54+
*/
55+
private $model;
56+
57+
protected function setUp(): void
58+
{
59+
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
60+
$this->searchCriteriaMock = $this->createMock(SearchCriteria::class);
61+
$this->searchResultMock = $this->createMock(CustomerSearchResultsInterface::class);
62+
$this->customerRepositoryMock = $this->createMock(CustomerRepositoryInterface::class);
63+
$this->customerMock = $this->getMockForAbstractClass(CustomerInterface::class);
64+
65+
$objectManagerHelper = new ObjectManagerHelper($this);
66+
$this->model = $objectManagerHelper->getObject(
67+
GetCustomerByToken::class,
68+
[
69+
'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
70+
'customerRepository' => $this->customerRepositoryMock
71+
]
72+
);
73+
74+
$this->searchCriteriaBuilderMock->expects($this->once())
75+
->method('create')
76+
->willReturn($this->searchCriteriaMock);
77+
$this->customerRepositoryMock->expects($this->once())
78+
->method('getList')
79+
->with($this->searchCriteriaMock)
80+
->willReturn($this->searchResultMock);
81+
}
82+
83+
public function testExecuteReturnWhenOneItemAvailable(): void
84+
{
85+
$totalCount = 1;
86+
$this->searchResultMock->method('getTotalCount')->willReturn($totalCount);
87+
$this->searchResultMock->expects($this->once())
88+
->method('getItems')
89+
->willReturn([$this->customerMock]);
90+
91+
$this->assertInstanceOf(
92+
CustomerInterface::class,
93+
$this->model->execute(self::RESET_PASSWORD)
94+
);
95+
}
96+
97+
public function testExecuteWithNoSuchEntityException(): void
98+
{
99+
$totalCount = 0;
100+
$this->searchResultMock->method('getTotalCount')->willReturn($totalCount);
101+
$this->expectExceptionObject(new NoSuchEntityException(
102+
new Phrase(
103+
'No such entity with rp_token = %value',
104+
['value' => self::RESET_PASSWORD]
105+
)
106+
));
107+
108+
$this->model->execute(self::RESET_PASSWORD);
109+
}
110+
111+
public function testExecuteWithExpireException(): void
112+
{
113+
$totalCount = 2;
114+
$this->searchResultMock->method('getTotalCount')->willReturn($totalCount);
115+
116+
$this->expectExceptionObject(new ExpiredException(
117+
new Phrase(
118+
'Reset password token expired.'
119+
)
120+
));
121+
122+
$this->model->execute(self::RESET_PASSWORD);
123+
}
124+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class GetCustomerByTokenTest extends TestCase
16+
{
17+
private const RESET_PASSWORD = '8ed8677e6c79e68b94e61658bd756ea5';
18+
19+
/** @var ObjectManagerInterface */
20+
private $objectManager;
21+
22+
/**
23+
* @var GetCustomerByToken
24+
*/
25+
private $customerByToken;
26+
27+
protected function setUp(): void
28+
{
29+
$this->objectManager = Bootstrap::getObjectManager();
30+
$this->customerByToken = $this->objectManager->get(GetCustomerByToken::class);
31+
}
32+
33+
/**
34+
* @magentoDataFixture Magento/Customer/_files/customer.php
35+
*/
36+
public function testExecuteWithNoSuchEntityException(): void
37+
{
38+
self::expectException(NoSuchEntityException::class);
39+
self::expectExceptionMessage('No such entity with rp_token = ' . self::RESET_PASSWORD);
40+
$this->customerByToken->execute(self::RESET_PASSWORD);
41+
}
42+
}

0 commit comments

Comments
 (0)