Mercurial > hg > cmmr2012-drupal-site
comparison core/tests/Drupal/KernelTests/Component/Utility/SafeMarkupKernelTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\KernelTests\Component\Utility; | |
4 | |
5 use Drupal\Component\Utility\SafeMarkup; | |
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 SafeMarkup::format() 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 protected static function getSafeMarkupUriArgs($uri, $options = []) { | |
43 $args[':url'] = Url::fromUri($uri, $options)->toString(); | |
44 return $args; | |
45 } | |
46 | |
47 /** | |
48 * Tests URL ":placeholders" in SafeMarkup::format(). | |
49 * | |
50 * @dataProvider providerTestSafeMarkupUri | |
51 */ | |
52 public function testSafeMarkupUri($string, $uri, $options, $expected) { | |
53 $args = self::getSafeMarkupUriArgs($uri, $options); | |
54 $this->assertEquals($expected, SafeMarkup::format($string, $args)); | |
55 } | |
56 | |
57 /** | |
58 * @return array | |
59 */ | |
60 public function providerTestSafeMarkupUri() { | |
61 $data = []; | |
62 $data['routed-url'] = [ | |
63 'Hey giraffe <a href=":url">MUUUH</a>', | |
64 'route:system.admin', | |
65 [], | |
66 'Hey giraffe <a href="/admin">MUUUH</a>', | |
67 ]; | |
68 $data['routed-with-query'] = [ | |
69 'Hey giraffe <a href=":url">MUUUH</a>', | |
70 'route:system.admin', | |
71 ['query' => ['bar' => 'baz#']], | |
72 'Hey giraffe <a href="/admin?bar=baz%23">MUUUH</a>', | |
73 ]; | |
74 $data['routed-with-fragment'] = [ | |
75 'Hey giraffe <a href=":url">MUUUH</a>', | |
76 'route:system.admin', | |
77 ['fragment' => 'bar<'], | |
78 'Hey giraffe <a href="/admin#bar&lt;">MUUUH</a>', | |
79 ]; | |
80 $data['unrouted-url'] = [ | |
81 'Hey giraffe <a href=":url">MUUUH</a>', | |
82 'base://foo', | |
83 [], | |
84 'Hey giraffe <a href="/foo">MUUUH</a>', | |
85 ]; | |
86 $data['unrouted-with-query'] = [ | |
87 'Hey giraffe <a href=":url">MUUUH</a>', | |
88 'base://foo', | |
89 ['query' => ['bar' => 'baz#']], | |
90 'Hey giraffe <a href="/foo?bar=baz%23">MUUUH</a>', | |
91 ]; | |
92 $data['unrouted-with-fragment'] = [ | |
93 'Hey giraffe <a href=":url">MUUUH</a>', | |
94 'base://foo', | |
95 ['fragment' => 'bar<'], | |
96 'Hey giraffe <a href="/foo#bar&lt;">MUUUH</a>', | |
97 ]; | |
98 $data['mailto-protocol'] = [ | |
99 'Hey giraffe <a href=":url">MUUUH</a>', | |
100 'mailto:test@example.com', | |
101 [], | |
102 'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>', | |
103 ]; | |
104 | |
105 return $data; | |
106 } | |
107 | |
108 /** | |
109 * @dataProvider providerTestSafeMarkupUriWithException | |
110 */ | |
111 public function testSafeMarkupUriWithExceptionUri($string, $uri) { | |
112 // Should throw an \InvalidArgumentException, due to Uri::toString(). | |
113 $this->setExpectedException(\InvalidArgumentException::class); | |
114 $args = self::getSafeMarkupUriArgs($uri); | |
115 | |
116 SafeMarkup::format($string, $args); | |
117 } | |
118 | |
119 /** | |
120 * @return array | |
121 */ | |
122 public function providerTestSafeMarkupUriWithException() { | |
123 $data = []; | |
124 $data['js-protocol'] = [ | |
125 'Hey giraffe <a href=":url">MUUUH</a>', | |
126 "javascript:alert('xss')", | |
127 ]; | |
128 $data['js-with-fromCharCode'] = [ | |
129 'Hey giraffe <a href=":url">MUUUH</a>', | |
130 "javascript:alert(String.fromCharCode(88,83,83))", | |
131 ]; | |
132 $data['non-url-with-colon'] = [ | |
133 'Hey giraffe <a href=":url">MUUUH</a>', | |
134 "llamas: they are not URLs", | |
135 ]; | |
136 $data['non-url-with-html'] = [ | |
137 'Hey giraffe <a href=":url">MUUUH</a>', | |
138 '<span>not a url</span>', | |
139 ]; | |
140 | |
141 return $data; | |
142 } | |
143 | |
144 } |