create();
$role = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
$user->roles()->sync([$role->id]);
$this->actingAs($user);
$docxV1 = $this->fakeDocxWithBody('{{contract.reference}}');
$this->post(route('admin.document-templates.store'), [
'name' => 'Povzetek pogodbe',
'slug' => 'contract-summary',
'file' => $docxV1,
])->assertRedirect();
$this->assertDatabaseHas('document_templates', [
'slug' => 'contract-summary',
'version' => 1,
]);
$docxV2 = $this->fakeDocxWithBody('{{contract.reference}} {{generation.date}}');
$this->post(route('admin.document-templates.store'), [
'name' => 'Povzetek pogodbe',
'slug' => 'contract-summary',
'file' => $docxV2,
])->assertRedirect();
$this->assertDatabaseHas('document_templates', [
'slug' => 'contract-summary',
'version' => 2,
]);
$this->assertEquals(2, \App\Models\DocumentTemplate::where('slug', 'contract-summary')->count());
}
private function fakeDocxWithBody(string $body): UploadedFile
{
$tmp = tempnam(sys_get_temp_dir(), 'docx');
$zip = new \ZipArchive;
$zip->open($tmp, \ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', '');
$zip->addFromString('word/document.xml', ''.$body.'');
$zip->close();
return UploadedFile::fake()->createWithContent('template.docx', file_get_contents($tmp));
}
}