18
18
use Magento \TestFramework \TestCase \AbstractBackendController ;
19
19
use Magento \User \Model \User as UserModel ;
20
20
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 ;
21
26
22
27
/**
23
28
* Test class for user reset password email
@@ -36,6 +41,21 @@ class UserResetPasswordEmailTest extends AbstractBackendController
36
41
*/
37
42
protected $ userModel ;
38
43
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
+
39
59
/**
40
60
* @throws LocalizedException
41
61
*/
@@ -44,6 +64,9 @@ protected function setUp(): void
44
64
parent ::setUp ();
45
65
$ this ->fixtures = DataFixtureStorageManager::getStorage ();
46
66
$ 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);
47
70
}
48
71
49
72
#[
@@ -74,4 +97,60 @@ private function getResetPasswordUri(EmailMessage $message): string
74
97
$ urlString = trim ($ match [0 ][0 ], $ store ->getBaseUrl ('web ' ));
75
98
return substr ($ urlString , 0 , strpos ($ urlString , "/key " ));
76
99
}
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
+ }
77
156
}
0 commit comments