8000 [10.x] Escape values before checking for their presence in HTML part of Mailables by turanjanin · Pull Request #42923 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[10.x] Escape values before checking for their presence in HTML part of Mailables #42923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,13 @@ public function metadata($key, $value)
* Assert that the given text is present in the HTML email body.
*
* @param string $string
* @param bool $escape
* @return $this
*/
public function assertSeeInHtml($string)
public function assertSeeInHtml($string, $escape = true)
{
$string = $escape ? e($string) : $string;

[$html, $text] = $this->renderForAssertions();

PHPUnit::assertTrue(
Expand All @@ -994,10 +997,13 @@ public function assertSeeInHtml($string)
* Assert that the given text is not present in the HTML email body.
*
* @param string $string
* @param bool $escape
* @return $this
*/
public function assertDontSeeInHtml($string)
public function assertDontSeeInHtml($string, $escape = true)
{
$string = $escape ? e($string) : $string;

[$html, $text] = $this->renderForAssertions();

PHPUnit::assertFalse(
Expand All @@ -1012,10 +1018,13 @@ public function assertDontSeeInHtml($string)
* Assert that the given text strings are present in order in the HTML email body.
*
* @param array $strings
* @param bool $escape
* @return $this
*/
public function assertSeeInOrderInHtml($strings)
public function assertSeeInOrderInHtml($strings, $escape = true)
{
$strings = $escape ? array_map('e', ($strings)) : $strings;

[$html, $text] = $this->renderForAssertions();

PHPUnit::assertThat($strings, new SeeInOrder($html));
Expand Down
29 changes: 25 additions & 4 deletions tests/Mail/MailMailableAssertionsTest.php
F3F6
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function testMailableAssertSeeInHtmlPassesWhenPresent()
{
$mailable = new MailableAssertionsStub;

$mailable->assertSeeInHtml('<li>First Item</li>');
$mailable->assertSeeInHtml('Fourth & Fifth Item');

$mailable->assertSeeInHtml('<li>First Item</li>', false);
}

public function testMailableAssertSeeInHtmlFailsWhenAbsent()
Expand All @@ -63,13 +65,22 @@ public function testMailableAssertDontSeeInHtmlPassesWhenAbsent()
$mailable->assertDontSeeInHtml('<li>Fourth Item</li>');
}

public function testMailableAssertDontSeeInHtmlFailsWhenPresent()
public function testMailableAssertDontSeeInHtmlEscapedFailsWhenPresent()
{
$mailable = new MailableAssertionsStub;

$this->expectException(AssertionFailedError::class);

$mailable->assertDontSeeInHtml('<li>First Item</li>');
$mailable->assertDontSeeInHtml('Fourth & Fifth Item');
}

public function testMailableAssertDontSeeInHtmlUnescapedFailsWhenPresent()
{
$mailable = new MailableAssertionsStub;

$this->expectException(AssertionFailedError::class);

$mailable->assertDontSeeInHtml('<li>First Item</li>', false);
}

public function testMailableAssertSeeInOrderTextPassesWhenPresentInOrder()
Expand Down Expand Up @@ -100,11 +111,17 @@ public function testMailableAssertInOrderHtmlPassesWhenPresentInOrder()
{
$mailable = new MailableAssertionsStub;

$mailable->assertSeeInOrderInHtml([
'Third Item',
'Fourth & Fifth Item',
'Sixth Item',
]);

$mailable->assertSeeInOrderInHtml([
'<li>First Item</li>',
'<li>Second Item</li>',
'<li>Third Item</li>',
]);
], false);
}

public function testMailableAssertInOrderHtmlFailsWhenAbsentInOrder()
Expand All @@ -130,6 +147,8 @@ protected function renderForAssertions()
- First Item
- Second Item
- Third Item
- Fourth & Fifth Item
- Sixth Item
EOD;

$html = <<<'EOD'
Expand All @@ -146,6 +165,8 @@ protected function renderForAssertions()
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
<li>Fourth &amp; Fifth Item</li>
<li>Sixth Item</li>
</ul>
</body>
</html>
Expand Down
0