Compare commits
8
Commits
58e05e88ae
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f01cad090c | ||
|
|
6f0e39b5f5 | ||
|
|
745f33f17d | ||
|
|
2d4b196b04 | ||
|
|
e97d96c1f5 | ||
|
|
32582e9279 | ||
|
|
f35645132e | ||
|
|
47214e0bbc |
@@ -11,7 +11,6 @@ return [
|
|||||||
*/
|
*/
|
||||||
"general" => [
|
"general" => [
|
||||||
"language" => "en",
|
"language" => "en",
|
||||||
"timezone" => "UTC",
|
|
||||||
"assets_url" => "/plugi-core/dist",
|
"assets_url" => "/plugi-core/dist",
|
||||||
"uploads_url" => "/uploads",
|
"uploads_url" => "/uploads",
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -8,11 +8,9 @@ The blog extension provides Website Manager post and category management plus tw
|
|||||||
2. Open Website Manager and apply the pending database definition update. Existing installations need this update to create or update the extension-owned post and category tables, including the optional featured-image field.
|
2. Open Website Manager and apply the pending database definition update. Existing installations need this update to create or update the extension-owned post and category tables, including the optional featured-image field.
|
||||||
3. Add categories if useful, then create posts as drafts or publish them from the Blog section.
|
3. Add categories if useful, then create posts as drafts or publish them from the Blog section.
|
||||||
|
|
||||||
Set `general.timezone` in the root `config.php` to the site's IANA timezone (for example, `Europe/Berlin`); it defaults to `UTC` and controls blog publication input and display. Publication dates are stored as site-local `DATETIME` values, so existing installations should configure the timezone that matches their existing post dates.
|
|
||||||
|
|
||||||
## Create the public pages
|
## Create the public pages
|
||||||
|
|
||||||
1. Create a regular Plugi page with the route `/blog` and add the **Published Posts** block. It lists published posts newest first; drafts are never shown. Public search matches titles, slugs, and excerpts (at least three characters), not full post bodies.
|
1. Create a regular Plugi page with the route `/blog` and add the **Published Posts** block. It lists published posts newest first; drafts are never shown.
|
||||||
2. Create another regular page with the route `/blog/{slug}` and add the **Blog Post** block. The core router supplies the named `slug` parameter to the block.
|
2. Create another regular page with the route `/blog/{slug}` and add the **Blog Post** block. The core router supplies the named `slug` parameter to the block.
|
||||||
3. Apply the site’s normal page layout and theme to both pages. Their presentation remains controlled by the page builder and theme.
|
3. Apply the site’s normal page layout and theme to both pages. Their presentation remains controlled by the page builder and theme.
|
||||||
|
|
||||||
|
|||||||
@@ -3,14 +3,12 @@
|
|||||||
namespace Plugi\Extensions\Blog;
|
namespace Plugi\Extensions\Blog;
|
||||||
|
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use DateTimeZone;
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Plugi\Repositories\BaseRepository;
|
use Plugi\Repositories\BaseRepository;
|
||||||
|
|
||||||
class PostsRepository extends BaseRepository
|
class PostsRepository extends BaseRepository
|
||||||
{
|
{
|
||||||
public const POSTS_PER_PAGE = 10;
|
public const POSTS_PER_PAGE = 10;
|
||||||
public const MIN_PUBLIC_SEARCH_LENGTH = 3;
|
|
||||||
|
|
||||||
protected $table = 'ext__blog_posts';
|
protected $table = 'ext__blog_posts';
|
||||||
|
|
||||||
@@ -34,6 +32,17 @@ class PostsRepository extends BaseRepository
|
|||||||
return $this->db->select("SELECT * FROM {$this->table} ORDER BY updated_at DESC, id DESC", []);
|
return $this->db->select("SELECT * FROM {$this->table} ORDER BY updated_at DESC, id DESC", []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPublishedForListing(): array
|
||||||
|
{
|
||||||
|
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
|
||||||
|
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
|
||||||
|
$posts = $this->db->select(
|
||||||
|
"SELECT posts.*, categories.name AS category_name, featured_image.public_id AS featured_image_public_id, featured_image.original_file AS featured_image_original_file, featured_image.alt_text AS featured_image_alt_text FROM {$this->table} posts LEFT JOIN {$categoriesTable} categories ON categories.id = posts.category_id LEFT JOIN {$uploadsTable} featured_image ON featured_image.public_id = posts.featured_image_id AND LOWER(featured_image.mime_type) LIKE 'image/%' WHERE posts.status = ? ORDER BY posts.published_at DESC, posts.id DESC",
|
||||||
|
['published']
|
||||||
|
);
|
||||||
|
return array_map(fn(array $post): array => $this->resolveFeaturedImage($post), $posts);
|
||||||
|
}
|
||||||
|
|
||||||
public function getPageForAdmin(string $search, int $page): array
|
public function getPageForAdmin(string $search, int $page): array
|
||||||
{
|
{
|
||||||
return $this->getPostPage($search, $page, false);
|
return $this->getPostPage($search, $page, false);
|
||||||
@@ -46,11 +55,6 @@ class PostsRepository extends BaseRepository
|
|||||||
|
|
||||||
private function getPostPage(string $search, int $page, bool $publishedOnly): array
|
private function getPostPage(string $search, int $page, bool $publishedOnly): array
|
||||||
{
|
{
|
||||||
$search = trim($search);
|
|
||||||
if ($publishedOnly && $search !== '' && mb_strlen($search) < self::MIN_PUBLIC_SEARCH_LENGTH) {
|
|
||||||
return ['posts' => [], 'total' => 0];
|
|
||||||
}
|
|
||||||
|
|
||||||
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
|
$categoriesTable = $_ENV['DATABASE_PREFIX'] . 'ext__blog_categories';
|
||||||
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
|
$uploadsTable = $_ENV['DATABASE_PREFIX'] . 'uploads';
|
||||||
$from = $publishedOnly
|
$from = $publishedOnly
|
||||||
@@ -64,14 +68,9 @@ class PostsRepository extends BaseRepository
|
|||||||
$parameters[] = 'published';
|
$parameters[] = 'published';
|
||||||
}
|
}
|
||||||
if (trim($search) !== '') {
|
if (trim($search) !== '') {
|
||||||
$filters[] = $publishedOnly
|
$filters[] = '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\' OR posts.body LIKE ? ESCAPE \'!\')';
|
||||||
? '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\')'
|
|
||||||
: '(posts.title LIKE ? ESCAPE \'!\' OR posts.slug LIKE ? ESCAPE \'!\' OR posts.excerpt LIKE ? ESCAPE \'!\' OR posts.body LIKE ? ESCAPE \'!\')';
|
|
||||||
$pattern = '%' . str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($search)) . '%';
|
$pattern = '%' . str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($search)) . '%';
|
||||||
array_push($parameters, $pattern, $pattern, $pattern);
|
array_push($parameters, $pattern, $pattern, $pattern, $pattern);
|
||||||
if (!$publishedOnly) {
|
|
||||||
$parameters[] = $pattern;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$where = $filters === [] ? '' : ' WHERE ' . implode(' AND ', $filters);
|
$where = $filters === [] ? '' : ' WHERE ' . implode(' AND ', $filters);
|
||||||
@@ -195,9 +194,8 @@ class PostsRepository extends BaseRepository
|
|||||||
|
|
||||||
private function validatedPublicationDate(mixed $value, string $status): ?string
|
private function validatedPublicationDate(mixed $value, string $status): ?string
|
||||||
{
|
{
|
||||||
$timezone = new DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC'));
|
|
||||||
if ($value === null || $value === '') {
|
if ($value === null || $value === '') {
|
||||||
return $status === 'published' ? (new DateTimeImmutable('now', $timezone))->format('Y-m-d H:i:s') : null;
|
return $status === 'published' ? date('Y-m-d H:i:s') : null;
|
||||||
}
|
}
|
||||||
if (!is_string($value)) {
|
if (!is_string($value)) {
|
||||||
throw new InvalidArgumentException('Enter a valid publication date.');
|
throw new InvalidArgumentException('Enter a valid publication date.');
|
||||||
@@ -205,12 +203,12 @@ class PostsRepository extends BaseRepository
|
|||||||
|
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
$format = str_contains($value, 'T') ? 'Y-m-d\\TH:i' : 'Y-m-d H:i:s';
|
$format = str_contains($value, 'T') ? 'Y-m-d\\TH:i' : 'Y-m-d H:i:s';
|
||||||
$date = DateTimeImmutable::createFromFormat('!' . $format, $value, $timezone);
|
$date = DateTimeImmutable::createFromFormat('!' . $format, $value);
|
||||||
$errors = DateTimeImmutable::getLastErrors();
|
$errors = DateTimeImmutable::getLastErrors();
|
||||||
if ($date === false || ($errors !== false && ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) || $date->format($format) !== $value) {
|
if ($date === false || ($errors !== false && ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) || $date->format($format) !== $value) {
|
||||||
throw new InvalidArgumentException('Enter a valid publication date.');
|
throw new InvalidArgumentException('Enter a valid publication date.');
|
||||||
}
|
}
|
||||||
if ($date > new DateTimeImmutable('now', $timezone)) {
|
if ($date > new DateTimeImmutable()) {
|
||||||
throw new InvalidArgumentException('Publication dates cannot be in the future.');
|
throw new InvalidArgumentException('Publication dates cannot be in the future.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use Plugi\Extensions\Blog\PostsRepository;
|
|||||||
use Plugi\Repositories\UploadRepository;
|
use Plugi\Repositories\UploadRepository;
|
||||||
|
|
||||||
$postsRepository = new PostsRepository();
|
$postsRepository = new PostsRepository();
|
||||||
$timezone = new \DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC'));
|
|
||||||
$post = [
|
$post = [
|
||||||
'title' => '',
|
'title' => '',
|
||||||
'slug' => '',
|
'slug' => '',
|
||||||
@@ -20,7 +19,7 @@ if (isset($_GET['id']) && ctype_digit((string) $_GET['id'])) {
|
|||||||
$post = $postsRepository->findWithId($_GET['id']) ?? $post;
|
$post = $postsRepository->findWithId($_GET['id']) ?? $post;
|
||||||
}
|
}
|
||||||
$isEditing = isset($post['id']);
|
$isEditing = isset($post['id']);
|
||||||
$publishedAt = $post['published_at'] ? (new \DateTimeImmutable($post['published_at'], $timezone))->format('Y-m-d\TH:i') : '';
|
$publishedAt = $post['published_at'] ? date('Y-m-d\TH:i', strtotime($post['published_at'])) : '';
|
||||||
$categories = (new CategoriesRepository())->getAllForAdmin();
|
$categories = (new CategoriesRepository())->getAllForAdmin();
|
||||||
$imageUploads = array_values(array_filter((new UploadRepository())->getAll(), static fn($file): bool => is_string($file->mime_type ?? null) && str_starts_with(strtolower($file->mime_type), 'image/')));
|
$imageUploads = array_values(array_filter((new UploadRepository())->getAll(), static fn($file): bool => is_string($file->mime_type ?? null) && str_starts_with(strtolower($file->mime_type), 'image/')));
|
||||||
?>
|
?>
|
||||||
@@ -148,7 +147,7 @@ $imageUploads = array_values(array_filter((new UploadRepository())->getAll(), st
|
|||||||
<div>
|
<div>
|
||||||
<label for="post-published-at" class="label"><span class="label-text font-medium">Publication date</span></label>
|
<label for="post-published-at" class="label"><span class="label-text font-medium">Publication date</span></label>
|
||||||
<input id="post-published-at" type="datetime-local" name="published_at" value="<?= phpb_e($publishedAt) ?>" class="input w-full" aria-describedby="post-published-at-help">
|
<input id="post-published-at" type="datetime-local" name="published_at" value="<?= phpb_e($publishedAt) ?>" class="input w-full" aria-describedby="post-published-at-help">
|
||||||
<p id="post-published-at-help" class="mt-1 text-sm text-base-content/60">Times use the site timezone (<?= phpb_e($timezone->getName()) ?>). Leave blank to use the current time when publishing. Future dates are not supported.</p>
|
<p id="post-published-at-help" class="mt-1 text-sm text-base-content/60">Leave blank to use the current time when publishing. Future dates are not supported.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ foreach ($categories as $category) {
|
|||||||
<th>Title</th>
|
<th>Title</th>
|
||||||
<th>Category</th>
|
<th>Category</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
<th>Published (site time)</th>
|
<th>Published</th>
|
||||||
<th class="w-40 text-right">Actions</th>
|
<th class="w-40 text-right">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$slug = phpb_route_parameter('slug');
|
$slug = phpb_route_parameter('slug');
|
||||||
$timezone = new \DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC'));
|
|
||||||
$post = is_string($slug) && $slug !== ''
|
$post = is_string($slug) && $slug !== ''
|
||||||
? (new \Plugi\Extensions\Blog\PostsRepository())->findPublishedBySlug($slug)
|
? (new \Plugi\Extensions\Blog\PostsRepository())->findPublishedBySlug($slug)
|
||||||
: null;
|
: null;
|
||||||
@@ -21,9 +20,9 @@ $post = is_string($slug) && $slug !== ''
|
|||||||
<span class="badge badge-outline"><?= phpb_e($post['category_name']) ?></span>
|
<span class="badge badge-outline"><?= phpb_e($post['category_name']) ?></span>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php if (!empty($post['published_at'])): ?>
|
<?php if (!empty($post['published_at'])): ?>
|
||||||
<?php $publishedDate = date_create($post['published_at'], $timezone); ?>
|
<?php $publishedTimestamp = strtotime($post['published_at']); ?>
|
||||||
<?php if ($publishedDate !== false): ?>
|
<?php if ($publishedTimestamp !== false): ?>
|
||||||
<time datetime="<?= phpb_e($publishedDate->format('c')) ?>"><?= phpb_e($publishedDate->format('F j, Y')) ?></time>
|
<time datetime="<?= phpb_e(date('c', $publishedTimestamp)) ?>"><?= phpb_e(date('F j, Y', $publishedTimestamp)) ?></time>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
|
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
|
||||||
$timezone = new \DateTimeZone((string) (phpb_config('general.timezone') ?: 'UTC'));
|
|
||||||
$search = is_string($_GET['q'] ?? null) ? trim($_GET['q']) : '';
|
$search = is_string($_GET['q'] ?? null) ? trim($_GET['q']) : '';
|
||||||
$pageValue = $_GET['page'] ?? null;
|
$pageValue = $_GET['page'] ?? null;
|
||||||
$page = is_string($pageValue) ? filter_var($pageValue, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
|
$page = is_string($pageValue) ? filter_var($pageValue, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) : false;
|
||||||
$page = $page === false ? 1 : $page;
|
$page = $page === false ? 1 : $page;
|
||||||
$listingPath = isset($_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']) ? phpb_full_url(phpb_current_relative_url(true)) : '/';
|
$listingPath = isset($_SERVER['SERVER_NAME'], $_SERVER['REQUEST_URI']) ? phpb_full_url(phpb_current_relative_url(true)) : '/';
|
||||||
$pageResult = $postsRepository->getPageForListing($search, $page);
|
$pageResult = $postsRepository->getPageForListing($search, $page);
|
||||||
$searchTooShort = $search !== '' && mb_strlen($search) < \Plugi\Extensions\Blog\PostsRepository::MIN_PUBLIC_SEARCH_LENGTH;
|
|
||||||
$totalPages = max(1, (int) ceil($pageResult['total'] / \Plugi\Extensions\Blog\PostsRepository::POSTS_PER_PAGE));
|
$totalPages = max(1, (int) ceil($pageResult['total'] / \Plugi\Extensions\Blog\PostsRepository::POSTS_PER_PAGE));
|
||||||
if ($page > $totalPages) {
|
if ($page > $totalPages) {
|
||||||
$page = $totalPages;
|
$page = $totalPages;
|
||||||
@@ -19,15 +17,13 @@ $searchParameters = $search === '' ? [] : ['q' => $search];
|
|||||||
<section class="space-y-6" aria-label="Blog posts">
|
<section class="space-y-6" aria-label="Blog posts">
|
||||||
<form class="flex flex-col gap-3 sm:flex-row sm:items-end" method="get" action="<?= phpb_e($listingPath) ?>">
|
<form class="flex flex-col gap-3 sm:flex-row sm:items-end" method="get" action="<?= phpb_e($listingPath) ?>">
|
||||||
<label class="flex w-full max-w-md flex-col gap-2">
|
<label class="flex w-full max-w-md flex-col gap-2">
|
||||||
<span class="text-sm font-medium">Search titles, slugs, and excerpts (3+ characters)</span>
|
<span class="text-sm font-medium">Search posts</span>
|
||||||
<input class="input w-full" type="search" name="q" value="<?= phpb_e($search) ?>" />
|
<input class="input w-full" type="search" name="q" value="<?= phpb_e($search) ?>" />
|
||||||
</label>
|
</label>
|
||||||
<button class="btn" type="submit">Search</button>
|
<button class="btn" type="submit">Search</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<?php if ($searchTooShort): ?>
|
<?php if ($pageResult['total'] === 0): ?>
|
||||||
<div class="alert alert-info" role="status">Enter at least <?= \Plugi\Extensions\Blog\PostsRepository::MIN_PUBLIC_SEARCH_LENGTH ?> characters to search.</div>
|
|
||||||
<?php elseif ($pageResult['total'] === 0): ?>
|
|
||||||
<div class="alert alert-info" role="status">
|
<div class="alert alert-info" role="status">
|
||||||
<?= $search === '' ? 'No published posts yet.' : 'No published posts match "' . phpb_e($search) . '".' ?>
|
<?= $search === '' ? 'No published posts yet.' : 'No published posts match "' . phpb_e($search) . '".' ?>
|
||||||
</div>
|
</div>
|
||||||
@@ -47,9 +43,9 @@ $searchParameters = $search === '' ? [] : ['q' => $search];
|
|||||||
<span class="badge badge-outline"><?= phpb_e($post['category_name']) ?></span>
|
<span class="badge badge-outline"><?= phpb_e($post['category_name']) ?></span>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php if (!empty($post['published_at'])): ?>
|
<?php if (!empty($post['published_at'])): ?>
|
||||||
<?php $publishedDate = date_create($post['published_at'], $timezone); ?>
|
<?php $publishedTimestamp = strtotime($post['published_at']); ?>
|
||||||
<?php if ($publishedDate !== false): ?>
|
<?php if ($publishedTimestamp !== false): ?>
|
||||||
<time datetime="<?= phpb_e($publishedDate->format('c')) ?>"><?= phpb_e($publishedDate->format('F j, Y')) ?></time>
|
<time datetime="<?= phpb_e(date('c', $publishedTimestamp)) ?>"><?= phpb_e(date('F j, Y', $publishedTimestamp)) ?></time>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -141,9 +141,6 @@ function renderBlogEditor(int $postId): string
|
|||||||
}
|
}
|
||||||
|
|
||||||
$_ENV['DATABASE_PREFIX'] = 'test_';
|
$_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 = new BlogTestDatabase();
|
||||||
$phpb_db->createSchema();
|
$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.');
|
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.');
|
||||||
@@ -186,8 +183,6 @@ $post = $posts->create([
|
|||||||
]);
|
]);
|
||||||
assertBlog(is_array($post) && $post['status'] === 'published', 'A valid published post should be created.');
|
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.');
|
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.');
|
assertBlog($post['featured_image_id'] === 'blog-cover-1', 'A valid image upload reference should be persisted on the post.');
|
||||||
expectBlogValidation(fn() => $posts->create([
|
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' => '',
|
'title' => 'Missing featured image', 'slug' => 'missing-featured-image', 'excerpt' => '', 'body' => 'Body', 'featured_image_id' => 'missing-upload', 'category_id' => '', 'status' => 'draft', 'published_at' => '',
|
||||||
@@ -205,7 +200,7 @@ $olderPost = $posts->create([
|
|||||||
'status' => 'published',
|
'status' => 'published',
|
||||||
'published_at' => '2020-01-01T00:00',
|
'published_at' => '2020-01-01T00:00',
|
||||||
]);
|
]);
|
||||||
$pastPublication = (new \DateTimeImmutable('now', new \DateTimeZone($siteTimezone)))->modify('-1 hour')->format('Y-m-d\\TH:i');
|
$pastPublication = date('Y-m-d\\TH:i', time() - 3600);
|
||||||
$datedPost = $posts->create([
|
$datedPost = $posts->create([
|
||||||
'title' => 'Dated draft',
|
'title' => 'Dated draft',
|
||||||
'slug' => 'dated-draft',
|
'slug' => 'dated-draft',
|
||||||
@@ -225,9 +220,13 @@ $secondPost = $posts->create([
|
|||||||
'published_at' => '',
|
'published_at' => '',
|
||||||
]);
|
]);
|
||||||
$_ENV['BASE_URL'] = 'https://blog.example';
|
$_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']);
|
$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, '<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, '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.');
|
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.');
|
||||||
@@ -241,12 +240,10 @@ 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.');
|
assertBlog(str_contains($editorHtml, 'window.blogMarkdownEditor.insertAtCursor(markdown)'), 'The inline picker should insert at the CodeMirror cursor when the editor is initialized.');
|
||||||
$listingHtml = renderBlogListing();
|
$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, '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 "featured" & <cover>"'), 'The public listing should display a featured image with escaped alt text.');
|
assertBlog(str_contains($listingHtml, 'src="https://blog.example/uploads/blog-cover-1/cover.jpg" alt="A "featured" & <cover>"'), '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(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.');
|
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');
|
$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, '<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, '<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.');
|
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.');
|
||||||
@@ -286,7 +283,7 @@ expectBlogValidation(fn() => $posts->create([
|
|||||||
'title' => 'Invalid date', 'slug' => 'invalid-date', 'excerpt' => '', 'body' => '', 'category_id' => '', 'status' => 'draft', 'published_at' => 'not-a-date',
|
'title' => 'Invalid date', 'slug' => 'invalid-date', 'excerpt' => '', 'body' => '', 'category_id' => '', 'status' => 'draft', 'published_at' => 'not-a-date',
|
||||||
]), 'Invalid publication dates should be rejected.');
|
]), 'Invalid publication dates should be rejected.');
|
||||||
expectBlogValidation(fn() => $posts->create([
|
expectBlogValidation(fn() => $posts->create([
|
||||||
'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'),
|
'title' => 'Future date', 'slug' => 'future-date', 'excerpt' => '', 'body' => '', 'category_id' => '', 'status' => 'draft', 'published_at' => date('Y-m-d\\TH:i', time() + 86400),
|
||||||
]), 'Future publication dates should be rejected.');
|
]), 'Future publication dates should be rejected.');
|
||||||
|
|
||||||
$updated = $posts->update(['id' => $post['id']], [
|
$updated = $posts->update(['id' => $post['id']], [
|
||||||
@@ -334,7 +331,7 @@ assertBlog($posts->destroy($olderPost['id']) === true, 'An older published post
|
|||||||
assertBlog($posts->destroy($post['id']) === true, 'The remaining draft should be deleted.');
|
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($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.');
|
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'], 'general' => ['uploads_url' => '/uploads', 'timezone' => $siteTimezone]];
|
$phpb_config = ['website_manager' => ['url' => '/website-manager']];
|
||||||
$_GET = [];
|
$_GET = [];
|
||||||
assertBlog(str_contains(renderBlogAdminList(), 'No posts yet'), 'The admin list should show its empty-list state when no posts exist.');
|
assertBlog(str_contains(renderBlogAdminList(), 'No posts yet'), 'The admin list should show its empty-list state when no posts exist.');
|
||||||
|
|
||||||
@@ -373,13 +370,8 @@ foreach ([
|
|||||||
$adminMatch = $posts->getPageForAdmin($term, 1);
|
$adminMatch = $posts->getPageForAdmin($term, 1);
|
||||||
$publicMatch = $posts->getPageForListing($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($adminMatch['total'] === 1 && $adminMatch['posts'][0]['slug'] === $fieldPost['slug'], 'Admin 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.');
|
assertBlog($publicMatch['total'] === 1 && $publicMatch['posts'][0]['slug'] === $fieldPost['slug'], 'Public search should match the ' . $field . ' field.');
|
||||||
}
|
}
|
||||||
$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 ([
|
foreach ([
|
||||||
['term' => '100%', 'match' => 'literal-percent', 'title' => 'Price 100% complete', 'decoy' => 'wildcard-percent-decoy', 'decoyTitle' => 'Price 100X complete'],
|
['term' => '100%', 'match' => 'literal-percent', 'title' => 'Price 100% complete', 'decoy' => 'wildcard-percent-decoy', 'decoyTitle' => 'Price 100X complete'],
|
||||||
|
|||||||
Reference in New Issue
Block a user