Skip to content

Commit e82531d

Browse files
committed
Add integration test for customer reset password
1 parent 03c65e9 commit e82531d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Magento\Customer\Model\CustomerRegistry;
1111
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
1212
use Magento\Customer\Model\Session;
13+
use Magento\Framework\Intl\DateTimeFactory;
1314
use Magento\Framework\Math\Random;
1415
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Framework\Stdlib\DateTime;
1517
use Magento\Framework\View\LayoutInterface;
1618
use Magento\Store\Api\WebsiteRepositoryInterface;
1719
use Magento\TestFramework\Helper\Bootstrap;
@@ -61,6 +63,7 @@ protected function setUp(): void
6163
$this->random = $this->objectManager->get(Random::class);
6264
$this->customerResource = $this->objectManager->get(CustomerResource::class);
6365
$this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
66+
$this->dateTimeFactory = $this->objectManager->get(DateTimeFactory::class);
6467
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
6568
}
6669

@@ -94,4 +97,69 @@ public function testCreatePassword(): void
9497
$block = $this->layout->getBlock('resetPassword');
9598
$this->assertEquals($token, $block->getResetPasswordLinkToken());
9699
}
100+
101+
/**
102+
* @magentoDataFixture Magento/Customer/_files/customer_with_website.php
103+
*
104+
* @return void
105+
*/
106+
public function testTokenHasExpired(): void
107+
{
108+
$defaultWebsite = $this->websiteRepository->get('base')->getId();
109+
$customer = $this->customerRegistry->retrieveByEmail('john.doe@magento.com', $defaultWebsite);
110+
$this->customerId = $customer->getId();
111+
$token = $this->random->getUniqueHash();
112+
$tooLongAgo = $this->dateTimeFactory->create()
113+
->sub(\DateInterval::createFromDateString('1 month'))
114+
->format(DateTime::DATETIME_PHP_FORMAT);
115+
116+
$customer->changeResetPasswordLinkToken($token);
117+
$customer->setData('confirmation', 'confirmation');
118+
$customerSecure = $this->customerRegistry->retrieveSecureData($this->customerId);
119+
$customerSecure->setRpTokenCreatedAt($tooLongAgo);
120+
$this->customerResource->save($customer);
121+
122+
$this->session->setRpToken($token);
123+
$this->session->setRpCustomerId($this->customerId);
124+
125+
$this->dispatch('customer/account/createPassword');
126+
127+
$this->assertRedirect($this->stringContains('customer/account/forgotpassword'));
128+
$this->assertSessionMessages(
129+
$this->equalTo(['Your password reset link has expired.']),
130+
MessageInterface::TYPE_ERROR
131+
);
132+
}
133+
134+
/**
135+
* @magentoDataFixture Magento/Customer/_files/customer_with_website.php
136+
*
137+
* @return void
138+
*/
139+
public function testTokenExtendedOnPageLoad(): void
140+
{
141+
$defaultWebsite = $this->websiteRepository->get('base')->getId();
142+
$customer = $this->customerRegistry->retrieveByEmail('john.doe@magento.com', $defaultWebsite);
143+
$this->customerId = $customer->getId();
144+
$token = $this->random->getUniqueHash();
145+
$anHourAgo = $this->dateTimeFactory->create()
146+
->sub(\DateInterval::createFromDateString('1 hour'))
147+
->format(DateTime::DATETIME_PHP_FORMAT);
148+
149+
$customer->changeResetPasswordLinkToken($token);
150+
$customer->setData('confirmation', 'confirmation');
151+
$customerSecure = $this->customerRegistry->retrieveSecureData($this->customerId);
152+
$customerSecure->setRpTokenCreatedAt($anHourAgo);
153+
$this->customerResource->save($customer);
154+
155+
$this->session->setRpToken($token);
156+
$this->session->setRpCustomerId($this->customerId);
157+
158+
$this->dispatch('customer/account/createPassword');
159+
$block = $this->layout->getBlock('resetPassword');
160+
$this->assertEquals($token, $block->getResetPasswordLinkToken());
161+
162+
$customerSecure = $this->customerRegistry->retrieveSecureData($this->customerId);
163+
$this->assertNotEquals($anHourAgo, $customerSecure->getRpTokenCreatedAt());
164+
}
97165
}

0 commit comments

Comments
 (0)