5 * Contains \Drupal\Tests\Component\Utility\SafeMarkupTest.
8 namespace Drupal\Tests\Component\Utility;
10 use Drupal\Component\Render\HtmlEscapedText;
11 use Drupal\Component\Utility\SafeMarkup;
12 use Drupal\Component\Render\MarkupInterface;
13 use Drupal\Component\Render\MarkupTrait;
14 use Drupal\Component\Utility\UrlHelper;
15 use PHPUnit\Framework\TestCase;
18 * Tests marking strings as safe.
22 * @coversDefaultClass \Drupal\Component\Utility\SafeMarkup
24 class SafeMarkupTest extends TestCase {
29 protected function tearDown() {
32 UrlHelper::setAllowedProtocols(['http', 'https']);
36 * Tests SafeMarkup::isSafe() with different objects.
39 * @expectedDeprecation SafeMarkup::isSafe() is scheduled for removal in Drupal 9.0.0. Instead, you should just check if a variable is an instance of \Drupal\Component\Render\MarkupInterface. See https://www.drupal.org/node/2549395.
41 public function testIsSafe() {
42 $safe_string = $this->getMockBuilder('\Drupal\Component\Render\MarkupInterface')->getMock();
43 $this->assertTrue(SafeMarkup::isSafe($safe_string));
44 $string_object = new SafeMarkupTestString('test');
45 $this->assertFalse(SafeMarkup::isSafe($string_object));
49 * Tests SafeMarkup::checkPlain().
51 * @dataProvider providerCheckPlain
52 * @covers ::checkPlain
53 * @expectedDeprecation SafeMarkup::checkPlain() is scheduled for removal in Drupal 9.0.0. Rely on Twig's auto-escaping feature, or use the @link theme_render #plain_text @endlink key when constructing a render array that contains plain text in order to use the renderer's auto-escaping feature. If neither of these are possible, \Drupal\Component\Utility\Html::escape() can be used in places where explicit escaping is needed. See https://www.drupal.org/node/2549395.
56 * The text to provide to SafeMarkup::checkPlain().
57 * @param string $expected
58 * The expected output from the function.
59 * @param string $message
60 * The message to provide as output for the test.
62 public function testCheckPlain($text, $expected, $message) {
63 $result = SafeMarkup::checkPlain($text);
64 $this->assertTrue($result instanceof HtmlEscapedText);
65 $this->assertEquals($expected, $result, $message);
69 * Tests Drupal\Component\Render\HtmlEscapedText.
71 * Verifies that the result of SafeMarkup::checkPlain() is the same as using
72 * HtmlEscapedText directly.
74 * @dataProvider providerCheckPlain
77 * The text to provide to the HtmlEscapedText constructor.
78 * @param string $expected
79 * The expected output from the function.
80 * @param string $message
81 * The message to provide as output for the test.
83 public function testHtmlEscapedText($text, $expected, $message) {
84 $result = new HtmlEscapedText($text);
85 $this->assertEquals($expected, $result, $message);
89 * Data provider for testCheckPlain() and testEscapeString().
91 * @see testCheckPlain()
93 public function providerCheckPlain() {
94 // Checks that invalid multi-byte sequences are escaped.
95 $tests[] = ["Foo\xC0barbaz", 'Foo�barbaz', 'Escapes invalid sequence "Foo\xC0barbaz"'];
96 $tests[] = ["\xc2\"", '�"', 'Escapes invalid sequence "\xc2\""'];
97 $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
99 // Checks that special characters are escaped.
100 $tests[] = [SafeMarkupTestMarkup::create("<script>"), '<script>', 'Escapes <script> even inside an object that implements MarkupInterface.'];
101 $tests[] = ["<script>", '<script>', 'Escapes <script>'];
102 $tests[] = ['<>&"\'', '<>&"'', 'Escapes reserved HTML characters.'];
103 $tests[] = [SafeMarkupTestMarkup::create('<>&"\''), '<>&"'', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
109 * Tests string formatting with SafeMarkup::format().
111 * @dataProvider providerFormat
113 * @expectedDeprecation SafeMarkup::format() is scheduled for removal in Drupal 9.0.0. Use \Drupal\Component\Render\FormattableMarkup. See https://www.drupal.org/node/2549395.
115 * @param string $string
116 * The string to run through SafeMarkup::format().
117 * @param string[] $args
118 * The arguments to pass into SafeMarkup::format().
119 * @param string $expected
120 * The expected result from calling the function.
121 * @param string $message
122 * The message to display as output to the test.
123 * @param bool $expected_is_safe
124 * Whether the result is expected to be safe for HTML display.
126 public function testFormat($string, array $args, $expected, $message, $expected_is_safe) {
127 UrlHelper::setAllowedProtocols(['http', 'https', 'mailto']);
129 $result = SafeMarkup::format($string, $args);
130 $this->assertEquals($expected, (string) $result, $message);
131 $this->assertEquals($expected_is_safe, $result instanceof MarkupInterface, 'SafeMarkup::format correctly sets the result as safe or not safe.');
135 * Data provider for testFormat().
139 public function providerFormat() {
140 $tests[] = ['Simple text', [], 'Simple text', 'SafeMarkup::format leaves simple text alone.', TRUE];
141 $tests[] = ['Escaped text: @value', ['@value' => '<script>'], 'Escaped text: <script>', 'SafeMarkup::format replaces and escapes string.', TRUE];
142 $tests[] = ['Escaped text: @value', ['@value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>')], 'Escaped text: <span>Safe HTML</span>', 'SafeMarkup::format does not escape an already safe string.', TRUE];
143 $tests[] = ['Placeholder text: %value', ['%value' => '<script>'], 'Placeholder text: <em class="placeholder"><script></em>', 'SafeMarkup::format replaces, escapes and themes string.', TRUE];
144 $tests[] = ['Placeholder text: %value', ['%value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>')], 'Placeholder text: <em class="placeholder"><span>Safe HTML</span></em>', 'SafeMarkup::format does not escape an already safe string themed as a placeholder.', TRUE];
146 $tests['javascript-protocol-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'javascript://example.com?foo&bar'], 'Simple text <a href="//example.com?foo&bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
147 $tests['external-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'http://example.com?foo&bar'], 'Simple text <a href="http://example.com?foo&bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
148 $tests['relative-url'] = ['Simple text <a href=":url">giraffe</a>', [':url' => '/node/1?foo&bar'], 'Simple text <a href="/node/1?foo&bar">giraffe</a>', 'Support for filtering bad protocols', TRUE];
149 $tests['fragment-with-special-chars'] = ['Simple text <a href=":url">giraffe</a>', [':url' => 'http://example.com/#<'], 'Simple text <a href="http://example.com/#&lt;">giraffe</a>', 'Support for filtering bad protocols', TRUE];
150 $tests['mailto-protocol'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => 'mailto:test@example.com'], 'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>', '', TRUE];
151 $tests['js-with-fromCharCode'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "javascript:alert(String.fromCharCode(88,83,83))"], 'Hey giraffe <a href="alert(String.fromCharCode(88,83,83))">MUUUH</a>', '', TRUE];
153 // Test some "URL" values that are not RFC 3986 compliant URLs. The result
154 // of SafeMarkup::format() should still be valid HTML (other than the
155 // value of the "href" attribute not being a valid URL), and not
156 // vulnerable to XSS.
157 $tests['non-url-with-colon'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "llamas: they are not URLs"], 'Hey giraffe <a href=" they are not URLs">MUUUH</a>', '', TRUE];
158 $tests['non-url-with-html'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "<span>not a url</span>"], 'Hey giraffe <a href="<span>not a url</span>">MUUUH</a>', '', TRUE];
160 // Tests non-standard placeholders that will not replace.
161 $tests['non-standard-placeholder'] = ['Hey hey', ['risky' => "<script>alert('foo');</script>"], 'Hey hey', '', TRUE];
167 class SafeMarkupTestString {
171 public function __construct($string) {
172 $this->string = $string;
175 public function __toString() {
176 return $this->string;
182 * Marks an object's __toString() method as returning markup.
184 class SafeMarkupTestMarkup implements MarkupInterface {