Skip to content

Commit 02d6f96

Browse files
ENGCOM-7461: Test coverage for PR #27357 (E-mail templates) #27609
- Merge Pull Request #27609 from lbajsarowicz/magento2:test-coverage/27357-integration-tests - Merged commits: 1. a46c024 2. 766a6ab 3. dcd807b 4. 5c489c7 5. 1a6b60f 6. ac4ba09 7. 65940b8 8. 8ad8df2
2 parents 849b6f0 + 8ad8df2 commit 02d6f96

File tree

1 file changed

+56
-26
lines changed

1 file changed

+56
-26
lines changed

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

Lines changed: 56 additions & 26 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\Exception\LocalizedException;
1213
use Magento\Framework\Exception\NoSuchEntityException;
1314
use Magento\Framework\ObjectManagerInterface;
@@ -22,13 +23,16 @@
2223
*/
2324
class SubscriberTest extends TestCase
2425
{
25-
/** @var ObjectManagerInterface */
26+
private const CONFIRMATION_SUBSCRIBE = 'You have been successfully subscribed to our newsletter.';
27+
private const CONFIRMATION_UNSUBSCRIBE = 'You have been unsubscribed from the newsletter.';
28+
29+
/** @var ObjectManagerInterface */
2630
private $objectManager;
2731

2832
/** @var SubscriberFactory */
2933
private $subscriberFactory;
3034

31-
/** @var TransportBuilderMock */
35+
/** @var TransportBuilderMock */
3236
private $transportBuilder;
3337

3438
/** @var CustomerRepositoryInterface */
@@ -89,27 +93,20 @@ public function testUnsubscribeSubscribe(): void
8993
$subscriber = $this->subscriberFactory->create();
9094
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1));
9195
$this->assertEquals($subscriber, $subscriber->unsubscribe());
92-
$this->assertStringContainsString(
93-
'You have been unsubscribed from the newsletter.',
94-
$this->getFilteredRawMessage($this->transportBuilder)
96+
$this->assertConfirmationParagraphExists(
97+
self::CONFIRMATION_UNSUBSCRIBE,
98+
$this->transportBuilder->getSentMessage()
9599
);
100+
96101
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
97102
// Subscribe and verify
98103
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('[email protected]'));
99104
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
100-
$this->assertStringContainsString(
101-
'You have been successfully subscribed to our newsletter.',
102-
$this->getFilteredRawMessage($this->transportBuilder)
103-
);
104-
}
105105

106-
/**
107-
* @param TransportBuilderMock $transportBuilderMock
108-
* @return string
109-
*/
110-
private function getFilteredRawMessage(TransportBuilderMock $transportBuilderMock): string
111-
{
112-
return $transportBuilderMock->getSentMessage()->getBody()->getParts()[0]->getRawContent();
106+
$this->assertConfirmationParagraphExists(
107+
self::CONFIRMATION_SUBSCRIBE,
108+
$this->transportBuilder->getSentMessage()
109+
);
113110
}
114111

115112
/**
@@ -125,16 +122,17 @@ public function testUnsubscribeSubscribeByCustomerId(): void
125122
// Unsubscribe and verify
126123
$this->assertSame($subscriber, $subscriber->unsubscribeCustomerById(1));
127124
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
128-
$this->assertStringContainsString(
129-
'You have been unsubscribed from the newsletter.',
130-
$this->getFilteredRawMessage($this->transportBuilder)
125+
$this->assertConfirmationParagraphExists(
126+
self::CONFIRMATION_UNSUBSCRIBE,
127+
$this->transportBuilder->getSentMessage()
131128
);
129+
132130
// Subscribe and verify
133131
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1));
134132
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
135-
$this->assertStringContainsString(
136-
'You have been successfully subscribed to our newsletter.',
137-
$this->getFilteredRawMessage($this->transportBuilder)
133+
$this->assertConfirmationParagraphExists(
134+
self::CONFIRMATION_SUBSCRIBE,
135+
$this->transportBuilder->getSentMessage()
138136
);
139137
}
140138

@@ -152,9 +150,10 @@ public function testConfirm(): void
152150
$subscriber->subscribe($customerEmail);
153151
$subscriber->loadByEmail($customerEmail);
154152
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
155-
$this->assertStringContainsString(
156-
'You have been successfully subscribed to our newsletter.',
157-
$this->getFilteredRawMessage($this->transportBuilder)
153+
154+
$this->assertConfirmationParagraphExists(
155+
self::CONFIRMATION_SUBSCRIBE,
156+
$this->transportBuilder->getSentMessage()
158157
);
159158
}
160159

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

0 commit comments

Comments
 (0)