Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Newsletter\Model;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Mail\EmailMessage;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
Expand All @@ -22,13 +23,16 @@
*/
class SubscriberTest extends TestCase
{
/** @var ObjectManagerInterface */
private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter.';
private const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter.';

/** @var ObjectManagerInterface */
private $objectManager;

/** @var SubscriberFactory */
private $subscriberFactory;

/** @var TransportBuilderMock */
/** @var TransportBuilderMock */
private $transportBuilder;

/** @var CustomerRepositoryInterface */
Expand Down Expand Up @@ -89,27 +93,20 @@ public function testUnsubscribeSubscribe(): void
$subscriber = $this->subscriberFactory->create();
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1));
$this->assertEquals($subscriber, $subscriber->unsubscribe());
$this->assertStringContainsString(
'You have been unsubscribed from the newsletter.',
$this->getFilteredRawMessage($this->transportBuilder)
$this->assertConfirmationParagraphExists(
self::CONFIRMATION_UNSUBSCRIBE,
$this->transportBuilder->getSentMessage()
);

$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
// Subscribe and verify
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('customer@example.com'));
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
$this->assertStringContainsString(
'You have been successfully subscribed to our newsletter.',
$this->getFilteredRawMessage($this->transportBuilder)
);
}

/**
* @param TransportBuilderMock $transportBuilderMock
* @return string
*/
private function getFilteredRawMessage(TransportBuilderMock $transportBuilderMock): string
{
return $transportBuilderMock->getSentMessage()->getBody()->getParts()[0]->getRawContent();
$this->assertConfirmationParagraphExists(
self::CONFIRMATION_SUBSCRIBE,
$this->transportBuilder->getSentMessage()
);
}

/**
Expand All @@ -125,16 +122,17 @@ public function testUnsubscribeSubscribeByCustomerId(): void
// Unsubscribe and verify
$this->assertSame($subscriber, $subscriber->unsubscribeCustomerById(1));
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
$this->assertStringContainsString(
'You have been unsubscribed from the newsletter.',
$this->getFilteredRawMessage($this->transportBuilder)
$this->assertConfirmationParagraphExists(
self::CONFIRMATION_UNSUBSCRIBE,
$this->transportBuilder->getSentMessage()
);

// Subscribe and verify
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1));
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
$this->assertStringContainsString(
'You have been successfully subscribed to our newsletter.',
$this->getFilteredRawMessage($this->transportBuilder)
$this->assertConfirmationParagraphExists(
self::CONFIRMATION_SUBSCRIBE,
$this->transportBuilder->getSentMessage()
);
}

Expand All @@ -152,9 +150,10 @@ public function testConfirm(): void
$subscriber->subscribe($customerEmail);
$subscriber->loadByEmail($customerEmail);
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
$this->assertStringContainsString(
'You have been successfully subscribed to our newsletter.',
$this->getFilteredRawMessage($this->transportBuilder)

$this->assertConfirmationParagraphExists(
self::CONFIRMATION_SUBSCRIBE,
$this->transportBuilder->getSentMessage()
);
}

Expand Down Expand Up @@ -189,4 +188,35 @@ public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void
$subscriber->subscribeCustomerById($customer->getId());
$this->assertEquals(Subscriber::STATUS_UNCONFIRMED, $subscriber->getStatus());
}

/**
* Verifies if Paragraph with specified message is in e-mail
*
* @param string $expectedMessage
* @param EmailMessage $message
*/
private function assertConfirmationParagraphExists(string $expectedMessage, EmailMessage $message): void
{
$messageContent = $this->getMessageRawContent($message);

$emailDom = new \DOMDocument();
$emailDom->loadHTML($messageContent);

$emailXpath = new \DOMXPath($emailDom);
$greeting = $emailXpath->query("//p[contains(text(), '$expectedMessage')]");

$this->assertSame(1, $greeting->length, "Cannot find the confirmation paragraph in e-mail contents");
}

/**
* Returns raw content of provided message
*
* @param EmailMessage $message
* @return string
*/
private function getMessageRawContent(EmailMessage $message): string
{
$emailParts = $message->getBody()->getParts();
return current($emailParts)->getRawContent();
}
}