M7: 全量测试(25 用例)+ README + 部署文档(nginx/systemd/cron)+ theme:publish 命令

This commit is contained in:
ak
2026-08-11 18:48:16 +08:00
parent 933736cfdd
commit fb9175402a
12 changed files with 802 additions and 65 deletions
+107
View File
@@ -0,0 +1,107 @@
<?php
namespace Tests\Feature;
use App\Blog\Services\LlmClient;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Plugins\Neatstudio\AiModeration\Jobs\AiModerateCommentJob;
use Tests\TestCase;
class AiModerationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
Setting::set('ai_moderation_enabled', '1');
Setting::set('comment_audit', '1'); // 新评论先进入待审核
Setting::set('llm_base_url', 'https://example.test/v1');
Setting::set('llm_api_key', 'test-key');
Setting::set('llm_model', 'test-model');
}
public function test_comment_created_dispatches_moderation_job(): void
{
\Illuminate\Support\Facades\Queue::fake();
$post = Post::create([
'title' => '评论审核',
'slug' => 'ai-comment',
'content' => '正文',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
$this->post('/posts/'.$post->id.'/comments', [
'author_name' => '访客',
'content' => '这是一条需要审核的评论',
]);
\Illuminate\Support\Facades\Queue::assertPushed(AiModerateCommentJob::class);
}
public function test_moderation_job_approves_comment(): void
{
$post = Post::create([
'title' => '评论审核',
'slug' => 'ai-comment-2',
'content' => '正文',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
$comment = Comment::create([
'post_id' => $post->id,
'author_name' => '访客',
'content' => '正常评论内容',
'status' => 'pending',
'created_at' => now(),
]);
$llm = $this->mock(LlmClient::class);
$llm->shouldReceive('chat')->once()->andReturn('{"verdict":"approved","reason":"正常"}');
(new AiModerateCommentJob($comment->id))->handle($llm);
$comment->refresh();
$this->assertSame('published', $comment->status);
$this->assertStringContainsString('approved', $comment->ai_review['raw'] ?? '');
}
public function test_moderation_job_rejects_spam(): void
{
$post = Post::create([
'title' => '评论审核',
'slug' => 'ai-comment-3',
'content' => '正文',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
$comment = Comment::create([
'post_id' => $post->id,
'author_name' => '广告机',
'content' => '点击链接赚钱',
'status' => 'pending',
'created_at' => now(),
]);
$llm = $this->mock(LlmClient::class);
$llm->shouldReceive('chat')->once()->andReturn('{"verdict":"spam","reason":"广告"}');
(new AiModerateCommentJob($comment->id))->handle($llm);
$comment->refresh();
$this->assertSame('spam', $comment->status);
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace Tests\Feature;
use App\Models\Category;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Tags\Tag;
use Tests\TestCase;
class LegacyRoutesTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
$user = User::create(['name' => '作者', 'email' => 'author@test.com', 'password' => bcrypt('x')]);
$category = Category::create(['name' => '技术', 'slug' => 'tech']);
\Illuminate\Support\Facades\DB::table('posts')->insert([
'id' => 12,
'user_id' => $user->id,
'category_id' => $category->id,
'title' => '老文章',
'slug' => 'legacy-post',
'content' => '内容',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
'created_at' => now(),
'updated_at' => now(),
]);
Comment::create(['post_id' => 12, 'author_name' => '访客', 'content' => '好文', 'status' => 'published', 'created_at' => now()]);
}
public function test_rewrite_style_urls_redirect(): void
{
$this->get('/show-12.html')->assertRedirect('/posts/legacy-post');
$this->get('/show-12-1.html')->assertRedirect('/posts/legacy-post');
$this->get('/category-1.html')->assertRedirect();
$this->get('/archives-'.now()->format('Ymd').'.html')->assertRedirect();
$this->get('/tagslist.html')->assertRedirect('/tags');
$this->get('/links.shtml')->assertRedirect('/links');
$this->get('/login.shtml')->assertRedirect('/login');
$this->get('/reg.shtml')->assertRedirect('/register');
}
public function test_query_style_urls_redirect(): void
{
$this->get('/?action=show&id=12')->assertRedirect('/posts/legacy-post');
$this->get('/?action=category&cid=1')->assertRedirect();
$this->get('/?action=index&setdate='.now()->format('Ym'))->assertRedirect();
$this->get('/?action=search&keyword=laravel')->assertRedirect('/search?q=laravel');
$this->get('/?action=links')->assertRedirect('/links');
$this->get('/?action=archives')->assertRedirect('/archives');
$this->get('/?action=comments')->assertRedirect('/comments');
}
public function test_trackback_endpoints_are_gone(): void
{
$this->get('/tburl.php')->assertStatus(410);
$this->get('/trackback.php')->assertStatus(410);
}
public function test_stable_paths_work(): void
{
$this->get('/rss.xml')->assertOk();
$this->get('/sitemap.xml')->assertOk();
$this->get('/robots.txt')->assertOk();
}
public function test_missing_records_return_404(): void
{
$this->get('/show-99999.html')->assertNotFound();
$this->get('/?action=show&id=99999')->assertNotFound();
}
public function test_legacy_tag_id_mapping(): void
{
$tag = Tag::create(['name' => 'laravel-tag', 'type' => 'post']);
Setting::set('legacy_tags', [1 => $tag->id]);
$this->get('/?action=tags&id=1')->assertRedirect('/tags/laravel-tag');
}
}
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace Tests\Feature;
use App\Models\Setting;
use App\Models\Post;
use App\Models\Category;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ThemeTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
$user = User::create(['name' => '作者', 'email' => 't@test.com', 'password' => bcrypt('x')]);
$category = Category::create(['name' => '分类', 'slug' => 'cat']);
Post::create([
'user_id' => $user->id,
'category_id' => $category->id,
'title' => '主题测试',
'slug' => 'theme-test',
'content' => '内容',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
}
public function test_sablog_theme_renders_classic_layout(): void
{
app(\App\Blog\Support\ThemeManager::class)->activate('sablog');
$this->get('/')->assertOk()->assertSee('outmain');
$this->get('/posts/theme-test')->assertOk()->assertSee('outmain');
}
public function test_modern_theme_renders_modern_layout(): void
{
app(\App\Blog\Support\ThemeManager::class)->activate('modern');
$this->get('/')->assertOk()->assertSee('m-header');
$this->get('/posts/theme-test')->assertOk()->assertSee('m-header');
}
public function test_theme_assets_are_served(): void
{
$this->get('/themes/sablog/assets/style.css')->assertOk()->assertHeader('Content-Type', 'text/css; charset=utf-8');
$this->get('/themes/modern/assets/style.css')->assertOk();
$this->get('/themes/nonexistent/assets/style.css')->assertNotFound();
}
public function test_unknown_theme_falls_back_to_default(): void
{
Setting::set('active_theme', 'does-not-exist');
$this->get('/')->assertOk();
}
}