diff --git a/.gitignore b/.gitignore index f6c86fa35..f310f9dca 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ Thumbs.db /lock/* /.vscode/settings.json /vendor/**/.git +/.phpunit.result.cache diff --git a/classes/urlhelper.php b/classes/urlhelper.php index 29a9528a8..57046b600 100644 --- a/classes/urlhelper.php +++ b/classes/urlhelper.php @@ -26,6 +26,7 @@ class UrlHelper { /** @var string */ static $fetch_last_modified; + /** @var string */ static $fetch_effective_url; @@ -68,6 +69,8 @@ class UrlHelper { $rel_parts = parse_url($rel_url); + if (!$rel_url) return $base_url; + /** * If parse_url failed to parse $rel_url return false to match the current "invalid thing" behavior * of UrlHelper::validate(). @@ -107,7 +110,7 @@ class UrlHelper { $rel_parts['host'] = $base_parts['host'] ?? ""; $rel_parts['scheme'] = $base_parts['scheme'] ?? ""; - if (isset($rel_parts['path'])) { + if ($rel_parts['path'] ?? false) { // we append dirname() of base path to relative URL path as per RFC 3986 section 5.2.2 $base_path = with_trailing_slash(dirname($base_parts['path'])); diff --git a/tests/UrlHelperTest.php b/tests/UrlHelperTest.php index 2d92c332b..fe4eb5db2 100644 --- a/tests/UrlHelperTest.php +++ b/tests/UrlHelperTest.php @@ -1,24 +1,9 @@ assertEquals( - 'http://example.com/example.html', - UrlHelper::rewrite_relative('http://example.com/example/', '/example.html') - ); - - $this->assertEquals( - 'http://example.com/example/example.html', - UrlHelper::rewrite_relative('http://example.com/example/', 'example.html') - );*/ - + public function test_rewrite_relative(): void { // protocol-neutral URL $this->assertEquals( 'https://example.com/example.html', @@ -50,5 +35,20 @@ final class UrlHelperTest extends TestCase { UrlHelper::rewrite_relative('https://apod.nasa.gov/apod/ap220315.html', './image/2203/Road2Stars_EsoHoralek_1080.jpg') ); + $this->assertEquals( + 'http://example.com/test/url', + UrlHelper::rewrite_relative('http://example.com/test/url', '') + ); + + $this->assertEquals( + 'http://www.example.com/test', + UrlHelper::rewrite_relative('http://www.example2.com ', 'http://www.example.com/test') + ); + + $this->assertEquals( + 'http://www.example.com/test', + UrlHelper::rewrite_relative('http://www.example.com/test2 ', 'http://www.example.com/test') + ); + } } diff --git a/tests/autoload.php b/tests/autoload.php new file mode 100644 index 000000000..1fb33424e --- /dev/null +++ b/tests/autoload.php @@ -0,0 +1,6 @@ +