user = User::factory()->create(['status' => 'active']); Permission::firstOrCreate(['name' => 'documents.access', 'guard_name' => 'web']); $this->user->givePermissionTo('documents.access'); $this->category = DocumentCategory::create(['name' => 'Test Documents']); } public function test_pdf_upload_is_stored_and_available_as_inline_preview(): void { $document = $this->upload('plan.pdf', 'application/pdf'); $response = $this->actingAs($this->user)->get(route('documents.preview', $document)); $response->assertOk(); $response->assertHeader('Content-Type', 'application/pdf'); $response->assertHeader('Content-Disposition', 'inline; filename="plan.pdf"'); Storage::disk('local')->assertExists($document->current_file_path); } public function test_image_upload_is_stored_and_available_as_inline_preview(): void { $file = UploadedFile::fake()->image('site-photo.jpg', 640, 480); $document = $this->uploadFile($file); $response = $this->actingAs($this->user)->get(route('documents.preview', $document)); $response->assertOk(); $response->assertHeader('Content-Type', 'image/jpeg'); $response->assertHeader('Content-Disposition', 'inline; filename="site-photo.jpg"'); Storage::disk('local')->assertExists($document->current_file_path); } public function test_docx_upload_is_stored_and_downloadable(): void { $document = $this->upload( 'specification.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ); $this->actingAs($this->user) ->get(route('documents.download', $document)) ->assertOk() ->assertHeader('Content-Disposition', 'attachment; filename=specification.docx'); Storage::disk('local')->assertExists($document->current_file_path); } public function test_valid_docx_is_rendered_as_html_preview(): void { $path = tempnam(sys_get_temp_dir(), 'document-preview-'); $archive = new \ZipArchive(); $archive->open($path, \ZipArchive::CREATE); $archive->addFromString('word/document.xml', 'Previewable document contentHeaderValue'); $archive->close(); $storedPath = 'documents/contracts/previewable.docx'; Storage::disk('local')->put($storedPath, file_get_contents($path)); unlink($path); $document = Document::create([ 'title' => 'Previewable DOCX', 'category_id' => $this->category->id, 'status' => 'Approved', 'uploaded_by' => $this->user->id, 'current_file_path' => $storedPath, 'current_file_name' => 'previewable.docx', 'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'file_size' => Storage::disk('local')->size($storedPath), 'version_count' => 1, ]); $this->actingAs($this->user) ->get(route('documents.preview', $document)) ->assertOk() ->assertHeader('Content-Type', 'text/html; charset=UTF-8') ->assertSee('Previewable document content') ->assertSee('', false) ->assertSee('Header') ->assertSee('Value') ->assertSee('width:8.5in'); } public function test_existing_public_disk_pdf_is_available_as_inline_preview(): void { Storage::fake('public'); $path = 'bid-submissions/legacy-submission.pdf'; Storage::disk('public')->put($path, '%PDF-1.4 legacy test file'); $document = Document::create([ 'title' => 'Legacy Bid Submission', 'category_id' => $this->category->id, 'status' => 'Approved', 'uploaded_by' => $this->user->id, 'current_file_path' => $path, 'current_file_name' => 'legacy-submission.pdf', 'mime_type' => 'application/pdf', 'file_size' => 24, 'version_count' => 1, ]); $this->actingAs($this->user) ->get(route('documents.preview', $document)) ->assertOk() ->assertHeader('Content-Type', 'application/pdf'); } private function upload(string $name, string $mimeType): Document { return $this->uploadFile(UploadedFile::fake()->create($name, 10, $mimeType)); } private function uploadFile(UploadedFile $file): Document { $this->actingAs($this->user) ->from(route('documents.index')) ->post(route('documents.upload'), [ 'title' => pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME), 'category_id' => $this->category->id, 'description' => 'Media preview test file', 'file' => $file, ]) ->assertRedirect(route('documents.index')); return Document::query()->latest('id')->firstOrFail(); } }