Files
fewo-stommer/tests/Feature/ContractDocumentTest.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

157 lines
7.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ConfirmBooking;
use App\Actions\SendOffer;
use App\Documents\ContractPlaceholders;
use App\Documents\OfferTerms;
use App\Enums\BookingStatus;
use App\Mail\BookingConfirmed;
use App\Mail\BookingDocumentation;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ContractTemplate;
use App\Models\Property;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
/**
* Contract templates as complete documents with placeholders, property facts and the portal assignment.
*/
class ContractDocumentTest 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();
$this->property->update(['contract_data' => ['wlan_passwort' => 'geheim&<1>']]);
Mail::fake();
Storage::fake('local');
$this->withoutVite();
}
public function test_placeholders_are_filled_escaped_and_unknown_ones_stay_visible(): void
{
$booking = $this->booking();
$html = '<p>{{ mieter }} reist {{ anreise_datum }} an, {{ naechte }} Nächte, {{ wohnung.wlan_passwort }} {{ gibtsnicht }}</p>';
$out = ContractPlaceholders::render($html, $booking, ['wlan_passwort' => 'geheim&<1>']);
$this->assertStringContainsString($booking->guest->full_name, $out);
$this->assertStringContainsString('07.11.2026 an, 3 Nächte', $out);
$this->assertStringContainsString('geheim&amp;&lt;1&gt;', $out);
$this->assertStringContainsString('[<b>gibtsnicht?</b>]', $out);
$this->assertSame(['gibtsnicht'], ContractPlaceholders::missing($html, $booking, ['wlan_passwort' => 'x']));
}
public function test_offer_with_a_document_template_freezes_body_and_property_facts_and_renders_the_contract(): void
{
$template = $this->template('Direkt', '<h3>§ 1</h3><p>Mieter {{ mieter }}, WLAN {{ wohnung.wlan_passwort }}</p>', 'Anlage Haus', '<p>Anlagentext {{ buchungsnummer }}</p>');
$offer = $this->offer(['contract_template_id' => $template->id]);
$this->assertSame($template->body, $offer->offer_terms['document']['body']);
$this->assertSame('geheim&<1>', $offer->offer_terms['property_data']['wlan_passwort']);
$document = OfferTerms::document($offer);
$this->assertStringContainsString($offer->guest->full_name, $document['body']);
$this->assertStringContainsString($offer->reference, $document['annex']);
$this->get(app(SendOffer::class)->offerUrl($offer))->assertOk()->assertSee('Mietvertrag')->assertSee('Anlage Haus')->assertSee($offer->guest->full_name);
Storage::disk('local')->assertExists($offer->offer_path);
// later changes of the template or the property facts do not touch the sent offer
$template->update(['body' => '<p>GEÄNDERT</p>']);
$this->property->update(['contract_data' => ['wlan_passwort' => 'neu']]);
$this->assertStringContainsString($offer->guest->full_name, OfferTerms::document($offer->fresh())['body']);
$this->assertStringNotContainsString('GEÄNDERT', OfferTerms::document($offer->fresh())['body']);
}
public function test_template_without_a_body_keeps_the_text_blocks(): void
{
$template = $this->template('Bausteine', null, null, null);
$template->fill(['texts' => ['terms' => '<p>NUR BAUSTEIN</p>']])->applyStatements(released: true, portalTerms: false, actor: 'Test');
$template->save();
$offer = $this->offer(['contract_template_id' => $template->id]);
$this->assertNull(OfferTerms::document($offer));
$this->assertStringContainsString('NUR BAUSTEIN', $offer->offerText('contract.terms'));
}
public function test_portal_reservation_uses_the_reservation_template_and_a_confirmation_without_payment_instructions(): void
{
$inquiry = $this->template('Anfrage', '<p>ANFRAGE-VERTRAG</p>', null, null);
$reservation = $this->template('FeWo-direkt', '<p>PORTAL-VERTRAG {{ mieter }}</p>', null, null);
$portal = Channel::factory()->for($this->property)->create([
'booking_model' => 'portal_booking',
'process_overrides' => ['inquiry' => ['contract_template_id' => $inquiry->id], 'booking' => ['contract_template_id' => $reservation->id, 'payments' => 'portal']],
]);
$booking = $this->booking(['import_channel_id' => $portal->id, 'source' => 'portal']);
app(ConfirmBooking::class)->handle($booking, notifyGuest: true);
$booking = $booking->fresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame('FeWo-direkt', $booking->offer_terms['template']['name']);
$this->assertStringContainsString('PORTAL-VERTRAG', OfferTerms::document($booking)['body']);
Storage::disk('local')->assertExists($booking->contract_path);
// Binding on the portal: booking documentation without payment instructions (payments run via the portal).
Mail::assertSent(BookingDocumentation::class, fn (BookingDocumentation $mail): bool => ! str_contains($mail->render(), 'überweisen') && str_contains($mail->render(), 'Die Zahlung läuft über'));
Mail::assertNotSent(BookingConfirmed::class);
$inquiryPortal = Channel::factory()->for($this->property)->create(['booking_model' => 'inquiry', 'process_overrides' => ['inquiry' => ['contract_template_id' => $inquiry->id], 'booking' => ['contract_template_id' => $reservation->id]]]);
$this->assertSame($inquiry->id, OfferTerms::templateFor($this->booking(['import_channel_id' => $inquiryPortal->id, 'source' => 'portal', 'arrival' => '2026-12-01', 'departure' => '2026-12-03']))->id);
}
public function test_installer_creates_the_fewo_direkt_template_once_and_sets_property_facts_without_storing_them_in_code(): void
{
$this->artisan('contracts:install-fewo-direkt', ['--data' => ['wlan_benutzer=Gast1', 'kellercode=1234']])->assertSuccessful();
$this->artisan('contracts:install-fewo-direkt')->assertSuccessful();
$template = ContractTemplate::query()->where('name', 'FeWo-direkt Direktbuchung')->sole();
$this->assertTrue($template->hasDocument());
$this->assertStringContainsString('{{ wohnung.kellercode }}', $template->body);
$this->assertStringNotContainsString('0050', $template->body.$template->annex_body);
$this->assertStringNotContainsString('ostseeurlaub', $template->body.$template->annex_body);
$data = $this->property->fresh()->contract_data;
$this->assertSame('1234', $data['kellercode']);
$this->assertSame('geheim&<1>', $data['wlan_passwort']);
}
private function template(string $name, ?string $body, ?string $annexTitle, ?string $annexBody): ContractTemplate
{
return ContractTemplate::query()->create(['released_at' => now(), 'name' => $name, 'version' => 1, 'body' => $body, 'annex_title' => $annexTitle, 'annex_body' => $annexBody]);
}
/**
* @param array<string, mixed> $attributes
*/
private function booking(array $attributes = []): Booking
{
return Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create([
'arrival' => '2026-11-07', 'departure' => '2026-11-10', 'guests' => 2, ...$attributes,
]);
}
/**
* @param array<string, mixed> $changes
*/
private function offer(array $changes): Booking
{
return app(SendOffer::class)->handle($this->booking(), $changes);
}
}