3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\UrlHelper;
6 use PHPUnit\Framework\TestCase;
11 * @coversDefaultClass \Drupal\Component\Utility\UrlHelper
13 class UrlHelperTest extends TestCase {
16 * Provides test data for testBuildQuery().
20 public function providerTestBuildQuery() {
22 [['a' => ' &#//+%20@۞'], 'a=%20%26%23//%2B%2520%40%DB%9E', 'Value was properly encoded.'],
23 [[' &#//+%20@۞' => 'a'], '%20%26%23%2F%2F%2B%2520%40%DB%9E=a', 'Key was properly encoded.'],
24 [['a' => '1', 'b' => '2', 'c' => '3'], 'a=1&b=2&c=3', 'Multiple values were properly concatenated.'],
25 [['a' => ['b' => '2', 'c' => '3'], 'd' => 'foo'], 'a%5Bb%5D=2&a%5Bc%5D=3&d=foo', 'Nested array was properly encoded.'],
26 [['foo' => NULL], 'foo', 'Simple parameters are properly added.'],
31 * Tests query building.
33 * @dataProvider providerTestBuildQuery
34 * @covers ::buildQuery
37 * The array of query parameters.
38 * @param string $expected
39 * The expected query string.
40 * @param string $message
41 * The assertion message.
43 public function testBuildQuery($query, $expected, $message) {
44 $this->assertEquals(UrlHelper::buildQuery($query), $expected, $message);
48 * Data provider for testValidAbsolute().
52 public function providerTestValidAbsoluteData() {
58 'example.com/parenthesis',
59 'example.com/index.html#pagetop',
61 'subdomain.example.com',
62 'example.com/index.php/node',
63 'example.com/index.php/node?param=false',
64 'user@www.example.com',
65 'user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop',
68 'john%20doe:secret:foo@example.org/',
69 'example.org/~,$\'*;',
70 'caf%C3%A9.example.org',
71 '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html',
74 return $this->dataEnhanceWithScheme($urls);
78 * Tests valid absolute URLs.
80 * @dataProvider providerTestValidAbsoluteData
85 * @param string $scheme
88 public function testValidAbsolute($url, $scheme) {
89 $test_url = $scheme . '://' . $url;
90 $valid_url = UrlHelper::isValid($test_url, TRUE);
91 $this->assertTrue($valid_url, $test_url . ' is a valid URL.');
95 * Provides data for testInvalidAbsolute().
99 public function providerTestInvalidAbsolute() {
105 return $this->dataEnhanceWithScheme($data);
109 * Tests invalid absolute URLs.
111 * @dataProvider providerTestInvalidAbsolute
116 * @param string $scheme
117 * The scheme to test.
119 public function testInvalidAbsolute($url, $scheme) {
120 $test_url = $scheme . '://' . $url;
121 $valid_url = UrlHelper::isValid($test_url, TRUE);
122 $this->assertFalse($valid_url, $test_url . ' is NOT a valid URL.');
126 * Provides data for testValidRelative().
130 public function providerTestValidRelativeData() {
133 'index.html#pagetop',
135 'index.php/node?param=false',
136 'login.php?do=login&style=%23#pagetop',
139 return $this->dataEnhanceWithPrefix($data);
143 * Tests valid relative URLs.
145 * @dataProvider providerTestValidRelativeData
150 * @param string $prefix
151 * The prefix to test.
153 public function testValidRelative($url, $prefix) {
154 $test_url = $prefix . $url;
155 $valid_url = UrlHelper::isValid($test_url);
156 $this->assertTrue($valid_url, $test_url . ' is a valid URL.');
160 * Provides data for testInvalidRelative().
164 public function providerTestInvalidRelativeData() {
170 return $this->dataEnhanceWithPrefix($data);
174 * Tests invalid relative URLs.
176 * @dataProvider providerTestInvalidRelativeData
181 * @param string $prefix
182 * The prefix to test.
184 public function testInvalidRelative($url, $prefix) {
185 $test_url = $prefix . $url;
186 $valid_url = UrlHelper::isValid($test_url);
187 $this->assertFalse($valid_url, $test_url . ' is NOT a valid URL.');
191 * Tests query filtering.
193 * @dataProvider providerTestFilterQueryParameters
194 * @covers ::filterQueryParameters
196 * @param array $query
197 * The array of query parameters.
198 * @param array $exclude
199 * A list of $query array keys to remove. Use "parent[child]" to exclude
201 * @param array $expected
202 * An array containing query parameters.
204 public function testFilterQueryParameters($query, $exclude, $expected) {
205 $filtered = UrlHelper::filterQueryParameters($query, $exclude);
206 $this->assertEquals($expected, $filtered, 'The query was not properly filtered.');
210 * Provides data to self::testFilterQueryParameters().
214 public static function providerTestFilterQueryParameters() {
216 // Test without an exclude filter.
218 'query' => ['a' => ['b' => 'c']],
220 'expected' => ['a' => ['b' => 'c']],
222 // Exclude the 'b' element.
224 'query' => ['a' => ['b' => 'c', 'd' => 'e']],
225 'exclude' => ['a[b]'],
226 'expected' => ['a' => ['d' => 'e']],
234 * @dataProvider providerTestParse
239 * @param array $expected
240 * Associative array with expected parameters.
242 public function testParse($url, $expected) {
243 $parsed = UrlHelper::parse($url);
244 $this->assertEquals($expected, $parsed, 'The URL was not properly parsed.');
248 * Provides data for self::testParse().
252 public static function providerTestParse() {
255 'http://www.example.com/my/path',
257 'path' => 'http://www.example.com/my/path',
263 'http://www.example.com/my/path?destination=home#footer',
265 'path' => 'http://www.example.com/my/path',
267 'destination' => 'home',
269 'fragment' => 'footer',
272 'absolute fragment, no query' => [
273 'http://www.example.com/my/path#footer',
275 'path' => 'http://www.example.com/my/path',
277 'fragment' => 'footer',
297 '/my/path?destination=home#footer',
299 'path' => '/my/path',
301 'destination' => 'home',
303 'fragment' => 'footer',
306 'relative fragment, no query' => [
309 'path' => '/my/path',
311 'fragment' => 'footer',
318 * Tests path encoding.
320 * @dataProvider providerTestEncodePath
321 * @covers ::encodePath
323 * @param string $path
325 * @param string $expected
326 * The expected encoded path.
328 public function testEncodePath($path, $expected) {
329 $encoded = UrlHelper::encodePath($path);
330 $this->assertEquals($expected, $encoded);
334 * Provides data for self::testEncodePath().
338 public static function providerTestEncodePath() {
340 ['unencoded path with spaces', 'unencoded%20path%20with%20spaces'],
341 ['slashes/should/be/preserved', 'slashes/should/be/preserved'],
346 * Tests external versus internal paths.
348 * @dataProvider providerTestIsExternal
349 * @covers ::isExternal
351 * @param string $path
352 * URL or path to test.
353 * @param bool $expected
356 public function testIsExternal($path, $expected) {
357 $isExternal = UrlHelper::isExternal($path);
358 $this->assertEquals($expected, $isExternal);
362 * Provides data for self::testIsExternal().
366 public static function providerTestIsExternal() {
368 ['/internal/path', FALSE],
369 ['https://example.com/external/path', TRUE],
370 ['javascript://fake-external-path', FALSE],
371 // External URL without an explicit protocol.
372 ['//www.drupal.org/foo/bar?foo=bar&bar=baz&baz#foo', TRUE],
373 // Internal URL starting with a slash.
374 ['/www.drupal.org', FALSE],
375 // Simple external URLs.
376 ['http://example.com', TRUE],
377 ['https://example.com', TRUE],
378 ['http://drupal.org/foo/bar?foo=bar&bar=baz&baz#foo', TRUE],
379 ['//drupal.org', TRUE],
380 // Some browsers ignore or strip leading control characters.
381 ["\x00//www.example.com", TRUE],
382 ["\x08//www.example.com", TRUE],
383 ["\x1F//www.example.com", TRUE],
384 ["\n//www.example.com", TRUE],
385 // JSON supports decoding directly from UTF-8 code points.
386 [json_decode('"\u00AD"') . "//www.example.com", TRUE],
387 [json_decode('"\u200E"') . "//www.example.com", TRUE],
388 [json_decode('"\uE0020"') . "//www.example.com", TRUE],
389 [json_decode('"\uE000"') . "//www.example.com", TRUE],
390 // Backslashes should be normalized to forward.
391 ['\\\\example.com', TRUE],
394 ['/system/ajax', FALSE],
395 ['?q=foo:bar', FALSE],
396 ['node/edit:me', FALSE],
397 ['/drupal.org', FALSE],
403 * Tests bad protocol filtering and escaping.
405 * @dataProvider providerTestFilterBadProtocol
406 * @covers ::setAllowedProtocols
407 * @covers ::filterBadProtocol
411 * @param string $expected
412 * Expected escaped value.
413 * @param array $protocols
414 * Protocols to allow.
416 public function testFilterBadProtocol($uri, $expected, $protocols) {
417 UrlHelper::setAllowedProtocols($protocols);
418 $this->assertEquals($expected, UrlHelper::filterBadProtocol($uri));
419 // Multiple calls to UrlHelper::filterBadProtocol() do not cause double
421 $this->assertEquals($expected, UrlHelper::filterBadProtocol(UrlHelper::filterBadProtocol($uri)));
425 * Provides data for self::testTestFilterBadProtocol().
429 public static function providerTestFilterBadProtocol() {
431 ['javascript://example.com?foo&bar', '//example.com?foo&bar', ['http', 'https']],
432 // Test custom protocols.
433 ['http://example.com?foo&bar', '//example.com?foo&bar', ['https']],
435 ['http://example.com?foo&bar', 'http://example.com?foo&bar', ['https', 'http']],
436 // Colon not part of the URL scheme.
437 ['/test:8888?foo&bar', '/test:8888?foo&bar', ['http']],
442 * Tests dangerous url protocol filtering.
444 * @dataProvider providerTestStripDangerousProtocols
445 * @covers ::setAllowedProtocols
446 * @covers ::stripDangerousProtocols
450 * @param string $expected
451 * Expected escaped value.
452 * @param array $protocols
453 * Protocols to allow.
455 public function testStripDangerousProtocols($uri, $expected, $protocols) {
456 UrlHelper::setAllowedProtocols($protocols);
457 $stripped = UrlHelper::stripDangerousProtocols($uri);
458 $this->assertEquals($expected, $stripped);
462 * Provides data for self::testStripDangerousProtocols().
466 public static function providerTestStripDangerousProtocols() {
468 ['javascript://example.com', '//example.com', ['http', 'https']],
469 // Test custom protocols.
470 ['http://example.com', '//example.com', ['https']],
472 ['http://example.com', 'http://example.com', ['https', 'http']],
473 // Colon not part of the URL scheme.
474 ['/test:8888', '/test:8888', ['http']],
479 * Enhances test urls with schemes
485 * A list of provider data with schemes.
487 protected function dataEnhanceWithScheme(array $urls) {
488 $url_schemes = ['http', 'https', 'ftp'];
490 foreach ($url_schemes as $scheme) {
491 foreach ($urls as $url) {
492 $data[] = [$url, $scheme];
499 * Enhances test urls with prefixes.
505 * A list of provider data with prefixes.
507 protected function dataEnhanceWithPrefix(array $urls) {
508 $prefixes = ['', '/'];
510 foreach ($prefixes as $prefix) {
511 foreach ($urls as $url) {
512 $data[] = [$url, $prefix];
519 * Test detecting external urls that point to local resources.
522 * The external url to test.
523 * @param string $base_url
525 * @param bool $expected
526 * TRUE if an external URL points to this installation as determined by the
529 * @covers ::externalIsLocal
530 * @dataProvider providerTestExternalIsLocal
532 public function testExternalIsLocal($url, $base_url, $expected) {
533 $this->assertSame($expected, UrlHelper::externalIsLocal($url, $base_url));
537 * Provider for local external url detection.
539 * @see \Drupal\Tests\Component\Utility\UrlHelperTest::testExternalIsLocal()
541 public function providerTestExternalIsLocal() {
543 // Different mixes of trailing slash.
544 ['http://example.com', 'http://example.com', TRUE],
545 ['http://example.com/', 'http://example.com', TRUE],
546 ['http://example.com', 'http://example.com/', TRUE],
547 ['http://example.com/', 'http://example.com/', TRUE],
548 // Sub directory of site.
549 ['http://example.com/foo', 'http://example.com/', TRUE],
550 ['http://example.com/foo/bar', 'http://example.com/foo', TRUE],
551 ['http://example.com/foo/bar', 'http://example.com/foo/', TRUE],
552 // Different sub-domain.
553 ['http://example.com', 'http://www.example.com/', FALSE],
554 ['http://example.com/', 'http://www.example.com/', FALSE],
555 ['http://example.com/foo', 'http://www.example.com/', FALSE],
557 ['http://example.com', 'http://example.ca', FALSE],
558 ['http://example.com', 'http://example.ca/', FALSE],
559 ['http://example.com/', 'http://example.ca/', FALSE],
560 ['http://example.com/foo', 'http://example.ca', FALSE],
561 ['http://example.com/foo', 'http://example.ca/', FALSE],
562 // Different site path.
563 ['http://example.com/foo', 'http://example.com/bar', FALSE],
564 ['http://example.com', 'http://example.com/bar', FALSE],
565 ['http://example.com/bar', 'http://example.com/bar/', FALSE],
566 // Ensure \ is normalised to / since some browsers do that.
567 ['http://www.example.ca\@example.com', 'http://example.com', FALSE],
568 // Some browsers ignore or strip leading control characters.
569 ["\x00//www.example.ca", 'http://example.com', FALSE],
574 * Test invalid url arguments.
578 * @param string $base_url
581 * @covers ::externalIsLocal
582 * @dataProvider providerTestExternalIsLocalInvalid
584 public function testExternalIsLocalInvalid($url, $base_url) {
585 if (method_exists($this, 'expectException')) {
586 $this->expectException(\InvalidArgumentException::class);
589 $this->setExpectedException(\InvalidArgumentException::class);
591 UrlHelper::externalIsLocal($url, $base_url);
595 * Provides invalid argument data for local external url detection.
597 * @see \Drupal\Tests\Component\Utility\UrlHelperTest::testExternalIsLocalInvalid()
599 public function providerTestExternalIsLocalInvalid() {
601 ['http://example.com/foo', ''],
602 ['http://example.com/foo', 'bar'],
603 ['http://example.com/foo', 'http://'],
604 // Invalid destination urls.
605 ['', 'http://example.com/foo'],
606 ['bar', 'http://example.com/foo'],
607 ['/bar', 'http://example.com/foo'],
608 ['bar/', 'http://example.com/foo'],
609 ['http://', 'http://example.com/foo'],