From: Christian Weiske Date: Wed, 10 Nov 2010 22:09:03 +0000 (+0100) Subject: add unit tests for rewrite_relative_url and fix a number of bugs in it X-Git-Tag: 1.5.0~299 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=f679105cb2adc6de3e41527830fa0bfde36324d1;hp=24eb4c780f531363e90fe2d8248d50189dcb6b38;p=tt-rss.git add unit tests for rewrite_relative_url and fix a number of bugs in it --- diff --git a/functions.php b/functions.php index 96fa1922..a52dafdd 100644 --- a/functions.php +++ b/functions.php @@ -7074,6 +7074,14 @@ return $parts['scheme'] . "://" . $parts['host'] . $parts['path']; } + /** + * Converts a (possibly) relative URL to a absolute one. + * + * @param string $url Base URL (i.e. from where the document is) + * @param string $rel_url Possibly relative URL in the document + * + * @return string Absolute URL + */ function rewrite_relative_url($url, $rel_url) { if (strpos($rel_url, "://") !== false) { return $rel_url; @@ -7086,8 +7094,15 @@ } else { $parts = parse_url($url); - - $parts['path'] = dirname($parts['path']) . "/$rel_url"; + if (!isset($parts['path'])) { + $parts['path'] = '/'; + } + $dir = $parts['path']; + if (substr($dir, -1) !== '/') { + $dir = dirname($parts['path']); + $dir !== '/' && $dir .= '/'; + } + $parts['path'] = $dir . $rel_url; return build_url($parts); } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 163e985d..f1bfa745 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -177,6 +177,69 @@ HTM ); $this->assertFalse(url_is_html($this->tmpFile)); } + + + + /** + * Test rewrite_relative_url() with a relative path + */ + public function testRewriteRelativeUrlRelative() + { + $this->assertEquals( + 'http://tt-rss.org/foo/bar', + rewrite_relative_url('http://tt-rss.org', 'foo/bar') + ); + $this->assertEquals( + 'http://tt-rss.org/foo/bar', + rewrite_relative_url('http://tt-rss.org/', 'foo/bar') + ); + $this->assertEquals( + 'http://tt-rss.org/bar', + rewrite_relative_url('http://tt-rss.org/foo', 'bar') + ); + $this->assertEquals( + 'http://tt-rss.org/foo/bar', + rewrite_relative_url('http://tt-rss.org/foo/', 'bar') + ); + $this->assertEquals( + 'http://tt-rss.org/f/o/bar', + rewrite_relative_url('http://tt-rss.org/f/o/o', 'bar') + ); + $this->assertEquals( + 'http://tt-rss.org/f/o/o/bar', + rewrite_relative_url('http://tt-rss.org/f/o/o/', 'bar') + ); + } + + /** + * Test rewrite_relative_url() with an absolute path + */ + public function testRewriteRelativeUrlAbsolutePath() + { + $this->assertEquals( + 'http://tt-rss.org/bar/', + rewrite_relative_url('http://tt-rss.org/foo/', '/bar/') + ); + $this->assertEquals( + 'http://tt-rss.org/bar/', + rewrite_relative_url('http://tt-rss.org/so/what/is/next', '/bar/') + ); + $this->assertEquals( + 'http://tt-rss.org/bar/', + rewrite_relative_url('http://tt-rss.org/so/what/is/next/', '/bar/') + ); + } + + /** + * Test rewrite_relative_url() with an absolute URL + */ + public function testRewriteRelativeUrlAbsoluteUrl() + { + $this->assertEquals( + 'http://example.org/bar/', + rewrite_relative_url('http://tt-rss.org/foo/', 'http://example.org/bar/') + ); + } } ?> \ No newline at end of file