Skip to content

Commit 959e5e2

Browse files
committed
MC-31262: Storefront: Reset/Forgot customer password
1 parent 1027e02 commit 959e5e2

File tree

5 files changed

+385
-79
lines changed

5 files changed

+385
-79
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\Block\Account;
9+
10+
use Magento\Framework\Math\Random;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\View\LayoutInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\Helper\Xpath;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Class checks password reset block output
19+
*
20+
* @see \Magento\Customer\Block\Account\Resetpassword
21+
* @magentoAppArea frontend
22+
*/
23+
class ResetPasswordTest extends TestCase
24+
{
25+
private const FORM_XPATH = "//form[contains(@action, '?token=%s')]";
26+
private const SET_NEW_PASSWORD_BUTTON_XPATH = "//button/span[contains(text(),'Set a New Password')]";
27+
private const NEW_PASSWORD_LABEL_XPATH = "//label[@for='password']/span[contains(text(), 'New Password')]";
28+
private const PASSWORD_CONFIRMATION_LABEL_XPATH = "//label[@for='password-confirmation']"
29+
. "/span[contains(text(), 'Confirm New Password')]";
30+
31+
/** @var ObjectManagerInterface */
32+
private $objectManager;
33+
34+
/** @var LayoutInterface */
35+
private $layout;
36+
37+
/** @var Resetpassword */
38+
private $block;
39+
40+
/** @var Random */
41+
private $random;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp()
47+
{
48+
parent::setUp();
49+
50+
$this->objectManager = Bootstrap::getObjectManager();
51+
$this->layout = $this->objectManager->get(LayoutInterface::class);
52+
$this->random = $this->objectManager->get(Random::class);
53+
$this->block = $this->layout->createBlock(Resetpassword::class);
54+
$this->block->setTemplate('Magento_Customer::form/resetforgottenpassword.phtml');
55+
}
56+
57+
/**
58+
* @return void
59+
*/
60+
public function testResetPasswordForm(): void
61+
{
62+
$token = $this->random->getUniqueHash();
63+
$this->block->setResetPasswordLinkToken($token);
64+
$output = $this->block->toHtml();
65+
$this->assertEquals(
66+
1,
67+
Xpath::getElementsCountForXpath(sprintf(self::FORM_XPATH, $token), $output),
68+
'Form action does not include correct token'
69+
);
70+
$this->assertEquals(
71+
1,
72+
Xpath::getElementsCountForXpath(self::NEW_PASSWORD_LABEL_XPATH, $output),
73+
'New password label was not found on the page'
74+
);
75+
$this->assertEquals(
76+
1,
77+
Xpath::getElementsCountForXpath(self::PASSWORD_CONFIRMATION_LABEL_XPATH, $output),
78+
'Confirm password label was not found on the page'
79+
);
80+
$this->assertEquals(
81+
1,
82+
Xpath::getElementsCountForXpath(self::SET_NEW_PASSWORD_BUTTON_XPATH, $output),
83+
'Set password button was not found on the page'
84+
);
85+
}
86+
}

dev/tests/integration/testsuite/Magento/Customer/Controller/AccountTest.php

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,6 @@ public function testLogoutAction()
132132
$this->assertRedirect($this->stringContains('customer/account/logoutSuccess'));
133133
}
134134

135-
/**
136-
* Test that forgot password email message displays special characters correctly.
137-
*
138-
* @codingStandardsIgnoreStart
139-
* @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 0
140-
* @magentoConfigFixture current_store customer/password/forgot_email_template customer_password_forgot_email_template
141-
* @magentoConfigFixture current_store customer/password/forgot_email_identity support
142-
* @magentoConfigFixture current_store general/store_information/name Test special' characters
143-
* @magentoConfigFixture current_store customer/captcha/enable 0
144-
* @magentoDataFixture Magento/Customer/_files/customer.php
145-
* @codingStandardsIgnoreEnd
146-
*/
147-
public function testForgotPasswordEmailMessageWithSpecialCharacters()
148-
{
149-
$email = 'customer@example.com';
150-
151-
$this->getRequest()->setPostValue(['email' => $email]);
152-
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
153-
154-
$this->dispatch('customer/account/forgotPasswordPost');
155-
$this->assertRedirect($this->stringContains('customer/account/'));
156-
157-
$subject = $this->transportBuilderMock->getSentMessage()->getSubject();
158-
$this->assertContains(
159-
'Test special\' characters',
160-
$subject
161-
);
162-
}
163-
164135
/**
165136
* @magentoDataFixture Magento/Customer/_files/customer.php
166137
*/
@@ -397,56 +368,6 @@ public function testActiveUserConfirmationAction()
397368
);
398369
}
399370

