92 lines
3.0 KiB
PHP
92 lines
3.0 KiB
PHP
<?php
|
|
|
|
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('用户可以打开已转换文档的 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', $document));
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertSee($document->title);
|
|
$response->assertSee(route('documents.preview-pdf', $document));
|
|
$response->assertSee('PDF 预览', false);
|
|
});
|
|
|
|
test('预览页面正确处理 PDF 预览不可用的情况', function () {
|
|
$user = User::factory()->create();
|
|
$user->givePermissionTo('document.view');
|
|
$document = Document::factory()->create([
|
|
'conversion_status' => 'completed',
|
|
'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('PDF 预览暂不可用');
|
|
$response->assertSee('下载原始文档');
|
|
});
|
|
|
|
test('预览页面显示下载按钮', 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', $document));
|
|
|
|
$response->assertStatus(200);
|
|
$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');
|
|
});
|