Files
fewo-stommer/tests/Feature/ChannelManagementTest.php
Lars e5a083ce71
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m41s
Ausstehenden Erstimport von veraltetem Kalender unterscheiden
2026-10-27 17:58:07 +01:00

164 lines
8.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Enums\AdminMode;
use App\Enums\ChannelExportMode;
use App\Enums\ChannelStatus;
use App\Enums\UserRole;
use App\Filament\Resources\Channels\ChannelResource;
use App\Filament\Resources\Channels\Pages\CreateChannel;
use App\Filament\Resources\Channels\Pages\EditChannel;
use App\Filament\Resources\Channels\Pages\ListChannels;
use App\Models\BlockedPeriod;
use App\Models\Channel;
use App\Models\Property;
use App\Models\Tenant;
use App\Models\User;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Filament\Actions\Testing\TestAction;
use Filament\Infolists\Components\TextEntry;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class ChannelManagementTest extends TestCase
{
use RefreshDatabase;
public function test_landlord_creates_arbitrary_portals_from_their_own_management_page_with_automatic_links(): void
{
$property = $this->property();
$this->actingAs(User::factory()->create());
foreach (['Mein Anfrageportal', 'Ein weiterer Anbieter'] as $name) {
Livewire::test(CreateChannel::class)->fillForm(['property_id' => $property->id, 'name' => $name, 'connector' => 'ical'])->call('create')->assertHasNoFormErrors();
}
$channels = Channel::all();
$this->assertSame(2, $channels->count());
$this->assertSame(2, $channels->pluck('export_token')->unique()->count());
$this->assertSame([ChannelStatus::Setup, false, false, ChannelExportMode::Test], [$channels[0]->status, $channels[0]->import_enabled, $channels[0]->export_enabled, $channels[0]->export_mode]);
$this->assertSame($property->id, $channels[1]->property_id);
Livewire::test(ListChannels::class)->assertCanSeeTableRecords($channels)->assertSee('Mein Anfrageportal')->assertSee('Ein weiterer Anbieter');
}
public function test_management_list_hides_secret_urls_until_the_link_action_is_opened(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/private-import-secret.ics']]);
$this->actingAs(User::factory()->create());
$page = Livewire::test(ListChannels::class)->assertCanSeeTableRecords([$channel])->assertDontSee($channel->export_token)->assertDontSee('private-import-secret');
$page->mountAction(TestAction::make('exportLink')->table($channel))->assertActionMounted(TestAction::make('exportLink')->table($channel));
$schema = $page->instance()->getSchema($page->instance()->getMountedActionSchemaName());
$entry = collect($schema->getFlatComponents())->first(fn ($component): bool => $component instanceof TextEntry && $component->getName() === 'export_url');
$this->assertSame($channel->exportUrl(), $entry->getState());
}
public function test_editing_preserves_the_link_and_the_independent_configuration_of_another_channel(): void
{
$property = $this->property();
$a = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/calendar.ics']]);
$b = Channel::factory()->for($property)->create();
$token = $a->export_token;
$this->actingAs(User::factory()->create());
Livewire::test(EditChannel::class, ['record' => $a->id])
->assertSee('Eigener iCal-Exportlink')
->assertSee($a->export_token)
->fillForm(['name' => 'Individuell gepflegt', 'import_enabled' => false, 'export_enabled' => true, 'external_poll_minutes' => 90])->call('save')->assertHasNoFormErrors();
$this->assertSame(['Individuell gepflegt', false, true, 90, $token], [$a->fresh()->name, $a->fresh()->import_enabled, $a->fresh()->export_enabled, $a->fresh()->external_poll_minutes, $a->fresh()->export_token]);
$this->assertTrue($b->fresh()->import_enabled);
$this->assertSame('https://portal.example/calendar.ics', $a->fresh()->setting('import_url'));
}
public function test_editor_uses_safe_export_pause_instead_of_a_bare_model_update(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create();
BlockedPeriod::factory()->for($property)->create(['starts_on' => '2027-05-23', 'ends_on' => '2027-05-30']);
$this->actingAs(User::factory()->create());
Livewire::test(EditChannel::class, ['record' => $channel->id])->fillForm(['export_enabled' => false])->call('save')->assertHasNoFormErrors();
$this->get($channel->exportUrl())->assertOk()->assertHeader('X-Fewo-Export-State', 'frozen')->assertSee('20270523');
$this->assertNotNull($channel->fresh()->export_snapshot_at);
}
public function test_editor_cannot_replace_the_source_of_an_existing_import_inventory(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/calendar.ics']]);
BlockedPeriod::factory()->for($property)->create(['source' => 'channel', 'channel_id' => $channel->id, 'external_uid' => 'existing']);
$this->actingAs(User::factory()->create());
Livewire::test(EditChannel::class, ['record' => $channel->id])->fillForm(['settings' => ['import_url' => 'https://other.example/new.ics']])->call('save')->assertHasFormErrors();
$this->assertSame('https://portal.example/calendar.ics', $channel->fresh()->setting('import_url'));
}
public function test_manual_import_works_from_the_standalone_list(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create(['settings' => ['import_url' => 'https://portal.example/synthetic.ics']]);
$this->actingAs(User::factory()->create());
Http::preventStrayRequests();
Http::fake(['https://portal.example/synthetic.ics' => Http::response("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Synthetic//DE\r\nBEGIN:VEVENT\r\nUID:one\r\nDTSTAMP:20261027T120000Z\r\nDTSTART;VALUE=DATE:20270523\r\nDTEND;VALUE=DATE:20270530\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n")]);
Mail::fake();
Livewire::test(ListChannels::class)->callTableAction('sync', $channel)->assertNotified('1 Belegungen eingelesen');
$this->assertSame('one', $channel->blockedPeriods()->sole()->external_uid);
Http::assertSentCount(1);
}
public function test_content_role_cannot_list_or_create_portal_connections(): void
{
$this->property();
$this->actingAs(User::factory()->role(UserRole::Content)->create());
Livewire::test(ListChannels::class)->assertForbidden();
Livewire::test(CreateChannel::class)->assertForbidden();
}
public function test_rentals_role_can_reach_portal_management_in_simple_mode(): void
{
$this->property();
$this->actingAs(User::factory()->role(UserRole::Rentals)->create(['admin_mode' => AdminMode::Simple]));
Livewire::test(ListChannels::class)->assertSuccessful();
$this->assertTrue(ChannelResource::shouldRegisterNavigation());
}
public function test_another_landlords_channel_cannot_be_seen_or_edited(): void
{
$property = $this->property();
$otherTenant = Tenant::factory()->create();
app(TenantContext::class)->set($otherTenant);
$other = Channel::factory()->create(['name' => 'Vertraulicher fremder Kanal']);
app(TenantContext::class)->set($property->tenant);
$this->actingAs(User::factory()->create());
Livewire::test(ListChannels::class)->assertCanNotSeeTableRecords([$other])->assertDontSee('Vertraulicher fremder Kanal');
Livewire::test(EditChannel::class, ['record' => $other->id])->assertNotFound();
}
public function test_a_new_enabled_import_is_waiting_for_its_first_run_rather_than_outdated(): void
{
$property = $this->property();
$channel = Channel::factory()->for($property)->create();
$this->actingAs(User::factory()->create());
Livewire::test(ListChannels::class)->assertCanSeeTableRecords([$channel])
->assertSee('Erster Import ausstehend')->assertDontSee('Import veraltet');
$channel->forceFill(['last_success_at' => now()->subHours(2)])->save();
Livewire::test(ListChannels::class)->assertSee('Import veraltet')->assertDontSee('Erster Import ausstehend');
$channel->forceFill(['last_success_at' => null, 'last_error' => 'Synthetischer Abruffehler'])->save();
Livewire::test(ListChannels::class)->assertSee('Importfehler')->assertDontSee('Erster Import ausstehend');
}
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;
}
}