400-
/**
401-
* @codingStandardsIgnoreStart
402-
* @magentoConfigFixture current_store customer/password/limit_password_reset_requests_method 0
403-
* @magentoConfigFixture current_store customer/password/forgot_email_template customer_password_forgot_email_template
404-
* @magentoConfigFixture current_store customer/password/forgot_email_identity support
405-
* @magentoConfigFixture current_store customer/captcha/enable 0
406-
* @magentoDataFixture Magento/Customer/_files/customer.php
407-
* @codingStandardsIgnoreEnd
408-
*/
409-
public function testForgotPasswordPostAction()
410-
{
411-
$email = 'customer@example.com';
412-
413-
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
414-
$this->getRequest()->setPostValue(['email' => $email]);
415-
416-
$this->dispatch('customer/account/forgotPasswordPost');
417-
$this->assertRedirect($this->stringContains('customer/account/'));
418-
419-
$message = __(
420-
'If there is an account associated with %1 you will receive an email with a link to reset your password.',
421-
$email
422-
);
423-
$this->assertSessionMessages(
424-
$this->equalTo([$message]),
425-
MessageInterface::TYPE_SUCCESS
426-
);
427-
}
428-
429-
/**
430-
* @magentoConfigFixture current_store customer/captcha/enable 0
431-
*/
432-
public function testForgotPasswordPostWithBadEmailAction()
433-
{
434-
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
435-
$this->getRequest()
436-
->setPostValue(
437-
[
438-
'email' => 'bad@email',
439-
]
440-
);
441-
442-
$this->dispatch('customer/account/forgotPasswordPost');
443-
$this->assertRedirect($this->stringContains('customer/account/forgotpassword'));
444-
$this->assertSessionMessages(
445-
$this->equalTo(['The email address is incorrect. Verify the email address and try again.']),
446-
MessageInterface::TYPE_ERROR
447-
);
448-
}
449-
450371
/**
451372
* @magentoDataFixture Magento/Customer/_files/customer.php
452373
*/
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Controller;
9+
10+
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Math\Random;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Framework\View\LayoutInterface;
16+
use Magento\Store\Api\WebsiteRepositoryInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\TestFramework\TestCase\AbstractController;
19+
20+
/**
21+
* Class checks password forgot scenarios
22+
*
23+
* @magentoDbIsolation enabled
24+
*/
25+
class CreatePasswordTest extends AbstractController
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var Session */
31+
private $session;
32+
33+
/** @var LayoutInterface */
34+
private $layout;
35+
36+
/** @var Random */
37+
private $random;
38+
39+
/** @var CustomerResource */
40+
private $customerResource;
41+
42+
/** @var CustomerRegistry */
43+
private $customerRegistry;
44+
45+
/** @var WebsiteRepositoryInterface */
46+
private $websiteRepository;
47+
48+
/** @var int */
49+
private $customerId;
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
protected function setUp()
55+
{
56+
parent::setUp();
57+
58+
$this->objectManager = Bootstrap::getObjectManager();
59+
$this->session = $this->objectManager->get(Session::class);
60+
$this->layout = $this->objectManager->get(LayoutInterface::class);
61+
$this->random = $this->objectManager->get(Random::class);
62+
$this->customerResource = $this->objectManager->get(CustomerResource::class);
63+
$this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
64+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
65+
}
66+
67+
/**
68+
* @inheritdoc
69+
*/
70+
protected function tearDown()
71+
{
72+
$this->customerRegistry->remove($this->customerId);
73+
74+
parent::tearDown();
75+
}
76+
77+
78+
/**
79+
* @magentoDataFixture Magento/Customer/_files/customer_with_website.php
80+
*
81+
* @return void
82+
*/
83+
public function testCreatePassword(): void
84+
{
85+
$defaultWebsite = $this->websiteRepository->get('base')->getId();
86+
$customer = $this->customerRegistry->retrieveByEmail('john.doe@magento.com', $defaultWebsite);
87+
$this->customerId = $customer->getId();
88+
$token = $this->random->getUniqueHash();
89+
$customer->changeResetPasswordLinkToken($token);
90+
$customer->setData('confirmation', 'confirmation');
91+
$this->customerResource->save($customer);
92+
$this->session->setRpToken($token);
93+
$this->session->setRpCustomerId($customer->getId());
94+
$this->dispatch('customer/account/createPassword');
95+
$block = $this->layout->getBlock('resetPassword');
96+
$this->assertEquals($token, $block->getResetPasswordLinkToken());
97+
}
98+
}

0 commit comments

Comments
 (0)