class HtmlEscapedTextTest
Same name in other branches
- 8.9.x core/tests/Drupal/Tests/Component/Render/HtmlEscapedTextTest.php \Drupal\Tests\Component\Render\HtmlEscapedTextTest
- 10 core/tests/Drupal/Tests/Component/Render/HtmlEscapedTextTest.php \Drupal\Tests\Component\Render\HtmlEscapedTextTest
- 11.x core/tests/Drupal/Tests/Component/Render/HtmlEscapedTextTest.php \Drupal\Tests\Component\Render\HtmlEscapedTextTest
Tests the HtmlEscapedText class.
@coversDefaultClass \Drupal\Component\Render\HtmlEscapedText @group utility
Hierarchy
- class \Drupal\Tests\Component\Render\HtmlEscapedTextTest extends \PHPUnit\Framework\TestCase
Expanded class hierarchy of HtmlEscapedTextTest
File
-
core/
tests/ Drupal/ Tests/ Component/ Render/ HtmlEscapedTextTest.php, line 15
Namespace
Drupal\Tests\Component\RenderView source
class HtmlEscapedTextTest extends TestCase {
/**
* @covers ::__toString
* @covers ::jsonSerialize
*
* @dataProvider providerToString
*/
public function testToString($text, $expected, $message) {
$escapable_string = new HtmlEscapedText($text);
$this->assertEquals($expected, (string) $escapable_string, $message);
$this->assertEquals($expected, $escapable_string->jsonSerialize());
}
/**
* Data provider for testToString().
*
* @see testToString()
*/
public function providerToString() {
// Checks that invalid multi-byte sequences are escaped.
$tests[] = [
"Foo\xc0bar",
'Foo�bar',
'Escapes invalid sequence "Foo\\xC0bar"',
];
$tests[] = [
"\xc2\"",
'�"',
'Escapes invalid sequence "\\xc2\\""',
];
$tests[] = [
"Foo ÿñ",
"Foo ÿñ",
'Does not escape valid sequence "Foo ÿñ"',
];
// Checks that special characters are escaped.
$script_tag = $this->prophesize(MarkupInterface::class);
$script_tag->__toString()
->willReturn('<script>');
$script_tag = $script_tag->reveal();
$tests[] = [
$script_tag,
'<script>',
'Escapes <script> even inside an object that implements MarkupInterface.',
];
$tests[] = [
"<script>",
'<script>',
'Escapes <script>',
];
$tests[] = [
'<>&"\'',
'<>&"'',
'Escapes reserved HTML characters.',
];
$specialchars = $this->prophesize(MarkupInterface::class);
$specialchars->__toString()
->willReturn('<>&"\'');
$specialchars = $specialchars->reveal();
$tests[] = [
$specialchars,
'<>&"'',
'Escapes reserved HTML characters even inside an object that implements MarkupInterface.',
];
return $tests;
}
/**
* @covers ::count
*/
public function testCount() {
$string = 'Can I please have a <em>kitten</em>';
$escapable_string = new HtmlEscapedText($string);
$this->assertEquals(strlen($string), $escapable_string->count());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
HtmlEscapedTextTest::providerToString | public | function | Data provider for testToString(). |
HtmlEscapedTextTest::testCount | public | function | @covers ::count |
HtmlEscapedTextTest::testToString | public | function | @covers ::__toString @covers ::jsonSerialize |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.