Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Hyde URL helper to not modify already qualified URLs #1757

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This serves two purposes:
- Fixed DataCollections file finding method not being able to be overridden https://github.com/hydephp/develop/issues/1716 in https://github.com/hydephp/develop/pull/1717
- Fixed PHP warning when trying to parse a Markdown file with just front matter without body https://github.com/hydephp/develop/issues/1705 in https://github.com/hydephp/develop/pull/1728
- Yaml data files no longer need to start with triple dashes to be parsed by DataCollections in https://github.com/hydephp/develop/pull/1733
- Updated the Hyde URL helper to not modify already qualified URLs in https://github.com/hydephp/develop/pull/1757

### Security
- in case of vulnerabilities.
Expand Down
4 changes: 4 additions & 0 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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", '/');
}
Expand Down
18 changes: 17 additions & 1 deletion packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading