From 094599125e0acd4f5755e4a6be69d656f3b91ecd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 09:15:07 +0200 Subject: [PATCH 1/5] Remove test skipping for feature to be implemented --- packages/framework/tests/Feature/HelpersTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/tests/Feature/HelpersTest.php b/packages/framework/tests/Feature/HelpersTest.php index 71294f9b20e..8e575c322b6 100644 --- a/packages/framework/tests/Feature/HelpersTest.php +++ b/packages/framework/tests/Feature/HelpersTest.php @@ -197,8 +197,6 @@ 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')); } From fe5e939836ad0cb366d06e3025d91052cc749235 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 09:19:46 +0200 Subject: [PATCH 2/5] Add more tests --- .../framework/tests/Feature/HelpersTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/framework/tests/Feature/HelpersTest.php b/packages/framework/tests/Feature/HelpersTest.php index 8e575c322b6..e0a36b3ceaa 100644 --- a/packages/framework/tests/Feature/HelpersTest.php +++ b/packages/framework/tests/Feature/HelpersTest.php @@ -201,6 +201,24 @@ public function testUrlFunctionWithAlreadyQualifiedUrl() $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')); + } + /** @covers ::\Hyde\hyde */ public function testHydeFunctionExistsInHydeNamespace() { From cb762c5cdb8ce1cb2c3539b55ab6b33815283d37 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 09:19:53 +0200 Subject: [PATCH 3/5] Add todo --- packages/framework/src/Foundation/Kernel/Hyperlinks.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index 9cec5ca989c..a58a5b099e3 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -142,6 +142,8 @@ public function hasSiteUrl(): bool * @param string $path An optional relative path suffix. Omit to return the base URL. * * @throws BaseUrlNotSetException If no site URL is set and no path is provided. + * + * TODO: Check if the URL is already qualified and return it as is. */ public function url(string $path = ''): string { From c886063b7f2416d09dd702c3e30409a64b463911 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 09:24:01 +0200 Subject: [PATCH 4/5] Return already qualified URLs as they are --- .../src/Foundation/Kernel/Hyperlinks.php | 6 ++- .../HyperlinksUrlPathHelpersTest.php | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index a58a5b099e3..b98efdb0d0a 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -142,13 +142,15 @@ public function hasSiteUrl(): bool * @param string $path An optional relative path suffix. Omit to return the base URL. * * @throws BaseUrlNotSetException If no site URL is set and no path is provided. - * - * TODO: Check if the URL is already qualified and return it as is. */ 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/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index 6f4355ef533..d228f3aee3d 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/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); From 4b46ce759374112370e502698613867bee50a9f5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 29 Jun 2024 09:28:14 +0200 Subject: [PATCH 5/5] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 3f11e9a0b9c..c26ed8f3110 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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.