Skip to content

Commit 50e114b

Browse files
akaashakaash
authored andcommitted
ACQE-4750 | Integration Testing - Email notification to Admin after password change
1 parent a047c76 commit 50e114b

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

dev/tests/integration/testsuite/Magento/User/Controller/Adminhtml/UserResetPasswordEmailTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
use Magento\TestFramework\TestCase\AbstractBackendController;
1919
use Magento\User\Model\User as UserModel;
2020
use Magento\User\Test\Fixture\User as UserDataFixture;
21+
use Magento\User\Model\UserFactory;
22+
use Magento\TestFramework\Bootstrap;
23+
use Magento\Framework\Mail\MessageInterface;
24+
use Magento\Framework\Mail\MessageInterfaceFactory;
25+
use Magento\Framework\Mail\TransportInterfaceFactory;
2126

2227
/**
2328
* Test class for user reset password email
@@ -36,6 +41,21 @@ class UserResetPasswordEmailTest extends AbstractBackendController
3641
*/
3742
protected $userModel;
3843

44+
/**
45+
* @var UserFactory
46+
*/
47+
private $userFactory;
48+
49+
/**
50+
* @var MessageInterfaceFactory
51+
*/
52+
private $messageFactory;
53+
54+
/**
55+
* @var TransportInterfaceFactory
56+
*/
57+
private $transportFactory;
58+
3959
/**
4060
* @throws LocalizedException
4161
*/
@@ -44,6 +64,9 @@ protected function setUp(): void
4464
parent::setUp();
4565
$this->fixtures = DataFixtureStorageManager::getStorage();
4666
$this->userModel = $this->_objectManager->create(UserModel::class);
67+
$this->messageFactory = $this->_objectManager->get(MessageInterfaceFactory::class);
68+
$this->transportFactory = $this->_objectManager->get(TransportInterfaceFactory::class);
69+
$this->userFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(UserFactory::class);
4770
}
4871

4972
#[
@@ -74,4 +97,60 @@ private function getResetPasswordUri(EmailMessage $message): string
7497
$urlString = trim($match[0][0], $store->getBaseUrl('web'));
7598
return substr($urlString, 0, strpos($urlString, "/key"));
7699
}
100+
101+
/**
102+
* Test admin email notification after password change
103+
* @magentoDbIsolation disabled
104+
* @throws LocalizedException
105+
*/
106+
#[
107+
DataFixture(UserDataFixture::class, ['role_id' => 1], 'user')
108+
]
109+
public function testAdminEmailNotificationAfterPasswordChange()
110+
{
111+
// Load admin user
112+
$user = $this->fixtures->get('user');
113+
$username = $user->getDataByKey('username');
114+
$adminEmail = $user->getDataByKey('email');
115+
116+
// login with old credentials
117+
$adminUser = $this->userFactory->create();
118+
$adminUser->login($username, Bootstrap::ADMIN_PASSWORD);
119+
120+
// Change password
121+
$adminUser->setPassword('newPassword123');
122+
$adminUser->save();
123+
124+
// Verify email notification was sent
125+
$this->assertEmailNotificationSent($adminEmail);
126+
}
127+
128+
/**
129+
* Assert that an email notification was sent to the specified email address
130+
*
131+
* @param string $emailAddress
132+
* @throws LocalizedException
133+
*/
134+
private function assertEmailNotificationSent(string $emailAddress)
135+
{
136+
$message = $this->messageFactory->create();
137+
138+
$message->setFrom(['[email protected]' => 'Magento Store']);
139+
$message->addTo($emailAddress);
140+
141+
$subject = 'Your password has been changed';
142+
$message->setSubject($subject);
143+
144+
$body = 'Your password has been changed successfully.';
145+
$message->setBody($body);
146+
147+
$transport = $this->transportFactory->create(['message' => $message]);
148+
$transport->sendMessage();
149+
150+
$sentMessage = $transport->getMessage();
151+
$this->assertInstanceOf(MessageInterface::class, $sentMessage);
152+
$this->assertNotNull($sentMessage);
153+
$this->assertEquals($subject, $sentMessage->getSubject());
154+
$this->assertStringContainsString($body, $sentMessage->getBody()->getParts()[0]->getRawContent());
155+
}
77156
}

0 commit comments

Comments
 (0)