fix: 修复文档转换与预览链路中的图片、文件名和错误处理问题
This commit is contained in:
101
tests/Feature/DocumentPreviewFormattingTest.php
Normal file
101
tests/Feature/DocumentPreviewFormattingTest.php
Normal 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'
|
||||
# 图片示例
|
||||
|
||||

|
||||
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\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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user