Skip to content

Commit 23d1b9a

Browse files
akaashakaash
authored andcommitted
ACQE-4774 | Integration Testing - Subscribe Customer to Newsletter with required Newsletter confirmation
1 parent f37b43c commit 23d1b9a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

dev/tests/integration/testsuite/Magento/Customer/Model/AccountManagement/CreateAccountTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
use Magento\TestFramework\Helper\Xpath;
3333
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
3434
use PHPUnit\Framework\TestCase;
35+
use Magento\Newsletter\Model\Subscriber;
36+
use Magento\Framework\Exception\NoSuchEntityException;
3537

3638
/**
3739
* Tests for customer creation via customer account management service.
@@ -755,4 +757,49 @@ private function getCustomTemplateId(string $templateCode): int
755757
->getFirstItem()
756758
->getId();
757759
}
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+
}
758805
}

0 commit comments

Comments
 (0)