Files
fewo-stommer/tests/Feature/AutoOfferTest.php
T
LarsandClaude Opus 5.5 40d20cb7d1 Pakete 7 und 9: Auskunft und Buchungswunsch, Aufenthalt und Abschluss (Zwischenstand)
- Haltefrist 0: unverbindliche Auskunft mit signiertem Link "Buchung anfragen",
  erneute Prüfung unter Sperre, automatische oder manuelle Bestätigung, Absage
  bei vergebenem Zeitraum, Ablauf unbeantworteter Auskünfte
- Aufenthalt: Anreisedaten, Aufgaben je Wohnung, gebuchte Leistungen,
  Abweichungen und Folgeaufgaben als Aufgaben am Vorgang
- Abschluss getrennt: Aufenthalt abgeschlossen / kaufmännisch abgeschlossen,
  Nachläufer (Forderung, Auszahlung) bleiben sichtbar
- Zahlungsstatus nur aus Geld auf unserem Konto; Altbestand per Vorschau-Befehl
- Verlauf weist Testzustellungen als solche aus
- Abnahmetests T16-T25, T23 über isolierten SMTP-Empfänger

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:29:19 +01:00

246 lines
9.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\AcceptOffer;
use App\Actions\CreateReservation;
use App\Actions\SendOffer;
use App\Enums\BookingStatus;
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
{
Settings::tenant()->update(['process_defaults' => ['pre_reservation' => 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_waits_for_the_released_information_text_and_says_why(): void
{
Settings::tenant()->update(['process_defaults' => ['processing' => 'automatic', 'pre_reservation' => false]]);
$booking = $this->request();
// Package 7: a request without a hold is answered with an information; its texts start as a working draft.
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->assertSame(0, $booking->information_version);
$this->assertStringContainsString('auskunft unter vorbehalt', 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
{
Settings::tenant()->update(['process_defaults' => ['processing' => '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',
]);
}
}