8
8
namespace Magento \Newsletter \Model ;
9
9
10
10
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
+ use Magento \Framework \Mail \EmailMessage ;
11
12
use Magento \Framework \ObjectManagerInterface ;
12
13
use Magento \TestFramework \Helper \Bootstrap ;
13
14
use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
20
21
*/
21
22
class SubscriberTest extends TestCase
22
23
{
23
- /** @var ObjectManagerInterface */
24
+ private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter. ' ;
25
+ const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter. ' ;
26
+
27
+ /** @var ObjectManagerInterface */
24
28
private $ objectManager ;
25
29
26
30
/** @var SubscriberFactory */
27
31
private $ subscriberFactory ;
28
32
29
- /** @var TransportBuilderMock */
33
+ /** @var TransportBuilderMock */
30
34
private $ transportBuilder ;
31
35
32
36
/** @var CustomerRepositoryInterface */
@@ -87,17 +91,19 @@ public function testUnsubscribeSubscribe(): void
87
91
$ subscriber = $ this ->subscriberFactory ->create ();
88
92
$ this ->assertSame ($ subscriber , $ subscriber ->loadByCustomerId (1 ));
89
93
$ this ->assertEquals ($ subscriber , $ subscriber ->unsubscribe ());
90
- $ this ->assertContains (
91
- ' You have been unsubscribed from the newsletter. ' ,
92
- $ this ->transportBuilder ->getSentMessage ()-> getRawMessage ()
94
+ $ this ->assertConfirmationParagraphExists (
95
+ self :: CONFIRMATION_UNSUBSCRIBE ,
96
+ $ this ->transportBuilder ->getSentMessage ()
93
97
);
98
+
94
99
$ this ->assertEquals (Subscriber::STATUS_UNSUBSCRIBED , $ subscriber ->getSubscriberStatus ());
95
100
// Subscribe and verify
96
101
$ this ->
assertEquals (Subscriber::
STATUS_SUBSCRIBED ,
$ subscriber->
subscribe (
'[email protected] ' ));
97
102
$ this ->assertEquals (Subscriber::STATUS_SUBSCRIBED , $ subscriber ->getSubscriberStatus ());
98
- $ this ->assertContains (
99
- 'You have been successfully subscribed to our newsletter. ' ,
100
- $ this ->transportBuilder ->getSentMessage ()->getRawMessage ()
103
+
104
+ $ this ->assertConfirmationParagraphExists (
105
+ self ::CONFIRMATION_SUBSCRIBE ,
106
+ $ this ->transportBuilder ->getSentMessage ()
101
107
);
102
108
}
103
109
@@ -114,16 +120,17 @@ public function testUnsubscribeSubscribeByCustomerId(): void
114
120
// Unsubscribe and verify
115
121
$ this ->assertSame ($ subscriber , $ subscriber ->unsubscribeCustomerById (1 ));
116
122
$ this ->assertEquals (Subscriber::STATUS_UNSUBSCRIBED , $ subscriber ->getSubscriberStatus ());
117
- $ this ->assertContains (
118
- ' You have been unsubscribed from the newsletter. ' ,
119
- $ this ->transportBuilder ->getSentMessage ()-> getRawMessage ()
123
+ $ this ->assertConfirmationParagraphExists (
124
+ self :: CONFIRMATION_UNSUBSCRIBE ,
125
+ $ this ->transportBuilder ->getSentMessage ()
120
126
);
127
+
121
128
// Subscribe and verify
122
129
$ this ->assertSame ($ subscriber , $ subscriber ->subscribeCustomerById (1 ));
123
130
$ this ->assertEquals (Subscriber::STATUS_SUBSCRIBED , $ subscriber ->getSubscriberStatus ());
124
- $ this ->assertContains (
125
- ' You have been successfully subscribed to our newsletter. ' ,
126
- $ this ->transportBuilder ->getSentMessage ()-> getRawMessage ()
131
+ $ this ->assertConfirmationParagraphExists (
132
+ self :: CONFIRMATION_SUBSCRIBE ,
133
+ $ this ->transportBuilder ->getSentMessage ()
127
134
);
128
135
}
129
136
@@ -141,9 +148,10 @@ public function testConfirm(): void
141
148
$ subscriber ->subscribe ($ customerEmail );
142
149
$ subscriber ->loadByEmail ($ customerEmail );
143
150
$ subscriber ->confirm ($ subscriber ->getSubscriberConfirmCode ());
144
- $ this ->assertContains (
145
- 'You have been successfully subscribed to our newsletter. ' ,
146
- $ this ->transportBuilder ->getSentMessage ()->getRawMessage ()
151
+
152
+ $ this ->assertConfirmationParagraphExists (
153
+ self ::CONFIRMATION_SUBSCRIBE ,
154
+ $ this ->transportBuilder ->getSentMessage ()
147
155
);
148
156
}
149
157
@@ -174,4 +182,35 @@ public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void
174
182
$ subscriber ->subscribeCustomerById ($ customer ->getId ());
175
183
$ this ->assertEquals (Subscriber::STATUS_UNCONFIRMED , $ subscriber ->getStatus ());
176
184
}
185
+
186
+ /**
187
+ * Verifies if Paragraph with specified message is in e-mail
188
+ *
189
+ * @param string $expectedMessage
190
+ * @param EmailMessage $message
191
+ */
192
+ private function assertConfirmationParagraphExists (string $ expectedMessage , EmailMessage $ message ): void
193
+ {
194
+ $ messageContent = $ this ->getMessageRawContent ($ message );
195
+
196
+ $ emailDom = new \DOMDocument ();
197
+ $ emailDom ->loadHTML ($ messageContent );
198
+
199
+ $ emailXpath = new \DOMXPath ($ emailDom );
200
+ $ greeting = $ emailXpath ->query ("//p[contains(text(), ' $ expectedMessage')] " );
201
+
202
+ $ this ->assertSame (1 , $ greeting ->length , "Cannot find the confirmation paragraph in e-mail contents " );
203
+ }
204
+
205
+ /**
206
+ * Returns raw content of provided message
207
+ *
208
+ * @param EmailMessage $message
209
+ * @return string
210
+ */
211
+ private function getMessageRawContent (EmailMessage $ message ): string
212
+ {
213
+ $ emailParts = $ message ->getBody ()->getParts ();
214
+ return current ($ emailParts )->getRawContent ();
215
+ }
177
216
}
0 commit comments