travelTo(CarbonImmutable::parse('2026-10-27 12:00'));
}
public function test_bookings_list_combines_own_requests_and_portal_reservations_without_creating_guests(): void
{
$property = $this->property();
$own = Booking::factory()->for($property)->reserved()->create(['arrival' => '2026-11-02', 'departure' => '2026-11-05']);
$portal = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$unknown = $this->import($property, ['ical_event_type' => 'block']);
$guestCount = Guest::count();
Livewire::test(ListBookings::class)
->assertCanSeeTableRecords([$own, 'portal-'.$portal->id])
->assertCanNotSeeTableRecords(['portal-'.$unknown->id])
->assertSee('Portalreservierung')->assertSee('Gastzuordnung offen');
$this->assertSame($guestCount, Guest::count());
$this->assertNull($portal->fresh()->guest_id);
}
public function test_calendar_uses_nights_all_occupation_types_and_ignores_expired_holds_and_representations(): void
{
$property = $this->property();
$booking = Booking::factory()->for($property)->create(['arrival' => '2026-11-02', 'departure' => '2026-11-05']);
$this->import($property, ['ical_event_type' => 'portal_reservation', 'starts_on' => '2026-11-05', 'ends_on' => '2026-11-07']);
BlockedPeriod::factory()->for($property)->create(['starts_on' => '2026-11-07', 'ends_on' => '2026-11-08']);
$this->import($property, ['starts_on' => '2026-11-10', 'ends_on' => '2026-11-11']);
$this->import($property, ['starts_on' => '2026-11-02', 'ends_on' => '2026-11-05', 'is_representation' => true, 'linked_booking_id' => $booking->id]);
Booking::factory()->for($property)->reserved(now()->subMinute())->create(['arrival' => '2026-11-08', 'departure' => '2026-11-09']);
Booking::factory()->for($property)->create(['status' => BookingStatus::Cancelled, 'arrival' => '2026-11-09', 'departure' => '2026-11-10']);
$calendar = app(OccupancyOverview::class)->calendar($property, CarbonImmutable::parse('2026-11-01'));
$this->assertSame([23, 7], [$calendar['free'], $calendar['occupied']]);
$this->assertCount(1, $calendar['days'][4]['occupations']);
$this->assertCount(1, $calendar['days'][4]['departures']);
$this->assertFalse($calendar['days'][4]['overlap']);
$this->assertTrue($calendar['days'][7]['free']);
$this->assertTrue($calendar['days'][8]['free']);
$this->assertFalse($calendar['days'][9]['free']);
$this->assertFalse(app(AvailabilityChecker::class)->isAvailable($property, CarbonImmutable::parse('2026-11-10'), CarbonImmutable::parse('2026-11-11')));
Livewire::test(OccupancyCalendar::class, ['month' => '2026-11', 'propertyId' => $property->id])->assertSee('23 freie Nächte')->assertSee('Ungeklärte Belegung')->assertSee('Grund: Eigennutzung');
}
public function test_only_explicit_blocks_remain_in_blocked_periods_and_unknown_imports_have_a_separate_queue(): void
{
$property = $this->property();
$manual = BlockedPeriod::factory()->for($property)->create(['starts_on' => '2026-11-01', 'ends_on' => '2026-11-02']);
$unknown = $this->import($property, ['ical_event_type' => 'block']);
$reservation = $this->import($property, ['ical_event_type' => 'portal_reservation']);
Livewire::test(ManageBlockedPeriods::class)->assertCanSeeTableRecords([$manual])->assertCanNotSeeTableRecords([$unknown, $reservation]);
Livewire::test(ManagePortalOccupations::class)->assertCanSeeTableRecords([$unknown])->assertCanNotSeeTableRecords([$manual, $reservation]);
$this->assertSame('1', PortalOccupationResource::getNavigationBadge());
}
public function test_manual_classification_preserves_calendar_identity_and_survives_rule_updates(): void
{
$property = $this->property();
$block = $this->import($property, ['needs_review' => true, 'export_approved' => false]);
$before = $block->only(['id', 'external_uid', 'starts_on', 'ends_on', 'is_representation', 'export_approved']);
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->callAction('classify', ['occupation_kind' => 'block', 'origin_channel_id' => null])->assertHasNoActionErrors();
app(ApplyChannelRules::class)->store($block->fresh(), ['ical_event_type' => 'portal_reservation']);
$this->assertSame(OccupationKind::Block, $block->fresh()->occupationKind());
$this->assertEquals($before, $block->fresh()->only(array_keys($before)));
$this->assertTrue($block->fresh()->needs_review);
Livewire::test(ManageBlockedPeriods::class)->assertCanSeeTableRecords([$block]);
}
public function test_a_guest_with_only_known_information_can_be_created_and_assigned_from_portal_details(): void
{
$property = $this->property();
$block = $this->import($property, ['ical_event_type' => 'portal_reservation']);
Mail::fake();
$uid = $block->external_uid;
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])
->callAction('assignGuest', ['mode' => 'new', 'first_name' => 'Maren', 'last_name' => null, 'email' => null, 'phone' => null])
->assertHasNoActionErrors()->assertNotified('Gast zugeordnet');
$guest = Guest::sole();
$this->assertSame(['Maren', '', null, $guest->id, $uid], [$guest->first_name, $guest->last_name, $guest->email, $block->fresh()->guest_id, $block->fresh()->external_uid]);
$this->get(GuestResource::getUrl('edit', ['record' => $guest]))->assertOk()->assertSee('Maren');
Mail::assertNothingSent();
}
public function test_guest_history_combines_direct_and_assigned_portal_stays_in_one_table(): void
{
$property = $this->property();
$own = Booking::factory()->for($property)->create();
$portal = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$unassigned = $this->import($property, ['ical_event_type' => 'portal_reservation']);
app(AssignOccupationGuest::class)->handle($portal, $own->guest_id);
Livewire::test(BookingsRelationManager::class, ['ownerRecord' => $own->guest, 'pageClass' => EditGuest::class])
->assertCanSeeTableRecords([$own, 'portal-'.$portal->id])->assertCanNotSeeTableRecords(['portal-'.$unassigned->id]);
$this->assertSame(1, $own->guest->portalStays()->count());
}
public function test_guest_can_be_entered_directly_without_fabricating_unknown_contact_details(): void
{
$this->property();
Livewire::test(CreateGuest::class)->fillForm(['first_name' => 'Tanja', 'country' => 'DE'])->call('create')->assertHasNoFormErrors();
$this->assertSame(['Tanja', '', null], [Guest::sole()->first_name, Guest::sole()->last_name, Guest::sole()->email]);
}
#[DataProvider('guestAssignmentSources')]
public function test_complete_guest_profile_can_be_entered_and_assigned_in_one_dialog(string $source): void
{
$property = $this->property();
$record = $source === 'portal' ? $this->import($property, ['ical_event_type' => 'portal_reservation']) : Booking::factory()->for($property)->reserved()->create();
$page = $source === 'portal' ? ViewPortalOccupation::class : ViewBooking::class;
$before = $record->only(['id', 'arrival', 'departure', 'starts_on', 'ends_on', 'external_uid']);
Mail::fake();
Livewire::test($page, ['record' => $record->id])->callAction('assignGuest', [
'mode' => 'new', 'first_name' => 'Samira', 'last_name' => 'Gast', 'email' => 'SAMIRA@example.test',
'phone' => '+43 123456', 'street' => 'Seestraße 12', 'postal_code' => '1010', 'city' => 'Wien', 'country' => 'AT',
'notes' => 'Anreise am Abend', 'tags' => ['Familie'],
])->assertHasNoActionErrors()->assertNotified('Gast zugeordnet');
$guest = Guest::query()->where('email', 'samira@example.test')->sole();
$this->assertSame(['Seestraße 12', '1010', 'Wien', 'AT', '+43 123456', 'Anreise am Abend', ['Familie']], [$guest->street, $guest->postal_code, $guest->city, $guest->country, $guest->phone, $guest->notes, $guest->tags]);
$this->assertSame($guest->id, $record->fresh()->guest_id);
$this->assertEquals($before, $record->fresh()->only(array_keys($before)));
Mail::assertNothingSent();
}
public static function guestAssignmentSources(): array
{
return ['portal reservation' => ['portal'], 'own booking' => ['booking']];
}
public function test_selecting_an_existing_guest_does_not_overwrite_their_contact_details(): void
{
$property = $this->property();
$record = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$guest = Guest::factory()->create(['tenant_id' => $property->tenant_id, 'street' => 'Bestehende Straße 1', 'postal_code' => '18225', 'city' => 'Kühlungsborn', 'country' => 'DE', 'notes' => 'Bestehende Notiz']);
$before = $guest->only(['first_name', 'last_name', 'street', 'postal_code', 'city', 'country', 'notes']);
Livewire::test(ViewPortalOccupation::class, ['record' => $record->id])->callAction('assignGuest', [
'mode' => 'existing', 'guest_id' => $guest->id, 'first_name' => 'Ersetzen', 'street' => 'Andere Straße', 'city' => 'Wien', 'country' => 'AT', 'notes' => 'Nicht übernehmen',
])->assertHasNoActionErrors();
$this->assertEquals($before, $guest->fresh()->only(array_keys($before)));
$this->assertSame($guest->id, $record->fresh()->guest_id);
$this->assertSame(1, Guest::count());
}
public function test_invalid_new_guest_address_does_not_leave_a_partial_guest_or_assignment(): void
{
$property = $this->property();
$record = $this->import($property, ['ical_event_type' => 'portal_reservation']);
Livewire::test(ViewPortalOccupation::class, ['record' => $record->id])->callAction('assignGuest', [
'mode' => 'new', 'first_name' => 'Samira', 'postal_code' => str_repeat('1', 21), 'country' => 'DE',
])->assertHasActionErrors(['postal_code' => 'max']);
$this->assertSame(0, Guest::count());
$this->assertNull($record->fresh()->guest_id);
}
public function test_guest_assignment_survives_import_changes_archiving_and_return_with_same_uid(): void
{
$property = $this->property();
$block = $this->import($property, ['ical_event_type' => 'portal_reservation', 'external_uid' => 'stay-17', 'occupation_kind' => OccupationKind::Reservation]);
$guest = Guest::factory()->create(['tenant_id' => $property->tenant_id]);
app(AssignOccupationGuest::class)->handle($block, $guest->id);
Http::preventStrayRequests();
Http::fake(['https://portal.example/calendar.ics' => Http::sequence()->push($this->feed('stay-17', '20261120', '20261125'))->push($this->feed())->push($this->feed())->push($this->feed('stay-17', '20261121', '20261126'))]);
Mail::fake();
$first = app(SyncChannel::class)->handle($block->channel);
$this->assertTrue($first->successful, $first->message ?? 'First import failed');
$second = app(SyncChannel::class)->handle($block->channel);
$this->assertTrue($second->successful, $second->message ?? 'First missing snapshot failed');
$third = app(SyncChannel::class)->handle($block->channel);
$this->assertTrue($third->successful, $third->message ?? 'Second missing snapshot failed');
$this->assertTrue($block->fresh()->trashed());
$history = app(OccupancyOverview::class)->records(guestId: $guest->id);
$this->assertSame('Nicht mehr im Kalenderfeed', $history['portal-'.$block->id]['status']);
app(SyncChannel::class)->handle($block->channel);
$restored = BlockedPeriod::withTrashed()->findOrFail($block->id);
$this->assertSame([$guest->id, 'stay-17', '2026-11-21', false], [$restored->guest_id, $restored->external_uid, $restored->starts_on->toDateString(), $restored->trashed()]);
$this->assertSame(1, BlockedPeriod::withTrashed()->where('external_uid', 'stay-17')->count());
Http::assertSentCount(4);
}
public function test_third_party_guests_are_not_prematurely_anonymized_as_inquiries_without_local_bookings(): void
{
$property = $this->property();
$guest = Guest::factory()->create(['tenant_id' => $property->tenant_id, 'created_at' => '2010-01-01', 'updated_at' => '2010-01-01']);
$this->import($property, ['ical_event_type' => 'portal_reservation', 'guest_id' => $guest->id]);
$this->assertSame(0, app(AnonymizeGuests::class)->handle());
$this->assertNull($guest->fresh()->anonymized_at);
}
public function test_archived_portal_stays_remain_readable_without_mutation_actions(): void
{
$property = $this->property();
$block = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$block->delete();
$this->get(PortalOccupationResource::getUrl('view', ['record' => $block->id]))->assertOk()->assertSee('Nicht mehr im Feed');
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->assertActionHidden('assignGuest')->assertActionHidden('classify')->assertActionHidden('resolve');
}
public function test_returning_guest_filter_counts_portal_only_and_mixed_stays(): void
{
$property = $this->property();
$portalGuest = Guest::factory()->create(['tenant_id' => $property->tenant_id]);
$mixed = Booking::factory()->for($property)->create();
$single = Guest::factory()->create(['tenant_id' => $property->tenant_id]);
$this->import($property, ['ical_event_type' => 'portal_reservation', 'guest_id' => $portalGuest->id]);
$this->import($property, ['ical_event_type' => 'portal_reservation', 'guest_id' => $portalGuest->id]);
$this->import($property, ['ical_event_type' => 'portal_reservation', 'guest_id' => $mixed->guest_id]);
$this->import($property, ['ical_event_type' => 'portal_reservation', 'guest_id' => $single->id]);
Livewire::test(ListGuests::class)->filterTable('returning')->assertCanSeeTableRecords([$portalGuest, $mixed->guest])->assertCanNotSeeTableRecords([$single]);
}
public function test_central_booking_search_and_filters_apply_to_both_sources(): void
{
$property = $this->property();
$own = Booking::factory()->for($property)->create();
$portal = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$portal->forceFill(['ical_interpretation' => ['fields' => ['guest_first_name' => 'Elisa']]])->saveQuietly();
Livewire::test(ListBookings::class)->filterTable('kind', 'portal')->assertCanSeeTableRecords(['portal-'.$portal->id])->assertCanNotSeeTableRecords([$own]);
Livewire::test(ListBookings::class)->filterTable('without_guest')->assertCanSeeTableRecords(['portal-'.$portal->id])->assertCanNotSeeTableRecords([$own]);
Livewire::test(ListBookings::class)->searchTable('Elisa')->assertCanSeeTableRecords(['portal-'.$portal->id])->assertCanNotSeeTableRecords([$own]);
}
public function test_confirming_only_a_business_type_does_not_clear_identity_review_or_allow_rule_origin_override(): void
{
$property = $this->property();
$block = $this->import($property, ['needs_review' => true, 'review_note' => 'Mehrere Darstellungen prüfen']);
$block->channel->forceFill(['ical_active_rules' => [[
'name' => 'Reservation', 'enabled' => true, 'priority' => 1, 'match' => 'all',
'conditions' => [['field' => 'SUMMARY', 'operator' => 'equals', 'value' => 'Reserved']],
'event_type' => 'portal_reservation', 'origin' => 'current', 'representation' => 'leading_candidate', 'extractions' => [],
]]])->save();
app(ClassifyPortalOccupation::class)->handle($block, OccupationKind::Unknown);
$block->refresh();
$attributes = app(ApplyChannelRules::class)->attributes($block->channel, $block, ['SUMMARY' => [['value' => 'Reserved', 'params' => []]]]);
$this->assertArrayNotHasKey('origin_channel_id', $attributes);
$this->assertNull($block->reviewed_at);
$this->assertTrue($block->needs_review);
$this->assertSame(OccupationKind::Unknown, $block->occupationKind());
}
public function test_guest_identity_of_a_representation_is_shown_from_the_leading_booking(): void
{
$property = $this->property();
$booking = Booking::factory()->for($property)->create();
$copy = $this->import($property, ['is_representation' => true, 'linked_booking_id' => $booking->id]);
$this->assertSame($booking->guest->full_name, $copy->guestName());
Livewire::test(ViewPortalOccupation::class, ['record' => $copy->id])->assertActionHidden('assignGuest')->assertSee($booking->reference)->assertSee($booking->guest->email);
}
public function test_invalid_calendar_month_change_is_rejected_without_changing_the_current_month(): void
{
$property = $this->property();
Livewire::test(OccupancyCalendar::class, ['month' => '2026-11', 'propertyId' => $property->id])
->call('moveMonth', 4)->assertHasErrors(['direction'])->assertSet('month', '2026-11');
}
public function test_confirmed_guest_names_obey_the_existing_per_target_export_permission(): void
{
$property = $this->property();
$portal = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$guest = Guest::factory()->create(['tenant_id' => $property->tenant_id, 'first_name' => 'Ida', 'last_name' => 'Beispiel']);
app(AssignOccupationGuest::class)->handle($portal, $guest->id);
$target = Channel::factory()->for($property)->create(['name' => 'Anderes Portal']);
$target->forceFill(['ical_export_names' => true, 'ical_export_mapping' => [['field' => 'SUMMARY', 'items' => ['guest_full_name']]]])->save();
$this->assertStringContainsString('SUMMARY:Ida Beispiel', app(ICalFeed::class)->build($target));
$this->assertStringNotContainsString('Ida Beispiel', app(ICalFeed::class)->build($portal->channel));
$target->forceFill(['ical_export_names' => false])->save();
$this->assertStringNotContainsString('Ida Beispiel', app(ICalFeed::class)->build($target));
}
public function test_foreign_guest_is_rejected_by_the_scoped_selection_and_does_not_change_the_portal_stay(): void
{
$property = $this->property();
$block = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$foreign = Guest::factory()->create(['tenant_id' => Tenant::factory()->create()->id]);
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->callAction('assignGuest', ['mode' => 'existing', 'guest_id' => $foreign->id])->assertHasActionErrors(['guest_id']);
$this->assertNull($block->fresh()->guest_id);
}
public function test_calendar_and_import_details_reject_foreign_properties_and_records(): void
{
$property = $this->property();
$foreignTenant = Tenant::factory()->create();
$foreignProperty = Property::factory()->create(['tenant_id' => $foreignTenant->id]);
$foreignBlock = BlockedPeriod::factory()->for($foreignProperty)->create(['source' => 'channel']);
$this->get(OccupancyCalendar::getUrl(['wohnung' => $foreignProperty->id]))->assertNotFound();
$this->get(PortalOccupationResource::getUrl('view', ['record' => $foreignBlock]))->assertNotFound();
}
public function test_content_role_cannot_access_central_occupancy_data(): void
{
$this->property();
$this->actingAs(User::factory()->role(UserRole::Content)->create());
Livewire::test(OccupancyCalendar::class)->assertForbidden();
Livewire::test(ManagePortalOccupations::class)->assertForbidden();
}
public function test_rendered_calendar_escapes_imported_names(): void
{
$property = $this->property();
$block = $this->import($property, ['ical_event_type' => 'portal_reservation']);
$block->forceFill(['ical_interpretation' => ['fields' => ['guest_first_name' => '']]])->saveQuietly();
$response = $this->get(OccupancyCalendar::getUrl(['monat' => '2026-11', 'wohnung' => $property->id]));
$response->assertOk()->assertSee('<script>alert(1)</script>', escape: false)->assertDontSee('', escape: false);
}
public function test_guest_assignment_to_an_unclassified_block_is_rejected_without_creating_a_guest(): void
{
$property = $this->property();
$block = $this->import($property);
try {
app(AssignOccupationGuest::class)->handle($block, null, ['first_name' => 'Maren']);
$this->fail('An unclassified occupation must first be classified.');
} catch (ValidationException $exception) {
$this->assertStringContainsString('Zuerst als eigenständige Portalreservierung', $exception->getMessage());
}
$this->assertSame(0, Guest::count());
}
#[DataProvider('mailActions')]
public function test_local_mail_workflows_require_a_real_guest_email(string $action): void
{
$property = $this->property();
$booking = Booking::factory()->for($property)->reserved()->create();
$booking->guest->update(['email' => null]);
Mail::fake();
try {
app($action)->handle($booking);
$this->fail('Missing guest email must prevent mail workflows.');
} catch (ReservationException $exception) {
$this->assertSame('Vor dem E-Mail-Versand bitte die E-Mail-Adresse des zugeordneten Gastes ergänzen.', $exception->getMessage());
}
$this->assertSame(BookingStatus::Reserved, $booking->fresh()->status);
Mail::assertNothingSent();
}
public static function mailActions(): array
{
return ['offer' => [SendOffer::class], 'confirmation' => [ConfirmBooking::class], 'decline' => [DeclineBooking::class]];
}
private function property(): Property
{
$this->seed(PropertySeeder::class);
$property = Property::sole();
app(TenantContext::class)->set($property->tenant);
$this->actingAs(User::factory()->create());
return $property;
}
private function import(Property $property, array $attributes = []): BlockedPeriod
{
$channel = Channel::factory()->for($property)->create(['name' => 'Beispielportal', 'settings' => ['import_url' => 'https://portal.example/calendar.ics']]);
$block = BlockedPeriod::factory()->for($property)->create([
'source' => 'channel', 'channel_id' => $channel->id, 'external_uid' => 'entry-'.$channel->id,
'starts_on' => '2026-11-12', 'ends_on' => '2026-11-15', ...$attributes,
]);
return $block;
}
private function feed(?string $uid = null, string $start = '20261112', string $end = '20261115'): string
{
$event = $uid === null ? '' : "BEGIN:VEVENT\r\nUID:{$uid}\r\nDTSTAMP:20261027T110000Z\r\nDTSTART;VALUE=DATE:{$start}\r\nDTEND;VALUE=DATE:{$end}\r\nSUMMARY:Reserved\r\nEND:VEVENT\r\n";
return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Synthetic Occupancy Test//DE\r\n{$event}END:VCALENDAR\r\n";
}
}