fix: use pdf previews for documents
This commit is contained in:
@@ -4,9 +4,8 @@ namespace Tests\Feature;
|
||||
|
||||
use App\Models\Document;
|
||||
use App\Models\User;
|
||||
use App\Services\DocumentPreviewService;
|
||||
use App\Services\DocumentPdfPreviewService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -14,56 +13,79 @@ class DocumentPreviewServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected DocumentPreviewService $previewService;
|
||||
protected DocumentPdfPreviewService $previewService;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->previewService = new DocumentPreviewService();
|
||||
$this->previewService = new DocumentPdfPreviewService();
|
||||
Storage::fake('local');
|
||||
Storage::fake('previews');
|
||||
config(['scout.driver' => 'null']);
|
||||
}
|
||||
|
||||
public function test_可以检查文档是否支持预览(): void
|
||||
public function test_可以检查文档是否支持_pdf_预览(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
// 创建一个 .docx 文档
|
||||
|
||||
$document = Document::factory()->create([
|
||||
'file_name' => 'test.docx',
|
||||
'uploaded_by' => $user->id,
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->previewService->canPreview($document));
|
||||
|
||||
// 创建一个 .doc 文档
|
||||
$document2 = Document::factory()->create([
|
||||
'file_name' => 'test.doc',
|
||||
'uploaded_by' => $user->id,
|
||||
]);
|
||||
|
||||
$this->assertTrue($this->previewService->canPreview($document2));
|
||||
|
||||
// 创建一个不支持的格式
|
||||
$document3 = Document::factory()->create([
|
||||
'conversion_status' => 'completed',
|
||||
'file_path' => 'documents/test.pdf',
|
||||
'file_name' => 'test.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
'uploaded_by' => $user->id,
|
||||
]);
|
||||
|
||||
$this->assertFalse($this->previewService->canPreview($document3));
|
||||
|
||||
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
|
||||
|
||||
$this->assertTrue($this->previewService->canPreview($document));
|
||||
|
||||
$document2 = Document::factory()->create([
|
||||
'conversion_status' => 'pending',
|
||||
'file_path' => 'documents/pending.pdf',
|
||||
'file_name' => 'pending.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
'uploaded_by' => $user->id,
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put($document2->file_path, '%PDF-1.4 test');
|
||||
|
||||
$this->assertFalse($this->previewService->canPreview($document2));
|
||||
}
|
||||
|
||||
public function test_文档不存在时抛出异常(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$document = Document::factory()->create([
|
||||
'file_path' => 'documents/2024/01/01/nonexistent.docx',
|
||||
'file_name' => 'nonexistent.docx',
|
||||
'conversion_status' => 'completed',
|
||||
'file_path' => 'documents/2024/01/01/nonexistent.pdf',
|
||||
'file_name' => 'nonexistent.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
'uploaded_by' => $user->id,
|
||||
]);
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('文档文件不存在');
|
||||
|
||||
$this->previewService->convertToHtml($document);
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('文档尚未完成转换或原文件不存在');
|
||||
|
||||
$this->previewService->getPreviewPath($document);
|
||||
}
|
||||
|
||||
public function test_非_pdf_文件在缺少_libreoffice_时给出明确错误(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$document = Document::factory()->create([
|
||||
'conversion_status' => 'completed',
|
||||
'file_path' => 'documents/test.docx',
|
||||
'file_name' => 'test.docx',
|
||||
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'uploaded_by' => $user->id,
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put($document->file_path, 'fake-docx-content');
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('LibreOffice');
|
||||
|
||||
$this->previewService->getPreviewPath($document);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user