fix: 修复文档转换与预览链路中的图片、文件名和错误处理问题

This commit is contained in:
2026-04-24 10:40:29 +08:00
parent 37dd58eff0
commit e935afddfe
16 changed files with 451 additions and 69 deletions

View File

@@ -0,0 +1,101 @@
<?php
namespace Tests\Feature;
use App\Models\Document;
use App\Services\DocumentConversionService;
use App\Services\DocumentPreviewService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class DocumentPreviewFormattingTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('markdown');
config(['scout.driver' => 'null']);
}
public function test_markdown_preview_strips_internal_front_matter(): void
{
$document = Document::factory()->converted()->create([
'title' => '技术文档',
'markdown_path' => '2026/04/24/test/test.md',
]);
Storage::disk('markdown')->put($document->markdown_path, <<<'MD'
---
author: 利爪然死肥宅
source_file: /tmp/demo.docx
---
# 正文标题
这是正文内容。
MD);
$html = app(DocumentPreviewService::class)->convertMarkdownToHtml($document);
$this->assertStringContainsString('正文标题', $html);
$this->assertStringContainsString('这是正文内容。', $html);
$this->assertStringNotContainsString('author:', $html);
$this->assertStringNotContainsString('source_file:', $html);
}
public function test_markdown_preview_rewrites_media_links_to_authenticated_route(): void
{
$document = Document::factory()->converted()->create([
'title' => '技术文档',
'markdown_path' => '2026/04/24/test-links/test.md',
]);
Storage::disk('markdown')->put($document->markdown_path, <<<'MD'
# 图片示例
![示意图](media/image2.png)
MD);
Storage::disk('markdown')->put('2026/04/24/test-links/media/image2.png', 'fake-image-binary');
$html = app(DocumentPreviewService::class)->convertMarkdownToHtml($document);
$this->assertStringContainsString('/markdown-media/2026/04/24/test-links/media/image2.png', $html);
}
public function test_generated_storage_name_falls_back_to_title_for_display(): void
{
$document = Document::factory()->create([
'title' => '技术文档',
'file_name' => '01KPW4SQJTT5X15QPZ412WGSFM.docx',
'file_path' => 'documents/2026/04/23/01KPW4SQJTT5X15QPZ412WGSFM.docx',
]);
$this->assertSame('技术文档.docx', $document->display_file_name);
}
public function test_save_markdown_to_file_persists_media_assets(): void
{
$document = Document::factory()->create([
'title' => '图片文档',
]);
$path = app(DocumentConversionService::class)->saveMarkdownToFile(
$document,
"# 图片文档\n\n![示意图](media/image2.png)\n",
[
'media/image2.png' => 'image-binary',
'media/nested/image3.png' => 'nested-image-binary',
]
);
$documentDir = dirname($path);
Storage::disk('markdown')->assertExists($path);
Storage::disk('markdown')->assertExists($documentDir . '/media/image2.png');
Storage::disk('markdown')->assertExists($documentDir . '/media/nested/image3.png');
}
}

View File

@@ -141,6 +141,35 @@ class SwooleQueueCompatibilityTest extends TestCase
$job->handle($conversionService);
}
/**
* 测试队列任务可以包装底层 Throwable
*
* @test
*/
public function test_queue_job_wraps_throwables_from_conversion_service()
{
$user = User::factory()->create();
$document = Document::factory()->create([
'uploaded_by' => $user->id,
'title' => 'Throwable 测试文档',
'file_path' => 'throwable-test.docx',
]);
$conversionService = $this->createMock(DocumentConversionService::class);
$conversionService->expects($this->once())
->method('convertToMarkdown')
->willThrowException(new \Error('底层转换错误'));
$this->app->instance(DocumentConversionService::class, $conversionService);
$job = new ConvertDocumentToMarkdown($document);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('底层转换错误');
$job->handle($conversionService);
}
/**
* 测试队列任务的重试机制
*
@@ -243,4 +272,4 @@ class SwooleQueueCompatibilityTest extends TestCase
$jobDocument = $documentProperty->getValue($unserialized);
$this->assertEquals($document->id, $jobDocument->id);
}
}
}