- 实现基于 Laravel 11 和 Filament 3.X 的文档管理系统 - 添加用户认证和分组管理功能 - 实现文档上传、分类和权限控制 - 集成 Word 文档自动转换为 Markdown - 集成 Meilisearch 全文搜索引擎 - 实现文档在线预览功能 - 添加安全日志和审计功能 - 完整的简体中文界面 - 包含完整的项目文档和部署指南 技术栈: - Laravel 11.x - Filament 3.X - Meilisearch 1.5+ - Pandoc 文档转换 - Redis 队列系统 - Pest PHP 测试框架
125 lines
4.0 KiB
PHP
125 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Document;
|
|
use App\Models\User;
|
|
use App\Services\DocumentService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Tests\TestCase;
|
|
|
|
class DocumentFileNameTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Storage::fake('local');
|
|
}
|
|
|
|
public function test_上传文档时保留原始文件名(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$documentService = new DocumentService();
|
|
|
|
// 创建一个测试文件,使用特定的文件名
|
|
$originalFileName = '测试文档_2024.docx';
|
|
$file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
|
|
|
// 上传文档
|
|
$document = $documentService->uploadDocument(
|
|
$file,
|
|
'测试文档',
|
|
'global',
|
|
null,
|
|
$user->id
|
|
);
|
|
|
|
// 验证文件名被正确保存
|
|
$this->assertEquals($originalFileName, $document->file_name);
|
|
|
|
// 验证文件路径包含原始文件名
|
|
$this->assertStringContainsString($originalFileName, $document->file_path);
|
|
}
|
|
|
|
public function test_下载文档时使用原始文件名(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$documentService = new DocumentService();
|
|
|
|
// 创建一个测试文件
|
|
$originalFileName = '重要文档.docx';
|
|
$file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
|
|
|
// 上传文档
|
|
$document = $documentService->uploadDocument(
|
|
$file,
|
|
'重要文档',
|
|
'global',
|
|
null,
|
|
$user->id
|
|
);
|
|
|
|
// 下载文档
|
|
$response = $documentService->downloadDocument($document, $user);
|
|
|
|
// 验证响应头中的文件名(可能被 URL 编码)
|
|
$contentDisposition = $response->headers->get('Content-Disposition');
|
|
|
|
// 检查是否包含文件名(可能是原始格式或 URL 编码格式)
|
|
$encodedFileName = rawurlencode($originalFileName);
|
|
$this->assertTrue(
|
|
str_contains($contentDisposition, $originalFileName) ||
|
|
str_contains($contentDisposition, $encodedFileName),
|
|
"Content-Disposition 应该包含原始文件名或其编码版本"
|
|
);
|
|
}
|
|
|
|
public function test_中文文件名正确处理(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$documentService = new DocumentService();
|
|
|
|
// 创建一个带中文名称的测试文件
|
|
$originalFileName = '知识库管理系统需求文档.docx';
|
|
$file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
|
|
|
// 上传文档
|
|
$document = $documentService->uploadDocument(
|
|
$file,
|
|
'需求文档',
|
|
'global',
|
|
null,
|
|
$user->id
|
|
);
|
|
|
|
// 验证中文文件名被正确保存
|
|
$this->assertEquals($originalFileName, $document->file_name);
|
|
}
|
|
|
|
public function test_特殊字符文件名正确处理(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$documentService = new DocumentService();
|
|
|
|
// 创建一个带特殊字符的测试文件
|
|
$originalFileName = '文档(2024-01-01)_v1.0.docx';
|
|
$file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
|
|
|
// 上传文档
|
|
$document = $documentService->uploadDocument(
|
|
$file,
|
|
'版本文档',
|
|
'global',
|
|
null,
|
|
$user->id
|
|
);
|
|
|
|
// 验证特殊字符文件名被正确保存
|
|
$this->assertEquals($originalFileName, $document->file_name);
|
|
}
|
|
}
|