Skip to content

Commit 766a6ab

Browse files
committed
magento#27357 Fix the test coverage for e-mail contents
1 parent a46c024 commit 766a6ab

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

dev/tests/integration/testsuite/Magento/Newsletter/Model/SubscriberTest.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Newsletter\Model;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\Mail\EmailMessage;
1112
use Magento\Framework\ObjectManagerInterface;
1213
use Magento\TestFramework\Helper\Bootstrap;
1314
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
@@ -20,13 +21,16 @@
2021
*/
2122
class SubscriberTest extends TestCase
2223
{
23-
/** @var ObjectManagerInterface */
24+
private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter.';
25+
const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter.';
26+
27+
/** @var ObjectManagerInterface */
2428
private $objectManager;
2529

2630
/** @var SubscriberFactory */
2731
private $subscriberFactory;
2832

29-
/** @var TransportBuilderMock */
33+
/** @var TransportBuilderMock */
3034
private $transportBuilder;
3135

3236
/** @var CustomerRepositoryInterface */
@@ -87,17 +91,19 @@ public function testUnsubscribeSubscribe(): void
8791
$subscriber = $this->subscriberFactory->create();
8892
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1));
8993
$this->assertEquals($subscriber, $subscriber->unsubscribe());
90-
$this->assertContains(
91-
'You have been unsubscribed from the newsletter.',
92-
$this->transportBuilder->getSentMessage()->getRawMessage()
94+
$this->assertConfirmationParagraphExists(
95+
self::CONFIRMATION_UNSUBSCRIBE,
96+
$this->transportBuilder->getSentMessage()
9397
);
98+
9499
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
95100
// Subscribe and verify
96101
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('[email protected]'));
97102
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
98-
$this->assertContains(
99-
'You have been successfully subscribed to our newsletter.',
100-
$this->transportBuilder->getSentMessage()->getRawMessage()
103+
104+
$this->assertConfirmationParagraphExists(
105+
self::CONFIRMATION_SUBSCRIBE,
106+
$this->transportBuilder->getSentMessage()
101107
);
102108
}
103109

@@ -114,16 +120,17 @@ public function testUnsubscribeSubscribeByCustomerId(): void
114120
// Unsubscribe and verify
115121
$this->assertSame($subscriber, $subscriber->unsubscribeCustomerById(1));
116122
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
117-
$this->assertContains(
118-
'You have been unsubscribed from the newsletter.',
119-
$this->transportBuilder->getSentMessage()->getRawMessage()
123+
$this->assertConfirmationParagraphExists(
124+
self::CONFIRMATION_UNSUBSCRIBE,
125+
$this->transportBuilder->getSentMessage()
120126
);
127+
121128
// Subscribe and verify
122129
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1));
123130
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
124-
$this->assertContains(
125-
'You have been successfully subscribed to our newsletter.',
126-
$this->transportBuilder->getSentMessage()->getRawMessage()
131+
$this->assertConfirmationParagraphExists(
132+
self::CONFIRMATION_SUBSCRIBE,
133+
$this->transportBuilder->getSentMessage()
127134
);
128135
}
129136

@@ -141,9 +148,10 @@ public function testConfirm(): void
141148
$subscriber->subscribe($customerEmail);
142149
$subscriber->loadByEmail($customerEmail);
143150
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
144-
$this->assertContains(
145-
'You have been successfully subscribed to our newsletter.',
146-
$this->transportBuilder->getSentMessage()->getRawMessage()
151+
152+
$this->assertConfirmationParagraphExists(
153+
self::CONFIRMATION_SUBSCRIBE,
154+
$this->transportBuilder->getSentMessage()
147155
);
148156
}
149157

@@ -174,4 +182,35 @@ public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void
174182
$subscriber->subscribeCustomerById($customer->getId());
175183
$this->assertEquals(Subscriber::STATUS_UNCONFIRMED, $subscriber->getStatus());
176184
}
185+
186+
/**
187+
* Verifies if Paragraph with specified message is in e-mail
188+
*
189+
* @param string $expectedMessage
190+
* @param EmailMessage $message
191+
*/
192+
private function assertConfirmationParagraphExists(string $expectedMessage, EmailMessage $message): void
193+
{
194+
$messageContent = $this->getMessageRawContent($message);
195+
196+
$emailDom = new \DOMDocument();
197+
$emailDom->loadHTML($messageContent);
198+
199+
$emailXpath = new \DOMXPath($emailDom);
200+
$greeting = $emailXpath->query("//p[contains(text(), '$expectedMessage')]");
201+
202+
$this->assertSame(1, $greeting->length, "Cannot find the confirmation paragraph in e-mail contents");
203+
}
204+
205+
/**
206+
* Returns raw content of provided message
207+
*
208+
* @param EmailMessage $message
209+
* @return string
210+
*/
211+
private function getMessageRawContent(EmailMessage $message): string
212+
{
213+
$emailParts = $message->getBody()->getParts();
214+
return current($emailParts)->getRawContent();
215+
}
177216
}

0 commit comments

Comments
 (0)