Files
fewo-stommer/tests/Feature/ContractDocumentTest.php
T
LarsandClaude Sonnet 5.5 89d569b6c8
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m38s
Vertragsvorlagen als ganzes Dokument mit Platzhaltern, Wohnungsdaten, Portal-Zuordnung
- Vorlage = kompletter Vertragstext plus Anlage mit Platzhaltern ({{ mieter }}, {{ anreise }}, {{ wohnung.x }}),
  eigene Vertrags-PDF, Anzeige auf der Angebotsseite; Text und Wohnungsdaten werden beim Angebot eingefroren
- Wohnungsdaten (WLAN, Codes, ...) als Vertragsdaten der Wohnung statt im Vorlagentext
- Je Portal eigene Vorlage fuer Anfragen und fuer beim Portal verbindliche Buchungen; direkte Bestaetigung
  nutzt dieselbe Aufloesung; Bestaetigungsmail ohne Zahlungsanweisung bei Portalzahlung
- Befehl contracts:install-fewo-direkt legt die FeWo-direkt-Vorlage aus dem Vertragstext des Vermieters an

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 16:04:30 +01:00

154 lines
7.3 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\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->update(['texts' => ['terms' => '<p>NUR BAUSTEIN</p>']]);
$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',
'contract_template_id' => $inquiry->id,
'contract_template_reservation_id' => $reservation->id,
]);
$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);
Mail::assertSent(BookingConfirmed::class, fn (BookingConfirmed $mail): bool => ! str_contains($mail->render(), 'überweisen'));
$inquiryPortal = Channel::factory()->for($this->property)->create(['booking_model' => 'inquiry', 'contract_template_id' => $inquiry->id, 'contract_template_reservation_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(['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);
}
}