Skip to content

Commit c886063

Browse files
committed
Return already qualified URLs as they are
1 parent cb762c5 commit c886063

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/framework/src/Foundation/Kernel/Hyperlinks.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,15 @@ public function hasSiteUrl(): bool
142142
* @param string $path An optional relative path suffix. Omit to return the base URL.
143143
*
144144
* @throws BaseUrlNotSetException If no site URL is set and no path is provided.
145-
*
146-
* TODO: Check if the URL is already qualified and return it as is.
147145
*/
148146
public function url(string $path = ''): string
149147
{
150148
$path = $this->formatLink(trim($path, '/'));
151149

150+
if (str_starts_with($path, 'http')) {
151+
return $path;
152+
}
153+
152154
if ($this->hasSiteUrl()) {
153155
return rtrim(rtrim(Config::getString('hyde.url'), '/')."/$path", '/');
154156
}

packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,51 @@ public function testQualifiedUrlAcceptsMultipleSchemes()
110110
$this->assertSame('http://example.com', $this->class->url());
111111
}
112112

113+
public function testQualifiedUrlHelperWithAlreadyQualifiedUrl()
114+
{
115+
$this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo'));
116+
$this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo'));
117+
}
118+
119+
public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSet()
120+
{
121+
$this->app['config']->set(['hyde.url' => 'https://example.com']);
122+
123+
$this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo'));
124+
$this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo'));
125+
}
126+
127+
public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSetToSomethingElse()
128+
{
129+
$this->app['config']->set(['hyde.url' => 'my-site.com']);
130+
131+
$this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo'));
132+
$this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo'));
133+
}
134+
135+
public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPath()
136+
{
137+
$this->assertSame('https://example.com/foo/bar.html', $this->class->url('https://example.com/foo/bar.html'));
138+
$this->assertSame('http://localhost/foo/bar.html', $this->class->url('http://localhost/foo/bar.html'));
139+
$this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/'));
140+
}
141+
142+
public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWhenSiteUrlIsSet()
143+
{
144+
$this->app['config']->set(['hyde.url' => 'https://example.com']);
145+
$this->assertSame('https://example.com/foo/bar.html', $this->class->url('https://example.com/foo/bar.html'));
146+
$this->assertSame('http://localhost/foo/bar.html', $this->class->url('http://localhost/foo/bar.html'));
147+
$this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/'));
148+
}
149+
150+
public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWithPrettyUrls()
151+
{
152+
$this->app['config']->set(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
153+
$this->assertSame('https://example.com/foo/bar', $this->class->url('https://example.com/foo/bar.html'));
154+
$this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar.html'));
155+
$this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/'));
156+
}
157+
113158
public function testQualifiedUrlThrowsExceptionWhenNoSiteUrlIsSet()
114159
{
115160
$this->withSiteUrl(null);

0 commit comments

Comments
 (0)