8000 Escape asserted value by default before checking for it's presence in… · laravel/framework@55a1ea2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 55a1ea2

Browse files
authored
Escape asserted value by default before checking for it's presence in HTML (#42923)
1 parent 8d23c67 commit 55a1ea2

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,13 @@ public function metadata($key, $value)
976976
* Assert that the given text is present in the HTML email body.
977977
*
978978
* @param string $string
979+
* @param bool $escape
979980
* @return $this
980981
*/
981-
public function assertSeeInHtml($string)
982+
public function assertSeeInHtml($string, $escape = true)
982983
{
984+
$string = $escape ? e($string) : $string;
985+
983986
[$html, $text] = $this->renderForAssertions();
984987

985988
PHPUnit::assertTrue(
@@ -994,10 +997,13 @@ public function assertSeeInHtml($string)
994997
* Assert that the given text is not present in the HTML email body.
995998
*
996999
* @param string $string
1000+
* @param bool $escape
9971001
* @return $this
9981002
*/
999-
public function assertDontSeeInHtml($string)
1003+
public function assertDontSeeInHtml($string, $escape = true)
10001004
{
1005+
$string = $escape ? e($string) : $string;
1006+
10011007
[$html, $text] = $this->renderForAssertions();
10021008

10031009
PHPUnit::assertFalse(
@@ -1012,10 +1018,13 @@ public function assertDontSeeInHtml($string)
10121018
* Assert that the given text strings are present in order in the HTML email body.
10131019
*
10141020
* @param array $strings
1021+
* @param bool $escape
10151022
* @return $this
10161023
*/
1017-
public function assertSeeInOrderInHtml($strings)
1024+
public function assertSeeInOrderInHtml($strings, $escape = true)
10181025
{
1026+
$strings = $escape ? array_map('e', ($strings)) : $strings;
1027+
10191028
[$html, $text] = $this->renderForAssertions();
10201029

10211030
PHPUnit::assertThat($strings, new SeeInOrder($html));

tests/Mail/MailMailableAssertionsTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public function testMailableAssertSeeInHtmlPassesWhenPresent()
4444
{
4545
$mailable = new MailableAssertionsStub;
4646

47-
$mailable->assertSeeInHtml('<li>First Item</li>');
47+
$mailable->assertSeeInHtml('Fourth & Fifth Item');
48+
49+
$mailable->assertSeeInHtml('<li>First Item</li>', false);
4850
}
4951

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

66-
public function testMailableAssertDontSeeInHtmlFailsWhenPresent()
68+
public function testMailableAssertDontSeeInHtmlEscapedFailsWhenPresent()
6769
{
6870
$mailable = new MailableAssertionsStub;
6971

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

72-
$mailable->assertDontSeeInHtml('<li>First Item</li>');
74+
$mailable->assertDontSeeInHtml('Fourth & Fifth Item');
75+
}
76+
77+
public function testMailableAssertDontSeeInHtmlUnescapedFailsWhenPresent()
78+
{
79+
$mailable = new MailableAssertionsStub;
80+
81+
$this->expectException(AssertionFailedError::class);
82+
83+
$mailable->assertDontSeeInHtml('<li>First Item</li>', false);
7384
}
7485

7586
public function testMailableAssertSeeInOrderTextPassesWhenPresentInOrder()
@@ -100,11 +111,17 @@ public function testMailableAssertInOrderHtmlPassesWhenPresentInOrder()
100111
{
101112
$mailable = new MailableAssertionsStub;
102113

114+
$mailable->assertSeeInOrderInHtml([
115+
'Third Item',
116+
'Fourth & Fifth Item',
117+
'Sixth Item',
118+
]);
119+
103120
$mailable->assertSeeInOrderInHtml([
104121
'<li>First Item</li>',
105122
'<li>Second Item</li>',
106123
'<li>Third Item</li>',
107-
]);
124+
], false);
108125
}
109126

110127
public function testMailableAssertInOrderHtmlFailsWhenAbsentInOrder()
@@ -130,6 +147,8 @@ protected function renderForAssertions()
130147
- First Item
131148
- Second Item
132149
- Third Item
150+
- Fourth & Fifth Item
151+
- Sixth Item
133152
EOD;
134153

135154
$html = <<<'EOD'
@@ -146,6 +165,8 @@ protected function renderForAssertions()
146165
<li>First Item</li>
147166
<li>Second Item</li>
148167
<li>Third Item</li>
168+
<li>Fourth &amp; Fifth Item</li>
169+
<li>Sixth Item</li>
149170
</ul>
150171
</body>
151172
</html>

0 commit comments

Comments
 (0)
0