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

Improve link kirbytag behavior when uuid point to non-existing page #6085

Merged
merged 4 commits into from
Jan 15, 2024
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
16 changes: 15 additions & 1 deletion config/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 cannot be found for the link text "' . $tag->text . '"');
} else {
throw new NotFoundException('The linked page cannot be found');
}
} else {
$tag->value = Url::to($tag->kirby()->site()->errorPageId());
}
}

return Html::a($tag->value, $tag->text, [
Expand Down
69 changes: 69 additions & 0 deletions tests/Text/KirbyTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -612,8 +613,76 @@ public function testLinkWithUuid()
$result = $app->kirbytags('(link: page://page-uuid)');
$this->assertSame('<a href="https://getkirby.com/a">getkirby.com/a</a>', $result);

$result = $app->kirbytags('(link: page://not-exists)');
$this->assertSame('<a href="https://getkirby.com/error">getkirby.com/error</a>', $result);

$result = $app->kirbytags('(link: file://file-uuid text: file)');
$this->assertSame('<a href="' . $app->file('a/foo.jpg')->url() . '">file</a>', $result);

$result = $app->kirbytags('(link: file://not-exists text: file)');
$this->assertSame('<a href="https://getkirby.com/error">file</a>', $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 cannot be 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 cannot be found for the link text "click here"');

$app->kirbytags('(link: page://not-exists text: click here)');
}

public function testLinkWithUuidAndLang()
Expand Down