Files
fewo-stommer/tests/Feature/PortalBookingTest.php
T
LarsandClaude Sonnet 5.5 81c025f1ee
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m34s
Akte auch fuer Portal-Reservierungen aus dem Kalender, Ablauf mit Aktivitaeten, Verlauf, Standardprozess je Portal
- Akte anlegen aus einer importierten Portal-Reservierung (Belegung bleibt durchgehend belegt, Kalendereintrag wird Spiegelung)
- Akte: Standardprozess (Portal/Vorlage/Storno/Anlagen), Meilensteine mit Aktivitaeten und Aktionshinweis, chronologischer Verlauf
- Kanalformular: Abschnitt Standardprozess je Buchungstyp mit Anzeige, was die Vorlage mitbringt
- Portalpreis als gemeinsame Logik (PortalPrice)

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

286 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\OpenPortalFile;
use App\Actions\ProvideGuestContact;
use App\Actions\RecordPortalBooking;
use App\Actions\ResolveChannelOccupation;
use App\Actions\SetBookingStatus;
use App\Availability\AvailabilityChecker;
use App\Enums\BookingStatus;
use App\Enums\ChannelBookingModel;
use App\Enums\OccupationKind;
use App\Exceptions\ReservationException;
use App\Filament\Resources\Bookings\Pages\CreateBooking;
use App\Filament\Resources\Bookings\Pages\ListBookings;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\PortalOccupations\Pages\ViewPortalOccupation;
use App\Mail\BookingConfirmed;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Support\BookingProgress;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Database\Seeders\SiteSeeder;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Portal bookings entered without contact data, the contract process that follows (automatic or by hand),
* manual status changes, the archive and the clarification of portal occupations.
*/
class PortalBookingTest extends TestCase
{
use RefreshDatabase;
private Property $property;
private Channel $portal;
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->portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => ChannelBookingModel::PortalBooking]);
Mail::fake();
Storage::fake('local');
$this->withoutVite();
}
public function test_a_portal_booking_without_contact_data_occupies_the_dates_but_creates_no_contract(): void
{
$booking = $this->record(RecordPortalBooking::CONFIRM, total: 123400, reference: 'HA-123');
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame($this->portal->id, $booking->origin_channel_id);
$this->assertSame('HA-123', $booking->portal_reference);
$this->assertSame(123400, $booking->total);
$this->assertSame('', $booking->guest->email);
$this->assertNull($booking->contract_path);
$this->assertSame(BookingProgress::PORTAL_BOOKING, BookingProgress::kind($booking));
$this->assertSame('Kontaktdaten fehlen', BookingProgress::next($booking)['title']);
Mail::assertNothingSent();
}
public function test_an_immediate_contract_needs_the_email_address(): void
{
$this->expectException(ReservationException::class);
$this->record(RecordPortalBooking::CONTRACT);
}
public function test_entering_the_contact_data_can_start_the_contract_automatically(): void
{
$booking = $this->record(RecordPortalBooking::CONFIRM);
$message = app(ProvideGuestContact::class)->handle($booking, ['email' => 'Gast@Example.test', 'street' => 'Hauptstr. 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn'], ProvideGuestContact::AUTOMATIC, guestConfirmed: true);
$booking->refresh();
$this->assertSame('Kontaktdaten gespeichert, Vertrag gesendet', $message);
$this->assertSame('gast@example.test', $booking->guest->email);
$this->assertNotNull($booking->contract_path);
$this->assertNotNull($booking->guest_confirmed_at);
Mail::assertSent(BookingConfirmed::class, 1);
}
public function test_entering_the_contact_data_by_hand_sends_nothing(): void
{
$booking = $this->record(RecordPortalBooking::CONFIRM);
app(ProvideGuestContact::class)->handle($booking, ['email' => 'gast@example.test'], ProvideGuestContact::MANUAL);
$this->assertNull($booking->fresh()->contract_path);
$this->assertSame('Vertrag senden', BookingProgress::next($booking->fresh())['title']);
Mail::assertNothingSent();
}
public function test_a_recorded_request_stays_open_and_automatic_continuation_needs_an_email(): void
{
$booking = $this->record(RecordPortalBooking::RECORD);
$this->assertSame(BookingStatus::Reserved, $booking->status);
$this->expectException(ReservationException::class);
app(ProvideGuestContact::class)->handle($booking, ['phone' => '0123'], ProvideGuestContact::AUTOMATIC);
}
public function test_the_booking_form_records_a_portal_booking_and_the_page_offers_the_contact_actions(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(CreateBooking::class)
->fillForm([
'property_id' => $this->property->id, 'guests' => 2, 'arrival' => '2026-11-07', 'departure' => '2026-11-10',
'channel_id' => $this->portal->id, 'portal_reference' => 'HA-9', 'portal_process' => RecordPortalBooking::CONFIRM,
'first_name' => 'Erika', 'last_name' => 'Muster',
])
->call('create')
->assertHasNoFormErrors();
$booking = Booking::query()->sole();
$this->assertSame('HA-9', $booking->portal_reference);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertActionVisible('requestContact')
->callAction('requestContact')
->assertNotified('Als angefragt vermerkt')
->callAction('provideContact', ['guest' => ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.test'], 'process' => ProvideGuestContact::MANUAL, 'guest_confirmed' => false])
->assertNotified('Kontaktdaten gespeichert');
$this->assertNotNull($booking->fresh()->contact_requested_at);
$this->assertSame('erika@example.test', $booking->fresh()->guest->email);
}
public function test_status_can_be_changed_by_hand_with_a_reason_and_the_calendar_is_checked(): void
{
$booking = $this->record(RecordPortalBooking::CONFIRM);
$set = app(SetBookingStatus::class);
$set->handle($booking, BookingStatus::Paid, 'Zahlung über das Portal erhalten', 'Test');
$this->assertSame(BookingStatus::Paid, $booking->fresh()->status);
$this->assertStringContainsString('Zahlung über das Portal erhalten', $booking->fresh()->internal_notes);
$booking->update(['status' => BookingStatus::Expired]);
$other = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-11-08', 'departure' => '2026-11-09']);
try {
$set->handle($booking->fresh(), BookingStatus::Confirmed, 'wieder aufnehmen');
$this->fail('The dates are taken, the booking must not become binding.');
} catch (ReservationException) {
$this->assertSame(BookingStatus::Expired, $booking->fresh()->status);
}
$other->delete();
$set->handle($booking->fresh(), BookingStatus::Confirmed, 'wieder aufnehmen');
$this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status);
$this->expectException(ReservationException::class);
$set->handle($booking->fresh(), BookingStatus::Cancelled, 'nicht erlaubt');
}
public function test_the_manual_status_action_is_not_offered_for_the_own_website_process(): void
{
$own = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['source' => 'website', 'arrival' => '2026-11-07', 'departure' => '2026-11-10']);
$portal = $this->record(RecordPortalBooking::CONFIRM, arrival: '2026-12-01', departure: '2026-12-04');
$this->actingAs(User::factory()->create());
Livewire::test(ViewBooking::class, ['record' => $own->getRouteKey()])->assertDontSee('Status von Hand ändern');
Livewire::test(ViewBooking::class, ['record' => $portal->getRouteKey()])->assertSee('Status von Hand ändern')
->callAction(TestAction::make('setStatus')->arguments([]), ['status' => BookingStatus::Paid->value, 'reason' => 'Portal zahlt aus'])
->assertNotified('Status geändert');
$this->assertSame(BookingStatus::Paid, $portal->fresh()->status);
}
public function test_archived_bookings_leave_the_list_but_keep_everything(): void
{
$past = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Completed, 'arrival' => '2026-09-01', 'departure' => '2026-09-05']);
$this->actingAs(User::factory()->create());
Livewire::test(ViewBooking::class, ['record' => $past->getRouteKey()])->callAction(TestAction::make('archive')->arguments([]))->assertNotified('Archiviert');
$this->assertNotNull($past->fresh()->archived_at);
Livewire::test(ListBookings::class)->assertDontSee($past->reference)
->filterTable('show_closed', true)->assertSee($past->reference);
}
public function test_the_list_filters_by_channel_period_and_missing_contact_data(): void
{
$portalBooking = $this->record(RecordPortalBooking::CONFIRM, arrival: '2026-12-01', departure: '2026-12-04');
$own = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-11-07', 'departure' => '2026-11-10']);
$this->actingAs(User::factory()->create());
Livewire::test(ListBookings::class)
->filterTable('channel', $this->portal->id)->assertSee($portalBooking->reference)->assertDontSee($own->reference)
->removeTableFilter('channel')
->filterTable('contact_missing', true)->assertSee($portalBooking->reference)->assertDontSee($own->reference)
->removeTableFilter('contact_missing')
->filterTable('period', ['arrival_from' => '2026-11-01', 'arrival_until' => '2026-11-30'])->assertSee($own->reference)->assertDontSee($portalBooking->reference);
}
public function test_an_independent_occupation_with_a_booking_channel_is_no_longer_unclear(): void
{
$block = BlockedPeriod::factory()->for($this->property)->create(['channel_id' => $this->portal->id, 'source' => 'channel', 'external_uid' => 'u1', 'starts_on' => '2027-05-23', 'ends_on' => '2027-05-30', 'occupation_kind' => OccupationKind::Unknown, 'needs_review' => true]);
$this->assertSame(1, BlockedPeriod::query()->unclear()->count());
app(ResolveChannelOccupation::class)->handle($block, 'independent', originId: $this->portal->id);
$this->assertSame(0, BlockedPeriod::query()->unclear()->count());
$this->assertSame(OccupationKind::Reservation, $block->fresh()->occupation_kind);
}
public function test_a_portal_reservation_from_the_calendar_gets_a_booking_file_and_stays_occupied(): void
{
$block = BlockedPeriod::factory()->for($this->property)->create(['channel_id' => $this->portal->id, 'source' => 'channel', 'external_uid' => 'u2', 'starts_on' => '2026-12-10', 'ends_on' => '2026-12-14', 'occupation_kind' => OccupationKind::Reservation]);
$checker = app(AvailabilityChecker::class);
$this->assertFalse($checker->isAvailable($this->property, CarbonImmutable::parse('2026-12-10'), CarbonImmutable::parse('2026-12-14')));
$booking = app(OpenPortalFile::class)->handle($block);
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame($this->portal->id, $booking->origin_channel_id);
$this->assertSame(BookingProgress::PORTAL_BOOKING, BookingProgress::kind($booking));
$this->assertSame('', $booking->guest->email);
$this->assertTrue($block->fresh()->is_representation);
$this->assertSame($booking->id, $block->fresh()->linked_booking_id);
$this->assertFalse($checker->isAvailable($this->property, CarbonImmutable::parse('2026-12-10'), CarbonImmutable::parse('2026-12-14')));
$this->assertSame($booking->id, app(OpenPortalFile::class)->handle($block->fresh())->id);
$this->assertSame(1, Booking::query()->count());
}
public function test_a_blocked_period_gets_no_booking_file(): void
{
$block = BlockedPeriod::factory()->for($this->property)->create(['channel_id' => $this->portal->id, 'source' => 'channel', 'external_uid' => 'u3', 'starts_on' => '2026-12-10', 'ends_on' => '2026-12-14', 'occupation_kind' => OccupationKind::Block]);
$this->expectException(ReservationException::class);
app(OpenPortalFile::class)->handle($block);
}
public function test_the_occupation_page_offers_the_file_and_leads_to_the_booking(): void
{
$block = BlockedPeriod::factory()->for($this->property)->create(['channel_id' => $this->portal->id, 'source' => 'channel', 'external_uid' => 'u4', 'starts_on' => '2026-12-10', 'ends_on' => '2026-12-14', 'occupation_kind' => OccupationKind::Reservation]);
$this->actingAs(User::factory()->create());
Livewire::test(ViewPortalOccupation::class, ['record' => $block->getRouteKey()])->callAction('openFile')->assertRedirect();
$booking = Booking::query()->sole();
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSee('Ablauf: Meilensteine und Aktivitäten')
->assertSee('Bisheriger Verlauf')
->assertSee('Standardprozess')
->assertSee('Kontaktdaten eintragen');
}
public function test_every_milestone_lists_its_activities_and_the_history_records_what_happened(): void
{
$booking = $this->record(RecordPortalBooking::CONFIRM);
$steps = collect(BookingProgress::steps($booking))->keyBy('label');
$this->assertFalse($steps['Gast & Kontaktdaten']['activities'][1]['done']);
$this->assertSame('Kontaktdaten eintragen', $steps['Gast & Kontaktdaten']['activities'][1]['action']);
$this->assertSame(['Vorgang angelegt (FeWo-direkt)', 'Buchung verbindlich bestätigt'], array_column(BookingProgress::history($booking), 'label'));
}
private function record(string $process, ?int $total = null, ?string $reference = null, string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking
{
return app(RecordPortalBooking::class)->handle(
$this->property,
$this->portal,
new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2),
['first_name' => 'Erika', 'last_name' => 'Muster'],
$process,
$reference,
$total,
);
}
}