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

Add language specific UUID URLs #6312

Draft
wants to merge 2 commits into
base: develop-minor
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions src/Cms/LanguageRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Kirby\Http\Router;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
use Kirby\Uuid\Uuid;

/**
* The language router is used internally
Expand Down Expand Up @@ -84,6 +85,27 @@ public function routes(): array
}
}

// Language-specific UUID URLs
$routes[] = [
'pattern' => '@/(page|file)/(:all)',
'method' => 'ALL',
'env' => 'site',
'action' => function (string $languageCode, string $type, string $id) use ($kirby, $language) {
// try to resolve to model, but only from UUID cache;
// this ensures that only existing UUIDs can be queried
// and attackers can't force Kirby to go through the whole
// site index with a non-existing UUID
if ($model = Uuid::for($type . '://' . $id)?->model(true)) {
return $kirby
->response()
->redirect($model->url($language->code()));
}

// render the error page
return false;
}
];

return $routes;
}

Expand Down
39 changes: 33 additions & 6 deletions tests/Cms/Languages/LanguageRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@

use Kirby\Exception\NotFoundException;
use Kirby\TestCase;
use Kirby\Uuid\Uuid;

class LanguageRouterTest extends TestCase
{
protected $app;

public const TMP = KIRBY_TMP_DIR . '/Cms.LanguageRouter';

public function setUp(): void
{
App::destroy();
Dir::make(static::TMP);

$this->app = new App([
'roots' => [
'index' => '/dev/null'
'index' => static::TMP
],
'languages' => [
[
Expand All @@ -25,6 +28,12 @@ public function setUp(): void
]);
}

public function tearDown(): void
{
Dir::remove(static::TMP);
App::destroy();
}

public function testRouteForSingleLanguage()
{
$app = $this->app->clone([
Expand All @@ -50,7 +59,6 @@ public function testRouteForSingleLanguage()
$router = $language->router();
$routes = $router->routes();

$this->assertCount(1, $routes);
$this->assertSame('(:any)', $routes[0]['pattern']);
$this->assertSame('en', $routes[0]['language']);
$this->assertSame('en', $router->call('anything'));
Expand All @@ -71,7 +79,7 @@ public function testRouteWithoutLanguageScope()

$language = $app->language('en');

$this->assertCount(0, $language->router()->routes());
$this->assertCount(1, $language->router()->routes());
}

public function testRouteForMultipleLanguages()
Expand All @@ -92,7 +100,6 @@ public function testRouteForMultipleLanguages()
$router = $language->router();
$routes = $router->routes();

$this->assertCount(1, $routes);
$this->assertSame('(:any)', $routes[0]['pattern']);
$this->assertSame('en|de', $routes[0]['language']);
$this->assertSame('slug', $router->call('slug'));
Expand All @@ -116,7 +123,6 @@ public function testRouteWildcard()
$router = $language->router();
$routes = $router->routes();

$this->assertCount(1, $routes);
$this->assertSame('(:any)', $routes[0]['pattern']);
$this->assertSame('*', $routes[0]['language']);
$this->assertSame('slug', $router->call('slug'));
Expand Down Expand Up @@ -204,4 +210,25 @@ public function testRouteWithPageScopeAndInvalidPage()
$language = $app->language('en');
$router = $language->router()->call('notes/a/slug');
}

public function testUUIDRoute()
{
$app = $this->app->clone([
'site' => [
'children' => [
[
'slug' => 'notes',
]
]
],
]);

$uuid = $app->page('notes')->uuid()->id();

$language = $app->language('en');
$router = $language->router()->call('@/page/' . $uuid);

// TODO: this should normally result in a redirect response
var_dump($router);
}
}
Loading