Buchungsprozess: Angebots-Snapshot, atomare Annahme, Auto-Angebot, Kanalfreigabe
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m38s

- Angebotsbedingungen und Zahlungsdaten werden je Version eingefroren (offer_terms, offer_hash)
- Annahme in einer gesperrten Transaktion, idempotent, mit Nachweis; Mail- und Portalfehler
  machen eine gespeicherte Buchung nicht ungueltig (Postfach, Nachversand)
- Je Wohnung: manuelle/automatische Angebote, optionale Vorreservierung
- Automatische Freigabe abhaengig von aktuellen Importen und Feed-Abruf bei Sofortbuchungsportalen
- Deutsche Fehlerseiten, Buchungsseite zeigt Angebots- und Zustellstatus
- Dokumentation: BOOKING-E2E-*.md, BOOKING-COMPLIANCE-HANDOVER.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-10-30 12:01:02 +01:00
1 parent e7cc37a15b
commit eafab8a2cb
47 files changed
+2099 -104

No files matched your search

+244
View File
@@ -0,0 +1,244 @@
<?php
namespace Tests\Feature;
use App\Actions\AcceptOffer;
use App\Actions\CreateReservation;
use App\Actions\SendOffer;
use App\Enums\BookingStatus;
use App\Enums\OfferMode;
use App\Exceptions\ReservationException;
use App\Mail\NewReservation;
use App\Mail\OfferMade;
use App\Mail\ReservationReceived;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\ChannelExportRequest;
use App\Models\Property;
use App\Pricing\StayRequest;
use App\Support\Content;
use App\Support\Settings;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use DateTimeInterface;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
/**
* Offer mode (manual / automatic), optional pre-reservation and the channel dependent release of automatic offers.
*/
class AutoOfferTest 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');
Http::fake(['*' => Http::response('', 500)]);
}
public function test_manual_mode_sends_the_receipt_and_no_offer(): void
{
$booking = $this->request();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertTrue($booking->isHoldActive());
Mail::assertSent(ReservationReceived::class, 1);
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => ! $mail->offerSent && $mail->blockers === []);
Mail::assertNotSent(OfferMade::class);
}
public function test_automatic_mode_sends_the_offer_instead_of_the_receipt(): void
{
$this->automatic();
$booking = $this->request();
$this->assertSame(BookingStatus::OfferSent, $booking->status);
$this->assertSame(1, $booking->offer_version);
$this->assertNotNull($booking->offer_hash);
Mail::assertSent(OfferMade::class, 1);
Mail::assertNotSent(ReservationReceived::class);
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => $mail->offerSent);
}
public function test_automatic_offer_can_be_accepted_end_to_end(): void
{
$this->automatic();
$booking = $this->request();
$this->assertTrue(app(AcceptOffer::class)->handle($booking->fresh(), 1));
$this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status);
}
public function test_without_pre_reservation_a_request_blocks_nothing(): void
{
$this->property->update(['pre_reservation_enabled' => false]);
$first = $this->request();
$second = $this->request('other@example.org');
$this->assertNull($first->hold_expires_at);
$this->assertFalse($first->isHoldActive());
$this->assertSame(0, Booking::query()->occupying()->count());
$this->assertSame(BookingStatus::Reserved, $second->status);
Mail::assertSent(ReservationReceived::class, fn (ReservationReceived $mail): bool => str_contains($mail->render(), 'nicht reserviert') && ! str_contains($mail->render(), 'vorgemerkt'));
}
public function test_automatic_mode_without_pre_reservation_falls_back_to_manual_and_says_why(): void
{
$this->property->update(['offer_mode' => OfferMode::Automatic, 'pre_reservation_enabled' => false]);
$booking = $this->request();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertStringContainsString('vorreservierung', mb_strtolower($booking->internal_notes));
Mail::assertNotSent(OfferMade::class);
Mail::assertSent(ReservationReceived::class, 1);
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => $mail->blockers !== []);
}
public function test_instant_booking_portal_holds_the_offer_back_until_it_fetched_our_feed(): void
{
$this->automatic();
$channel = $this->channel(['name' => 'Sofort-Portal', 'booking_model' => 'portal_booking']);
$booking = $this->request();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertStringContainsString('Sofort-Portal', $booking->internal_notes);
Mail::assertNotSent(OfferMade::class);
$this->travel(10)->minutes();
$this->feedFetch($channel);
$this->artisan('bookings:auto-offers')->assertSuccessful();
$this->assertSame(BookingStatus::OfferSent, $booking->fresh()->status);
Mail::assertSent(OfferMade::class, 1);
}
public function test_feed_fetch_before_the_block_or_failed_fetch_or_other_channel_is_no_proof(): void
{
$this->automatic();
$channel = $this->channel(['booking_model' => 'portal_booking']);
$other = $this->channel(['name' => 'Anderes Portal', 'booking_model' => 'inquiry']);
$this->feedFetch($channel, at: now()->subMinutes(5));
$booking = $this->request();
$this->travel(5)->minutes();
$this->feedFetch($channel, status: 503);
$this->feedFetch($other);
$this->artisan('bookings:auto-offers')->assertSuccessful();
$this->assertSame(BookingStatus::Reserved, $booking->fresh()->status);
}
public function test_portal_without_instant_booking_needs_no_proof(): void
{
$this->automatic();
$this->channel(['booking_model' => 'inquiry']);
$this->assertSame(BookingStatus::OfferSent, $this->request()->status);
}
public function test_stale_calendar_import_blocks_the_automatic_offer(): void
{
$this->automatic();
$this->channel(['booking_model' => 'inquiry', 'last_success_at' => now()->subHours(3), 'import_enabled' => true]);
$booking = $this->request();
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertStringContainsString('nicht aktuell', $booking->internal_notes);
}
public function test_unattributable_feed_request_is_not_recorded_as_proof(): void
{
$this->get('/kalender/falsches-token.ics')->assertNotFound();
$this->assertSame(0, ChannelExportRequest::query()->count());
}
public function test_acceptance_is_refused_while_a_portal_calendar_is_stale(): void
{
$offer = app(SendOffer::class)->handle(Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['arrival' => '2026-11-07', 'departure' => '2026-11-10', 'guests' => 2]));
$this->channel(['booking_model' => 'inquiry', 'last_success_at' => now()->subHours(3)]);
try {
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$this->fail('Acceptance needs a recent calendar.');
} catch (ReservationException $exception) {
$this->assertSame(ReservationException::calendarUnverified()->getMessage(), $exception->getMessage());
}
$this->assertSame(BookingStatus::OfferSent, $offer->fresh()->status);
$this->assertNull($offer->fresh()->acceptance);
}
public function test_live_operation_needs_released_contract_texts_for_automatic_offers(): void
{
$this->automatic();
Settings::tenant()->update(['operating_mode' => Settings::MODE_LIVE]);
$sample = $this->request();
$this->assertSame(BookingStatus::Reserved, $sample->status);
$this->assertStringContainsString('freigegeben', $sample->internal_notes);
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051']);
$this->assertSame(BookingStatus::OfferSent, $this->request('real@example.org', '2026-12-01', '2026-12-05')->status);
}
private function automatic(): void
{
$this->property->update(['offer_mode' => OfferMode::Automatic]);
}
private function request(string $email = 'gast@example.org', string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking
{
return app(CreateReservation::class)->handle(
$this->property->fresh(),
new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2),
['first_name' => 'Gast', 'last_name' => 'Test', 'email' => $email],
)->fresh(['guest', 'property']);
}
/**
* @param array<string, mixed> $attributes
*/
private function channel(array $attributes = []): Channel
{
return Channel::factory()->for($this->property)->create([
'last_success_at' => now(),
'export_enabled' => true,
...$attributes,
]);
}
private function feedFetch(Channel $channel, int $status = 200, ?DateTimeInterface $at = null): void
{
ChannelExportRequest::query()->create([
'tenant_id' => $channel->tenant_id,
'channel_id' => $channel->id,
'requested_at' => $at ?? now(),
'method' => 'GET',
'user_agent' => 'Portal/1.0',
'response_status' => $status,
'export_state' => $status === 200 ? 'live' : 'frozen',
]);
}
}
+334
View File
@@ -0,0 +1,334 @@
<?php
namespace Tests\Feature;
use App\Actions\AcceptOffer;
use App\Actions\ConfirmBooking;
use App\Actions\CreateChannelTasks;
use App\Actions\SendOffer;
use App\Documents\OfferTerms;
use App\Enums\BookingStatus;
use App\Enums\InboxMessageKind;
use App\Exceptions\ReservationException;
use App\Mail\BookingConfirmed;
use App\Mail\OfferAnswered;
use App\Mail\OfferMade;
use App\Models\Booking;
use App\Models\InboxMessage;
use App\Models\Property;
use App\Support\Content;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
use Tests\TestCase;
/**
* Safety core of the acceptance: frozen offer, locked and idempotent acceptance, contained mail/sync failures.
*/
class BookingAcceptanceTest extends TestCase
{
use RefreshDatabase;
private Property $property;
private Booking $booking;
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();
$this->booking = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create([
'arrival' => '2026-11-07',
'departure' => '2026-11-10',
'guests' => 2,
]);
}
public function test_offer_freezes_terms_hash_and_payment_data(): void
{
$offer = $this->makeOffer();
$this->assertSame(Content::get('contract.terms'), $offer->offer_terms['texts']['contract.terms']);
$this->assertSame(7, $offer->offer_terms['deposit_due_days']);
$this->assertSame(OfferTerms::hash($offer), $offer->offer_hash);
$this->assertSame(64, strlen($offer->offer_hash));
}
public function test_changed_website_texts_do_not_alter_a_sent_offer_nor_the_contract(): void
{
$offer = $this->makeOffer();
$original = Content::get('contract.terms');
Content::save(['contract.terms' => '<p>NEUE BEDINGUNGEN NACH VERSAND</p>']);
$this->get(app(SendOffer::class)->offerUrl($offer))->assertOk()->assertDontSee('NEUE BEDINGUNGEN NACH VERSAND');
$this->assertSame($original, $offer->fresh()->offerText('contract.terms'));
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$booking = $offer->fresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame($original, $booking->offerText('contract.terms'));
$this->assertSame(hash('sha256', json_encode($booking->offer_terms['texts'])), $booking->acceptance['terms_sha256']);
}
public function test_acceptance_records_the_evidence_and_confirms_once(): void
{
$offer = $this->makeOffer();
$this->assertTrue(app(AcceptOffer::class)->handle($offer->fresh(), 1));
$booking = $offer->fresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame($offer->offer_hash, $booking->acceptance['offer_hash']);
$this->assertSame(1, $booking->acceptance['offer_version']);
$this->assertNotNull($booking->acceptance['valid_until']);
$this->assertNotNull($booking->confirmation_sent_at);
Storage::disk('local')->assertExists($booking->contract_path);
Mail::assertSent(BookingConfirmed::class, 1);
Mail::assertSent(OfferAnswered::class, 1);
}
public function test_second_acceptance_of_the_same_version_is_idempotent(): void
{
$offer = $this->makeOffer();
$accept = app(AcceptOffer::class);
$this->assertTrue($accept->handle($offer->fresh(), 1));
$confirmedAt = $offer->fresh()->confirmed_at;
$this->travel(5)->minutes();
$this->assertFalse($accept->handle($offer->fresh(), 1));
$this->assertEquals($confirmedAt, $offer->fresh()->confirmed_at);
Mail::assertSent(BookingConfirmed::class, 1);
Mail::assertSent(OfferAnswered::class, 1);
}
public function test_stale_copy_of_the_booking_cannot_accept_again(): void
{
$offer = $this->makeOffer();
$stale = $offer->fresh();
app(AcceptOffer::class)->handle($offer->fresh(), 1);
// The second request still holds the model as it was before the first one committed.
$this->assertFalse(app(AcceptOffer::class)->handle($stale, 1));
Mail::assertSent(BookingConfirmed::class, 1);
}
public function test_tampered_offer_is_refused_without_a_trace_of_acceptance(): void
{
$offer = $this->makeOffer();
$offer->update(['total' => $offer->total - 10000]);
try {
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$this->fail('A changed offer must not be accepted.');
} catch (ReservationException $exception) {
$this->assertSame(ReservationException::offerChanged()->getMessage(), $exception->getMessage());
}
$booking = $offer->fresh();
$this->assertSame(BookingStatus::OfferSent, $booking->status);
$this->assertNull($booking->accepted_at);
$this->assertNull($booking->acceptance);
$this->assertSame(1, InboxMessage::query()->where('kind', InboxMessageKind::OfferRejected)->count());
Mail::assertNotSent(BookingConfirmed::class);
}
public function test_changed_terms_snapshot_is_refused(): void
{
$offer = $this->makeOffer();
$terms = $offer->offer_terms;
$terms['texts']['stay.cancellation'] = 'Kostenlos stornierbar';
$offer->update(['offer_terms' => $terms]);
$this->expectException(ReservationException::class);
app(AcceptOffer::class)->handle($offer->fresh(), 1);
}
public function test_conflicting_occupation_prevents_acceptance_and_leaves_no_evidence(): void
{
$offer = $this->makeOffer();
$this->property->blockedPeriods()->create(['starts_on' => '2026-11-08', 'ends_on' => '2026-11-09', 'reason' => 'Portal']);
try {
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$this->fail('A conflicting occupation must prevent the booking.');
} catch (ReservationException $exception) {
$this->assertSame(ReservationException::notAvailable()->getMessage(), $exception->getMessage());
}
$booking = $offer->fresh();
$this->assertSame(BookingStatus::OfferSent, $booking->status);
$this->assertNull($booking->accepted_at);
$this->assertNull($booking->acceptance);
}
public function test_expired_offer_cannot_be_accepted(): void
{
$offer = $this->makeOffer();
$this->travel(49)->hours();
$this->expectExceptionMessage(ReservationException::offerExpired()->getMessage());
app(AcceptOffer::class)->handle($offer->fresh(), 1);
}
public function test_failing_confirmation_mail_keeps_the_binding_booking_and_can_be_resent(): void
{
$offer = $this->makeOffer();
$mailServer = $this->useRealMailer();
$this->assertTrue(app(AcceptOffer::class)->handle($offer->fresh(), 1));
$booking = $offer->fresh();
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertNull($booking->confirmation_sent_at);
$this->assertNotNull($booking->confirmation_failed_at);
$message = InboxMessage::query()->where('kind', InboxMessageKind::ConfirmationFailed)->sole();
$this->assertStringNotContainsString('guest@example.org', $message->subject.$message->body);
$mailServer->down = false;
$this->assertTrue(app(ConfirmBooking::class)->resend($booking));
$booking = $booking->fresh();
$this->assertNotNull($booking->confirmation_sent_at);
$this->assertNull($booking->confirmation_failed_at);
$this->assertSame(1, $mailServer->delivered);
}
public function test_failing_offer_mail_is_reported_but_the_offer_stays(): void
{
$this->useRealMailer();
try {
app(SendOffer::class)->handle($this->booking->fresh());
$this->fail('The caller must learn that the mail was not sent.');
} catch (ReservationException) {
// expected
}
$this->assertSame(BookingStatus::OfferSent, $this->booking->fresh()->status);
$this->assertSame(1, InboxMessage::query()->where('kind', InboxMessageKind::OfferMailFailed)->count());
}
public function test_failing_portal_task_creation_does_not_fail_the_acceptance(): void
{
$offer = $this->makeOffer();
$this->mock(CreateChannelTasks::class)->shouldReceive('handle')->andThrow(new RuntimeException('db gone'));
$this->assertTrue(app(AcceptOffer::class)->handle($offer->fresh(), 1));
$this->assertSame(BookingStatus::Confirmed, $offer->fresh()->status);
$this->assertSame(1, InboxMessage::query()->where('kind', InboxMessageKind::SyncFailed)->count());
}
public function test_get_on_the_offer_link_never_changes_the_booking(): void
{
$offer = $this->makeOffer();
$url = app(SendOffer::class)->offerUrl($offer);
$this->get($url)->assertOk();
$this->get($url)->assertOk();
$booking = $offer->fresh();
$this->assertSame(BookingStatus::OfferSent, $booking->status);
$this->assertNull($booking->accepted_at);
}
public function test_two_overlapping_stays_cannot_both_become_binding(): void
{
$offer = $this->makeOffer();
$other = Booking::factory()->for($this->property)->reserved(now()->subHour())->create([
'arrival' => '2026-11-07', 'departure' => '2026-11-10', 'guests' => 2,
]);
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$this->expectException(ReservationException::class);
app(ConfirmBooking::class)->handle($other->fresh(), notifyGuest: false);
}
public function test_accepting_twice_over_http_shows_success_both_times_and_mails_once(): void
{
$offer = $this->makeOffer();
$page = app(SendOffer::class)->offerUrl($offer);
preg_match('~action="([^"]*/annehmen[^"]*)"~', $this->get($page)->getContent(), $match);
$action = html_entity_decode($match[1]);
$this->followingRedirects()->post($action, ['terms' => '1'])->assertOk()->assertSee('Ihre Buchung ist verbindlich');
$this->followingRedirects()->post($action, ['terms' => '1'])->assertOk()->assertSee('bereits verbindlich bestätigt');
Mail::assertSent(BookingConfirmed::class, 1);
}
public function test_invalid_signature_shows_the_german_error_page(): void
{
$offer = $this->makeOffer();
$this->get(app(SendOffer::class)->offerUrl($offer).'x')->assertForbidden()->assertSee('Link nicht mehr gültig');
}
public function test_command_retries_failed_confirmations_once_the_mail_server_is_back(): void
{
$offer = $this->makeOffer();
$mailServer = $this->useRealMailer();
app(AcceptOffer::class)->handle($offer->fresh(), 1);
$this->assertNotNull($offer->fresh()->confirmation_failed_at);
$mailServer->down = false;
$this->artisan('bookings:resend-confirmations')->assertSuccessful();
$this->assertNotNull($offer->fresh()->confirmation_sent_at);
$this->assertNull($offer->fresh()->confirmation_failed_at);
$this->assertSame(1, $mailServer->delivered);
}
/**
* Replaces the mail fake by the real (array) mailer behind a switchable "mail server".
*/
private function useRealMailer(): object
{
Mail::clearResolvedInstance('mail.manager');
$this->app->forgetInstance('mail.manager');
$this->app->forgetInstance('mailer');
$server = new class
{
public bool $down = true;
public int $delivered = 0;
};
Event::listen(MessageSending::class, function () use ($server): void {
if ($server->down) {
throw new RuntimeException('SMTP down: guest@example.org');
}
});
Event::listen(MessageSent::class, function () use ($server): void {
$server->delivered++;
});
return $server;
}
private function makeOffer(): Booking
{
$offer = app(SendOffer::class)->handle($this->booking->fresh());
Mail::assertSent(OfferMade::class);
Mail::fake();
return $offer;
}
}
+1 -3
View File
@@ -96,9 +96,7 @@ public function test_guest_accepts_the_offer_and_the_booking_becomes_binding():
Mail::assertSent(BookingConfirmed::class);
Mail::assertSent(OfferAnswered::class, fn (OfferAnswered $mail): bool => $mail->accepted);
$html = $this->get($url)->getContent();
preg_match('~<main.*</main>~s', $html, $m);
dump(strip_tags($m[0] ?? substr($html, 0, 3000)));
$this->get($url)->assertOk()->assertSee('angenommen');
}
public function test_link_of_an_older_offer_version_cannot_be_used(): void