Files
InvoiceShelf/tests/Feature/Pdf/CustomTemplateCommandTest.php
Darko Gjorgjijoski 8ab860a1ae fix(pdf): make custom templates behave the way the docs describe (#730)
Custom templates are a real feature with no test coverage at all, and several
rough edges that only show up once someone actually uses one.

make:template validated nothing. --type was checked only by the interactive
prompt, so `--type=payment` skipped the prompt and died on an uncaught
FileNotFoundException looking for payment1.blade.php: a stack trace instead of a
message. The name was not checked either, so `../escaped` wrote outside the
templates directory. Both are refused now.

Every custom template of a type shared one partials/table.blade.php. It was
written on first use and reused thereafter, so editing the items table for one
custom template silently changed it for all of them -- a file that looks
per-template and behaves globally. Each template now gets its own copy under
partials/{name}/, and its include is rewritten to match. Existing templates keep
including the old shared path, which still resolves.

A custom template sharing a built-in's name appeared twice in the picker with
the same label, and findFormattedTemplate() array_reverses and takes the first
match, so the custom one silently won. The listing is keyed by name now, so it
appears once, as the entry that will actually be used.

A custom template with no same-named .png rendered <img src=""> in the picker: a
blank tile, no error, no hint anything was missing. It falls back to the preview
of the template it was cloned from.

template_name was validated as `required` and nothing else, so any string was
accepted and stored. findFormattedTemplate() returns null for an unknown name,
the null reads as "not custom", and rendering falls through to
app.pdf.{type}.{name} -- a raw "view not found" 500 at PDF time, long after the
save that caused it. New PdfTemplateExists rule, scoped per document type.

make:template also copies a _header/_footer companion when the source template
has one, so a scaffolded template keeps the repeating page furniture.

Both getEstimateTemplateName/getInvoiceTemplateName asked for the template list
with the default image format, base64-encoding a preview of every template just
to read the names back.

Claude-Session: https://claude.ai/code/session_01QmECndmNZwzN65Zz9P87dF
2026-08-01 12:56:39 +02:00

93 lines
3.5 KiB
PHP

<?php
use App\Support\Pdf\PdfTemplateUtils;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Console\Command\Command;
/**
* make:template is the documented way to create a custom template, and had no
* test at all.
*/
beforeEach(function () {
Storage::fake('pdf_templates');
});
function customTemplatePath(string $type, string $file): string
{
return Storage::disk('pdf_templates')->path("{$type}/{$file}");
}
test('it clones a template that renders through the custom namespace', function () {
Artisan::call('make:template', ['name' => 'branded', '--type' => 'invoice']);
expect(File::exists(customTemplatePath('invoice', 'branded.blade.php')))->toBeTrue();
$markup = File::get(customTemplatePath('invoice', 'branded.blade.php'));
expect($markup)->not->toContain('app.pdf.invoice')
->and($markup)->toContain('pdf_templates::invoice');
});
/**
* Every custom template of a type used to include the same
* partials/table.blade.php, written once and then reused, so editing the table
* for one silently changed it for all of them.
*/
test('each template gets its own copy of the shared partial', function () {
Artisan::call('make:template', ['name' => 'first', '--type' => 'invoice']);
Artisan::call('make:template', ['name' => 'second', '--type' => 'invoice']);
expect(File::exists(customTemplatePath('invoice', 'partials/first/table.blade.php')))->toBeTrue()
->and(File::exists(customTemplatePath('invoice', 'partials/second/table.blade.php')))->toBeTrue();
expect(File::get(customTemplatePath('invoice', 'first.blade.php')))
->toContain('pdf_templates::invoice.partials.first.table');
});
test('a preview image is written so the picker has something to show', function () {
Artisan::call('make:template', ['name' => 'branded', '--type' => 'invoice']);
expect(File::exists(customTemplatePath('invoice', 'branded.png')))->toBeTrue();
});
test('the new template shows up in the picker', function () {
Artisan::call('make:template', ['name' => 'branded', '--type' => 'invoice']);
$names = array_column(PdfTemplateUtils::getFormattedTemplates('invoice', ''), 'name');
expect($names)->toContain('branded');
});
/**
* --type was never checked against the supported list. An unsupported value
* skipped the interactive prompt and then died on an uncaught
* FileNotFoundException looking for e.g. payment1.blade.php.
*/
test('an unsupported type is refused with a message rather than a stack trace', function () {
$exit = Artisan::call('make:template', ['name' => 'receipt', '--type' => 'payment']);
expect($exit)->toBe(Command::INVALID)
->and(Artisan::output())->toContain('Unsupported template type');
});
test('a name that would escape the templates directory is refused', function (string $name) {
$exit = Artisan::call('make:template', ['name' => $name, '--type' => 'invoice']);
expect($exit)->toBe(Command::INVALID);
})->with([
'../escaped',
'nested/path',
]);
test('an existing name is not overwritten', function () {
Artisan::call('make:template', ['name' => 'branded', '--type' => 'invoice']);
File::put(customTemplatePath('invoice', 'branded.blade.php'), 'EDITED BY USER');
$exit = Artisan::call('make:template', ['name' => 'branded', '--type' => 'invoice']);
expect($exit)->toBe(Command::INVALID)
->and(File::get(customTemplatePath('invoice', 'branded.blade.php')))->toBe('EDITED BY USER');
});