Files
fewo-stommer/tests/Feature/PortalBookingTest.php
T
LarsandClaude Opus 5.5 3bcfc09417
Deploy FeWo mit PipelineManager / deploy (push) Successful in 4m44s
Pakete 8, 5 und 6: Beträge und Zahlungen, Ändern und Stornieren, A6 im Vorgang
- Paket 8: Zahlungsarten (Gast an uns, laut Portal, Auszahlung, Erstattung),
  Portalbeträge als Angaben mit Quelle, Übersicht nach Zuständigkeit (R09).
- Paket 5: Ändern mit Folgenvorschau, vereinbart oder dem Gast vorgeschlagen
  (signierter Link), ein Stornoweg mit Grund und Erstattung, Portalstorno erst
  nach Bestätigung.
- Paket 6: eigene Nutzung, Besuch und Sperre als Vorgang mit Kurabgabe und
  Aufenthaltsbestätigung; Bestandsübernahme der Sperren mit identischem Export.
- Wirksame Einstellungen (Expertenansicht), Abnahmekatalog T01–T14, Ergebnis-,
  Abnahme- und Konfigurationsdokument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 22:16:47 +01:00

366 lines
19 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Actions\OpenPortalFile;
use App\Actions\ProvideGuestContact;
use App\Actions\ResolveChannelOccupation;
use App\Actions\SetBookingStatus;
use App\Actions\TakeOverExternalBooking;
use App\Availability\AvailabilityChecker;
use App\Documents\DocumentReleases;
use App\Enums\AdminMode;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\CaseType;
use App\Enums\ChannelBookingModel;
use App\Enums\DocumentKind;
use App\Enums\OccupationKind;
use App\Enums\ProcessingMode;
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\Bookings\Schemas\BookingForm;
use App\Filament\Resources\PortalOccupations\Pages\ViewPortalOccupation;
use App\Mail\BookingConfirmed;
use App\Mail\BookingDocumentation;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\Channel;
use App\Models\ContractTemplate;
use App\Models\Property;
use App\Models\User;
use App\Process\CaseActivities;
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;
/**
* Bookings already binding on a portal (A3–A5): one way to take them over – entered by hand or found in the portal
* calendar –, the documents that follow (automatic or released by hand), manual status corrections, ending, 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_binding_portal_booking_is_taken_over_without_offer_acceptance_or_contract(): void
{
$booking = $this->takeOver(total: 123400, reference: 'HA-123', guest: ['first_name' => 'Erika', 'last_name' => 'Muster']);
$this->assertSame(BookingStatus::Confirmed, $booking->status);
$this->assertSame(CaseType::A3, $booking->case_type);
$this->assertSame($this->portal->id, $booking->origin_channel_id);
$this->assertSame('HA-123', $booking->portal_reference);
$this->assertSame(123400, $booking->total);
$this->assertSame(0, $booking->offer_version);
$this->assertSame('', $booking->guest->email);
$this->assertNull($booking->contract_path);
$this->assertSame('provideContact', CaseActivities::next($booking)?->action);
Mail::assertNothingSent();
}
public function test_entering_the_contact_data_can_send_the_documents_automatically(): void
{
$this->releaseDocumentation();
$booking = $this->takeOver(total: 50000);
$message = app(ProvideGuestContact::class)->handle($booking, ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'Gast@Example.test', 'street' => 'Hauptstr. 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn'], ProvideGuestContact::AUTOMATIC, guestConfirmed: true);
$booking->refresh();
$this->assertSame('Kontaktdaten gespeichert, Unterlagen gesendet', $message);
$this->assertSame('gast@example.test', $booking->guest->email);
$this->assertNotNull($booking->contract_path);
$this->assertNotNull($booking->guest_confirmed_at);
$this->assertSame(ProcessingMode::Automatic, $booking->processing_mode);
Mail::assertSent(BookingDocumentation::class, 1);
Mail::assertNotSent(BookingConfirmed::class);
}
public function test_without_released_documentation_the_automatic_run_stops_and_says_what_to_check(): void
{
$booking = $this->takeOver(total: 50000);
try {
app(ProvideGuestContact::class)->handle($booking, ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'gast@example.test'], ProvideGuestContact::AUTOMATIC);
$this->fail('The documents must not be sent automatically.');
} catch (ReservationException $exception) {
$this->assertStringContainsString('Arbeitsfassung', $exception->getMessage());
$this->assertStringContainsString('keine eigene Vorlage', $exception->getMessage());
}
$this->assertNull($booking->fresh()->contract_path);
Mail::assertNothingSent();
$this->assertSame('sendDocuments', CaseActivities::next($booking->fresh())?->action);
}
public function test_automatic_documents_wait_for_the_portal_price(): void
{
$this->releaseDocumentation();
$booking = $this->takeOver();
$this->expectException(ReservationException::class);
$this->expectExceptionMessage('Gesamtpreis');
app(ProvideGuestContact::class)->handle($booking, ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'gast@example.test'], ProvideGuestContact::AUTOMATIC);
}
public function test_entering_the_contact_data_by_hand_sends_nothing_and_the_documents_become_the_next_step(): void
{
$booking = $this->takeOver(total: 50000);
app(ProvideGuestContact::class)->handle($booking, ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'gast@example.test'], ProvideGuestContact::MANUAL);
$this->assertNull($booking->fresh()->contract_path);
$this->assertSame('sendDocuments', CaseActivities::next($booking->fresh())?->action);
Mail::assertNothingSent();
}
public function test_the_new_dialog_takes_over_a_portal_booking_and_the_page_leads_to_the_contact_data(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(CreateBooking::class)
->fillForm([
'kind' => BookingForm::EXTERNAL,
'property_id' => $this->property->id, 'arrival' => '2026-11-07', 'departure' => '2026-11-10',
'channel_id' => $this->portal->id, 'portal_reference' => 'HA-9',
])
->call('create')
->assertHasNoFormErrors();
$booking = Booking::query()->sole();
$this->assertSame('HA-9', $booking->portal_reference);
$this->assertNull($booking->guest_id);
$this->assertNull($booking->guests);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSee('Name und Kontaktdaten des Gastes eintragen')
->assertSee('Gesamtpreis laut Portal eintragen')
->callAction(TestAction::make('requestContact')->arguments([]))
->assertNotified('Als angefragt vermerkt')
->callAction(TestAction::make('provideContact')->arguments([]), ['guest' => ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.test'], 'process' => ProvideGuestContact::MANUAL, 'guest_confirmed' => false])
->assertNotified('Kontaktdaten gespeichert')
->callAction(TestAction::make('portalPrice')->arguments([]), ['portal_total' => '979.51', 'guests' => 2])
->assertNotified('Beträge gespeichert');
$booking->refresh();
$this->assertNotNull($booking->contact_requested_at);
$this->assertSame('erika@example.test', $booking->guest->email);
$this->assertSame(97951, $booking->total);
$this->assertSame(2, $booking->guests);
}
public function test_status_can_be_corrected_by_hand_but_a_binding_booking_never_falls_back_to_a_request(): void
{
$booking = $this->takeOver();
$set = app(SetBookingStatus::class);
$set->handle($booking, BookingStatus::Paid, 'Zahlung über das Portal erhalten', 'Test');
$this->assertSame(BookingStatus::Paid, $booking->fresh()->status);
$this->assertSame('Von Hand gesetzt: Zahlung über das Portal erhalten', $booking->events()->where('type', 'status')->reorder()->latest('id')->value('reason'));
try {
$set->handle($booking->fresh(), BookingStatus::Reserved, 'zurück');
$this->fail('A binding booking must not become a request again.');
} catch (ReservationException) {
$this->assertSame(BookingStatus::Paid, $booking->fresh()->status);
}
$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_an_expert_function_and_not_offered_for_the_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->takeOver(arrival: '2026-12-01', departure: '2026-12-04');
$this->actingAs(User::factory()->create(['admin_mode' => AdminMode::Simple]));
Livewire::test(ViewBooking::class, ['record' => $portal->getRouteKey()])->assertDontSee('Status von Hand ändern');
$this->actingAs(User::factory()->create(['admin_mode' => AdminMode::Expert]));
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_ending_a_portal_booking_requests_the_cancellation_on_the_portal_and_cancels_once_confirmed(): void
{
$booking = $this->takeOver(total: 50000);
$this->actingAs(User::factory()->create());
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('end', ['initiated_by' => 'guest', 'reason' => 'Gast storniert im Portal'])
->assertNotified('Stornierung im Portal veranlassen');
// The contract was made on the portal: the case stays booked until the cancellation is effective there.
$this->assertSame(BookingStatus::Confirmed, $booking->fresh()->status);
$this->assertNotNull($booking->fresh()->portal_cancellation_requested_at);
$this->assertTrue(BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::PortalCancellation)->where('status', 'open')->exists());
$this->assertSame('Gast storniert im Portal', $booking->events()->where('type', 'status')->reorder()->latest('id')->value('reason'));
$this->assertSame('confirmPortalCancellation', CaseActivities::next($booking->fresh())?->action);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('confirmPortalCancellation', ['initiated_by' => 'guest', 'reason' => null, 'effective' => true])
->assertNotified('Buchung storniert');
$this->assertSame(BookingStatus::Cancelled, $booking->fresh()->status);
$this->assertSame('guest', $booking->fresh()->cancellation->initiated_by);
$this->assertFalse(BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::PortalCancellation)->where('status', 'open')->exists());
}
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->takeOver(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_becomes_one_case_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, 'ical_interpretation' => ['fields' => ['guest_first_name' => 'Mia']]]);
$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(CaseType::A3, $booking->case_type);
$this->assertNull($booking->guest_id);
$this->assertNull($booking->total);
$this->assertStringContainsString('Name laut Kalender: Mia', $booking->internal_notes);
$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_does_not_become_a_booking(): 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_takes_the_reservation_over_and_leads_to_the_case(): 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('Als Nächstes')
->assertSee('Ablauf')
->assertSee('Verlauf')
->assertSee('Gebucht über FeWo-direkt')
->assertSee('Gast noch nicht bekannt');
}
public function test_the_history_records_what_happened_with_its_actor(): void
{
$booking = $this->takeOver();
$this->assertSame(['Vorgang angelegt: Portalbuchung, Abwicklung bei uns (FeWo-direkt)', 'Beim Portal verbindlich gebucht – übernommen'], $booking->events()->pluck('summary')->all());
}
/**
* @param array<string, ?string>|null $guest
*/
private function takeOver(?int $total = null, ?string $reference = null, string $arrival = '2026-11-07', string $departure = '2026-11-10', ?array $guest = null): Booking
{
return app(TakeOverExternalBooking::class)->handle(
$this->property,
$this->portal,
CarbonImmutable::parse($arrival),
CarbonImmutable::parse($departure),
['portal_total' => $total, 'portal_reference' => $reference, 'guest' => $guest],
);
}
/**
* The landlord released the booking documentation and set a template for bookings via the portal that only
* reproduces the portal's terms.
*/
private function releaseDocumentation(): void
{
DocumentReleases::set(DocumentKind::ExternalRecord, true, 'Test');
$template = ContractTemplate::query()->create(['name' => 'FeWo-direkt', 'version' => 1, 'body' => '<p>Buchung von {{ mieter }}</p>', 'released_at' => now(), 'portal_terms_confirmed_at' => now()]);
$this->portal->update(['process_overrides' => ['booking' => ['contract_template_id' => $template->id]]]);
}
}