From 36226182f73ad4c22a70653ae8d0d876c7fe9d2a Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Fri, 22 Dec 2023 12:51:37 +0300 Subject: [PATCH 1/4] Don't throw exception when uuid point to non-existing page #6083 --- config/tags.php | 2 +- tests/Text/KirbyTagsTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/config/tags.php b/config/tags.php index 1f09ae3922..5235b6b527 100644 --- a/config/tags.php +++ b/config/tags.php @@ -197,7 +197,7 @@ Uuid::is($tag->value, 'page') === true || Uuid::is($tag->value, 'file') === true ) { - $tag->value = Uuid::for($tag->value)->model()->url(); + $tag->value = Uuid::for($tag->value)->model()?->url() ?? ''; } return Html::a($tag->value, $tag->text, [ diff --git a/tests/Text/KirbyTagsTest.php b/tests/Text/KirbyTagsTest.php index 49c7d386cc..1c4e3ea936 100644 --- a/tests/Text/KirbyTagsTest.php +++ b/tests/Text/KirbyTagsTest.php @@ -612,8 +612,14 @@ public function testLinkWithUuid() $result = $app->kirbytags('(link: page://page-uuid)'); $this->assertSame('getkirby.com/a', $result); + $result = $app->kirbytags('(link: page://not-exists)'); + $this->assertSame('getkirby.com', $result); + $result = $app->kirbytags('(link: file://file-uuid text: file)'); $this->assertSame('file', $result); + + $result = $app->kirbytags('(link: file://not-exists text: file)'); + $this->assertSame('file', $result); } public function testLinkWithUuidAndLang() From 5ba932ddcfb96288d104051834c1a0a26b7bbd78 Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Tue, 2 Jan 2024 14:25:37 +0300 Subject: [PATCH 2/4] Improve link kirbytag behaviour --- config/tags.php | 16 ++++++++- tests/Text/KirbyTagsTest.php | 67 ++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/config/tags.php b/config/tags.php index 5235b6b527..d2de99edef 100644 --- a/config/tags.php +++ b/config/tags.php @@ -2,6 +2,7 @@ use Kirby\Cms\Html; use Kirby\Cms\Url; +use Kirby\Exception\NotFoundException; use Kirby\Text\KirbyTag; use Kirby\Toolkit\A; use Kirby\Toolkit\Str; @@ -197,7 +198,20 @@ Uuid::is($tag->value, 'page') === true || Uuid::is($tag->value, 'file') === true ) { - $tag->value = Uuid::for($tag->value)->model()?->url() ?? ''; + $tag->value = Uuid::for($tag->value)->model()?->url(); + } + + // if url is empty, throw exception or link to the error page + if ($tag->value === null) { + if ($tag->kirby()->option('debug', false) === true) { + if (empty($tag->text) === false) { + throw new NotFoundException('The linked page is not found for the link text "' . $tag->text . '"'); + } else { + throw new NotFoundException('The linked page is not found'); + } + } else { + $tag->value = $tag->kirby()->site()->errorPageId(); + } } return Html::a($tag->value, $tag->text, [ diff --git a/tests/Text/KirbyTagsTest.php b/tests/Text/KirbyTagsTest.php index 1c4e3ea936..f322d30fb2 100644 --- a/tests/Text/KirbyTagsTest.php +++ b/tests/Text/KirbyTagsTest.php @@ -5,6 +5,7 @@ use Exception; use Kirby\Cms\App; use Kirby\Exception\InvalidArgumentException; +use Kirby\Exception\NotFoundException; use Kirby\Filesystem\Dir; use Kirby\Filesystem\F; use PHPUnit\Framework\TestCase; @@ -613,13 +614,75 @@ public function testLinkWithUuid() $this->assertSame('getkirby.com/a', $result); $result = $app->kirbytags('(link: page://not-exists)'); - $this->assertSame('getkirby.com', $result); + $this->assertSame('getkirby.com/error', $result); $result = $app->kirbytags('(link: file://file-uuid text: file)'); $this->assertSame('file', $result); $result = $app->kirbytags('(link: file://not-exists text: file)'); - $this->assertSame('file', $result); + $this->assertSame('file', $result); + } + + public function testLinkWithUuidDebug() + { + $app = $this->app->clone([ + 'urls' => [ + 'index' => 'https://getkirby.com' + ], + 'site' => [ + 'children' => [ + [ + 'slug' => 'a', + 'content' => ['uuid' => 'page-uuid'], + 'files' => [ + [ + 'filename' => 'foo.jpg', + 'content' => ['uuid' => 'file-uuid'], + ] + ] + ] + ] + ], + 'options' => [ + 'debug' => true + ] + ]); + + $this->expectException(NotFoundException::class); + $this->expectExceptionMessage('The linked page is not found'); + + $app->kirbytags('(link: page://not-exists)'); + } + + public function testLinkWithUuidDebugText() + { + $app = $this->app->clone([ + 'urls' => [ + 'index' => 'https://getkirby.com' + ], + 'site' => [ + 'children' => [ + [ + 'slug' => 'a', + 'content' => ['uuid' => 'page-uuid'], + 'files' => [ + [ + 'filename' => 'foo.jpg', + 'content' => ['uuid' => 'file-uuid'], + ] + ] + ] + ] + ], + 'options' => [ + 'debug' => true + ] + ]); + + $this->expectException(NotFoundException::class); + $this->expectExceptionMessage('The linked page is not found for the link text "click here"'); + + $app->kirbytags('(link: page://not-exists text: click here)'); } public function testLinkWithUuidAndLang() From f67fdc1b059b8b06b58c13dfe4945f56e628e865 Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Tue, 9 Jan 2024 12:46:46 +0300 Subject: [PATCH 3/4] Code reviews --- config/tags.php | 4 ++-- tests/Text/KirbyTagsTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/tags.php b/config/tags.php index d2de99edef..5e340aac71 100644 --- a/config/tags.php +++ b/config/tags.php @@ -205,9 +205,9 @@ if ($tag->value === null) { if ($tag->kirby()->option('debug', false) === true) { if (empty($tag->text) === false) { - throw new NotFoundException('The linked page is not found for the link text "' . $tag->text . '"'); + throw new NotFoundException('The linked page cannot be found for the link text "' . $tag->text . '"'); } else { - throw new NotFoundException('The linked page is not found'); + throw new NotFoundException('The linked page cannot be found'); } } else { $tag->value = $tag->kirby()->site()->errorPageId(); diff --git a/tests/Text/KirbyTagsTest.php b/tests/Text/KirbyTagsTest.php index f322d30fb2..f68abfbdba 100644 --- a/tests/Text/KirbyTagsTest.php +++ b/tests/Text/KirbyTagsTest.php @@ -649,7 +649,7 @@ public function testLinkWithUuidDebug() ]); $this->expectException(NotFoundException::class); - $this->expectExceptionMessage('The linked page is not found'); + $this->expectExceptionMessage('The linked page cannot be found'); $app->kirbytags('(link: page://not-exists)'); } @@ -680,7 +680,7 @@ public function testLinkWithUuidDebugText() ]); $this->expectException(NotFoundException::class); - $this->expectExceptionMessage('The linked page is not found for the link text "click here"'); + $this->expectExceptionMessage('The linked page cannot be found for the link text "click here"'); $app->kirbytags('(link: page://not-exists text: click here)'); } From 5acd88ae408f6fc61f20d60ff7bf3219c51800af Mon Sep 17 00:00:00 2001 From: Ahmet Bora Date: Mon, 15 Jan 2024 12:51:03 +0300 Subject: [PATCH 4/4] Update config/tags.php Co-Authored-By: Bastian Allgeier --- config/tags.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/tags.php b/config/tags.php index 5e340aac71..4e1cabc772 100644 --- a/config/tags.php +++ b/config/tags.php @@ -210,7 +210,7 @@ throw new NotFoundException('The linked page cannot be found'); } } else { - $tag->value = $tag->kirby()->site()->errorPageId(); + $tag->value = Url::to($tag->kirby()->site()->errorPageId()); } }