feat(ext-blog): Add timezone support for blog publication dates and search

Configure `general.timezone` in root config to control blog publication input and display. Update PostsRepository to validate dates against the site timezone and enforce a minimum search length for public queries. Modify admin views to display publication dates in the site's local timezone and update search labels. Add tests for the new timezone handling and search validation logic.
This commit is contained in:
2026-12-02 12:08:45 +01:00
parent 8ebed89e73
commit 58e05e88ae
8 changed files with 56 additions and 37 deletions
@@ -141,6 +141,9 @@ function renderBlogEditor(int $postId): string
}
$_ENV['DATABASE_PREFIX'] = 'test_';
$siteTimezone = 'Pacific/Kiritimati';
date_default_timezone_set('Pacific/Pago_Pago');
$phpb_config = ['website_manager' => ['url' => '/website-manager'], 'general' => ['uploads_url' => '/uploads', 'timezone' => $siteTimezone]];
$phpb_db = new BlogTestDatabase();
$phpb_db->createSchema();
assertBlog(isset($phpb_db->getDBDefinition()['ext__blog_posts'], $phpb_db->getDBDefinition()['ext__blog_categories']), 'The blog tables should be included in the core database definition.');
@@ -183,6 +186,8 @@ $post = $posts->create([
]);
assertBlog(is_array($post) && $post['status'] === 'published', 'A valid published post should be created.');
assertBlog($post['published_at'] !== null, 'Publishing without a date should set the current publication timestamp.');
$storedPublication = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', $post['published_at'], new \DateTimeZone($siteTimezone));
assertBlog($storedPublication !== false && abs($storedPublication->getTimestamp() - time()) < 60, 'The automatic publication timestamp should use the configured site timezone.');
assertBlog($post['featured_image_id'] === 'blog-cover-1', 'A valid image upload reference should be persisted on the post.');
expectBlogValidation(fn() => $posts->create([
'title' => 'Missing featured image', 'slug' => 'missing-featured-image', 'excerpt' => '', 'body' => 'Body', 'featured_image_id' => 'missing-upload', 'category_id' => '', 'status' => 'draft', 'published_at' => '',
@@ -200,7 +205,7 @@ $olderPost = $posts->create([
'status' => 'published',
'published_at' => '2020-01-01T00:00',
]);
$pastPublication = date('Y-m-d\\TH:i', time() - 3600);
$pastPublication = (new \DateTimeImmutable('now', new \DateTimeZone($siteTimezone)))->modify('-1 hour')->format('Y-m-d\\TH:i');
$datedPost = $posts->create([
'title' => 'Dated draft',
'slug' => 'dated-draft',
@@ -220,13 +225,9 @@ $secondPost = $posts->create([
'published_at' => '',
]);
$_ENV['BASE_URL'] = 'https://blog.example';
$phpb_config = ['website_manager' => ['url' => '/website-manager'], 'general' => ['uploads_url' => '/uploads']];
$publishedPosts = $posts->getPublishedForListing();
assertBlog(count($publishedPosts) === 2, 'The public listing should exclude drafts.');
assertBlog($publishedPosts[0]['slug'] === $post['slug'] && $publishedPosts[0]['category_name'] === 'News', 'Published posts should be newest first and include their category name.');
assertBlog($publishedPosts[1]['slug'] === $olderPost['slug'], 'Older published posts should follow newer posts.');
$editorHtml = renderBlogEditor((int) $post['id']);
assertBlog(str_contains($editorHtml, 'site timezone (Pacific/Kiritimati)'), 'The editor should identify the timezone used for publication dates.');
assertBlog(str_contains($editorHtml, '<textarea id="post-body" name="body"') && str_contains($editorHtml, 'Post body'), 'The visible fallback textarea should retain and submit the stored body value.');
assertBlog(str_contains($editorHtml, 'id="post-body-toolbar"') && str_contains($editorHtml, 'data-format="bold"') && str_contains($editorHtml, 'data-format="heading"'), 'The editor should provide accessible Markdown formatting controls.');
assertBlog(str_contains($editorHtml, 'data-format="list"') && str_contains($editorHtml, 'data-format="ordered-list"') && str_contains($editorHtml, 'data-format="quote"') && str_contains($editorHtml, 'data-format="horizontal-rule"') && str_contains($editorHtml, 'data-format="code-block"'), 'The editor toolbar should offer unordered and ordered lists, quotes, horizontal rules, and code blocks.');
@@ -240,10 +241,12 @@ assertBlog(str_contains($editorHtml, 'selectionStart') && str_contains($editorHt
assertBlog(str_contains($editorHtml, 'window.blogMarkdownEditor.insertAtCursor(markdown)'), 'The inline picker should insert at the CodeMirror cursor when the editor is initialized.');
$listingHtml = renderBlogListing();
assertBlog(str_contains($listingHtml, 'href="https://blog.example/blog/first-post"'), 'The listing should link to the configured post detail URL.');
assertBlog(str_contains($listingHtml, 'datetime="2020-01-01T00:00:00+14:00"'), 'Public listing dates should use the configured site timezone.');
assertBlog(str_contains($listingHtml, 'src="https://blog.example/uploads/blog-cover-1/cover.jpg" alt="A &quot;featured&quot; &amp; &lt;cover&gt;"'), 'The public listing should display a featured image with escaped alt text.');
assertBlog(strpos($listingHtml, 'first-post') < strpos($listingHtml, 'older-published-post'), 'The rendered listing should preserve newest-first order.');
assertBlog(!str_contains($listingHtml, 'dated-draft') && !str_contains($listingHtml, 'second-post'), 'Draft posts should not appear in the rendered listing.');
$detailHtml = renderBlogDetail('older-published-post');
assertBlog(str_contains($detailHtml, 'datetime="2020-01-01T00:00:00+14:00"'), 'Public detail dates should use the configured site timezone.');
assertBlog(str_contains($detailHtml, '<h1 class="text-3xl font-bold tracking-tight text-base-content sm:text-4xl">Older published post</h1>'), 'The detail block should resolve a published post from the named slug route parameter.');
assertBlog(str_contains($detailHtml, '<h2 class="text-2xl font-bold tracking-tight">Markdown heading</h2>') && str_contains($detailHtml, '<strong>bold</strong>') && str_contains($detailHtml, '<em>emphasized</em>'), 'The detail block should render Markdown headings and emphasis.');
assertBlog(str_contains($detailHtml, '<a class="link link-hover" href="https://example.com">safe link</a>') && !str_contains($detailHtml, 'href="javascript:') && !str_contains($detailHtml, 'href="java'), 'The detail block should render safe links and reject unsafe protocols.');
@@ -283,7 +286,7 @@ expectBlogValidation(fn() => $posts->create([
'title' => 'Invalid date', 'slug' => 'invalid-date', 'excerpt' => '', 'body' => '', 'category_id' => '', 'status' => 'draft', 'published_at' => 'not-a-date',
]), 'Invalid publication dates should be rejected.');
expectBlogValidation(fn() => $posts->create([
'title' => 'Future date', 'slug' => 'future-date', 'excerpt' => '', 'body' => '', 'category_id' => '', 'status' => 'draft', 'published_at' => date('Y-m-d\\TH:i', time() + 86400),
'title' => 'Future date', 'slug' => 'future-date', 'excerpt' => '', 'body' => '', 'category_id' => '', 'status' => 'draft', 'published_at' => (new \DateTimeImmutable('now', new \DateTimeZone($siteTimezone)))->modify('+1 day')->format('Y-m-d\\TH:i'),
]), 'Future publication dates should be rejected.');
$updated = $posts->update(['id' => $post['id']], [
@@ -331,7 +334,7 @@ assertBlog($posts->destroy($olderPost['id']) === true, 'An older published post
assertBlog($posts->destroy($post['id']) === true, 'The remaining draft should be deleted.');
assertBlog($categories->findWithId($category['id']) === null && $posts->findWithId($secondPost['id']) === null, 'Deleted records should no longer be found.');
assertBlog(str_contains(renderBlogListing(), 'No published posts yet.'), 'The listing should show an empty state when no posts are published.');
$phpb_config = ['website_manager' => ['url' => '/website-manager']];
$phpb_config = ['website_manager' => ['url' => '/website-manager'], 'general' => ['uploads_url' => '/uploads', 'timezone' => $siteTimezone]];
$_GET = [];
assertBlog(str_contains(renderBlogAdminList(), 'No posts yet'), 'The admin list should show its empty-list state when no posts exist.');
@@ -370,8 +373,13 @@ foreach ([
$adminMatch = $posts->getPageForAdmin($term, 1);
$publicMatch = $posts->getPageForListing($term, 1);
assertBlog($adminMatch['total'] === 1 && $adminMatch['posts'][0]['slug'] === $fieldPost['slug'], 'Admin search should match the ' . $field . ' field.');
assertBlog($publicMatch['total'] === 1 && $publicMatch['posts'][0]['slug'] === $fieldPost['slug'], 'Public search should match the ' . $field . ' field.');
assertBlog($field === 'body' ? $publicMatch['total'] === 0 : $publicMatch['total'] === 1 && $publicMatch['posts'][0]['slug'] === $fieldPost['slug'], 'Public search should match only title, slug, and excerpt fields.');
}
$shortPublicSearch = $posts->getPageForListing('po', 1);
assertBlog($shortPublicSearch['total'] === 0 && $shortPublicSearch['posts'] === [], 'Public search should ignore queries shorter than three characters.');
$_GET = ['q' => 'po'];
assertBlog(str_contains(renderBlogListing(), 'Enter at least 3 characters to search.'), 'The public listing should explain the minimum search length.');
$_GET = [];
foreach ([
['term' => '100%', 'match' => 'literal-percent', 'title' => 'Price 100% complete', 'decoy' => 'wildcard-percent-decoy', 'decoyTitle' => 'Price 100X complete'],