|
32 | 32 | use Magento\TestFramework\Helper\Xpath;
|
33 | 33 | use Magento\TestFramework\Mail\Template\TransportBuilderMock;
|
34 | 34 | use PHPUnit\Framework\TestCase;
|
| 35 | +use Magento\Newsletter\Model\Subscriber; |
| 36 | +use Magento\Framework\Exception\NoSuchEntityException; |
35 | 37 |
|
36 | 38 | /**
|
37 | 39 | * Tests for customer creation via customer account management service.
|
@@ -755,4 +757,49 @@ private function getCustomTemplateId(string $templateCode): int
|
755 | 757 | ->getFirstItem()
|
756 | 758 | ->getId();
|
757 | 759 | }
|
| 760 | + |
| 761 | + /** |
| 762 | + * @magentoConfigFixture default_store newsletter/general/active 1 |
| 763 | + * @magentoDataFixture Magento/Customer/_files/customer.php |
| 764 | + * @magentoDataFixture Magento/Newsletter/_files/subscribers.php |
| 765 | + * |
| 766 | + * @return void |
| 767 | + * @throws InputException |
| 768 | + * @throws InputMismatchException |
| 769 | + * @throws LocalizedException |
| 770 | + * @throws NoSuchEntityException |
| 771 | + */ |
| 772 | + public function testCreateAccountWithNewsLetterSubscription() :void |
| 773 | + { |
| 774 | + $customerEmail = '[email protected]'; |
| 775 | + $firstName = 'John'; |
| 776 | + $lastName = 'Doe'; |
| 777 | + |
| 778 | + $customer = $this->customerRepository->getById(1); |
| 779 | + $customer->setEmail($customerEmail) |
| 780 | + ->setFirstname($firstName) |
| 781 | + ->setLastname($lastName); |
| 782 | + $this->customerRepository->save($customer); |
| 783 | + |
| 784 | + /** @var Subscriber $subscriber */ |
| 785 | + $subscriber = $this->objectManager->create(Subscriber::class); |
| 786 | + $subscriber->subscribe($customerEmail); |
| 787 | + $subscriber->confirm($subscriber->getSubscriberConfirmCode()); |
| 788 | + |
| 789 | + // Verify if the customer is subscribed to newsletter |
| 790 | + $this->assertTrue($subscriber->isSubscribed()); |
| 791 | + |
| 792 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 793 | + $transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class); |
| 794 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 795 | + |
| 796 | + // Verify an email was dispatched to the correct user |
| 797 | + $this->assertNotNull($sentMessage); |
| 798 | + $this->assertEquals($customerEmail, $sentMessage->getTo()[0]->getEmail()); |
| 799 | + |
| 800 | + // Assert the email contains the expected content |
| 801 | + $this->assertEquals('Newsletter subscription success', $sentMessage->getSubject()); |
| 802 | + $messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent(); |
| 803 | + $this->assertStringContainsString('You have been successfully subscribed to our newsletter.', $messageRaw); |
| 804 | + } |
758 | 805 | }
|
0 commit comments