-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes unstable email integration tests, by asserting the content usin…
…g DomNodes.
- Loading branch information
Showing
3 changed files
with
147 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
namespace Magento\Newsletter\Model; | ||
|
||
use Magento\Customer\Api\CustomerRepositoryInterface; | ||
use Magento\Framework\Mail\EmailMessage; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\Mail\Template\TransportBuilderMock; | ||
|
@@ -55,10 +56,11 @@ public function testEmailConfirmation(): void | |
$subscriber = $this->subscriberFactory->create(); | ||
$subscriber->subscribe('[email protected]'); | ||
// confirmationCode 'ysayquyajua23iq29gxwu2eax2qb6gvy' is taken from fixture | ||
$this->assertContains( | ||
$this->assertEmailContains( | ||
'/newsletter/subscriber/confirm/id/' . $subscriber->getSubscriberId() | ||
. '/code/ysayquyajua23iq29gxwu2eax2qb6gvy', | ||
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent() | ||
'//p/a/@href', | ||
$this->transportBuilder->getSentMessage() | ||
); | ||
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $subscriber->getSubscriberStatus()); | ||
} | ||
|
@@ -87,17 +89,19 @@ public function testUnsubscribeSubscribe(): void | |
$subscriber = $this->subscriberFactory->create(); | ||
$this->assertSame($subscriber, $subscriber->loadByCustomerId(1)); | ||
$this->assertEquals($subscriber, $subscriber->unsubscribe()); | ||
$this->assertContains( | ||
$this->assertEmailContains( | ||
'You have been unsubscribed from the newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
'//td[@class="main-content"]', | ||
$this->transportBuilder->getSentMessage() | ||
); | ||
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
// Subscribe and verify | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('[email protected]')); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
$this->assertContains( | ||
$this->assertEmailContains( | ||
'You have been successfully subscribed to our newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
'//td[@class="main-content"]', | ||
$this->transportBuilder->getSentMessage() | ||
); | ||
} | ||
|
||
|
@@ -114,16 +118,18 @@ public function testUnsubscribeSubscribeByCustomerId(): void | |
// Unsubscribe and verify | ||
$this->assertSame($subscriber, $subscriber->unsubscribeCustomerById(1)); | ||
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
$this->assertContains( | ||
$this->assertEmailContains( | ||
'You have been unsubscribed from the newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
'//td[@class="main-content"]', | ||
$this->transportBuilder->getSentMessage() | ||
); | ||
// Subscribe and verify | ||
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1)); | ||
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus()); | ||
$this->assertContains( | ||
$this->assertEmailContains( | ||
'You have been successfully subscribed to our newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
'//td[@class="main-content"]', | ||
$this->transportBuilder->getSentMessage() | ||
); | ||
} | ||
|
||
|
@@ -141,9 +147,10 @@ public function testConfirm(): void | |
$subscriber->subscribe($customerEmail); | ||
$subscriber->loadByEmail($customerEmail); | ||
$subscriber->confirm($subscriber->getSubscriberConfirmCode()); | ||
$this->assertContains( | ||
$this->assertEmailContains( | ||
'You have been successfully subscribed to our newsletter.', | ||
$this->transportBuilder->getSentMessage()->getRawMessage() | ||
'//td[@class="main-content"]', | ||
$this->transportBuilder->getSentMessage() | ||
); | ||
} | ||
|
||
|
@@ -174,4 +181,37 @@ public function testSubscribeUnconfirmedCustomerWithoutSubscription(): void | |
$subscriber->subscribeCustomerById($customer->getId()); | ||
$this->assertEquals(Subscriber::STATUS_UNCONFIRMED, $subscriber->getStatus()); | ||
} | ||
|
||
/** | ||
* Verifies if certain content is contained in the provided xpath in the email template | ||
* The xpath provided can only match a single node! | ||
* | ||
* @param string $expectedGreeting | ||
* @param string $xpath | ||
* @param EmailMessage $message | ||
*/ | ||
private function assertEmailContains(string $expected, string $xpath, EmailMessage $message) | ||
{ | ||
$messageContent = $this->getMessageRawContent($message); | ||
$emailDom = new \DOMDocument(); | ||
$emailDom->loadHTML($messageContent); | ||
|
||
$emailXpath = new \DOMXPath($emailDom); | ||
$emailDomNodes = $emailXpath->query($xpath); | ||
|
||
$this->assertSame(1, $emailDomNodes->length); | ||
$this->assertContains($expected, $emailDomNodes->item(0)->textContent); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters