Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\Component\Render;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Render\HtmlEscapedText;
|
Chris@0
|
6 use Drupal\Component\Render\MarkupInterface;
|
Chris@0
|
7 use PHPUnit\Framework\TestCase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests the HtmlEscapedText class.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @coversDefaultClass \Drupal\Component\Render\HtmlEscapedText
|
Chris@0
|
13 * @group utility
|
Chris@0
|
14 */
|
Chris@0
|
15 class HtmlEscapedTextTest extends TestCase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * @covers ::__toString
|
Chris@0
|
19 * @covers ::jsonSerialize
|
Chris@0
|
20 *
|
Chris@0
|
21 * @dataProvider providerToString
|
Chris@0
|
22 */
|
Chris@0
|
23 public function testToString($text, $expected, $message) {
|
Chris@0
|
24 $escapeable_string = new HtmlEscapedText($text);
|
Chris@0
|
25 $this->assertEquals($expected, (string) $escapeable_string, $message);
|
Chris@0
|
26 $this->assertEquals($expected, $escapeable_string->jsonSerialize());
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Data provider for testToString().
|
Chris@0
|
31 *
|
Chris@0
|
32 * @see testToString()
|
Chris@0
|
33 */
|
Chris@0
|
34 public function providerToString() {
|
Chris@0
|
35 // Checks that invalid multi-byte sequences are escaped.
|
Chris@0
|
36 $tests[] = ["Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"'];
|
Chris@0
|
37 $tests[] = ["\xc2\"", '�"', 'Escapes invalid sequence "\xc2\""'];
|
Chris@0
|
38 $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
|
Chris@0
|
39
|
Chris@0
|
40 // Checks that special characters are escaped.
|
Chris@0
|
41 $script_tag = $this->prophesize(MarkupInterface::class);
|
Chris@0
|
42 $script_tag->__toString()->willReturn('<script>');
|
Chris@0
|
43 $script_tag = $script_tag->reveal();
|
Chris@0
|
44 $tests[] = [$script_tag, '<script>', 'Escapes <script> even inside an object that implements MarkupInterface.'];
|
Chris@0
|
45 $tests[] = ["<script>", '<script>', 'Escapes <script>'];
|
Chris@0
|
46 $tests[] = ['<>&"\'', '<>&"'', 'Escapes reserved HTML characters.'];
|
Chris@0
|
47 $specialchars = $this->prophesize(MarkupInterface::class);
|
Chris@0
|
48 $specialchars->__toString()->willReturn('<>&"\'');
|
Chris@0
|
49 $specialchars = $specialchars->reveal();
|
Chris@0
|
50 $tests[] = [$specialchars, '<>&"'', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
|
Chris@0
|
51
|
Chris@0
|
52 return $tests;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * @covers ::count
|
Chris@0
|
57 */
|
Chris@0
|
58 public function testCount() {
|
Chris@0
|
59 $string = 'Can I please have a <em>kitten</em>';
|
Chris@0
|
60 $escapeable_string = new HtmlEscapedText($string);
|
Chris@0
|
61 $this->assertEquals(strlen($string), $escapeable_string->count());
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 }
|