|
10 | 10 | use Magento\Customer\Model\CustomerRegistry;
|
11 | 11 | use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
|
12 | 12 | use Magento\Customer\Model\Session;
|
| 13 | +use Magento\Framework\Intl\DateTimeFactory; |
13 | 14 | use Magento\Framework\Math\Random;
|
14 | 15 | use Magento\Framework\ObjectManagerInterface;
|
| 16 | +use Magento\Framework\Stdlib\DateTime; |
15 | 17 | use Magento\Framework\View\LayoutInterface;
|
16 | 18 | use Magento\Store\Api\WebsiteRepositoryInterface;
|
17 | 19 | use Magento\TestFramework\Helper\Bootstrap;
|
@@ -61,6 +63,7 @@ protected function setUp(): void
|
61 | 63 | $this->random = $this->objectManager->get(Random::class);
|
62 | 64 | $this->customerResource = $this->objectManager->get(CustomerResource::class);
|
63 | 65 | $this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
|
| 66 | + $this->dateTimeFactory = $this->objectManager->get(DateTimeFactory::class); |
64 | 67 | $this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
|
65 | 68 | }
|
66 | 69 |
|
@@ -94,4 +97,69 @@ public function testCreatePassword(): void
|
94 | 97 | $block = $this->layout->getBlock('resetPassword');
|
95 | 98 | $this->assertEquals($token, $block->getResetPasswordLinkToken());
|
96 | 99 | }
|
| 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 | + } |
97 | 165 | }
|
0 commit comments