@@ -761,7 +761,8 @@ private function getCustomTemplateId(string $templateCode): int
761
761
}
762
762
763
763
/**
764
- * @magentoConfigFixture default_store newsletter/general/active 1
764
+ * @magentoConfigFixture current_store newsletter/general/active 1
765
+ * @magentoConfigFixture current_store newsletter/subscription/confirm 1
765
766
* @magentoDataFixture Magento/Customer/_files/customer.php
766
767
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
767
768
*
@@ -773,47 +774,53 @@ private function getCustomTemplateId(string $templateCode): int
773
774
*/
774
775
public function testCreateAccountWithNewsLetterSubscription () :void
775
776
{
776
- $ customerEmail = 'test@example.com ' ;
777
- $ firstName = 'John ' ;
778
- $ lastName = 'Doe ' ;
779
-
780
777
$ customer = $ this ->customerRepository ->getById (1 );
781
- $ customer ->setEmail ($ customerEmail )
782
- ->setFirstname ($ firstName )
783
- ->setLastname ($ lastName );
784
- $ this ->customerRepository ->save ($ customer );
785
-
778
+ $ customerEmail = $ customer ->getEmail ();
779
+ $ customerData = [
780
+ 'name ' => $ customer ->getFirstname () . ' ' . $ customer ->getLastname (),
781
+ 'email ' => $ customerEmail ,
782
+ 'id ' => $ customer ->getId (),
783
+ 'rp_token ' => 'randomstring '
784
+ ];
786
785
$ this ->assertAndSendEmailNotification (
787
- $ customer ,
786
+ $ customerData ,
788
787
$ customerEmail ,
789
- 'customer_create_account_email_template '
788
+ 'customer_create_account_email_template ' ,
789
+ null
790
790
);
791
791
792
792
/** @var Subscriber $subscriber */
793
793
$ subscriber = $ this ->objectManager ->create (Subscriber::class);
794
794
$ subscriber ->subscribe ($ customerEmail );
795
795
$ subscriber ->confirm ($ subscriber ->getSubscriberConfirmCode ());
796
+ $ confirmationLink = $ subscriber ->getConfirmationLink ();
796
797
797
798
// Verify if the customer is subscribed to newsletter
798
799
$ this ->assertTrue ($ subscriber ->isSubscribed ());
799
800
800
801
$ this ->assertAndSendEmailNotification (
801
- $ customer ,
802
+ $ customerData ,
802
803
$ customerEmail ,
803
- 'newsletter_subscription_confirm_email_template '
804
+ 'newsletter_subscription_confirm_email_template ' ,
805
+ $ confirmationLink
804
806
);
805
807
}
806
808
807
809
/**
808
- * @param $customer
810
+ * @param $customerData
809
811
* @param $customerEmail
810
812
* @param $templateIdentifier
813
+ * @param $confirmationLink
811
814
* @return void
812
815
* @throws LocalizedException
813
816
* @throws \Magento\Framework\Exception\MailException
814
817
*/
815
- private function assertAndSendEmailNotification ($ customer , $ customerEmail , $ templateIdentifier ) :void
816
- {
818
+ private function assertAndSendEmailNotification (
819
+ $ customerData ,
820
+ $ customerEmail ,
821
+ $ templateIdentifier ,
822
+ $ confirmationLink = null
823
+ ) :void {
817
824
/** @var TransportBuilder $transportBuilder */
818
825
$ transportBuilder = $ this ->objectManager ->get (TransportBuilder::class);
819
826
$ transport = $ transportBuilder ->setTemplateIdentifier ($ templateIdentifier )
@@ -823,7 +830,14 @@ private function assertAndSendEmailNotification($customer, $customerEmail, $temp
823
830
'store ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID
824
831
]
825
832
)
826
- ->setTemplateVars (['customer ' => $ customer ])
833
+ ->setTemplateVars (
834
+ [
835
+ 'customer ' => $ customerData ,
836
+ 'subscriber_data ' => [
837
+ 'confirmation_link ' => $ confirmationLink ,
838
+ ],
839
+ ]
840
+ )
827
841
->addTo ($ customerEmail )
828
842
->getTransport ();
829
843
$ transport ->sendMessage ();
@@ -833,8 +847,20 @@ private function assertAndSendEmailNotification($customer, $customerEmail, $temp
833
847
switch ($ templateIdentifier ) {
834
848
case 'customer_create_account_email_template ' :
835
849
$ this ->assertEquals ('Welcome to Default ' , $ sendMessage ->getSubject ());
850
+ $ this ->assertStringContainsString (
851
+ $ customerData ['name ' ],
852
+ $ sendMessage ->getBody ()->getParts ()[0 ]->getRawContent ()
853
+ );
854
+ $ this ->assertStringContainsString (
855
+ $ customerData ['email ' ],
856
+ $ sendMessage ->getBody ()->getParts ()[0 ]->getRawContent ()
857
+ );
836
858
break ;
837
859
case 'newsletter_subscription_confirm_email_template ' :
860
+ $ this ->assertStringContainsString (
861
+ $ confirmationLink ,
862
+ $ sendMessage ->getBody ()->getParts ()[0 ]->getRawContent ()
863
+ );
838
864
$ this ->assertEquals ('Newsletter subscription confirmation ' , $ sendMessage ->getSubject ());
839
865
break ;
840
866
}
0 commit comments