Files
fewo-stommer/tests/Feature/OccupancyManagementTest.php
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

596 lines
33 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\AnonymizeGuests;
use App\Actions\ApplyChannelRules;
use App\Actions\AssignOccupationGuest;
use App\Actions\ClassifyPortalOccupation;
use App\Actions\ConfirmBooking;
use App\Actions\DeclineBooking;
use App\Actions\ResolveChannelOccupation;
use App\Actions\SendOffer;
use App\Actions\SyncChannel;
use App\Availability\AvailabilityChecker;
use App\Availability\OccupancyOverview;
use App\Channels\ICal\ICalFeed;
use App\Enums\BlockedPeriodPurpose;
use App\Enums\BookingStatus;
use App\Enums\OccupationKind;
use App\Enums\UserRole;
use App\Exceptions\ReservationException;
use App\Filament\Pages\OccupancyCalendar;
use App\Filament\Resources\BlockedPeriods\Pages\ManageBlockedPeriods;
use App\Filament\Resources\Bookings\Pages\ListBookings;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Guests\GuestResource;
use App\Filament\Resources\Guests\Pages\CreateGuest;
use App\Filament\Resources\Guests\Pages\EditGuest;
use App\Filament\Resources\Guests\Pages\ListGuests;
use App\Filament\Resources\Guests\RelationManagers\BookingsRelationManager;
use App\Filament\Resources\PortalOccupations\Pages\ManagePortalOccupations;
use App\Filament\Resources\PortalOccupations\Pages\ViewPortalOccupation;
use App\Filament\Resources\PortalOccupations\PortalOccupationResource;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Guest;
use App\Models\Property;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class OccupancyManagementTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->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_imported_block_can_be_classified_as_private_own_use_without_exporting_it_back_to_its_source(): void
{
$property = $this->property();
$block = $this->import($property, ['needs_review' => true]);
$identity = $block->only(['channel_id', 'external_uid', 'starts_on', 'ends_on', 'export_approved']);
$target = Channel::factory()->for($property)->create();
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->callAction('classify', [
'occupation_kind' => 'block', 'block_purpose' => 'own_use', 'block_note' => 'Familienaufenthalt', 'origin_channel_id' => 'private',
])->assertHasNoActionErrors()->assertNotified('Belegungsart gespeichert');
$block->refresh();
$this->assertSame(BlockedPeriodPurpose::OwnUse, $block->block_purpose);
$this->assertTrue($block->is_private);
$this->assertNull($block->origin_channel_id);
$this->assertTrue($block->needs_review);
$this->assertEquals($identity, $block->only(array_keys($identity)));
$this->assertStringNotContainsString('UID:block-'.$block->id, app(ICalFeed::class)->build($block->channel));
$this->assertStringContainsString('UID:block-'.$block->id, app(ICalFeed::class)->build($target));
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->assertSee('Eigenbelegung')->assertSee('Familienaufenthalt')->assertSee('Privat')->assertSee($block->channel->name);
$row = app(OccupancyOverview::class)->records(includeBlocks: true)['portal-'.$block->id];
$this->assertSame('Privat', $row['source']);
$this->assertSame('Eigenbelegung · Familienaufenthalt', $row['reason']);
}
public function test_private_purpose_and_note_survive_a_changed_calendar_import_and_rule_evaluation(): void
{
$property = $this->property();
$block = $this->import($property, ['external_uid' => 'private-stay']);
app(ClassifyPortalOccupation::class)->handle($block, OccupationKind::Block, details: [
'block_purpose' => 'own_use', 'block_note' => 'Persönlich bestätigt', 'is_private' => true,
]);
Http::preventStrayRequests();
Http::fake(['https://portal.example/calendar.ics' => Http::response($this->feed('private-stay', '20261120', '20261125'))]);
$result = app(SyncChannel::class)->handle($block->channel);
$this->assertTrue($result->successful, $result->message ?? 'Import failed');
$block->refresh();
$this->assertSame('2026-11-20', $block->starts_on->toDateString());
$this->assertSame(OccupationKind::Block, $block->occupationKind());
$this->assertTrue($block->is_private);
$this->assertNull($block->origin_channel_id);
$this->assertSame('Eigenbelegung · Persönlich bestätigt', $block->blockDescription());
Http::assertSentCount(1);
}
public function test_an_existing_manual_block_can_still_be_edited_with_own_use_and_private_channel(): void
{
$property = $this->property();
// New blocks are A6 cases; blocks from before stay editable in the list.
BlockedPeriod::factory()->for($property)->create(['source' => 'manual', 'starts_on' => '2026-11-10', 'ends_on' => '2026-11-12',
'block_purpose' => 'own_use', 'block_note' => 'Eigener Aufenthalt', 'is_private' => true]);
$block = BlockedPeriod::sole();
$this->assertSame('manual', $block->source);
$this->assertTrue($block->is_private);
$this->assertSame(BlockedPeriodPurpose::OwnUse, $block->block_purpose);
Livewire::test(ManageBlockedPeriods::class)->callTableAction('edit', $block, [
'block_purpose' => 'maintenance', 'block_note' => 'Bad renovieren', 'is_private' => false,
])->assertHasNoActionErrors();
$block->refresh();
$this->assertFalse($block->is_private);
$this->assertSame('Wartung / Renovierung · Bad renovieren', $block->blockDescription());
}
public function test_imported_block_purpose_can_be_changed_directly_in_blocked_periods_and_cleared(): void
{
$property = $this->property();
$block = $this->import($property, ['occupation_kind' => OccupationKind::Block, 'block_purpose' => 'own_use', 'block_note' => 'Alter Hinweis', 'is_private' => true]);
Livewire::test(ManageBlockedPeriods::class)->callTableAction('classify', $block, [
'occupation_kind' => 'block', 'block_purpose' => 'other', 'block_note' => null, 'origin_channel_id' => null,
])->assertHasNoActionErrors();
$block->refresh();
$this->assertSame(BlockedPeriodPurpose::Other, $block->block_purpose);
$this->assertNull($block->block_note);
$this->assertFalse($block->is_private);
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->callAction('classify', [
'occupation_kind' => 'reservation', 'origin_channel_id' => $block->channel_id,
])->assertHasNoActionErrors();
$this->assertNull($block->fresh()->block_purpose);
$this->assertSame($block->channel_id, $block->fresh()->origin_channel_id);
}
public function test_invalid_block_note_is_rejected_without_changing_the_classification(): void
{
$block = $this->import($this->property());
Livewire::test(ViewPortalOccupation::class, ['record' => $block->id])->callAction('classify', [
'occupation_kind' => 'block', 'block_purpose' => 'own_use', 'block_note' => str_repeat('x', 256), 'origin_channel_id' => 'private',
])->assertHasActionErrors(['block_note' => 'max']);
$this->assertNull($block->fresh()->occupation_kind);
$this->assertFalse($block->fresh()->is_private);
}
public function test_private_and_external_origin_cannot_be_saved_together(): void
{
$block = $this->import($this->property());
try {
app(ClassifyPortalOccupation::class)->handle($block, OccupationKind::Block, $block->channel_id, ['is_private' => true]);
$this->fail('Contradicting booking channels must be rejected.');
} catch (ValidationException $exception) {
$this->assertSame('Privat und ein externer Buchungskanal können nicht gleichzeitig gewählt werden.', $exception->errors()['origin_channel_id'][0]);
}
$this->assertNull($block->fresh()->occupation_kind);
$this->assertFalse($block->fresh()->is_private);
}
public function test_identity_resolution_preserves_private_origin_until_an_external_origin_is_explicitly_selected(): void
{
$block = $this->import($this->property(), ['occupation_kind' => OccupationKind::Block, 'is_private' => true]);
app(ResolveChannelOccupation::class)->handle($block, 'independent');
$this->assertTrue($block->fresh()->is_private);
$this->assertNull($block->fresh()->origin_channel_id);
app(ResolveChannelOccupation::class)->handle($block, 'independent', originId: $block->channel_id);
$this->assertFalse($block->fresh()->is_private);
$this->assertSame($block->channel_id, $block->fresh()->origin_channel_id);
}
public function test_invalid_business_purpose_is_rejected_by_the_action(): void
{
$block = $this->import($this->property());
try {
app(ClassifyPortalOccupation::class)->handle($block, OccupationKind::Block, details: ['block_purpose' => 'invented-purpose']);
$this->fail('Unknown purposes must be rejected.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('block_purpose', $exception->errors());
}
$this->assertNull($block->fresh()->occupation_kind);
}
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' => '<script>alert(1)</script>']]])->saveQuietly();
$response = $this->get(OccupancyCalendar::getUrl(['monat' => '2026-11', 'wohnung' => $property->id]));
$response->assertOk()->assertSee('&lt;script&gt;alert(1)&lt;/script&gt;', escape: false)->assertDontSee('<script>alert(1)</script>', 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";
}
}