Mercurial > hg > isophonics-drupal-site
comparison core/modules/system/src/Tests/Common/UrlTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\system\Tests\Common; | |
4 | |
5 use Drupal\Component\Utility\UrlHelper; | |
6 use Drupal\Core\Cache\Cache; | |
7 use Drupal\Core\Language\Language; | |
8 use Drupal\Core\Render\RenderContext; | |
9 use Drupal\Core\Url; | |
10 use Drupal\simpletest\WebTestBase; | |
11 | |
12 /** | |
13 * Confirm that \Drupal\Core\Url, | |
14 * \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), | |
15 * \Drupal\Component\Utility\UrlHelper::buildQuery(), and | |
16 * \Drupal\Core\Utility\LinkGeneratorInterface::generate() | |
17 * work correctly with various input. | |
18 * | |
19 * @group Common | |
20 */ | |
21 class UrlTest extends WebTestBase { | |
22 | |
23 public static $modules = ['common_test', 'url_alter_test']; | |
24 | |
25 /** | |
26 * Confirms that invalid URLs are filtered in link generating functions. | |
27 */ | |
28 public function testLinkXSS() { | |
29 // Test \Drupal::l(). | |
30 $text = $this->randomMachineName(); | |
31 $path = "<SCRIPT>alert('XSS')</SCRIPT>"; | |
32 $encoded_path = "3CSCRIPT%3Ealert%28%27XSS%27%29%3C/SCRIPT%3E"; | |
33 | |
34 $link = \Drupal::l($text, Url::fromUserInput('/' . $path)); | |
35 $this->assertTrue(strpos($link, $encoded_path) !== FALSE && strpos($link, $path) === FALSE, format_string('XSS attack @path was filtered by \Drupal\Core\Utility\LinkGeneratorInterface::generate().', ['@path' => $path])); | |
36 | |
37 // Test \Drupal\Core\Url. | |
38 $link = Url::fromUri('base:' . $path)->toString(); | |
39 $this->assertTrue(strpos($link, $encoded_path) !== FALSE && strpos($link, $path) === FALSE, format_string('XSS attack @path was filtered by #theme', ['@path' => $path])); | |
40 } | |
41 | |
42 /** | |
43 * Tests that #type=link bubbles outbound route/path processors' metadata. | |
44 */ | |
45 public function testLinkBubbleableMetadata() { | |
46 $cases = [ | |
47 ['Regular link', 'internal:/user', [], ['contexts' => [], 'tags' => [], 'max-age' => Cache::PERMANENT], []], | |
48 ['Regular link, absolute', 'internal:/user', ['absolute' => TRUE], ['contexts' => ['url.site'], 'tags' => [], 'max-age' => Cache::PERMANENT], []], | |
49 ['Route processor link', 'route:system.run_cron', [], ['contexts' => ['session'], 'tags' => [], 'max-age' => Cache::PERMANENT], ['placeholders' => []]], | |
50 ['Route processor link, absolute', 'route:system.run_cron', ['absolute' => TRUE], ['contexts' => ['url.site', 'session'], 'tags' => [], 'max-age' => Cache::PERMANENT], ['placeholders' => []]], | |
51 ['Path processor link', 'internal:/user/1', [], ['contexts' => [], 'tags' => ['user:1'], 'max-age' => Cache::PERMANENT], []], | |
52 ['Path processor link, absolute', 'internal:/user/1', ['absolute' => TRUE], ['contexts' => ['url.site'], 'tags' => ['user:1'], 'max-age' => Cache::PERMANENT], []], | |
53 ]; | |
54 | |
55 foreach ($cases as $case) { | |
56 list($title, $uri, $options, $expected_cacheability, $expected_attachments) = $case; | |
57 $expected_cacheability['contexts'] = Cache::mergeContexts($expected_cacheability['contexts'], ['languages:language_interface', 'theme', 'user.permissions']); | |
58 $link = [ | |
59 '#type' => 'link', | |
60 '#title' => $title, | |
61 '#options' => $options, | |
62 '#url' => Url::fromUri($uri), | |
63 ]; | |
64 \Drupal::service('renderer')->renderRoot($link); | |
65 $this->pass($title); | |
66 $this->assertEqual($expected_cacheability, $link['#cache']); | |
67 $this->assertEqual($expected_attachments, $link['#attached']); | |
68 } | |
69 } | |
70 | |
71 /** | |
72 * Tests that default and custom attributes are handled correctly on links. | |
73 */ | |
74 public function testLinkAttributes() { | |
75 /** @var \Drupal\Core\Render\RendererInterface $renderer */ | |
76 $renderer = $this->container->get('renderer'); | |
77 | |
78 // Test that hreflang is added when a link has a known language. | |
79 $language = new Language(['id' => 'fr', 'name' => 'French']); | |
80 $hreflang_link = [ | |
81 '#type' => 'link', | |
82 '#options' => [ | |
83 'language' => $language, | |
84 ], | |
85 '#url' => Url::fromUri('https://www.drupal.org'), | |
86 '#title' => 'bar', | |
87 ]; | |
88 $langcode = $language->getId(); | |
89 | |
90 // Test that the default hreflang handling for links does not override a | |
91 // hreflang attribute explicitly set in the render array. | |
92 $hreflang_override_link = $hreflang_link; | |
93 $hreflang_override_link['#options']['attributes']['hreflang'] = 'foo'; | |
94 | |
95 $rendered = $renderer->renderRoot($hreflang_link); | |
96 $this->assertTrue($this->hasAttribute('hreflang', $rendered, $langcode), format_string('hreflang attribute with value @langcode is present on a rendered link when langcode is provided in the render array.', ['@langcode' => $langcode])); | |
97 | |
98 $rendered = $renderer->renderRoot($hreflang_override_link); | |
99 $this->assertTrue($this->hasAttribute('hreflang', $rendered, 'foo'), format_string('hreflang attribute with value @hreflang is present on a rendered link when @hreflang is provided in the render array.', ['@hreflang' => 'foo'])); | |
100 | |
101 // Test the active class in links produced by | |
102 // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'. | |
103 $options_no_query = []; | |
104 $options_query = [ | |
105 'query' => [ | |
106 'foo' => 'bar', | |
107 'one' => 'two', | |
108 ], | |
109 ]; | |
110 $options_query_reverse = [ | |
111 'query' => [ | |
112 'one' => 'two', | |
113 'foo' => 'bar', | |
114 ], | |
115 ]; | |
116 | |
117 // Test #type link. | |
118 $path = 'common-test/type-link-active-class'; | |
119 | |
120 $this->drupalGet($path, $options_no_query); | |
121 $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active']); | |
122 $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page is marked active.'); | |
123 | |
124 $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active']); | |
125 $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string when the current page has no query string is not marked active.'); | |
126 | |
127 $this->drupalGet($path, $options_query); | |
128 $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active']); | |
129 $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that matches the current query string is marked active.'); | |
130 | |
131 $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query_reverse)->toString(), ':class' => 'is-active']); | |
132 $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.'); | |
133 | |
134 $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active']); | |
135 $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page without a query string when the current page has a query string is not marked active.'); | |
136 | |
137 // Test adding a custom class in links produced by | |
138 // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'. | |
139 // Test the link generator. | |
140 $class_l = $this->randomMachineName(); | |
141 $link_l = \Drupal::l($this->randomMachineName(), new Url('<current>', [], ['attributes' => ['class' => [$class_l]]])); | |
142 $this->assertTrue($this->hasAttribute('class', $link_l, $class_l), format_string('Custom class @class is present on link when requested by l()', ['@class' => $class_l])); | |
143 | |
144 // Test #type. | |
145 $class_theme = $this->randomMachineName(); | |
146 $type_link = [ | |
147 '#type' => 'link', | |
148 '#title' => $this->randomMachineName(), | |
149 '#url' => Url::fromRoute('<current>'), | |
150 '#options' => [ | |
151 'attributes' => [ | |
152 'class' => [$class_theme], | |
153 ], | |
154 ], | |
155 ]; | |
156 $link_theme = $renderer->renderRoot($type_link); | |
157 $this->assertTrue($this->hasAttribute('class', $link_theme, $class_theme), format_string('Custom class @class is present on link when requested by #type', ['@class' => $class_theme])); | |
158 } | |
159 | |
160 /** | |
161 * Tests that link functions support render arrays as 'text'. | |
162 */ | |
163 public function testLinkRenderArrayText() { | |
164 /** @var \Drupal\Core\Render\RendererInterface $renderer */ | |
165 $renderer = $this->container->get('renderer'); | |
166 | |
167 // Build a link with the link generator for reference. | |
168 $l = \Drupal::l('foo', Url::fromUri('https://www.drupal.org')); | |
169 | |
170 // Test a renderable array passed to the link generator. | |
171 $renderer->executeInRenderContext(new RenderContext(), function () use ($renderer, $l) { | |
172 $renderable_text = ['#markup' => 'foo']; | |
173 $l_renderable_text = \Drupal::l($renderable_text, Url::fromUri('https://www.drupal.org')); | |
174 $this->assertEqual($l_renderable_text, $l); | |
175 }); | |
176 | |
177 // Test a themed link with plain text 'text'. | |
178 $type_link_plain_array = [ | |
179 '#type' => 'link', | |
180 '#title' => 'foo', | |
181 '#url' => Url::fromUri('https://www.drupal.org'), | |
182 ]; | |
183 $type_link_plain = $renderer->renderRoot($type_link_plain_array); | |
184 $this->assertEqual($type_link_plain, $l); | |
185 | |
186 // Build a themed link with renderable 'text'. | |
187 $type_link_nested_array = [ | |
188 '#type' => 'link', | |
189 '#title' => ['#markup' => 'foo'], | |
190 '#url' => Url::fromUri('https://www.drupal.org'), | |
191 ]; | |
192 $type_link_nested = $renderer->renderRoot($type_link_nested_array); | |
193 $this->assertEqual($type_link_nested, $l); | |
194 } | |
195 | |
196 /** | |
197 * Checks for class existence in link. | |
198 * | |
199 * @param $link | |
200 * URL to search. | |
201 * @param $class | |
202 * Element class to search for. | |
203 * | |
204 * @return bool | |
205 * TRUE if the class is found, FALSE otherwise. | |
206 */ | |
207 private function hasAttribute($attribute, $link, $class) { | |
208 return preg_match('|' . $attribute . '="([^\"\s]+\s+)*' . $class . '|', $link); | |
209 } | |
210 | |
211 /** | |
212 * Tests UrlHelper::filterQueryParameters(). | |
213 */ | |
214 public function testDrupalGetQueryParameters() { | |
215 $original = [ | |
216 'a' => 1, | |
217 'b' => [ | |
218 'd' => 4, | |
219 'e' => [ | |
220 'f' => 5, | |
221 ], | |
222 ], | |
223 'c' => 3, | |
224 ]; | |
225 | |
226 // First-level exclusion. | |
227 $result = $original; | |
228 unset($result['b']); | |
229 $this->assertEqual(UrlHelper::filterQueryParameters($original, ['b']), $result, "'b' was removed."); | |
230 | |
231 // Second-level exclusion. | |
232 $result = $original; | |
233 unset($result['b']['d']); | |
234 $this->assertEqual(UrlHelper::filterQueryParameters($original, ['b[d]']), $result, "'b[d]' was removed."); | |
235 | |
236 // Third-level exclusion. | |
237 $result = $original; | |
238 unset($result['b']['e']['f']); | |
239 $this->assertEqual(UrlHelper::filterQueryParameters($original, ['b[e][f]']), $result, "'b[e][f]' was removed."); | |
240 | |
241 // Multiple exclusions. | |
242 $result = $original; | |
243 unset($result['a'], $result['b']['e'], $result['c']); | |
244 $this->assertEqual(UrlHelper::filterQueryParameters($original, ['a', 'b[e]', 'c']), $result, "'a', 'b[e]', 'c' were removed."); | |
245 } | |
246 | |
247 /** | |
248 * Tests UrlHelper::parse(). | |
249 */ | |
250 public function testDrupalParseUrl() { | |
251 // Relative, absolute, and external URLs, without/with explicit script path, | |
252 // without/with Drupal path. | |
253 foreach (['', '/', 'https://www.drupal.org/'] as $absolute) { | |
254 foreach (['', 'index.php/'] as $script) { | |
255 foreach (['', 'foo/bar'] as $path) { | |
256 $url = $absolute . $script . $path . '?foo=bar&bar=baz&baz#foo'; | |
257 $expected = [ | |
258 'path' => $absolute . $script . $path, | |
259 'query' => ['foo' => 'bar', 'bar' => 'baz', 'baz' => ''], | |
260 'fragment' => 'foo', | |
261 ]; | |
262 $this->assertEqual(UrlHelper::parse($url), $expected, 'URL parsed correctly.'); | |
263 } | |
264 } | |
265 } | |
266 | |
267 // Relative URL that is known to confuse parse_url(). | |
268 $url = 'foo/bar:1'; | |
269 $result = [ | |
270 'path' => 'foo/bar:1', | |
271 'query' => [], | |
272 'fragment' => '', | |
273 ]; | |
274 $this->assertEqual(UrlHelper::parse($url), $result, 'Relative URL parsed correctly.'); | |
275 | |
276 // Test that drupal can recognize an absolute URL. Used to prevent attack vectors. | |
277 $url = 'https://www.drupal.org/foo/bar?foo=bar&bar=baz&baz#foo'; | |
278 $this->assertTrue(UrlHelper::isExternal($url), 'Correctly identified an external URL.'); | |
279 | |
280 // Test that UrlHelper::parse() does not allow spoofing a URL to force a malicious redirect. | |
281 $parts = UrlHelper::parse('forged:http://cwe.mitre.org/data/definitions/601.html'); | |
282 $this->assertFalse(UrlHelper::isValid($parts['path'], TRUE), '\Drupal\Component\Utility\UrlHelper::isValid() correctly parsed a forged URL.'); | |
283 } | |
284 | |
285 /** | |
286 * Tests external URL handling. | |
287 */ | |
288 public function testExternalUrls() { | |
289 $test_url = 'https://www.drupal.org/'; | |
290 | |
291 // Verify external URL can contain a fragment. | |
292 $url = $test_url . '#drupal'; | |
293 $result = Url::fromUri($url)->toString(); | |
294 $this->assertEqual($url, $result, 'External URL with fragment works without a fragment in $options.'); | |
295 | |
296 // Verify fragment can be overridden in an external URL. | |
297 $url = $test_url . '#drupal'; | |
298 $fragment = $this->randomMachineName(10); | |
299 $result = Url::fromUri($url, ['fragment' => $fragment])->toString(); | |
300 $this->assertEqual($test_url . '#' . $fragment, $result, 'External URL fragment is overridden with a custom fragment in $options.'); | |
301 | |
302 // Verify external URL can contain a query string. | |
303 $url = $test_url . '?drupal=awesome'; | |
304 $result = Url::fromUri($url)->toString(); | |
305 $this->assertEqual($url, $result); | |
306 | |
307 // Verify external URL can be extended with a query string. | |
308 $url = $test_url; | |
309 $query = [$this->randomMachineName(5) => $this->randomMachineName(5)]; | |
310 $result = Url::fromUri($url, ['query' => $query])->toString(); | |
311 $this->assertEqual($url . '?' . http_build_query($query, '', '&'), $result, 'External URL can be extended with a query string in $options.'); | |
312 | |
313 // Verify query string can be extended in an external URL. | |
314 $url = $test_url . '?drupal=awesome'; | |
315 $query = [$this->randomMachineName(5) => $this->randomMachineName(5)]; | |
316 $result = Url::fromUri($url, ['query' => $query])->toString(); | |
317 $this->assertEqual($url . '&' . http_build_query($query, '', '&'), $result); | |
318 } | |
319 | |
320 } |