refactor: 重构知识库文件上传和处理, 支持 pdf
This commit is contained in:
@@ -1,124 +0,0 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
<?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, '您没有权限访问此文档');
|
||||
|
||||
Reference in New Issue
Block a user