diff --git a/src/Foundation/Kernel/Hyperlinks.php b/src/Foundation/Kernel/Hyperlinks.php index 9cec5ca9..b98efdb0 100644 --- a/src/Foundation/Kernel/Hyperlinks.php +++ b/src/Foundation/Kernel/Hyperlinks.php @@ -147,6 +147,10 @@ public function url(string $path = ''): string { $path = $this->formatLink(trim($path, '/')); + if (str_starts_with($path, 'http')) { + return $path; + } + if ($this->hasSiteUrl()) { return rtrim(rtrim(Config::getString('hyde.url'), '/')."/$path", '/'); } diff --git a/tests/Feature/HelpersTest.php b/tests/Feature/HelpersTest.php index 733a832e..e761b21e 100644 --- a/tests/Feature/HelpersTest.php +++ b/tests/Feature/HelpersTest.php @@ -197,7 +197,23 @@ public function testUrlFunctionWithLocalhostBaseUrlButNoPath() /** @covers ::url */ public function testUrlFunctionWithAlreadyQualifiedUrl() { - $this->markTestSkipped('The url function does not check if the URL is already qualified.'); + $this->assertSame('https://example.com/foo', url('https://example.com/foo')); + $this->assertSame('http://localhost/foo', url('http://localhost/foo')); + } + + /** @covers ::url */ + public function testUrlFunctionWithAlreadyQualifiedUrlWhenSiteUrlIsSet() + { + $this->app['config']->set(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/foo', url('https://example.com/foo')); + $this->assertSame('http://localhost/foo', url('http://localhost/foo')); + } + + /** @covers ::url */ + public function testUrlFunctionWithAlreadyQualifiedUrlWhenSiteUrlIsSetToSomethingElse() + { + $this->app['config']->set(['hyde.url' => 'my-site.com']); $this->assertSame('https://example.com/foo', url('https://example.com/foo')); $this->assertSame('http://localhost/foo', url('http://localhost/foo')); diff --git a/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index 6f4355ef..d228f3ae 100644 --- a/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -110,6 +110,51 @@ public function testQualifiedUrlAcceptsMultipleSchemes() $this->assertSame('http://example.com', $this->class->url()); } + public function testQualifiedUrlHelperWithAlreadyQualifiedUrl() + { + $this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo')); + $this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo')); + } + + public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSet() + { + $this->app['config']->set(['hyde.url' => 'https://example.com']); + + $this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo')); + $this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo')); + } + + public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSetToSomethingElse() + { + $this->app['config']->set(['hyde.url' => 'my-site.com']); + + $this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo')); + $this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo')); + } + + public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPath() + { + $this->assertSame('https://example.com/foo/bar.html', $this->class->url('https://example.com/foo/bar.html')); + $this->assertSame('http://localhost/foo/bar.html', $this->class->url('http://localhost/foo/bar.html')); + $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/')); + } + + public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWhenSiteUrlIsSet() + { + $this->app['config']->set(['hyde.url' => 'https://example.com']); + $this->assertSame('https://example.com/foo/bar.html', $this->class->url('https://example.com/foo/bar.html')); + $this->assertSame('http://localhost/foo/bar.html', $this->class->url('http://localhost/foo/bar.html')); + $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/')); + } + + public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWithPrettyUrls() + { + $this->app['config']->set(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + $this->assertSame('https://example.com/foo/bar', $this->class->url('https://example.com/foo/bar.html')); + $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar.html')); + $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/')); + } + public function testQualifiedUrlThrowsExceptionWhenNoSiteUrlIsSet() { $this->withSiteUrl(null);