Files
fewo-stommer/tests/Feature/ChannelOccupationResolutionTest.php
T
Lars 783068501c
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m33s
Generische iCal-Kanäle mit Bestandsklärung und sicheren Exporten bereitstellen
2026-10-27 17:14:45 +01:00

281 lines
14 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ResolveChannelOccupation;
use App\Actions\SyncChannel;
use App\Availability\AvailabilityChecker;
use App\Channels\ICal\ICalFeed;
use App\Enums\BookingStatus;
use App\Enums\ChannelEchoBehavior;
use App\Enums\UserRole;
use App\Filament\Resources\BlockedPeriods\Pages\ManageBlockedPeriods;
use App\Filament\Resources\Properties\Pages\EditProperty;
use App\Filament\Resources\Properties\RelationManagers\ChannelsRelationManager;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use App\Models\Property;
use App\Models\User;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Filament\Actions\CreateAction;
use Filament\Actions\Testing\TestAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use Tests\TestCase;
class ChannelOccupationResolutionTest extends TestCase
{
use RefreshDatabase;
public function test_equal_dates_with_different_channel_uids_are_not_automatically_merged_or_rebroadcast(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/a.ics']]);
$b = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/b.ics']]);
Http::preventStrayRequests();
Http::fake(['https://portal.example/a.ics' => Http::response(self::feed('uid-a')), 'https://portal.example/b.ics' => Http::response(self::feed('uid-b'))]);
Mail::fake();
app(SyncChannel::class)->handle($a);
app(SyncChannel::class)->handle($b);
app(SyncChannel::class)->handle($b);
$this->assertSame(2, $property->blockedPeriods()->count());
$this->assertSame([false, false, true], [$b->blockedPeriods()->sole()->is_representation, $b->blockedPeriods()->sole()->export_approved, $b->blockedPeriods()->sole()->needs_review]);
$this->assertStringNotContainsString('block-'.$b->blockedPeriods()->sole()->id.'@', app(ICalFeed::class)->build($a));
Http::assertSentCount(3);
}
public function test_reexporting_channel_holds_new_events_internally_until_individually_confirmed(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create(['echo_behavior' => ChannelEchoBehavior::Reexports, 'settings' => ['import_url' => 'https://portal.example/a.ics']]);
$b = Channel::factory()->for($property)->create();
Http::preventStrayRequests();
Http::fake(['https://portal.example/a.ics' => Http::response(self::feed('external'))]);
Mail::fake();
app(SyncChannel::class)->handle($a);
$block = $a->blockedPeriods()->sole();
$this->assertFalse($block->export_approved);
$this->assertFalse($this->available($property));
$this->assertStringNotContainsString('block-'.$block->id.'@', app(ICalFeed::class)->build($b));
app(ResolveChannelOccupation::class)->handle($block, 'independent', originId: $a->id);
$this->assertStringContainsString('block-'.$block->id.'@', app(ICalFeed::class)->build($b));
$this->assertStringNotContainsString('block-'.$block->id.'@', app(ICalFeed::class)->build($a));
$this->assertFalse($block->fresh()->needs_review);
Http::assertSentCount(1);
}
public function test_linked_calendar_representation_never_keeps_a_cancelled_primary_block_occupied(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create();
$b = Channel::factory()->for($property)->create();
$primary = $this->block($property, $a, 'primary');
$copy = $this->block($property, $b, 'copy');
app(ResolveChannelOccupation::class)->handle($copy, 'block', $primary->id, $a->id);
$this->assertFalse($this->available($property));
$primary->delete();
$this->assertTrue($this->available($property));
$this->assertTrue($copy->fresh()->is_representation);
$this->assertSame($primary->id, $copy->fresh()->canonical_block_id);
$this->assertSoftDeleted($primary);
$this->assertStringNotContainsString('BEGIN:VEVENT', app(ICalFeed::class)->build($a));
}
public function test_representation_of_local_booking_follows_its_cancellation(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create();
$booking = Booking::factory()->for($property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2027-05-23', 'departure' => '2027-05-30']);
$copy = $this->block($property, $channel, 'copy');
app(ResolveChannelOccupation::class)->handle($copy, 'booking', $booking->id);
$booking->update(['status' => BookingStatus::Cancelled]);
$this->assertTrue($this->available($property));
$this->assertSame($booking->id, $copy->fresh()->linked_booking_id);
$this->assertStringNotContainsString('BEGIN:VEVENT', app(ICalFeed::class)->build($channel));
}
public function test_primary_uid_reappearing_restores_same_local_identity_and_keeps_alias_assignment(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/a.ics']]);
$b = Channel::factory()->for($property)->create();
$primary = $this->block($property, $a, 'primary');
$copy = $this->block($property, $b, 'copy');
app(ResolveChannelOccupation::class)->handle($copy, 'block', $primary->id);
$primary->delete();
Http::preventStrayRequests();
Http::fake(['https://portal.example/a.ics' => Http::response(self::feed('primary'))]);
Mail::fake();
$result = app(SyncChannel::class)->handle($a);
$this->assertTrue($result->successful);
$this->assertSame($primary->id, $a->blockedPeriods()->sole()->id);
$this->assertSame($primary->id, $copy->fresh()->canonical_block_id);
$this->assertFalse($this->available($property));
Http::assertSentCount(1);
}
public function test_linked_representation_changed_by_portal_is_flagged_without_reopening_its_old_period(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create();
$b = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/b.ics']]);
$primary = $this->block($property, $a, 'primary');
$copy = $this->block($property, $b, 'copy');
app(ResolveChannelOccupation::class)->handle($copy, 'block', $primary->id);
$primary->update(['starts_on' => '2027-06-01', 'ends_on' => '2027-06-08']);
Http::preventStrayRequests();
Http::fake(['https://portal.example/b.ics' => Http::response(self::feed('copy'))]);
Mail::fake();
app(SyncChannel::class)->handle($b);
$this->assertTrue($copy->fresh()->needs_review);
$this->assertTrue($this->available($property));
$this->assertStringNotContainsString('20270523', app(ICalFeed::class)->build($a));
Http::assertSentCount(1);
}
public function test_mapping_rejects_different_dates_or_a_different_property_without_changing_inventory(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create();
$block = $this->block($property, $channel, 'copy');
$other = BlockedPeriod::factory()->create(['starts_on' => '2027-05-23', 'ends_on' => '2027-05-30']);
$wrongDates = BlockedPeriod::factory()->for($property)->create(['starts_on' => '2027-05-23', 'ends_on' => '2027-05-31']);
foreach ([$other, $wrongDates, $block] as $invalidTarget) {
try {
app(ResolveChannelOccupation::class)->handle($block, 'block', $invalidTarget->id);
$this->fail('An unverified target must not be accepted.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('target_id', $exception->errors());
}
}
$this->assertFalse($block->fresh()->is_representation);
$this->assertSame(3, BlockedPeriod::count());
}
public function test_alias_chains_are_rejected_and_an_assignment_can_be_explicitly_reversed(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create();
$a = $this->block($property, $channel, 'a');
$b = $this->block($property, $channel, 'b');
$c = $this->block($property, $channel, 'c');
app(ResolveChannelOccupation::class)->handle($b, 'block', $a->id);
try {
app(ResolveChannelOccupation::class)->handle($a, 'block', $c->id);
$this->fail('A primary with linked representations cannot become a representation itself.');
} catch (ValidationException $exception) {
$this->assertArrayHasKey('target_id', $exception->errors());
}
app(ResolveChannelOccupation::class)->handle($b, 'independent');
$this->assertNull($b->fresh()->canonical_block_id);
$this->assertFalse($b->fresh()->is_representation);
$this->assertTrue($b->fresh()->export_approved);
}
public function test_same_uid_in_different_channels_is_independently_identified(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/a.ics']]);
$b = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/b.ics']]);
Http::preventStrayRequests();
Http::fake(['https://portal.example/a.ics' => Http::response(self::feed('same')), 'https://portal.example/b.ics' => Http::response(self::feed('same', '20270601', '20270608'))]);
Mail::fake();
app(SyncChannel::class)->handle($a);
app(SyncChannel::class)->handle($b);
$this->assertNotSame($a->blockedPeriods()->sole()->id, $b->blockedPeriods()->sole()->id);
$this->assertSame('2027-05-23', $a->blockedPeriods()->sole()->starts_on->toDateString());
$this->assertSame('2027-06-01', $b->blockedPeriods()->sole()->starts_on->toDateString());
Http::assertSentCount(2);
}
public function test_landlord_can_resolve_a_duplicate_through_the_existing_admin(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create();
$primary = BlockedPeriod::factory()->for($property)->create(['starts_on' => '2027-05-23', 'ends_on' => '2027-05-30']);
$copy = $this->block($property, $channel, 'copy');
$this->actingAs(User::factory()->create());
Livewire::test(ManageBlockedPeriods::class)->callTableAction('resolve', $copy, ['resolution' => 'block', 'target_id' => $primary->id])->assertHasNoActionErrors();
$this->assertSame($primary->id, $copy->fresh()->canonical_block_id);
$this->assertTrue($copy->fresh()->is_representation);
}
public function test_content_role_cannot_open_the_occupation_resolution_screen(): void
{
$this->property();
$this->actingAs(User::factory()->role(UserRole::Content)->create());
Livewire::test(ManageBlockedPeriods::class)->assertForbidden();
}
public function test_any_portal_created_in_the_ui_automatically_gets_its_own_stable_export_link(): void
{
$property = $this->property();
$this->actingAs(User::factory()->create());
$ui = Livewire::test(ChannelsRelationManager::class, ['ownerRecord' => $property, 'pageClass' => EditProperty::class]);
foreach (['Portal Alpha', 'Portal Beta', 'Ein beliebiger weiterer Anbieter'] as $name) {
$ui->callAction(TestAction::make(CreateAction::class)->table(), ['name' => $name, 'connector' => 'ical'])->assertHasNoFormErrors();
}
$channels = $property->channels()->get();
$links = $channels->map(fn (Channel $channel): string => $channel->exportUrl());
$this->assertSame(3, $links->unique()->count());
foreach ($channels as $channel) {
$this->assertSame(48, strlen($channel->export_token));
$link = $channel->exportUrl();
$channel->update(['name' => $channel->name.' umbenannt']);
$this->assertSame($link, $channel->fresh()->exportUrl());
$this->get($link)->assertStatus(503);
}
}
private function property(): Property
{
$this->travelTo(CarbonImmutable::parse('2026-10-27 12:00'));
$this->seed(PropertySeeder::class);
$property = Property::sole();
app(TenantContext::class)->set($property->tenant);
return $property;
}
private function block(Property $property, Channel $channel, string $uid): BlockedPeriod
{
return BlockedPeriod::factory()->for($property)->create(['channel_id' => $channel->id, 'source' => 'channel', 'external_uid' => $uid, 'starts_on' => '2027-05-23', 'ends_on' => '2027-05-30']);
}
private function available(Property $property): bool
{
return app(AvailabilityChecker::class)->isAvailable($property, CarbonImmutable::parse('2027-05-23'), CarbonImmutable::parse('2027-05-30'));
}
private static function feed(string $uid, string $start = '20270523', string $end = '20270530'): string
{
return "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Synthetic//DE\r\nBEGIN:VEVENT\r\nUID:{$uid}\r\nDTSTAMP:20261027T090000Z\r\nDTSTART;VALUE=DATE:{$start}\r\nDTEND;VALUE=DATE:{$end}\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
}
}