fix: use pdf previews for documents
This commit is contained in:
@@ -4,51 +4,66 @@ use App\Models\Document;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
Storage::fake('local');
|
||||
Storage::fake('previews');
|
||||
config(['scout.driver' => 'null']);
|
||||
|
||||
Permission::findOrCreate('document.view', 'web');
|
||||
});
|
||||
|
||||
test('用户可以预览已转换的文档', function () {
|
||||
test('用户可以打开已转换文档的 PDF 预览页', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->givePermissionTo('document.view');
|
||||
$document = Document::factory()->create([
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => 'markdown/test.md',
|
||||
'file_path' => 'documents/test.pdf',
|
||||
'file_name' => 'test.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put($document->markdown_path, '# 测试标题\n\n这是测试内容。');
|
||||
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
|
||||
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($document->title);
|
||||
$response->assertSee('测试标题');
|
||||
$response->assertSee(route('documents.preview-pdf', $document));
|
||||
$response->assertSee('PDF 预览', false);
|
||||
});
|
||||
|
||||
test('预览页面正确处理 Markdown 内容为空的情况', function () {
|
||||
test('预览页面正确处理 PDF 预览不可用的情况', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->givePermissionTo('document.view');
|
||||
$document = Document::factory()->create([
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => null,
|
||||
'file_path' => 'documents/missing.pdf',
|
||||
'file_name' => 'missing.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Markdown 内容为空');
|
||||
$response->assertSee('PDF 预览暂不可用');
|
||||
$response->assertSee('下载原始文档');
|
||||
});
|
||||
|
||||
test('预览页面显示下载按钮', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->givePermissionTo('document.view');
|
||||
$document = Document::factory()->create([
|
||||
'conversion_status' => 'completed',
|
||||
'markdown_path' => 'markdown/test.md',
|
||||
'file_path' => 'documents/test.pdf',
|
||||
'file_name' => 'test.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put($document->markdown_path, '# 测试');
|
||||
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
|
||||
|
||||
$response = $this->actingAs($user)->get(route('documents.preview', $document));
|
||||
|
||||
@@ -56,3 +71,21 @@ test('预览页面显示下载按钮', function () {
|
||||
$response->assertSee('下载原文档');
|
||||
$response->assertSee(route('documents.download', $document));
|
||||
});
|
||||
|
||||
test('PDF 原文件直接以内联 PDF 响应预览', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->givePermissionTo('document.view');
|
||||
$document = Document::factory()->create([
|
||||
'conversion_status' => 'completed',
|
||||
'file_path' => 'documents/test.pdf',
|
||||
'file_name' => 'test.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
]);
|
||||
|
||||
Storage::disk('local')->put($document->file_path, '%PDF-1.4 test');
|
||||
|
||||
$response = $this->actingAs($user)->get(route('documents.preview-pdf', $document));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertHeader('content-type', 'application/pdf');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user