Skip to content

Commit eac1a53

Browse files
committed
MC-32130: Admin: Reset customer password on edit customer page in admin
1 parent 90012f2 commit eac1a53

File tree

2 files changed

+147
-67
lines changed

2 files changed

+147
-67
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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\AccountManagement;
9+
10+
use Magento\Customer\Api\AccountManagementInterface;
11+
use Magento\Customer\Model\AccountManagement;
12+
use Magento\Customer\Model\CustomerRegistry;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\Helper\Xpath;
18+
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Tests for customer password reset via customer account management service.
23+
*
24+
* @magentoDbIsolation enabled
25+
*/
26+
class ResetPasswordTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var AccountManagementInterface */
32+
private $accountManagement;
33+
34+
/** @var TransportBuilderMock*/
35+
private $transportBuilderMock;
36+
37+
/** @var CustomerRegistry */
38+
private $customerRegistry;
39+
40+
/** @var StoreManagerInterface */
41+
private $storeManager;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp()
47+
{
48+
$this->objectManager = Bootstrap::getObjectManager();
49+
$this->accountManagement = $this->objectManager->get(AccountManagementInterface::class);
50+
$this->transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
51+
$this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
52+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
53+
parent::setUp();
54+
}
55+
56+
/**
57+
* Assert that when you reset customer password via admin, link with "Set a New Password" is send to customer email.
58+
*
59+
* @magentoDataFixture Magento/Customer/_files/customer.php
60+
* @return void
61+
*/
62+
public function testSendEmailWithSetNewPasswordLink(): void
63+
{
64+
$this->accountManagement->initiatePasswordReset(
65+
66+
AccountManagement::EMAIL_REMINDER,
67+
1
68+
);
69+
$customerSecure = $this->customerRegistry->retrieveSecureData(1);
70+
$mailTemplate = $this->transportBuilderMock->getSentMessage()->getBody()->getParts()[0]->getRawContent();
71+
72+
$this->assertEquals(
73+
1,
74+
Xpath::getElementsCountForXpath(
75+
sprintf(
76+
'//a[contains(@href, \'customer/account/createPassword/?id=%1$d&token=%2$s\')]',
77+
$customerSecure->getId(),
78+
$customerSecure->getRpToken()
79+
),
80+
$mailTemplate
81+
),
82+
'Reset password creation link was not found.'
83+
);
84+
}
85+
86+
/**
87+
* @magentoAppArea frontend
88+
* @magentoDataFixture Magento/Customer/_files/customer.php
89+
* @return void
90+
*/
91+
public function testSendPasswordResetLink(): void
92+
{
93+
$email = '[email protected]';
94+
$websiteId = (int)$this->storeManager->getWebsite('base')->getId();
95+
96+
$this->accountManagement->initiatePasswordReset($email, AccountManagement::EMAIL_RESET, $websiteId);
97+
}
98+
99+
/**
100+
* @magentoAppArea frontend
101+
* @magentoDataFixture Magento/Customer/_files/customer.php
102+
* @return void
103+
*/
104+
public function testSendPasswordResetLinkDefaultWebsite(): void
105+
{
106+
$email = '[email protected]';
107+
108+
$this->accountManagement->initiatePasswordReset($email, AccountManagement::EMAIL_RESET);
109+
}
110+
111+
/**
112+
* @magentoAppArea frontend
113+
* @dataProvider passwordResetErrorsProvider
114+
* @magentoDataFixture Magento/Customer/_files/customer.php
115+
* @param string $email
116+
* @param int|null $websiteId
117+
* @return void
118+
*/
119+
public function testPasswordResetErrors(string $email, ?int $websiteId = null): void
120+
{
121+
$websiteId = $websiteId ?? (int)$this->storeManager->getWebsite('base')->getId();
122+
$this->expectExceptionObject(
123+
NoSuchEntityException::doubleField('email', $email, 'websiteId', $websiteId)
124+
);
125+
$this->accountManagement->initiatePasswordReset(
126+
$email,
127+
AccountManagement::EMAIL_RESET,
128+
$websiteId
129+
);
130+
}
131+
132+
/**
133+
* @return array
134+
*/
135+
public function passwordResetErrorsProvider(): array
136+
{
137+
return [
138+
'wrong_email' => [
139+
'email' => '[email protected]',
140+
],
141+
'wrong_website_id' => [
142+
'email' => '[email protected]',
143+
'website_id' => 0,
144+
],
145+
];
146+
}
147+
}

dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagementTest.php

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -387,73 +387,6 @@ public function testValidateResetPasswordLinkTokenAmbiguous()
387387
$this->accountManagement->validateResetPasswordLinkToken(null, $token);
388388
}
389389

390-
/**
391-
* @magentoAppArea frontend
392-
* @magentoDataFixture Magento/Customer/_files/customer.php
393-
*/
394-
public function testSendPasswordResetLink()
395-
{
396-
$email = '[email protected]';
397-
398-
$this->accountManagement->initiatePasswordReset($email, AccountManagement::EMAIL_RESET, 1);
399-
}
400-
401-
/**
402-
* @magentoAppArea frontend
403-
* @magentoDataFixture Magento/Customer/_files/customer.php
404-
*/
405-
public function testSendPasswordResetLinkDefaultWebsite()
406-
{
407-
$email = '[email protected]';
408-
409-
$this->accountManagement->initiatePasswordReset($email, AccountManagement::EMAIL_RESET);
410-
}
411-
412-
/**
413-
* @magentoDataFixture Magento/Customer/_files/customer.php
414-
*
415-
*/
416-
public function testSendPasswordResetLinkBadEmailOrWebsite()
417-
{
418-
$email = '[email protected]';
419-
420-
try {
421-
$this->accountManagement->initiatePasswordReset(
422-
$email,
423-
AccountManagement::EMAIL_RESET,
424-
0
425-
);
426-
$this->fail('Expected exception not thrown.');
427-
} catch (NoSuchEntityException $e) {
428-
$expectedParams = [
429-
'fieldName' => 'email',
430-
'fieldValue' => $email,
431-
'field2Name' => 'websiteId',
432-
'field2Value' => 0,
433-
];
434-
$this->assertEquals($expectedParams, $e->getParameters());
435-
}
436-
}
437-
438-
/**
439-
* @magentoDataFixture Magento/Customer/_files/customer.php
440-
*/
441-
public function testSendPasswordResetLinkBadEmailDefaultWebsite()
442-
{
443-
$email = '[email protected]';
444-
445-
try {
446-
$this->accountManagement->initiatePasswordReset(
447-
$email,
448-
AccountManagement::EMAIL_RESET
449-
);
450-
$this->fail('Expected exception not thrown.');
451-
} catch (NoSuchEntityException $nsee) {
452-
// App area is frontend, so we expect websiteId of 1.
453-
$this->assertEquals('No such entity with email = [email protected], websiteId = 1', $nsee->getMessage());
454-
}
455-
}
456-
457390
/**
458391
* @magentoDataFixture Magento/Customer/_files/customer.php
459392
*/

0 commit comments

Comments
 (0)