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 \Exception \LocalizedException ;
12
13
use Magento \Framework \Exception \NoSuchEntityException ;
13
14
use Magento \Framework \ObjectManagerInterface ;
22
23
*/
23
24
class SubscriberTest extends TestCase
24
25
{
25
- /** @var ObjectManagerInterface */
26
+ private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter. ' ;
27
+ private const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter. ' ;
28
+
29
+ /** @var ObjectManagerInterface */
26
30
private $ objectManager ;
27
31
28
32
/** @var SubscriberFactory */
29
33
private $ subscriberFactory ;
30
34
31
- /** @var TransportBuilderMock */
35
+ /** @var TransportBuilderMock */
32
36
private $ transportBuilder ;
33
37
34
38
/** @var CustomerRepositoryInterface */
@@ -89,27 +93,20 @@ public function testUnsubscribeSubscribe(): void
89
93
$ subscriber = $ this ->subscriberFactory ->create ();
90
94
$ this ->assertSame ($ subscriber , $ subscriber ->loadByCustomerId (1 ));
91
95
$ this ->assertEquals ($ subscriber , $ subscriber ->unsubscribe ());
92
- $ this ->assertStringContainsString (
93
- ' You have been unsubscribed from the newsletter. ' ,
94
- $ this ->getFilteredRawMessage ( $ this -> transportBuilder )
96
+ $ this ->assertConfirmationParagraphExists (
97
+ self :: CONFIRMATION_UNSUBSCRIBE ,
98
+ $ this ->transportBuilder -> getSentMessage ( )
95
99
);
100
+
96
101
$ this ->assertEquals (Subscriber::STATUS_UNSUBSCRIBED , $ subscriber ->getSubscriberStatus ());
97
102
// Subscribe and verify
98
103
$ this ->
assertEquals (Subscriber::
STATUS_SUBSCRIBED ,
$ subscriber->
subscribe (
'[email protected] ' ));
99
104
$ this ->assertEquals (Subscriber::STATUS_SUBSCRIBED , $ subscriber ->getSubscriberStatus ());
100
- $ this ->assertStringContainsString (
101
- 'You have been successfully subscribed to our newsletter. ' ,
102
- $ this ->getFilteredRawMessage ($ this ->transportBuilder )
103
- );
104
- }
105
105
106
- /**
107
- * @param TransportBuilderMock $transportBuilderMock
108
- * @return string
109
- */
110
- private function getFilteredRawMessage (TransportBuilderMock $ transportBuilderMock ): string
111
- {
112
- return $ transportBuilderMock ->getSentMessage ()->getBody ()->getParts ()[0 ]->getRawContent ();
106
+ $ this ->assertConfirmationParagraphExists (
107
+ self ::CONFIRMATION_SUBSCRIBE ,
108
+ $ this ->transportBuilder ->getSentMessage ()
109
+ );
113
110
}
114
111
115
112
/**
@@ -125,16 +122,17 @@ public function testUnsubscribeSubscribeByCustomerId(): void
125
122
// Unsubscribe and verify
126
123
$ this ->assertSame ($ subscriber , $ subscriber ->unsubscribeCustomerById (1 ));
127
124
$ this ->assertEquals (Subscriber::STATUS_UNSUBSCRIBED , $ subscriber ->getSubscriberStatus ());
128
- $ this ->assertStringContainsString (
129
- ' You have been unsubscribed from the newsletter. ' ,
130
- $ this ->getFilteredRawMessage ( $ this -> transportBuilder )
125
+ $ this ->assertConfirmationParagraphExists (
126
+ self :: CONFIRMATION_UNSUBSCRIBE ,
127
+ $ this ->transportBuilder -> getSentMessage ( )
131
128
);
129
+
132
130
// Subscribe and verify
133
131
$ this ->assertSame ($ subscriber , $ subscriber ->subscribeCustomerById (1 ));
134
132
$ this ->assertEquals (Subscriber::STATUS_SUBSCRIBED , $ subscriber ->getSubscriberStatus ());
135
- $ this ->assertStringContainsString (
136
- ' You have been successfully subscribed to our newsletter. ' ,
137
- $ this ->getFilteredRawMessage ( $ this -> transportBuilder )
133
+ $ this ->assertConfirmationParagraphExists (
134
+ self :: CONFIRMATION_SUBSCRIBE ,
135
+ $ this ->transportBuilder -> getSentMessage ( )
138
136
);
139
137
}
140
138
@@ -152,9 +150,10 @@ public function testConfirm(): void
152
150
$ subscriber ->subscribe ($ customerEmail );
153
151
$ subscriber ->loadByEmail ($ customerEmail );
154
152
$ subscriber ->confirm ($ subscriber ->getSubscriberConfirmCode ());
155
- $ this ->assertStringContainsString (
156
- 'You have been successfully subscribed to our newsletter. ' ,
157
- $ this ->getFilteredRawMessage ($ this ->transportBuilder )
153
+
154
+ $ this ->assertConfirmationParagraphExists (
155
+ self ::CONFIRMATION_SUBSCRIBE ,
156
+ $ this ->transportBuilder ->getSentMessage ()
158
157
);
159
158
}
160
159
@@ -189,4 +188,35 @@ public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void
189
188
$ subscriber ->subscribeCustomerById ($ customer ->getId ());
190
189
$ this ->assertEquals (Subscriber::STATUS_UNCONFIRMED , $ subscriber ->getStatus ());
191
190
}
191
+
192
+ /**
193
+ * Verifies if Paragraph with specified message is in e-mail
194
+ *
195
+ * @param string $expectedMessage
196
+ * @param EmailMessage $message
197
+ */
198
+ private function assertConfirmationParagraphExists (string $ expectedMessage , EmailMessage $ message ): void
199
+ {
200
+ $ messageContent = $ this ->getMessageRawContent ($ message );
201
+
202
+ $ emailDom = new \DOMDocument ();
203
+ $ emailDom ->loadHTML ($ messageContent );
204
+
205
+ $ emailXpath = new \DOMXPath ($ emailDom );
206
+ $ greeting = $ emailXpath ->query ("//p[contains(text(), ' $ expectedMessage')] " );
207
+
208
+ $ this ->assertSame (1 , $ greeting ->length , "Cannot find the confirmation paragraph in e-mail contents " );
209
+ }
210
+
211
+ /**
212
+ * Returns raw content of provided message
213
+ *
214
+ * @param EmailMessage $message
215
+ * @return string
216
+ */
217
+ private function getMessageRawContent (EmailMessage $ message ): string
218
+ {
219
+ $ emailParts = $ message ->getBody ()->getParts ();
220
+ return current ($ emailParts )->getRawContent ();
221
+ }
192
222
}
0 commit comments