|
| 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 | + |
| 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 | + |
| 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 | + |
| 140 | + ], |
| 141 | + 'wrong_website_id' => [ |
| 142 | + |
| 143 | + 'website_id' => 0, |
| 144 | + ], |
| 145 | + ]; |
| 146 | + } |
| 147 | +} |
0 commit comments