c064e2c2a078d7b61a986933179c6a13920836a1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Render / HtmlEscapedTextTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Render;
4
5 use Drupal\Component\Render\HtmlEscapedText;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * Tests the HtmlEscapedText class.
11  *
12  * @coversDefaultClass \Drupal\Component\Render\HtmlEscapedText
13  * @group utility
14  */
15 class HtmlEscapedTextTest extends UnitTestCase {
16
17   /**
18    * @covers ::__toString
19    * @covers ::jsonSerialize
20    *
21    * @dataProvider providerToString
22    */
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());
27   }
28
29   /**
30    * Data provider for testToString().
31    *
32    * @see testToString()
33    */
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\"", '�&quot;', 'Escapes invalid sequence "\xc2\""'];
38     $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
39
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, '&lt;script&gt;', 'Escapes &lt;script&gt; even inside an object that implements MarkupInterface.'];
45     $tests[] = ["<script>", '&lt;script&gt;', 'Escapes &lt;script&gt;'];
46     $tests[] = ['<>&"\'', '&lt;&gt;&amp;&quot;&#039;', 'Escapes reserved HTML characters.'];
47     $specialchars = $this->prophesize(MarkupInterface::class);
48     $specialchars->__toString()->willReturn('<>&"\'');
49     $specialchars = $specialchars->reveal();
50     $tests[] = [$specialchars, '&lt;&gt;&amp;&quot;&#039;', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
51
52     return $tests;
53   }
54
55   /**
56    * @covers ::count
57    */
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());
62   }
63
64 }