66namespace Magento \Newsletter \Model \Plugin ;
77
88use Magento \TestFramework \Helper \Bootstrap ;
9+ use Magento \TestFramework \Mail \Template \TransportBuilderMock ;
910
1011/**
12+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13+ * phpcs:disable Magento2.Security.Superglobal
1114 * @magentoAppIsolation enabled
1215 */
1316class PluginTest extends \PHPUnit \Framework \TestCase
@@ -24,6 +27,11 @@ class PluginTest extends \PHPUnit\Framework\TestCase
2427 */
2528 protected $ customerRepository ;
2629
30+ /**
31+ * @var TransportBuilderMock
32+ */
33+ protected $ transportBuilderMock ;
34+
2735 protected function setUp (): void
2836 {
2937 $ this ->accountManagement = Bootstrap::getObjectManager ()->get (
@@ -32,6 +40,9 @@ protected function setUp(): void
3240 $ this ->customerRepository = Bootstrap::getObjectManager ()->get (
3341 \Magento \Customer \Api \CustomerRepositoryInterface::class
3442 );
43+ $ this ->transportBuilderMock = Bootstrap::getObjectManager ()->get (
44+ TransportBuilderMock::class
45+ );
3546 }
3647
3748 protected function tearDown (): void
@@ -223,4 +234,67 @@ public function testCustomerWithTwoNewsLetterSubscriptions()
223234 $ extensionAttributes = $ customer ->getExtensionAttributes ();
224235 $ this ->assertTrue ($ extensionAttributes ->getIsSubscribed ());
225236 }
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+ }
226300}
0 commit comments