Files
fewo-stommer/tests/Feature/ContractTemplateTest.php
LarsandClaude Opus 5.5 cf11c8a474
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m59s
Dokumentenzusammenstellung und Vorlagensteuerung (Paket 3, Prozessarchitektur v0.10)
Eine Zusammenstellung vor jedem Dokument (Angebot, Bestaetigung, Buchungsunterlagen
extern), Dokumentart je Vorgangsart, Freigaben fuer Vorlagen und Dokumentarten,
Schutz vor unvereinbarten Bedingungen bei Portalbuchungen (E-004), kein doppelter
Versand, Absicherung Hold 0 bis Paket 7, A6-Aufenthaltsbestaetigung vorbereitet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 19:44:52 +01:00

139 lines
5.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\AcceptOffer;
use App\Actions\ConfirmBooking;
use App\Actions\SendOffer;
use App\Documents\RentalContract;
use App\Filament\Resources\ContractTemplates\ContractTemplateResource;
use App\Filament\Resources\ContractTemplates\Pages\ListContractTemplates;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ContractTemplate;
use App\Models\Property;
use App\Models\User;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Contract templates with versions and the documents of record (stored PDFs with fingerprint).
*/
class ContractTemplateTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
$this->seed([PropertySeeder::class, SiteSeeder::class]);
$this->property = Property::sole();
Mail::fake();
Storage::fake('local');
$this->withoutVite();
}
public function test_offer_uses_the_chosen_template_and_freezes_it_with_name_and_version(): void
{
$template = $this->template('Portal XY', ['terms' => '<p>Bedingungen Portal XY</p>', 'cancellation' => '<p>Storno bis 30 Tage</p>']);
$offer = $this->offer(['contract_template_id' => $template->id]);
$this->assertSame('Bedingungen Portal XY', trim(strip_tags($offer->offerText('contract.terms'))));
$this->assertSame(['id' => $template->id, 'name' => 'Portal XY', 'version' => 1], $offer->offer_terms['template']);
$this->assertNotNull($offer->offer_pdf_sha256);
// blank template fields fall back to the website text
$this->assertNotEmpty($offer->offerText('contract.closing'));
}
public function test_default_order_is_explicit_choice_then_portal_then_property(): void
{
$propertyDefault = $this->template('Direkt', ['terms' => '<p>DIREKT</p>']);
$portalDefault = $this->template('Portal', ['terms' => '<p>PORTAL</p>']);
$this->property->update(['contract_template_id' => $propertyDefault->id]);
$channel = Channel::factory()->for($this->property)->create(['process_overrides' => ['booking' => ['contract_template_id' => $portalDefault->id]]]);
$direct = $this->offer();
$viaPortal = $this->offer([], ['import_channel_id' => $channel->id, 'arrival' => '2026-12-01', 'departure' => '2026-12-04']);
$this->assertSame('DIREKT', trim(strip_tags($direct->offerText('contract.terms'))));
$this->assertSame('PORTAL', trim(strip_tags($viaPortal->offerText('contract.terms'))));
}
public function test_editing_a_template_later_does_not_change_a_sent_offer_or_the_contract(): void
{
$template = $this->template('Direkt', ['terms' => '<p>VERSION EINS</p>']);
$offer = $this->offer(['contract_template_id' => $template->id]);
$template->update(['texts' => ['terms' => '<p>GEÄNDERT</p>']]);
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$this->assertSame('VERSION EINS', trim(strip_tags($offer->fresh()->offerText('contract.terms'))));
}
public function test_used_versions_are_locked_and_a_new_version_can_be_made(): void
{
$template = $this->template('Direkt', ['terms' => '<p>A</p>']);
$this->assertTrue(ContractTemplateResource::canEdit($template));
$this->offer(['contract_template_id' => $template->id]);
$this->assertFalse(ContractTemplateResource::canEdit($template->fresh()));
$this->assertFalse(ContractTemplateResource::canDelete($template->fresh()));
$this->actingAs(User::factory()->create());
Livewire::test(ListContractTemplates::class)->callAction(TestAction::make('newVersion')->table($template), []);
$this->assertSame([1, 2], ContractTemplate::query()->where('name', 'Direkt')->orderBy('version')->pluck('version')->all());
$this->assertTrue(ContractTemplateResource::canEdit(ContractTemplate::query()->where('version', 2)->sole()));
}
public function test_final_contract_is_a_document_of_record_that_is_never_rewritten(): void
{
$offer = $this->offer();
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$booking = $offer->fresh();
$content = Storage::disk('local')->get($booking->contract_path);
$this->assertSame(hash('sha256', $content), $booking->contract_pdf_sha256);
$this->travel(3)->days();
app(ConfirmBooking::class)->resend($booking);
app(RentalContract::class)->generate($booking->fresh());
$this->assertSame($content, Storage::disk('local')->get($booking->contract_path));
$this->assertSame($booking->contract_pdf_sha256, $booking->fresh()->contract_pdf_sha256);
}
/**
* @param array<string, string> $texts
*/
private function template(string $name, array $texts): ContractTemplate
{
return ContractTemplate::query()->create(['released_at' => now(), 'name' => $name, 'version' => 1, 'texts' => $texts]);
}
/**
* @param array<string, mixed> $changes
* @param array<string, mixed> $attributes
*/
private function offer(array $changes = [], array $attributes = []): Booking
{
$booking = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create([
'arrival' => '2026-11-07', 'departure' => '2026-11-10', 'guests' => 2, ...$attributes,
]);
return app(SendOffer::class)->handle($booking, $changes);
}
}