Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / SafeMarkupTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Component\Utility\SafeMarkupTest.
6  */
7
8 namespace Drupal\Tests\Component\Utility;
9
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;
16
17 /**
18  * Tests marking strings as safe.
19  *
20  * @group Utility
21  * @group legacy
22  * @coversDefaultClass \Drupal\Component\Utility\SafeMarkup
23  */
24 class SafeMarkupTest extends TestCase {
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function tearDown() {
30     parent::tearDown();
31
32     UrlHelper::setAllowedProtocols(['http', 'https']);
33   }
34
35   /**
36    * Tests SafeMarkup::isSafe() with different objects.
37    *
38    * @covers ::isSafe
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.
40    */
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));
46   }
47
48   /**
49    * Tests SafeMarkup::checkPlain().
50    *
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.
54    *
55    * @param string $text
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.
61    */
62   public function testCheckPlain($text, $expected, $message) {
63     $result = SafeMarkup::checkPlain($text);
64     $this->assertTrue($result instanceof HtmlEscapedText);
65     $this->assertEquals($expected, $result, $message);
66   }
67
68   /**
69    * Tests Drupal\Component\Render\HtmlEscapedText.
70    *
71    * Verifies that the result of SafeMarkup::checkPlain() is the same as using
72    * HtmlEscapedText directly.
73    *
74    * @dataProvider providerCheckPlain
75    *
76    * @param string $text
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.
82    */
83   public function testHtmlEscapedText($text, $expected, $message) {
84     $result = new HtmlEscapedText($text);
85     $this->assertEquals($expected, $result, $message);
86   }
87
88   /**
89    * Data provider for testCheckPlain() and testEscapeString().
90    *
91    * @see testCheckPlain()
92    */
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\"", '�&quot;', 'Escapes invalid sequence "\xc2\""'];
97     $tests[] = ["Fooÿñ", "Fooÿñ", 'Does not escape valid sequence "Fooÿñ"'];
98
99     // Checks that special characters are escaped.
100     $tests[] = [SafeMarkupTestMarkup::create("<script>"), '&lt;script&gt;', 'Escapes &lt;script&gt; even inside an object that implements MarkupInterface.'];
101     $tests[] = ["<script>", '&lt;script&gt;', 'Escapes &lt;script&gt;'];
102     $tests[] = ['<>&"\'', '&lt;&gt;&amp;&quot;&#039;', 'Escapes reserved HTML characters.'];
103     $tests[] = [SafeMarkupTestMarkup::create('<>&"\''), '&lt;&gt;&amp;&quot;&#039;', 'Escapes reserved HTML characters even inside an object that implements MarkupInterface.'];
104
105     return $tests;
106   }
107
108   /**
109    * Tests string formatting with SafeMarkup::format().
110    *
111    * @dataProvider providerFormat
112    * @covers ::format
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.
114    *
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.
125    */
126   public function testFormat($string, array $args, $expected, $message, $expected_is_safe) {
127     UrlHelper::setAllowedProtocols(['http', 'https', 'mailto']);
128
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.');
132   }
133
134   /**
135    * Data provider for testFormat().
136    *
137    * @see testFormat()
138    */
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: &lt;script&gt;', '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">&lt;script&gt;</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];
145
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&amp;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&amp;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&amp;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/#&lt;'], 'Simple text <a href="http://example.com/#&amp;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];
152
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="&lt;span&gt;not a url&lt;/span&gt;">MUUUH</a>', '', TRUE];
159
160     // Tests non-standard placeholders that will not replace.
161     $tests['non-standard-placeholder'] = ['Hey hey', ['risky' => "<script>alert('foo');</script>"], 'Hey hey', '', TRUE];
162     return $tests;
163   }
164
165 }
166
167 class SafeMarkupTestString {
168
169   protected $string;
170
171   public function __construct($string) {
172     $this->string = $string;
173   }
174
175   public function __toString() {
176     return $this->string;
177   }
178
179 }
180
181 /**
182  * Marks an object's __toString() method as returning markup.
183  */
184 class SafeMarkupTestMarkup implements MarkupInterface {
185   use MarkupTrait;
186
187 }