6
6
namespace Magento \Newsletter \Model \Plugin ;
7
7
8
8
use Magento \TestFramework \Helper \Bootstrap ;
9
+ use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
9
10
10
11
/**
12
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13
+ * phpcs:disable Magento2.Security.Superglobal
11
14
* @magentoAppIsolation enabled
12
15
*/
13
16
class PluginTest extends \PHPUnit \Framework \TestCase
@@ -24,6 +27,11 @@ class PluginTest extends \PHPUnit\Framework\TestCase
24
27
*/
25
28
protected $ customerRepository ;
26
29
30
+ /**
31
+ * @var TransportBuilderMock
32
+ */
33
+ protected $ transportBuilderMock ;
34
+
27
35
protected function setUp (): void
28
36
{
29
37
$ this ->accountManagement = Bootstrap::getObjectManager ()->get (
@@ -32,6 +40,9 @@ protected function setUp(): void
32
40
$ this ->customerRepository = Bootstrap::getObjectManager ()->get (
33
41
\Magento \Customer \Api \CustomerRepositoryInterface::class
34
42
);
43
+ $ this ->transportBuilderMock = Bootstrap::getObjectManager ()->get (
44
+ TransportBuilderMock::class
45
+ );
35
46
}
36
47
37
48
protected function tearDown (): void
@@ -223,4 +234,67 @@ public function testCustomerWithTwoNewsLetterSubscriptions()
223
234
$ extensionAttributes = $ customer ->getExtensionAttributes ();
224
235
$ this ->assertTrue ($ extensionAttributes ->getIsSubscribed ());
225
236
}
237
+
238
+ /**
239
+ * @magentoAppArea adminhtml
240
+ * @magentoDbIsolation enabled
241
+ * @magentoConfigFixture current_store newsletter/general/active 1
242
+ * @magentoDataFixture Magento/Customer/_files/customer_welcome_email_template.php
243
+ *
244
+ * @return void
245
+ * @throws \Magento\Framework\Exception\LocalizedException
246
+ */
247
+ public function testCreateAccountWithNewsLetterSubscription (): void
248
+ {
249
+ $ objectManager = Bootstrap::getObjectManager ();
250
+ /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
251
+ $ customerFactory = $ objectManager ->get (\Magento \Customer \Api \Data \CustomerInterfaceFactory::class);
252
+ $ customerDataObject = $ customerFactory ->create ()
253
+ ->setFirstname ('John ' )
254
+ ->setLastname ('Doe ' )
255
+ ->setEmail ('customer@example.com ' );
256
+ $ extensionAttributes = $ customerDataObject ->getExtensionAttributes ();
257
+ $ extensionAttributes ->setIsSubscribed (true );
258
+ $ customerDataObject ->setExtensionAttributes ($ extensionAttributes );
259
+ $ this ->accountManagement ->createAccount ($ customerDataObject , '123123qW ' );
260
+ $ message = $ this ->transportBuilderMock ->getSentMessage ();
261
+
262
+ $ this ->assertNotNull ($ message );
263
+ $ this ->assertEquals ('Welcome to Main Website Store ' , $ message ->getSubject ());
264
+ $ this ->assertStringContainsString (
265
+ 'John ' ,
266
+ $ message ->getBody ()->getParts ()[0 ]->getRawContent ()
267
+ );
268
+ $ this ->assertStringContainsString (
269
+ 'customer@example.com ' ,
270
+ $ message ->getBody ()->getParts ()[0 ]->getRawContent ()
271
+ );
272
+
273
+ /** @var \Magento\Newsletter\Model\Subscriber $subscriber */
274
+ $ subscriber = $ objectManager ->create (\Magento \Newsletter \Model \Subscriber::class);
275
+ $ subscriber ->loadByEmail ('customer@example.com ' );
276
+ $ this ->assertTrue ($ subscriber ->isSubscribed ());
277
+
278
+ $ this ->transportBuilderMock ->setTemplateIdentifier (
279
+ 'newsletter_subscription_confirm_email_template '
280
+ )->setTemplateVars ([
281
+ 'subscriber_data ' => [
282
+ 'confirmation_link ' => $ subscriber ->getConfirmationLink (),
283
+ ],
284
+ ])->setTemplateOptions ([
285
+ 'area ' => \Magento \Framework \App \Area::AREA_FRONTEND ,
286
+ 'store ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID
287
+ ])
288
+ ->addTo ('customer@example.com ' )
289
+ ->getTransport ();
290
+
291
+ $ message = $ this ->transportBuilderMock ->getSentMessage ();
292
+
293
+ $ this ->assertNotNull ($ message );
294
+ $ this ->assertStringContainsString (
295
+ $ subscriber ->getConfirmationLink (),
296
+ $ message ->getBody ()->getParts ()[0 ]->getRawContent ()
297
+ );
298
+ $ this ->assertEquals ('Newsletter subscription confirmation ' , $ message ->getSubject ());
299
+ }
226
300
}
0 commit comments