3 namespace Drupal\Tests\Component\Render;
5 use Drupal\Component\Render\HtmlEscapedText;
6 use Drupal\Component\Render\MarkupInterface;
7 use PHPUnit\Framework\TestCase;
10 * Tests the HtmlEscapedText class.
12 * @coversDefaultClass \Drupal\Component\Render\HtmlEscapedText
15 class HtmlEscapedTextTest extends TestCase {
18 * @covers ::__toString
19 * @covers ::jsonSerialize
21 * @dataProvider providerToString
23 public function testToString($text, $expected, $message) {
24 $escapeable_string = new HtmlEscapedText($text);
25 $this->assertEquals($expected, (string) $escapeable_string, $message);
26 $this->assertEquals($expected, $escapeable_string->jsonSerialize());
30 * Data provider for testToString().
34 public function providerToString() {
35 // Checks that invalid multi-byte sequences are escaped.
36 $tests[] = ["Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"'];
37 $tests[] = ["\xc2\"", '�"', 'Escapes invalid sequence "\xc2\""'];
38 $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
40 // Checks that special characters are escaped.
41 $script_tag = $this->prophesize(MarkupInterface::class);
42 $script_tag->__toString()->willReturn('<script>');
43 $script_tag = $script_tag->reveal();
44 $tests[] = [$script_tag, '<script>', 'Escapes <script> even inside an object that implements MarkupInterface.'];
45 $tests[] = ["<script>", '<script>', 'Escapes <script>'];
46 $tests[] = ['<>&"\'', '<>&"'', 'Escapes reserved HTML characters.'];
47 $specialchars = $this->prophesize(MarkupInterface::class);
48 $specialchars->__toString()->willReturn('<>&"\'');
49 $specialchars = $specialchars->reveal();
50 $tests[] = [$specialchars, '<>&"'', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
58 public function testCount() {
59 $string = 'Can I please have a <em>kitten</em>';
60 $escapeable_string = new HtmlEscapedText($string);
61 $this->assertEquals(strlen($string), $escapeable_string->count());