-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from roelofr/feature/html5-tests-and-support
HTML5 and user-generated content support
- Loading branch information
Showing
7 changed files
with
243 additions
and
67 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Eduardokum\LaravelMailAutoEmbed\Tests; | ||
|
||
use Eduardokum\LaravelMailAutoEmbed\Listeners\SwiftEmbedImages; | ||
use Eduardokum\LaravelMailAutoEmbed\Tests\Traits\InteractsWithSwift; | ||
|
||
/** | ||
* Tests some scenarios, like HTML5 mails and mail with "invalid" HTML that mail clients | ||
* like | ||
*/ | ||
class FormatTest extends TestCase | ||
{ | ||
use InteractsWithSwift; | ||
private const HANDLE_CONFIG = [ | ||
'enabled' => true, | ||
'method' => 'attachment' | ||
]; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testValidHtml5Message() | ||
{ | ||
$message = $this->handleBeforeSendPerformedEvent('formats/html5-valid.html', self::HANDLE_CONFIG); | ||
|
||
$this->assertEmailImageTags([ | ||
'url' => 'cid:', | ||
'entity' => 'cid:', | ||
], $message->getBody()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testUserGeneratedHtml5Message() | ||
{ | ||
$message = $this->handleBeforeSendPerformedEvent('formats/html5-user-generated.html', self::HANDLE_CONFIG); | ||
|
||
$this->assertEmailImageTags([ | ||
'url' => 'cid:', | ||
'entity' => 'cid:', | ||
], $message->getBody()); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Eduardokum\LaravelMailAutoEmbed\Tests\Traits; | ||
|
||
use Eduardokum\LaravelMailAutoEmbed\Listeners\SwiftEmbedImages; | ||
|
||
/** | ||
* Shared code for creating messages and events | ||
*/ | ||
trait InteractsWithSwift | ||
{ | ||
/** | ||
* @param string $htmlMessage | ||
* @return \Swift_Message | ||
*/ | ||
protected function createSwiftMessage($htmlMessage) | ||
{ | ||
$message = new \Swift_Message('test', $htmlMessage); | ||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* @param \Swift_Message $message | ||
* @return \Swift_Events_SendEvent | ||
*/ | ||
protected function createSwiftEvent(\Swift_Message $message) | ||
{ | ||
$dispatcher = new \Swift_Events_SimpleEventDispatcher(); | ||
$transport = new \Swift_Transport_NullTransport($dispatcher); | ||
$event = new \Swift_Events_SendEvent($transport, $message); | ||
|
||
return $event; | ||
} | ||
|
||
/** | ||
* @param string $libraryFile | ||
* @param array $options | ||
* @return \Swift_Message | ||
*/ | ||
protected function handleBeforeSendPerformedEvent($libraryFile, $options) | ||
{ | ||
$htmlMessage = $this->getLibraryFile($libraryFile); | ||
$message = $this->createSwiftMessage($htmlMessage); | ||
|
||
$embedPlugin = new SwiftEmbedImages($options); | ||
$embedPlugin->beforeSendPerformed($this->createSwiftEvent($message)); | ||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* Check the body for image tags with the given keys as comment preceding them. | ||
* @param array $expectations | ||
* @param string $body | ||
* @return void | ||
*/ | ||
protected function assertEmailImageTags($expectations, $body) | ||
{ | ||
foreach ($expectations as $comment => $src) { | ||
// Fix for PHPUnit <8.0 | ||
// phpcs:ignore Generic.Files.LineLength.TooLong | ||
$method = \method_exists($this, 'assertStringContainsString') ? 'assertStringContainsString' : 'assertContains'; | ||
|
||
// Check if the string is contained within the string | ||
$this->$method( | ||
sprintf('<!-- %s --><img src="%s', $comment, $src), | ||
$body | ||
); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
|
||
<body> | ||
<main> | ||
<article> | ||
<h1>Lorem Ipsum</h1> | ||
|
||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, dolorum assumenda aliquam blanditiis, | ||
necessitatibus mollitia delectus sapiente amet earum minima qui non deserunt quidem, doloremque | ||
architecto voluptatem eveniet illo aperiam. | ||
</p> | ||
|
||
<p> | ||
<!-- Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/colgroup --> | ||
<table> | ||
<caption>Superheros and sidekicks</caption> | ||
<colgroup> | ||
<col> | ||
<col span="2" class="batman"> | ||
<col span="2" class="flash"> | ||
</colgroup> | ||
<tr> | ||
<td> </td> | ||
<th scope="col">Batman</th> | ||
<th scope="col">Robin</th> | ||
<th scope="col">The Flash</th> | ||
<th scope="col">Kid Flash</th> | ||
</tr> | ||
<tr> | ||
<th scope="row">Skill</th> | ||
<td>Smarts</td> | ||
<td>Dex, acrobat</td> | ||
<td>Super speed</td> | ||
<td>Super speed</td> | ||
</tr> | ||
</table> | ||
</p> | ||
|
||
<p> | ||
<figure> | ||
<!-- url --><img src="http://localhost/test.png" /> | ||
<caption>Lorem ipsum</caption> | ||
</figure> | ||
</p> | ||
</article> | ||
|
||
<aside> | ||
<header> | ||
<h1>Lorem Ipsum</h1> | ||
</header> | ||
|
||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore cum, blanditiis a minima aspernatur | ||
corporis pariatur, perferendis explicabo consectetur tenetur iste, fuga possimus corrupti dolorem | ||
laudantium sapiente sunt error autem. | ||
</p> | ||
|
||
<figure> | ||
<!-- entity --><img src="embed:Eduardokum\LaravelMailAutoEmbed\Tests\fixtures\PictureEntity:1" /> | ||
<caption>Lorem Ipsum</caption> | ||
</figure> | ||
</aside> | ||
</main> | ||
</body> | ||
|
||
</html> |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
|
||
<body> | ||
<main> | ||
<article> | ||
<h1>Lorem Ipsum</h1> | ||
|
||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, dolorum assumenda aliquam blanditiis, | ||
necessitatibus mollitia delectus sapiente amet earum minima qui non deserunt quidem, doloremque | ||
architecto voluptatem eveniet illo aperiam. | ||
</p> | ||
|
||
|
||
<figure> | ||
<!-- url --><img src="http://localhost/test.png" /> | ||
<caption>Lorem ipsum</caption> | ||
</figure> | ||
</article> | ||
|
||
<aside> | ||
<header> | ||
<h1>Lorem Ipsum</h1> | ||
</header> | ||
|
||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore cum, blanditiis a minima aspernatur | ||
corporis pariatur, perferendis explicabo consectetur tenetur iste, fuga possimus corrupti dolorem | ||
laudantium sapiente sunt error autem. | ||
</p> | ||
|
||
<figure> | ||
<!-- entity --><img src="embed:Eduardokum\LaravelMailAutoEmbed\Tests\fixtures\PictureEntity:1" /> | ||
<caption>Lorem Ipsum</caption> | ||
</figure> | ||
</aside> | ||
</main> | ||
</body> | ||
|
||
</html> |