Files
fewo-stommer/tests/Feature/BookingAcceptanceTest.php
LarsandClaude Sonnet 5.5 eafab8a2cb
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m38s
Buchungsprozess: Angebots-Snapshot, atomare Annahme, Auto-Angebot, Kanalfreigabe
- 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>
2026-10-30 12:01:02 +01:00

335 lines
13 KiB
PHP

<?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;
}
}