documents

This commit is contained in:
Simon Pocrnjič
2025-10-12 12:24:17 +02:00
parent 3ab1c05fcc
commit e0303ece74
22 changed files with 898 additions and 88 deletions
@@ -0,0 +1,62 @@
<?php
namespace Tests\Feature;
use App\Models\Contract;
use App\Models\DocumentTemplate;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class DocumentCustomTokensTest extends TestCase
{
public function test_custom_tokens_defaults_and_overrides(): void
{
Storage::fake('public');
$user = User::factory()->create();
$role = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
$user->roles()->sync([$role->id]);
$this->actingAs($user);
// Minimal docx with custom tokens
$tmp = tempnam(sys_get_temp_dir(), 'doc');
$zip = new \ZipArchive;
$zip->open($tmp, \ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', '<Types></Types>');
$zip->addFromString('word/document.xml', '<w:document><w:body>{{custom.order_id}}|{{custom.missing}}</w:body></w:document>');
$zip->close();
$bytes = file_get_contents($tmp);
$upload = UploadedFile::fake()->createWithContent('template.docx', $bytes);
// Store template via controller to ensure tokens/metadata recorded
$resp = $this->post(route('admin.document-templates.store'), [
'name' => 'Custom Test',
'slug' => 'custom-test',
'file' => $upload,
]);
$resp->assertRedirect();
// Add defaults via meta
$template = DocumentTemplate::where('slug', 'custom-test')->orderByDesc('version')->firstOrFail();
$template->meta = ['custom_defaults' => ['order_id' => 'DEF-123']];
$template->save();
$contract = Contract::factory()->create();
// Call generation with override for order_id; missing should become empty by default
$gen = $this->postJson(route('contracts.generate-document', ['contract' => $contract->uuid]), [
'template_slug' => 'custom-test',
'custom' => ['order_id' => 'OVR-999'],
]);
$gen->assertOk()->assertJson(['status' => 'ok']);
$doc = \App\Models\Document::latest('id')->first();
$this->assertNotNull($doc);
// For strictness, we could open the docx to verify replacements, but that is heavy.
// A lighter check: ensure no unresolved/fail occurred and file exists.
$this->assertTrue(Storage::disk('public')->exists($doc->path));
}
}
@@ -0,0 +1,90 @@
<?php
namespace Tests\Feature;
use App\Models\Contract;
use App\Models\DocumentTemplate;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class DocumentCustomTokensTypesTest extends TestCase
{
public function test_typed_custom_tokens_are_formatted(): void
{
Storage::fake('public');
$user = User::factory()->create();
$role = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
$user->roles()->sync([$role->id]);
$this->actingAs($user);
// Minimal docx with custom tokens for number and date
$tmp = tempnam(sys_get_temp_dir(), 'doc');
$zip = new \ZipArchive;
$zip->open($tmp, \ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', '<Types></Types>');
$zip->addFromString('word/document.xml', '<w:document><w:body>{{custom.total_amount}}|{{custom.due_date}}</w:body></w:document>');
$zip->close();
$bytes = file_get_contents($tmp);
$upload = UploadedFile::fake()->createWithContent('typed.docx', $bytes);
// Create template
$resp = $this->post(route('admin.document-templates.store'), [
'name' => 'Typed Custom',
'slug' => 'typed-custom',
'file' => $upload,
]);
$resp->assertRedirect();
$template = DocumentTemplate::where('slug', 'typed-custom')->orderByDesc('version')->firstOrFail();
$template->meta = [
'custom_defaults' => [
'total_amount' => null,
'due_date' => null,
],
'custom_default_types' => [
'total_amount' => 'number',
'due_date' => 'date',
],
];
$template->formatting_options = [
'number_decimals' => 2,
'decimal_separator' => ',',
'thousands_separator' => '.',
// date format may be controlled via template->date_format or formatting_options['default_date_format']
'default_date_format' => 'd.m.Y',
];
$template->save();
$contract = Contract::factory()->create();
// Generate with explicit overrides
$gen = $this->postJson(route('contracts.generate-document', ['contract' => $contract->uuid]), [
'template_slug' => 'typed-custom',
'custom' => [
'total_amount' => '1234.5',
'due_date' => '2025-10-12',
],
]);
$gen->assertOk()->assertJson(['status' => 'ok']);
$doc = \App\Models\Document::latest('id')->first();
$this->assertNotNull($doc);
$this->assertTrue(Storage::disk('public')->exists($doc->path));
// Inspect generated document.xml to assert formatted values exist
$path = Storage::disk('public')->path($doc->path);
$z = new \ZipArchive;
$this->assertTrue($z->open($path) === true);
$xml = $z->getFromName('word/document.xml');
$z->close();
$this->assertIsString($xml);
// Expect EU formatted number and date
$this->assertStringContainsString('1.234,50', $xml);
$this->assertStringContainsString('12.10.2025', $xml);
}
}
@@ -0,0 +1,80 @@
<?php
namespace Tests\Feature;
use App\Models\Client;
use App\Models\ClientCase;
use App\Models\Contract;
use App\Models\DocumentTemplate;
use App\Models\Person\Person;
use App\Models\Person\PersonAddress;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class DocumentNestedEntitiesTest extends TestCase
{
public function test_client_person_and_person_address_tokens(): void
{
Storage::fake('public');
$user = User::factory()->create();
$role = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
$user->roles()->sync([$role->id]);
$this->actingAs($user);
// Build a simple DOCX containing nested tokens
$tmp = tempnam(sys_get_temp_dir(), 'doc');
$zip = new \ZipArchive;
$zip->open($tmp, \ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', '<Types></Types>');
$zip->addFromString('word/document.xml', '<w:document><w:body>{{client.person.full_name}}|{{person.person_address.address}},{{person.person_address.post_code}} {{person.person_address.city}}</w:body></w:document>');
$zip->close();
$bytes = file_get_contents($tmp);
$upload = UploadedFile::fake()->createWithContent('nested.docx', $bytes);
$resp = $this->post(route('admin.document-templates.store'), [
'name' => 'Nested',
'slug' => 'nested',
'file' => $upload,
]);
$resp->assertRedirect();
$template = DocumentTemplate::where('slug', 'nested')->orderByDesc('version')->firstOrFail();
// Create models
$person = Person::factory()->create(['full_name' => 'Jane Doe']);
$type = \App\Models\Person\AddressType::factory()->create();
PersonAddress::create([
'person_id' => $person->id,
'address' => 'Main 1',
'post_code' => '1000',
'city' => 'Ljubljana',
'type_id' => $type->id,
'active' => 1,
]);
$client = Client::factory()->create(['person_id' => $person->id]);
$case = ClientCase::factory()->create(['client_id' => $client->id, 'person_id' => $person->id]);
$contract = Contract::factory()->create(['client_case_id' => $case->id]);
$gen = $this->postJson(route('contracts.generate-document', ['contract' => $contract->uuid]), [
'template_slug' => 'nested',
]);
$gen->assertOk()->assertJson(['status' => 'ok']);
$doc = \App\Models\Document::latest('id')->first();
$this->assertTrue(Storage::disk('public')->exists($doc->path));
// Inspect XML
$path = Storage::disk('public')->path($doc->path);
$z = new \ZipArchive;
$this->assertTrue($z->open($path) === true);
$xml = $z->getFromName('word/document.xml');
$z->close();
$this->assertIsString($xml);
$this->assertStringContainsString('Jane Doe', $xml);
$this->assertStringContainsString('Main 1,1000 Ljubljana', $xml);
}
}