diff --git a/extensions/plugi-ext-blog/config.php b/extensions/plugi-ext-blog/config.php
index 132045b..1cb9949 100644
--- a/extensions/plugi-ext-blog/config.php
+++ b/extensions/plugi-ext-blog/config.php
@@ -13,6 +13,7 @@ return [
'slug' => 'varchar(191) NOT NULL',
'excerpt' => 'text NOT NULL',
'body' => 'longtext NOT NULL',
+ 'featured_image_id' => 'varchar(50) NULL',
'category_id' => 'int NULL',
'status' => "varchar(20) NOT NULL DEFAULT 'draft'",
'published_at' => 'datetime NULL',
diff --git a/extensions/plugi-ext-blog/routes/savePost.php b/extensions/plugi-ext-blog/routes/savePost.php
index 12ac4f9..808281b 100644
--- a/extensions/plugi-ext-blog/routes/savePost.php
+++ b/extensions/plugi-ext-blog/routes/savePost.php
@@ -12,7 +12,7 @@ if (!phpb_validate_csrf()) {
$postsRepository = new \Plugi\Extensions\Blog\PostsRepository();
$redirectUrl = phpb_url('website_manager', ['tab' => 'ext--blog', 'action' => 'posts']);
-$fields = ['title', 'slug', 'excerpt', 'body', 'category_id', 'status', 'published_at'];
+$fields = ['title', 'slug', 'excerpt', 'body', 'featured_image_id', 'category_id', 'status', 'published_at'];
$data = array_intersect_key($_POST, array_flip($fields));
try {
if (isset($_POST['id']) && is_string($_POST['id']) && ctype_digit($_POST['id']) && (int) $_POST['id'] > 0) {
diff --git a/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php b/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php
index ad2f907..42cea6e 100644
--- a/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php
+++ b/extensions/plugi-ext-blog/tests/BlogRepositoriesTest.php
@@ -3,11 +3,14 @@
use Plugi\Core\DB;
use Plugi\Extensions\Blog\CategoriesRepository;
use Plugi\Extensions\Blog\PostsRepository;
+use Plugi\Repositories\UploadRepository;
require_once dirname(__DIR__, 3) . '/plugi-core/src/Extensions.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Core/DB.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Core/helpers.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Repositories/BaseRepository.php';
+require_once dirname(__DIR__, 3) . '/plugi-core/src/Repositories/UploadRepository.php';
+require_once dirname(__DIR__, 3) . '/plugi-core/src/UploadedFile.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Contracts/PageContract.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Contracts/ThemeContract.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Page.php';
@@ -18,10 +21,12 @@ require_once dirname(__DIR__, 3) . '/plugi-core/src/ThemeBlock.php';
require_once dirname(__DIR__, 3) . '/plugi-core/src/Modules/GrapesJS/Block/BlockRenderer.php';
require_once dirname(__DIR__) . '/admin/Classes/PostsRepository.php';
require_once dirname(__DIR__) . '/admin/Classes/CategoriesRepository.php';
+require_once dirname(__DIR__) . '/admin/Classes/PostContentRenderer.php';
$blogConfig = require dirname(__DIR__) . '/config.php';
\Plugi\Extensions::registerConfig('blog', $blogConfig);
require_once dirname(__DIR__) . '/extension.php';
+assertBlog(($blogConfig['tables']['posts']['columns']['featured_image_id'] ?? null) === 'varchar(50) NULL', 'Featured image metadata should be declared as nullable extension schema.');
class BlogTestDatabase extends DB
{
@@ -40,12 +45,22 @@ class BlogTestDatabase extends DB
slug VARCHAR(191) NOT NULL UNIQUE,
excerpt TEXT NOT NULL,
body TEXT NOT NULL,
+ featured_image_id VARCHAR(50) NULL,
category_id INTEGER NULL,
status VARCHAR(20) NOT NULL,
published_at DATETIME NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)');
+ $this->query('CREATE TABLE test_uploads (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ public_id VARCHAR(50) NOT NULL UNIQUE,
+ original_file VARCHAR(512) NOT NULL,
+ mime_type VARCHAR(50) NOT NULL,
+ server_file VARCHAR(512) NOT NULL,
+ alt_text VARCHAR(1000) NOT NULL DEFAULT "",
+ folder VARCHAR(255) NOT NULL DEFAULT ""
+ )');
$this->query('CREATE TABLE test_ext__blog_categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255) NOT NULL,
@@ -117,6 +132,14 @@ function renderBlogAdminList(): string
return (string) ob_get_clean();
}
+function renderBlogEditor(int $postId): string
+{
+ $_GET = ['id' => (string) $postId];
+ ob_start();
+ include dirname(__DIR__) . '/admin/Views/EditPost.php';
+ return (string) ob_get_clean();
+}
+
$_ENV['DATABASE_PREFIX'] = 'test_';
$phpb_db = new BlogTestDatabase();
$phpb_db->createSchema();
@@ -131,24 +154,38 @@ $posts = new PostsRepository();
$categories = new CategoriesRepository();
$category = $categories->create(['name' => 'News', 'slug' => 'news']);
assertBlog(is_array($category) && (int) $category['id'] > 0, 'Category creation should return the new category.');
+$phpb_db->query('INSERT INTO test_uploads (public_id, original_file, mime_type, server_file, alt_text) VALUES (?, ?, ?, ?, ?)', [
+ 'blog-cover-1', 'cover.jpg', 'image/jpeg', 'cover.jpg', 'A "featured" & ',
+]);
+$phpb_db->query('INSERT INTO test_uploads (public_id, original_file, mime_type, server_file, alt_text) VALUES (?, ?, ?, ?, ?)', [
+ 'blog-document-1', 'notes.pdf', 'application/pdf', 'notes.pdf', 'A document',
+]);
$post = $posts->create([
'title' => 'First post',
'slug' => 'first-post',
'excerpt' => 'A short summary',
'body' => 'Post body',
+ 'featured_image_id' => 'blog-cover-1',
'category_id' => $category['id'],
'status' => 'published',
'published_at' => '',
]);
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['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' => '',
+]), 'Missing upload references should be rejected.');
+expectBlogValidation(fn() => $posts->create([
+ 'title' => 'Non-image featured image', 'slug' => 'non-image-featured-image', 'excerpt' => '', 'body' => 'Body', 'featured_image_id' => 'blog-document-1', 'category_id' => '', 'status' => 'draft', 'published_at' => '',
+]), 'Non-image upload references should be rejected.');
$olderPost = $posts->create([
'title' => 'Older published post',
'slug' => 'older-published-post',
'excerpt' => 'Older excerpt',
- 'body' => '&safe',
+ 'body' => "## Markdown heading\n\nA **bold** and *emphasized* phrase with `inline code`, a [safe link](https://example.com), an [unsafe link](javascript:alert), and an [encoded unsafe link](javascript:alert).\n\n- first item\n- second item\n\n1. first numbered item\n2. second numbered item\n\n![Inline & ]](https://images.example/photo.jpg)\n\n\n\n\n&safe",
'category_id' => '',
'status' => 'published',
'published_at' => '2020-01-01T00:00',
@@ -172,19 +209,48 @@ $secondPost = $posts->create([
'status' => 'draft',
'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.');
-$_ENV['BASE_URL'] = 'https://blog.example';
+$editorHtml = renderBlogEditor((int) $post['id']);
+assertBlog(str_contains($editorHtml, 'name="featured_image_id"') && str_contains($editorHtml, 'value="blog-cover-1" selected'), 'The editor should select a post featured image.');
+assertBlog(!str_contains($editorHtml, 'notes.pdf'), 'The featured-image selector should omit non-image uploads.');
+assertBlog(str_contains($editorHtml, 'id="post-inline-image"') && str_contains($editorHtml, 'id="insert-post-inline-image"'), 'The editor should offer a control for inserting inline images.');
+assertBlog(str_contains($editorHtml, 'data-url="https://blog.example/uploads/blog-cover-1/cover.jpg"') && str_contains($editorHtml, 'data-alt="A "featured" & <cover>"'), 'The inline picker should expose escaped media URL and alt metadata.');
+assertBlog(str_contains($editorHtml, 'selectionStart') && str_contains($editorHtml, 'setRangeText'), 'The inline picker should insert Markdown at the textarea cursor.');
$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, '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(!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, 'Older published post
'), 'The detail block should resolve a published post from the named slug route parameter.');
-assertBlog(str_contains($detailHtml, '<script>alert("x")</script>&safe') && !str_contains($detailHtml, '