From fb9175402a7d425438cccc37e770215e25ddd8e5 Mon Sep 17 00:00:00 2001 From: ak Date: Tue, 11 Aug 2026 18:48:16 +0800 Subject: [PATCH] =?UTF-8?q?M7:=20=E5=85=A8=E9=87=8F=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=8825=20=E7=94=A8=E4=BE=8B=EF=BC=89+=20README=20+=20?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E6=96=87=E6=A1=A3=EF=BC=88nginx/systemd/cron?= =?UTF-8?q?=EF=BC=89+=20theme:publish=20=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 138 ++++++++++++----- app/Blog/Providers/ThemeServiceProvider.php | 23 +-- app/Blog/Support/ThemeManager.php | 24 +++ app/Console/Commands/ThemePublish.php | 42 +++++ docs/deploy.md | 144 ++++++++++++++++++ .../src/Jobs/AiModerateCommentJob.php | 8 +- .../src/Jobs/AiPolishContentJob.php | 9 +- public/themes/modern/style.css | 104 +++++++++++++ public/themes/sablog/style.css | 115 ++++++++++++++ tests/Feature/AiModerationTest.php | 107 +++++++++++++ tests/Feature/LegacyRoutesTest.php | 91 +++++++++++ tests/Feature/ThemeTest.php | 62 ++++++++ 12 files changed, 802 insertions(+), 65 deletions(-) create mode 100644 app/Console/Commands/ThemePublish.php create mode 100644 docs/deploy.md create mode 100644 public/themes/modern/style.css create mode 100644 public/themes/sablog/style.css create mode 100644 tests/Feature/AiModerationTest.php create mode 100644 tests/Feature/LegacyRoutesTest.php create mode 100644 tests/Feature/ThemeTest.php diff --git a/README.md b/README.md index 0165a77..089719b 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,119 @@ -

Laravel Logo

+# LaraLog -

-Build Status -Total Downloads -Latest Stable Version -License -

