|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\User\Controller\Adminhtml; |
| 7 | + |
| 8 | +use Magento\Framework\Exception\LocalizedException; |
| 9 | +use Magento\Framework\Mail\EmailMessage; |
| 10 | +use Magento\TestFramework\Fixture\Config as Config; |
| 11 | +use Magento\TestFramework\Fixture\DataFixture; |
| 12 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 13 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 14 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 15 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 16 | +use Magento\User\Model\User as UserModel; |
| 17 | +use Magento\User\Test\Fixture\User as UserDataFixture; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test class for user reset password email |
| 21 | + * |
| 22 | + * @magentoAppArea adminhtml |
| 23 | + */ |
| 24 | +class UserResetPasswordEmailTest extends AbstractBackendController |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var DataFixtureStorage |
| 28 | + */ |
| 29 | + private $fixtures; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var UserModel |
| 33 | + */ |
| 34 | + protected $userModel; |
| 35 | + |
| 36 | + /** |
| 37 | + * @throws LocalizedException |
| 38 | + */ |
| 39 | + protected function setUp(): void |
| 40 | + { |
| 41 | + parent::setUp(); |
| 42 | + $this->fixtures = DataFixtureStorageManager::getStorage(); |
| 43 | + $this->userModel = $this->_objectManager->create(UserModel::class); |
| 44 | + } |
| 45 | + |
| 46 | + #[ |
| 47 | + Config('admin/emails/forgot_email_template', 'admin_emails_forgot_email_template'), |
| 48 | + Config('admin/emails/forgot_email_identity', 'general'), |
| 49 | + Config('web/url/use_store', 1), |
| 50 | + DataFixture(UserDataFixture::class, ['role_id' => 1], 'user') |
| 51 | + ] |
| 52 | + public function testUserResetPasswordEmail() |
| 53 | + { |
| 54 | + $user = $this->fixtures->get('user'); |
| 55 | + $userEmail = $user->getDataByKey('email'); |
| 56 | + $transportMock = $this->_objectManager->get(TransportBuilderMock::class); |
| 57 | + $this->getRequest()->setPostValue('email', $userEmail); |
| 58 | + $this->dispatch('backend/admin/auth/forgotpassword'); |
| 59 | + $message = $transportMock->getSentMessage(); |
| 60 | + $this->assertNotEmpty($message); |
| 61 | + $this->assertEquals('backend/admin/auth/resetpassword', $this->getResetPasswordUri($message)); |
| 62 | + } |
| 63 | + |
| 64 | + private function getResetPasswordUri(EmailMessage $message): string |
| 65 | + { |
| 66 | + $emailParts = $message->getBody()->getParts(); |
| 67 | + $messageContent = current($emailParts)->getRawContent(); |
| 68 | + $pattern = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#'; |
| 69 | + preg_match_all($pattern, $messageContent, $match); |
| 70 | + $urlString = strstr($match[0][0], 'backend'); |
| 71 | + return substr($urlString, 0, strpos($urlString, "/key")); |
| 72 | + } |
| 73 | +} |
0 commit comments