View source
<?php
namespace Drupal\Tests\Component\Render;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Tests\UnitTestCase;
class HtmlEscapedTextTest extends UnitTestCase {
public function testToString($text, $expected, $message) {
$escapeable_string = new HtmlEscapedText($text);
$this
->assertEquals($expected, (string) $escapeable_string, $message);
$this
->assertEquals($expected, $escapeable_string
->jsonSerialize());
}
function providerToString() {
$tests[] = array(
"",
'Foo�barbaz',
'Escapes invalid sequence "Foo\\xC0barbaz"',
);
$tests[] = array(
"",
'�"',
'Escapes invalid sequence "\\xc2\\""',
);
$tests[] = array(
"Fooÿñ",
"Fooÿñ",
'Does not escape valid sequence "Fooÿñ"',
);
$script_tag = $this
->prophesize(MarkupInterface::class);
$script_tag
->__toString()
->willReturn('<script>');
$script_tag = $script_tag
->reveal();
$tests[] = array(
$script_tag,
'<script>',
'Escapes <script> even inside an object that implements MarkupInterface.',
);
$tests[] = array(
"<script>",
'<script>',
'Escapes <script>',
);
$tests[] = array(
'<>&"\'',
'<>&"'',
'Escapes reserved HTML characters.',
);
$specialchars = $this
->prophesize(MarkupInterface::class);
$specialchars
->__toString()
->willReturn('<>&"\'');
$specialchars = $specialchars
->reveal();
$tests[] = array(
$specialchars,
'<>&"'',
'Escapes reserved HTML characters even inside an object that implements MarkupInterface.',
);
return $tests;
}
public function testCount() {
$string = 'Can I please have a <em>kitten</em>';
$escapeable_string = new HtmlEscapedText($string);
$this
->assertEquals(strlen($string), $escapeable_string
->count());
}
}