+基于 **Laravel 12 + Filament 5 + Livewire 4 + Spatie 生态** 的现代化博客系统,从 SaBlog-X 1.6 完整迁移而来: -## About Laravel +- **脚本迁移**:`php artisan sablog:import` 一键从老库迁入全部数据(文章/分类/评论/标签/友链/用户/附件),保留原始 ID,老 URL 全部 301 兼容 +- **多主题**:`themes/` 目录即皮肤,后台一键切换/上传 ZIP;内置 sablog 经典风 + modern 简约风两套 +- **插件系统**:`plugins/` 目录 + 钩子系统(action/filter),ZIP 安装 + 可选远程市场 +- **内置插件**:SEO & GEO(JSON-LD/OG/llms.txt/Markdown 导出)、AI 评论审核 + 内容润色(Workerman 异步)、支付(支付宝/微信/沙箱)、会员(套餐订阅 + 付费文章) +- **附件全走 S3**:R2 / COS / OSS 兼容,新上传零落盘,`attachments:sync-s3` 一键同步老附件(大文件 Multipart) -Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as: +## 技术栈 -- [Simple, fast routing engine](https://laravel.com/docs/routing). -- [Powerful dependency injection container](https://laravel.com/docs/container). -- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage. -- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent). -- Database agnostic [schema migrations](https://laravel.com/docs/migrations). -- [Robust background job processing](https://laravel.com/docs/queues). -- [Real-time event broadcasting](https://laravel.com/docs/broadcasting). +| 组件 | 版本 | 说明 | +|------|------|------| +| Laravel | ^12.0 | 应用框架 | +| Filament | ^5.7 | 后台面板(zh_CN) | +| Livewire | ^4.4 | 前台交互 | +| Workerman | ^5.2 | 常驻进程:队列消费者 + WebSocket 进度推送 | +| yansongda/pay | ^3.7 | 支付宝 / 微信支付 | +| spatie 库 | — | permission / medialibrary / sitemap / robots / tags / backup | +| league/commonmark | ^2.9 | Markdown 渲染(GFM) | +| league/html-to-markdown | ^5.1 | 老 HTML 内容转换 | +| MySQL | 8.x | 数据库 | -Laravel is accessible, powerful, and provides tools required for large, robust applications. +## 快速开始 -## Learning Laravel +```bash +# 1. 安装依赖 +composer install +cp .env.example .env +php artisan key:generate -Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework. You can also check out [Laravel Learn](https://laravel.com/learn), where you will be guided through building a modern Laravel application. +# 2. 配置 .env(MySQL 8) +DB_DATABASE=laralog +DB_USERNAME=root +DB_PASSWORD= -If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library. +# 3. 迁移 + 种子(创建 admin@laralog.test / password) +php artisan migrate --seed -## Laravel Sponsors +# 4. 启动(Herd 或 artisan serve) +php artisan serve -We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com). +# 5. 后台:http://laralog.test/admin +``` -### Premium Partners +## 迁移老 Sablog -- **[Vehikl](https://vehikl.com)** -- **[Tighten Co.](https://tighten.co)** -- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)** -- **[64 Robots](https://64robots.com)** -- **[Curotec](https://www.curotec.com/services/technologies/laravel)** -- **[DevSquad](https://devsquad.com/hire-laravel-developers)** -- **[Redberry](https://redberry.international/laravel-development)** -- **[Active Logic](https://activelogic.com)** +```bash +# 原始导入(内容保持 HTML,[attach=xx] 渲染时解析) +php artisan sablog:import \ + --database=sablog --host=127.0.0.1 --port=3306 \ + --username=root --password=xxx \ + --attachments-dir=/path/to/old/attachments --sync-attachments -## Contributing +# 同时把内容转换为 Markdown([attach=xx] 转为 {{attach:xx}} 令牌) +php artisan sablog:import ... --convert-markdown -Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions). +# 事后单独转换 / 同步附件到 S3 +php artisan content:convert --all +S3_ACCESS_KEY=... S3_BUCKET=... S3_ENDPOINT=... php artisan attachments:sync-s3 --legacy-dir=/path/to/old/attachments +``` -## Code of Conduct +> 不迁移的过时功能:trackback(引用通告,垃圾来源)、搜索缓存表、验证码(由 AI 审核 + 蜜罐 + 频控取代)、WAP、广告位变量。 -In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct). +## 主题 -## Security Vulnerabilities +- 目录:`themes/{name}/`,含 `theme.json`(标题/版本/描述/作者)+ `views/**` + `assets/` +- 激活主题的 views 目录前置到全局视图路径:主题覆盖同名视图,缺失自动回退默认视图 +- 资产开发期由 `/themes/{name}/assets/...` 流式返回;生产可 `php artisan theme:publish`(见 docs/deploy.md) +- 后台「主题管理」可激活 / 上传 ZIP / 删除 -If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed. +## 插件 -## License +- 目录:`plugins/{vendor}.{name}/`,含 `plugin.json` + `src/ServiceProvider.php`(继承 `App\Blog\Support\PluginServiceProvider`)+ 可选 `database/migrations`、`routes/web.php`、`views/` +- 钩子:`addAction(hook, cb)` / `addFilter(hook, cb, priority)`,核心钩子: + - `comment.created`(AI 审核)、`payment.paid`(订阅激活/解锁)、`post.rendered`(付费内容过滤)、`seo.structured_data` +- 后台「插件管理」:启停 / 上传 ZIP 安装 / 卸载 / 市场拉取(配置 `MARKET_URL` 后可用) +- 插件自带迁移由 `migrate` 自动加载(`app/Providers/AppServiceProvider` 注册 plugins/*/database/migrations) -The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). +## 内容格式 + +- 每篇文章有 `content_format`(markdown / html),新文章默认 Markdown +- 老数据导入标记为 html 原样保留;`content:convert` 批量转 Markdown +- 附件短代码:HTML 内容用 `[attach=1]` / `[img=1]`,Markdown 内容用 `{{attach:1}}` / `{{img:1}}`(`[]` 是 Markdown 保留字符),渲染时统一解析 +- 付费内容:`[paid]...[/paid]` 短代码,非会员/未解锁访客隐藏(会员插件) + +## 异步处理(Workerman) + +```bash +php artisan workerman:serve start # 队列消费者 × N + WebSocket :8787 +php artisan workerman:serve stop +``` + +- 队列消费者在常驻进程内复用 Laravel 容器,省去每次任务的框架启动开销(LLM 调用主路径) +- 任务统一实现 `App\Blog\Jobs\AiJob` 接口(`handle(LlmClient)`),Workerman 反序列化执行 +- 降级路径:`php artisan queue:work` 照常可用(同一队列) + +## 测试 + +```bash +php artisan test # 25 个用例:迁移/老路由 301/主题/插件/会员支付流/AI 审核 +``` + +## 目录结构 + +``` +app/ + Blog/ # 前台域模块(Controllers/Jobs/Services/Support/View) + Filament/ # 后台资源与页面 + Console/Commands # sablog:import / attachments:sync-s3 / workerman:serve / content:convert +themes/ # 主题(sablog / modern) +plugins/ # 内置插件(blog-seo / ai-moderation / payment / membership) +docs/deploy.md # 部署指南 +``` + +## 许可 + +MIT diff --git a/app/Blog/Providers/ThemeServiceProvider.php b/app/Blog/Providers/ThemeServiceProvider.php index bd213f0..c115980 100644 --- a/app/Blog/Providers/ThemeServiceProvider.php +++ b/app/Blog/Providers/ThemeServiceProvider.php @@ -18,7 +18,7 @@ class ThemeServiceProvider extends ServiceProvider public function boot(): void { // 激活主题的 views 目录前置到全局视图路径,实现主题覆盖 + 默认兜底 - $this->prependActiveThemeViews(); + app(ThemeManager::class)->prependViews(); // 侧边栏数据共享:所有前台视图自动获得 $categories/$recentPosts/$hotTags/$links 等 View::composer( @@ -26,25 +26,4 @@ class ThemeServiceProvider extends ServiceProvider SidebarComposer::class ); } - - private function prependActiveThemeViews(): void - { - $manager = app(ThemeManager::class); - - // 迁移尚未执行(如 migrate:fresh 首轮)时跳过 DB 查询,使用默认主题 - $active = \Illuminate\Support\Facades\Schema::hasTable('settings') - ? $manager->active() - : config('themes.default'); - - $views = $manager->path($active).'/views'; - - if (! is_dir($views)) { - return; - } - - $paths = config('view.paths'); - array_unshift($paths, $views); - config(['view.paths' => $paths]); - app('view')->getFinder()->setPaths($paths); - } } diff --git a/app/Blog/Support/ThemeManager.php b/app/Blog/Support/ThemeManager.php index 82550de..a79672b 100644 --- a/app/Blog/Support/ThemeManager.php +++ b/app/Blog/Support/ThemeManager.php @@ -69,6 +69,30 @@ class ThemeManager } Setting::set('active_theme', $theme); + $this->prependViews(); + } + + /** + * 把激活主题的 views 目录前置到全局视图路径。 + * 在 Provider boot 与运行时切换主题时调用。 + */ + public function prependViews(): void + { + // 迁移尚未执行(migrate:fresh 首轮)时跳过 DB 查询,使用默认主题 + $active = \Illuminate\Support\Facades\Schema::hasTable('settings') + ? $this->active() + : config('themes.default'); + + $views = $this->path($active).'/views'; + + if (! is_dir($views)) { + return; + } + + $paths = config('view.paths'); + array_unshift($paths, $views); + config(['view.paths' => $paths]); + app('view')->getFinder()->setPaths($paths); } /** diff --git a/app/Console/Commands/ThemePublish.php b/app/Console/Commands/ThemePublish.php new file mode 100644 index 0000000..28eee6f --- /dev/null +++ b/app/Console/Commands/ThemePublish.php @@ -0,0 +1,42 @@ +argument('name') + ? [$this->argument('name')] + : array_keys($themes->all()); + + foreach ($names as $name) { + if (! $themes->exists($name)) { + $this->warn("跳过不存在的主题:{$name}"); + continue; + } + + $source = $themes->path($name).'/assets'; + $target = public_path('themes/'.$name); + + if (! is_dir($source)) { + $this->warn("主题 {$name} 没有 assets 目录"); + continue; + } + + File::deleteDirectory($target); + File::copyDirectory($source, $target); + $this->info("已发布:{$name} -> public/themes/{$name}"); + } + + return self::SUCCESS; + } +} diff --git a/docs/deploy.md b/docs/deploy.md new file mode 100644 index 0000000..b1997ed --- /dev/null +++ b/docs/deploy.md @@ -0,0 +1,144 @@ +# 部署指南 + +## 环境变量(.env 关键项) + +```dotenv +APP_URL=https://blog.example.com +APP_LOCALE=zh_CN + +# MySQL 8 +DB_CONNECTION=mysql +DB_HOST=127.0.0.1 +DB_DATABASE=laralog +DB_USERNAME=laralog +DB_PASSWORD=*** + +# S3 兼容附件(R2 / COS / OSS) +S3_ACCESS_KEY=xxx +S3_SECRET_KEY=xxx +S3_REGION=auto # R2 用 auto;COS/OSS 填对应区域 +S3_BUCKET=laralog +S3_ENDPOINT=https://.r2.cloudflarestorage.com # COS: https://cos..myqcloud.com / OSS: https://oss-.aliyuncs.com +S3_URL=https://media.example.com # 公开访问域名(CDN),留空则用 endpoint+bucket 拼 +S3_VISIBILITY=public +UPLOAD_DISK=uploads + +# LLM(OpenAI 兼容:DeepSeek / 通义 / 自建代理) +LLM_BASE_URL=https://api.deepseek.com/v1 +LLM_API_KEY=sk-xxx +LLM_MODEL=deepseek-chat + +# Workerman +WORKERMAN_QUEUE_WORKERS=2 +WORKERMAN_WS_ENABLED=true +WORKERMAN_WS_PORT=8787 + +# 插件/主题市场(留空仅支持本地 ZIP) +MARKET_URL= +MARKET_TOKEN= +``` + +## 生产迁移顺序 + +1. `php artisan migrate --seed` +2. 导入老数据:`php artisan sablog:import --database=sablog ... --attachments-dir=... --sync-attachments --convert-markdown` +3. 配置 S3 后同步附件:`php artisan attachments:sync-s3 --legacy-dir=...` +4. 生成主题资产(可选):`php artisan theme:publish` +5. 启动 Workerman + 队列 + 调度器 + +## Nginx(前端由 Laravel 接管,老 URL 由应用层 301) + +```nginx +server { + listen 443 ssl http2; + server_name blog.example.com; + + root /var/www/laralog/public; + index index.php; + + ssl_certificate /etc/nginx/ssl/blog.crt; + ssl_certificate_key /etc/nginx/ssl/blog.key; + + # 主题资产(生产已 theme:publish 时可交给 Nginx;未发布则走 Laravel 路由) + location ~ ^/themes/.+\.(css|js|png|jpg|jpeg|gif|svg|woff2?|ico)$ { + try_files $uri /index.php?$query_string; + expires 7d; + add_header Cache-Control "public"; + } + + # 附件已全部走 S3,无需本地静态附件目录 + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_pass unix:/run/php/php8.2-fpm.sock; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } + + location ~ /\.(?!well-known).* { deny all; } +} +``` + +> 兼容性说明:所有 sablog 老 URL(`/show-12.html`、`/?action=show&id=12`、`rss.php`、`attachment.php` 等)均由 `routes/web.php` 处理为 301,无需在 Nginx 额外配置重写。 + +## systemd:Workerman 常驻服务 + +```ini +# /etc/systemd/system/laralog-workerman.service +[Unit] +Description=LaraLog Workerman (queue + websocket) +After=network.target mysql.service + +[Service] +User=www-data +Group=www-data +WorkingDirectory=/var/www/laralog +ExecStart=/usr/bin/php artisan workerman:serve start +ExecStop=/usr/bin/php artisan workerman:serve stop +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target +``` + +```bash +systemctl enable --now laralog-workerman +``` + +## 调度器(cron) + +```cron +* * * * * cd /var/www/laralog && php artisan schedule:run >> /dev/null 2>&1 +``` + +调度器内注册的内容:`spatie/laravel-backup` 备份、缓存清理等(按需在 `routes/console.php` 增删)。 + +## 队列降级(不使用 Workerman 时) + +```ini +# /etc/systemd/system/laralog-queue.service +[Service] +User=www-data +WorkingDirectory=/var/www/laralog +ExecStart=/usr/bin/php artisan queue:work --sleep=3 --tries=3 +Restart=always +``` + +## 备份 + +```bash +# spatie/laravel-backup(备份 DB + 附件;S3 附件无需本地备份) +php artisan backup:run --only-db +``` + +## 常见操作 + +```bash +php artisan theme:publish # 把主题 assets 拷贝到 public/themes +php artisan workerman:serve status # 查看常驻进程 +php artisan content:convert --all # 老 HTML 文章批量转 Markdown +``` diff --git a/plugins/neatstudio.ai-moderation/src/Jobs/AiModerateCommentJob.php b/plugins/neatstudio.ai-moderation/src/Jobs/AiModerateCommentJob.php index 83c062f..fd7f147 100644 --- a/plugins/neatstudio.ai-moderation/src/Jobs/AiModerateCommentJob.php +++ b/plugins/neatstudio.ai-moderation/src/Jobs/AiModerateCommentJob.php @@ -1,15 +1,19 @@ 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); + } +} diff --git a/tests/Feature/LegacyRoutesTest.php b/tests/Feature/LegacyRoutesTest.php new file mode 100644 index 0000000..3ae93da --- /dev/null +++ b/tests/Feature/LegacyRoutesTest.php @@ -0,0 +1,91 @@ +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'); + } +} diff --git a/tests/Feature/ThemeTest.php b/tests/Feature/ThemeTest.php new file mode 100644 index 0000000..bdc8ccd --- /dev/null +++ b/tests/Feature/ThemeTest.php @@ -0,0 +1,62 @@ +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(); + } +}