travelTo(CarbonImmutable::parse('2026-10-22 10:00')); $this->seed([PropertySeeder::class, SiteSeeder::class]); $this->property = Property::sole(); $this->portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'portal_booking', 'process_overrides' => ['booking' => ['payments' => 'portal']]]); Mail::fake(); Storage::fake('local'); Http::fake(['*' => Http::response('', 500)]); $this->withoutVite(); $this->actingAs(User::factory()->create(['name' => 'Ingrid', 'admin_mode' => AdminMode::Simple])); } /** 3.1 */ public function test_the_offer_composition_shows_everything_and_an_adjustment_updates_it(): void { $booking = $this->request(); $composition = app(DocumentComposer::class)->compose($booking, DocumentKind::Offer); $this->assertSame('Gesa Gast', $composition->recipientName()); $this->assertSame('gesa@example.org', $composition->recipientEmail()); $this->assertSame('aus Ihren Preisen berechnet', $composition->priceSource); $this->assertTrue($composition->kind->hasAcceptanceLink()); $this->assertTrue($composition->canSendAutomatically()); $total = $composition->draft->total; // The same picture the landlord sees in the dialog; an adjustment recalculates it. $adjusted = app(DocumentComposer::class)->compose($booking, DocumentKind::Offer, ['price_adjustment' => -5000, 'price_adjustment_label' => 'Stammgast']); $html = view('filament.document-composition', ['composition' => $adjusted, 'expert' => false, 'mailPreview' => '', 'documentPreview' => '', 'pdfUrl' => '#'])->render(); foreach (['Angebot mit Annahmelink', 'Gesa Gast · gesa@example.org', 'Herkunft: aus Ihren Preisen berechnet', 'Stornoregel', 'Vorlage', 'Anlagen', 'Stammgast', Money::format($total - 5000)] as $text) { $this->assertStringContainsString(e($text), $html); } Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()]) ->callAction('sendOffer', ['price_adjustment' => '-50', 'price_adjustment_label' => 'Stammgast']) ->assertHasNoFormErrors() ->assertNotified('Angebot gesendet'); $this->assertSame($total - 5000, $booking->fresh()->total); $this->assertSame('offer', $booking->events()->where('type', 'document')->sole()->data['kind']); } /** 3.2 */ public function test_each_case_gets_its_document_without_choosing_a_template(): void { $inquiryTemplate = $this->template('Direktanfrage', '
ANGEBOTSVERTRAG {{ mieter }}
'); $portalTemplate = $this->template('FeWo-direkt', 'PORTALUNTERLAGEN {{ mieter }}
', portalTerms: true); Settings::tenant()->update(['process_defaults' => ['contract_template_id' => $inquiryTemplate->id]]); $this->portal->update(['process_overrides' => ['booking' => ['payments' => 'portal', 'contract_template_id' => $portalTemplate->id]]]); DocumentReleases::set(DocumentKind::ExternalRecord, true, 'Ingrid'); $request = app(SendOffer::class)->handle($this->request()); $this->assertSame('Direktanfrage', $request->offer_terms['template']['name']); Mail::assertSent(OfferMade::class, fn (OfferMade $mail): bool => str_contains($mail->offerUrl, '/angebot/')); $portalBooking = $this->portalBooking(); $composition = app(SendBookingDocuments::class)->compose($portalBooking); $this->assertSame(DocumentKind::ExternalRecord, $composition->kind); $this->assertFalse($composition->kind->hasAcceptanceLink()); $this->assertStringContainsString('eingestellt für FeWo-direkt', $composition->templateSource); $this->assertTrue($composition->canSendAutomatically()); $this->assertTrue(app(SendBookingDocuments::class)->handle($portalBooking)); $portalBooking->refresh(); $this->assertSame('FeWo-direkt', $portalBooking->offer_terms['template']['name']); $this->assertStringEndsWith('Buchungsunterlagen-'.$portalBooking->reference.'.pdf', $portalBooking->contract_path); Mail::assertSent(BookingDocumentation::class, function (BookingDocumentation $mail): bool { $html = $mail->render(); return ! str_contains($html, '/angebot/') && str_contains($html, 'Die Zahlung läuft über FeWo-direkt') && ! str_contains($html, 'überweisen'); }); Mail::assertNotSent(BookingConfirmed::class); [$view, $data] = app(RentalContract::class)->view($portalBooking, DocumentKind::ExternalRecord); $this->assertStringContainsString('Buchungsunterlagen', view($view, $data)->render()); } /** 3.3 */ public function test_an_unconfirmed_fee_in_a_portal_template_needs_a_conscious_confirmation(): void { $policy = CancellationPolicy::query()->create(['name' => 'Direktbuchung Standard', 'base' => CancellationPolicy::BASE_RENT, 'tiers' => [['min_days' => 30, 'percent' => 0], ['min_days' => 0, 'percent' => 100]], 'replacement_fee' => 3500]); $template = $this->template('FeWo-direkt Direktbuchung', 'Mietvertrag mit {{ mieter }}. Endreinigung 60,00 € extra.
'); $template->fill(['cancellation_policy_id' => $policy->id])->applyStatements(released: true, portalTerms: false, actor: 'Ingrid'); $template->save(); $this->portal->update(['process_overrides' => ['booking' => ['payments' => 'portal', 'contract_template_id' => $template->id]]]); DocumentReleases::set(DocumentKind::ExternalRecord, true, 'Ingrid'); $booking = $this->portalBooking(); $points = app(SendBookingDocuments::class)->compose($booking)->toConfirm(); $this->assertStringContainsString('nicht als Wiedergabe der Bedingungen von FeWo-direkt bestätigt', implode(' ', $points)); $this->assertStringContainsString('Ersatzmieter-Gebühr '.Money::format(3500), implode(' ', $points)); $this->assertStringContainsString('60,00 € (im Text)', implode(' ', $points)); // Automatic: nothing goes out, the case says what to check. $booking->update(['processing_mode' => 'automatic']); $this->assertNotSame([], app(SendBookingDocuments::class)->automatically($booking->fresh())); Mail::assertNothingSent(); $this->assertStringContainsString('vor dem Versand zu prüfen', CaseActivities::next($booking->fresh())->detail); // By hand: refused without, sent with the explicit confirmation, which is recorded. try { app(SendBookingDocuments::class)->handle($booking->fresh()); $this->fail('Sending without confirmation must be refused.'); } catch (ReservationException $exception) { $this->assertStringContainsString('ausdrücklich bestätigen', $exception->getMessage()); } Mail::assertNothingSent(); Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()]) ->callAction('sendDocuments', ['acknowledged' => false]) ->assertHasFormErrors(['acknowledged']); Mail::assertNothingSent(); Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()]) ->callAction('sendDocuments', ['acknowledged' => true]) ->assertNotified('Unterlagen gesendet'); Mail::assertSent(BookingDocumentation::class, 1); $confirmed = $booking->events()->where('type', 'document')->where('summary', 'Hinweise vor dem Versand bestätigt')->sole(); $this->assertSame('Ingrid', $confirmed->actor); $this->assertStringContainsString('Ersatzmieter-Gebühr', implode(' ', $confirmed->data['confirmed'])); } public function test_without_any_template_a_portal_booking_does_not_get_our_website_terms_unchecked(): void { DocumentReleases::set(DocumentKind::ExternalRecord, true, 'Ingrid'); $booking = $this->portalBooking(); $this->assertStringContainsString('keine eigene Vorlage', implode(' ', app(SendBookingDocuments::class)->compose($booking)->toConfirm())); } public function test_documents_are_sent_once_whatever_runs_again(): void { $template = $this->template('FeWo-direkt', 'Buchung {{ mieter }}
', portalTerms: true); $this->portal->update(['process_overrides' => ['booking' => ['payments' => 'portal', 'contract_template_id' => $template->id]]]); DocumentReleases::set(DocumentKind::ExternalRecord, true, 'Ingrid'); $booking = $this->portalBooking(); $this->assertSame([], app(SendBookingDocuments::class)->automatically($booking)); $this->assertSame([], app(SendBookingDocuments::class)->automatically($booking->fresh())); $this->assertTrue(app(SendBookingDocuments::class)->handle($booking)); Mail::assertSent(BookingDocumentation::class, 1); } public function test_an_offer_is_not_sent_twice_by_a_repeated_release_or_automatic_run(): void { $booking = $this->request(); $send = app(SendOffer::class); $send->handle($booking, ['expected_version' => 0]); try { $send->handle($booking->fresh(), ['expected_version' => 0]); $this->fail('A repeated release must not send a second offer.'); } catch (ReservationException $exception) { $this->assertStringContainsString('schon ein Angebot gesendet', $exception->getMessage()); } Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic']]); app(AutoOffer::class)->handle($booking->fresh()); $this->assertSame(1, $booking->fresh()->offer_version); Mail::assertSent(OfferMade::class, 1); } /** Hold = 0 until package 7: no binding offer by accident. */ public function test_without_reservation_an_offer_with_acceptance_link_needs_an_explicit_decision(): void { Settings::tenant()->update(['process_defaults' => ['pre_reservation' => false]]); $booking = $this->request(); $this->assertNull($booking->hold_expires_at); try { app(SendOffer::class)->handle($booking); $this->fail('An offer must not be made by accident when requests reserve nothing.'); } catch (ReservationException $exception) { $this->assertStringContainsString('reservieren Anfragen den Zeitraum nicht', $exception->getMessage()); } $this->assertSame(BookingStatus::Reserved, $booking->fresh()->status); Mail::assertNothingSent(); Settings::tenant()->update(['process_defaults' => ['pre_reservation' => false, 'processing' => 'automatic']]); $this->assertNotSame([], app(AutoOffer::class)->handle($booking->fresh())); Mail::assertNothingSent(); app(SendOffer::class)->handle($booking->fresh(), ['acknowledged' => true]); $this->assertSame(BookingStatus::OfferSent, $booking->fresh()->status); } public function test_missing_contract_data_stops_the_automatic_offer(): void { $template = $this->template('Direkt', 'WLAN: {{ wohnung.wlan_passwort }}
'); Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'contract_template_id' => $template->id]]); Storage::fake('local'); $booking = $this->request(sendMails: true); $this->assertSame(BookingStatus::Reserved, $booking->fresh()->status); $this->assertStringContainsString('wohnung.wlan_passwort', implode(' ', app(AutoOffer::class)->blockers($booking->fresh()))); Mail::assertNotSent(OfferMade::class); } public function test_documents_already_sent_stay_as_they_were(): void { $template = $this->template('Direkt', 'ALTER TEXT {{ mieter }}
'); Settings::tenant()->update(['process_defaults' => ['contract_template_id' => $template->id]]); $booking = app(SendOffer::class)->handle($this->request()); $offerHash = $booking->offer_hash; app(ConfirmBooking::class)->handle($booking->fresh()); $booking->refresh(); $contract = $booking->contract_path; $pdfHash = $booking->contract_pdf_sha256; $template->fill(['body' => 'NEUER TEXT
'])->applyStatements(released: true, portalTerms: false, actor: 'Ingrid'); $template->save(); Settings::tenant()->update(['process_defaults' => ['contract_template_id' => null]]); $composition = app(SendBookingDocuments::class)->compose($booking->fresh()); $this->assertTrue($composition->termsFrozen); $this->assertStringContainsString('ALTER TEXT', $composition->terms['document']['body']); $this->assertSame([], $composition->toConfirm()); app(ConfirmBooking::class)->resend($booking->fresh()); $booking->refresh(); $this->assertSame($contract, $booking->contract_path); $this->assertSame($pdfHash, $booking->contract_pdf_sha256); $this->assertSame($offerHash, $booking->offer_hash); $this->assertStringContainsString('ALTER TEXT', $booking->offer_terms['document']['body']); } public function test_a_template_statement_lapses_when_its_text_changes(): void { $template = $this->template('Direkt', 'Text
', portalTerms: true); $this->assertTrue($template->isReleased() && $template->confirmsPortalTerms()); $template->update(['body' => 'Geänderter Text
']); $this->assertFalse($template->fresh()->isReleased()); $this->assertFalse($template->fresh()->confirmsPortalTerms()); $this->assertFalse($template->newVersion()->isReleased()); } public function test_the_landlord_releases_the_booking_documentation_on_the_settings_page(): void { $this->actingAs(User::factory()->create(['name' => 'Ingrid', 'admin_mode' => AdminMode::Expert])); $this->assertFalse(DocumentReleases::isReleased(DocumentKind::ExternalRecord)); Livewire::test(HandlingSettings::class) ->assertSee('Buchungsunterlagen (beim Portal gebucht) – freigegeben') ->fillForm(['document_releases' => ['external_record' => true]]) ->call('save') ->assertHasNoFormErrors(); $this->assertTrue(DocumentReleases::isReleased(DocumentKind::ExternalRecord)); $this->assertSame('Ingrid', DocumentReleases::decision(DocumentKind::ExternalRecord)['by']); } /** A6: a private stay gets a confirmation that is no rental contract; its text is a working draft until released. */ public function test_a_private_stay_confirmation_is_prepared_without_a_rental_contract(): void { $booking = $this->request(); $booking->forceFill(['intake' => IntakeKind::Internal])->save(); $composition = app(DocumentComposer::class)->compose($booking->fresh()); $this->assertSame(DocumentKind::StayConfirmation, $composition->kind); $this->assertFalse($composition->kind->containsContract()); $this->assertStringContainsString('Arbeitsfassung', implode(' ', $composition->toConfirm())); [$view, $data] = app(RentalContract::class)->view($booking, DocumentKind::StayConfirmation); $html = view($view, $data)->render(); $this->assertStringContainsString('nicht um einen Mietvertrag', $html); $this->assertStringNotContainsString('Mietbedingungen', $html); } public function test_the_pdf_preview_shows_the_composition_without_saving_anything(): void { $booking = $this->request(); $response = $this->get(route('filament.admin.document-preview', ['booking' => $booking->getRouteKey(), 'kind' => 'offer', 'changes' => base64_encode(json_encode(['price_adjustment' => -5000]))])); $response->assertOk()->assertHeader('Content-Type', 'application/pdf'); $this->assertSame(0, $booking->fresh()->offer_version); $this->assertNull($booking->fresh()->offer_path); } private function request(bool $sendMails = false): Booking { return app(CreateReservation::class)->handle( $this->property->fresh(), new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org'], sendMails: $sendMails, ); } private function portalBooking(): Booking { return app(TakeOverExternalBooking::class)->handle( $this->property, $this->portal, CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-12'), ['portal_total' => 97951, 'portal_reference' => 'HA-77', 'guest' => ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.org']], ); } private function template(string $name, string $body, bool $portalTerms = false): ContractTemplate { return ContractTemplate::query()->create([ 'name' => $name, 'version' => 1, 'body' => $body, 'released_at' => now(), 'portal_terms_confirmed_at' => $portalTerms ? now() : null, ]); } }