feat: 初始化知识库系统项目
- 实现基于 Laravel 11 和 Filament 3.X 的文档管理系统 - 添加用户认证和分组管理功能 - 实现文档上传、分类和权限控制 - 集成 Word 文档自动转换为 Markdown - 集成 Meilisearch 全文搜索引擎 - 实现文档在线预览功能 - 添加安全日志和审计功能 - 完整的简体中文界面 - 包含完整的项目文档和部署指南 技术栈: - Laravel 11.x - Filament 3.X - Meilisearch 1.5+ - Pandoc 文档转换 - Redis 队列系统 - Pest PHP 测试框架
This commit is contained in:
123
tests/Feature/DocumentPreviewTest.php
Normal file
123
tests/Feature/DocumentPreviewTest.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Document;
|
||||
use App\Models\Group;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
// 设置存储磁盘
|
||||
Storage::fake('local');
|
||||
});
|
||||
|
||||
test('用户可以预览有权限的全局文档', function () {
|
||||
// 创建用户和文档
|
||||
$user = User::factory()->create();
|
||||
$document = Document::factory()->create([
|
||||
'type' => 'global',
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => 'markdown/test.md',
|
||||
]);
|
||||
|
||||
// 创建 Markdown 文件
|
||||
Storage::disk('local')->put($document->markdown_path, '# 测试标题\n\n这是测试内容。');
|
||||
|
||||
// 访问预览页面
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
// 验证响应
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($document->title);
|
||||
$response->assertSee('测试标题');
|
||||
});
|
||||
|
||||
test('用户可以预览有权限的专用文档', function () {
|
||||
// 创建分组和用户
|
||||
$group = Group::factory()->create();
|
||||
$user = User::factory()->create();
|
||||
$user->groups()->attach($group);
|
||||
|
||||
// 创建专用文档
|
||||
$document = Document::factory()->create([
|
||||
'type' => 'dedicated',
|
||||
'group_id' => $group->id,
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => 'markdown/test.md',
|
||||
]);
|
||||
|
||||
// 创建 Markdown 文件
|
||||
Storage::disk('local')->put($document->markdown_path, '# 专用文档\n\n这是专用内容。');
|
||||
|
||||
// 访问预览页面
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
// 验证响应
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($document->title);
|
||||
$response->assertSee('专用文档');
|
||||
});
|
||||
|
||||
test('用户无法预览无权限的专用文档', function () {
|
||||
// 创建两个分组
|
||||
$group1 = Group::factory()->create();
|
||||
$group2 = Group::factory()->create();
|
||||
|
||||
// 用户属于分组1
|
||||
$user = User::factory()->create();
|
||||
$user->groups()->attach($group1);
|
||||
|
||||
// 文档属于分组2
|
||||
$document = Document::factory()->create([
|
||||
'type' => 'dedicated',
|
||||
'group_id' => $group2->id,
|
||||
'conversion_status' => 'completed',
|
||||
]);
|
||||
|
||||
// 尝试访问预览页面
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
// 验证被拒绝
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
||||
test('预览页面正确处理 Markdown 内容为空的情况', function () {
|
||||
// 创建用户和文档(没有 Markdown 内容)
|
||||
$user = User::factory()->create();
|
||||
$document = Document::factory()->create([
|
||||
'type' => 'global',
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => null,
|
||||
]);
|
||||
|
||||
// 访问预览页面
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
// 验证响应
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Markdown 内容为空');
|
||||
$response->assertSee('下载原始文档');
|
||||
});
|
||||
|
||||
test('预览页面显示下载按钮', function () {
|
||||
// 创建用户和文档
|
||||
$user = User::factory()->create();
|
||||
$document = Document::factory()->create([
|
||||
'type' => 'global',
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => 'markdown/test.md',
|
||||
]);
|
||||
|
||||
// 创建 Markdown 文件
|
||||
Storage::disk('local')->put($document->markdown_path, '# 测试');
|
||||
|
||||
// 访问预览页面
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
// 验证下载按钮存在
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('下载原文档');
|
||||
$response->assertSee(route('documents.download', $document));
|
||||
});
|
||||
Reference in New Issue
Block a user