3028b62dd0270f75db63d6ded5b43e5d6819d7a1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Component / Utility / SafeMarkupKernelTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Component\Utility;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Url;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Provides a test covering integration of SafeMarkup with other systems.
11  *
12  * @group Utility
13 */
14 class SafeMarkupKernelTest extends KernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['system'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     $this->container->get('router.builder')->rebuild();
28   }
29
30   /**
31    * Gets arguments for FormattableMarkup based on Url::fromUri() parameters.
32    *
33    * @param string $uri
34    *   The URI of the resource.
35    * @param array $options
36    *   The options to pass to Url::fromUri().
37    *
38    * @return array
39    *   Array containing:
40    *   - ':url': A URL string.
41    *
42    * @see \Drupal\Component\Render\FormattableMarkup
43    */
44   protected static function getSafeMarkupUriArgs($uri, $options = []) {
45     $args[':url'] = Url::fromUri($uri, $options)->toString();
46     return $args;
47   }
48
49   /**
50    * Tests URL ":placeholders" in \Drupal\Component\Render\FormattableMarkup.
51    *
52    * @dataProvider providerTestSafeMarkupUri
53    */
54   public function testSafeMarkupUri($string, $uri, $options, $expected) {
55     $args = self::getSafeMarkupUriArgs($uri, $options);
56     $this->assertEquals($expected, new FormattableMarkup($string, $args));
57   }
58
59   /**
60    * @return array
61    */
62   public function providerTestSafeMarkupUri() {
63     $data = [];
64     $data['routed-url'] = [
65       'Hey giraffe <a href=":url">MUUUH</a>',
66       'route:system.admin',
67       [],
68       'Hey giraffe <a href="/admin">MUUUH</a>',
69     ];
70     $data['routed-with-query'] = [
71       'Hey giraffe <a href=":url">MUUUH</a>',
72       'route:system.admin',
73       ['query' => ['bar' => 'baz#']],
74       'Hey giraffe <a href="/admin?bar=baz%23">MUUUH</a>',
75     ];
76     $data['routed-with-fragment'] = [
77       'Hey giraffe <a href=":url">MUUUH</a>',
78       'route:system.admin',
79       ['fragment' => 'bar&lt;'],
80       'Hey giraffe <a href="/admin#bar&amp;lt;">MUUUH</a>',
81     ];
82     $data['unrouted-url'] = [
83       'Hey giraffe <a href=":url">MUUUH</a>',
84       'base://foo',
85       [],
86       'Hey giraffe <a href="/foo">MUUUH</a>',
87     ];
88     $data['unrouted-with-query'] = [
89       'Hey giraffe <a href=":url">MUUUH</a>',
90       'base://foo',
91       ['query' => ['bar' => 'baz#']],
92       'Hey giraffe <a href="/foo?bar=baz%23">MUUUH</a>',
93     ];
94     $data['unrouted-with-fragment'] = [
95       'Hey giraffe <a href=":url">MUUUH</a>',
96       'base://foo',
97       ['fragment' => 'bar&lt;'],
98       'Hey giraffe <a href="/foo#bar&amp;lt;">MUUUH</a>',
99     ];
100     $data['mailto-protocol'] = [
101       'Hey giraffe <a href=":url">MUUUH</a>',
102       'mailto:test@example.com',
103       [],
104       'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>',
105     ];
106
107     return $data;
108   }
109
110   /**
111    * @dataProvider providerTestSafeMarkupUriWithException
112    */
113   public function testSafeMarkupUriWithExceptionUri($string, $uri) {
114     // Should throw an \InvalidArgumentException, due to Uri::toString().
115     $this->setExpectedException(\InvalidArgumentException::class);
116     $args = self::getSafeMarkupUriArgs($uri);
117
118     new FormattableMarkup($string, $args);
119   }
120
121   /**
122    * @return array
123    */
124   public function providerTestSafeMarkupUriWithException() {
125     $data = [];
126     $data['js-protocol'] = [
127       'Hey giraffe <a href=":url">MUUUH</a>',
128       "javascript:alert('xss')",
129     ];
130     $data['js-with-fromCharCode'] = [
131       'Hey giraffe <a href=":url">MUUUH</a>',
132       "javascript:alert(String.fromCharCode(88,83,83))",
133     ];
134     $data['non-url-with-colon'] = [
135       'Hey giraffe <a href=":url">MUUUH</a>',
136       "llamas: they are not URLs",
137     ];
138     $data['non-url-with-html'] = [
139       'Hey giraffe <a href=":url">MUUUH</a>',
140       '<span>not a url</span>',
141     ];
142
143     return $data;
144   }
145
146 }