Files
KnowledgeBase/tests/Feature/DocumentServiceTest.php
Knowledge Base System acf549c43c feat: 初始化知识库系统项目
- 实现基于 Laravel 11 和 Filament 3.X 的文档管理系统
- 添加用户认证和分组管理功能
- 实现文档上传、分类和权限控制
- 集成 Word 文档自动转换为 Markdown
- 集成 Meilisearch 全文搜索引擎
- 实现文档在线预览功能
- 添加安全日志和审计功能
- 完整的简体中文界面
- 包含完整的项目文档和部署指南

技术栈:
- Laravel 11.x
- Filament 3.X
- Meilisearch 1.5+
- Pandoc 文档转换
- Redis 队列系统
- Pest PHP 测试框架
2025-12-05 14:44:44 +08:00

118 lines
3.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use App\Models\Document;
use App\Models\Group;
use App\Models\User;
use App\Services\DocumentService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
uses(RefreshDatabase::class);
beforeEach(function () {
Storage::fake('local');
$this->service = new DocumentService();
});
test('可以上传有效的 Word 文档', function () {
$user = User::factory()->create();
$file = UploadedFile::fake()->create('test.docx', 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$document = $this->service->uploadDocument(
$file,
'测试文档',
'global',
null,
$user->id
);
expect($document)->toBeInstanceOf(Document::class);
expect($document->title)->toBe('测试文档');
expect($document->type)->toBe('global');
expect($document->uploaded_by)->toBe($user->id);
});
test('上传非 Word 文档应该抛出异常', function () {
$user = User::factory()->create();
$file = UploadedFile::fake()->create('test.pdf', 100, 'application/pdf');
$this->service->uploadDocument(
$file,
'测试文档',
'global',
null,
$user->id
);
})->throws(\InvalidArgumentException::class, '文件格式不支持,请上传 Word 文档(.doc 或 .docx');
test('专用文档没有分组应该抛出异常', function () {
$user = User::factory()->create();
$file = UploadedFile::fake()->create('test.docx', 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$this->service->uploadDocument(
$file,
'测试文档',
'dedicated',
null,
$user->id
);
})->throws(\InvalidArgumentException::class, '专用知识库文档必须指定所属分组');
test('用户可以访问全局文档', function () {
$user = User::factory()->create();
$document = Document::factory()->global()->create();
$hasAccess = $this->service->validateDocumentAccess($document, $user);
expect($hasAccess)->toBeTrue();
});
test('用户可以访问自己分组的专用文档', function () {
$group = Group::factory()->create();
$user = User::factory()->create();
$user->groups()->attach($group->id);
$document = Document::factory()->dedicated($group->id)->create();
$hasAccess = $this->service->validateDocumentAccess($document, $user);
expect($hasAccess)->toBeTrue();
});
test('用户不能访问其他分组的专用文档', function () {
$group1 = Group::factory()->create();
$group2 = Group::factory()->create();
$user = User::factory()->create();
$user->groups()->attach($group1->id);
$document = Document::factory()->dedicated($group2->id)->create();
$hasAccess = $this->service->validateDocumentAccess($document, $user);
expect($hasAccess)->toBeFalse();
});
test('可以记录文档下载日志', function () {
$user = User::factory()->create();
$document = Document::factory()->global()->create();
$log = $this->service->logDownload($document, $user, '127.0.0.1');
expect($log->document_id)->toBe($document->id);
expect($log->user_id)->toBe($user->id);
expect($log->ip_address)->toBe('127.0.0.1');
expect($log->downloaded_at)->not->toBeNull();
});
test('下载文档时验证权限', function () {
$group = Group::factory()->create();
$user = User::factory()->create();
// 用户不属于该分组
$document = Document::factory()->dedicated($group->id)->create();
$this->service->downloadDocument($document, $user);
})->throws(\Exception::class, '您没有权限访问此文档');