Files
fewo-stommer/tests/Feature/BookingFileTest.php
T
LarsandClaude Sonnet 5.5 5baf39441f
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m38s
Buchungsakte: Vorgang, naechster Schritt, Vertrag, Mailverlauf, Storno mit Mail, Archiv
- Buchungsseite zeigt Art des Vorgangs, Verlaufsleiste und den naechsten Schritt in Klartext
- Vertragsabschnitt (Vorlage, Dokumente mit Pruefsumme, Zustellung) und Vertragstext-Ansicht
- Reiter Aufgaben und Mailverlauf (jede Mail zur Buchung wird protokolliert)
- Stornieren in der Verwaltung informiert Gast und Vermieter und legt eine Aufgabe fuer Stornokosten an
- Stornierte/abgelehnte/abgelaufene Buchungen aus der Hauptliste, Filter Archiv anzeigen

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 16:16:42 +01:00

159 lines
8.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\CancelBooking;
use App\Actions\SendOffer;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Filament\Resources\Bookings\Pages\ListBookings;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Bookings\RelationManagers\MessagesRelationManager;
use App\Mail\BookingCancelled;
use App\Mail\CancellationRequested;
use App\Models\Booking;
use App\Models\BookingMessage;
use App\Models\BookingTask;
use App\Models\Channel;
use App\Models\Property;
use App\Models\User;
use App\Support\BookingProgress;
use App\Support\Settings;
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;
/**
* The booking file: where a process stands, next step, communication log, cancellation by the landlord, archive.
*/
class BookingFileTest extends TestCase
{
use RefreshDatabase;
private Property $property;
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();
Storage::fake('local');
$this->withoutVite();
}
public function test_next_step_follows_the_state_of_the_booking(): void
{
$request = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['arrival' => '2026-11-07', 'departure' => '2026-11-10']);
$this->assertSame('Anfrage prüfen', BookingProgress::next($request)['title']);
Mail::fake();
$offer = app(SendOffer::class)->handle($request);
$this->assertSame('Wartet auf den Gast', BookingProgress::next($offer)['title']);
$confirmed = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-12-01', 'departure' => '2026-12-05', 'deposit_due_on' => '2026-10-25', 'balance_due_on' => '2026-11-20', 'total' => 100000, 'deposit' => 30000]);
$this->assertSame('Anzahlung offen', BookingProgress::next($confirmed)['title']);
$this->travelTo(CarbonImmutable::parse('2026-12-02 12:00'));
$confirmed->update(['status' => BookingStatus::Paid, 'total' => 0, 'deposit' => 0]);
$this->assertSame('Gast ist vor Ort', BookingProgress::next($confirmed->fresh())['title']);
$this->assertContains('current', array_column(BookingProgress::steps($confirmed->fresh()), 'state'));
}
public function test_portal_booking_is_recognised_without_payment_steps_and_asks_to_send_the_contract(): void
{
$portal = Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'booking_model' => 'portal_booking']);
$booking = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'import_channel_id' => $portal->id, 'source' => 'portal', 'arrival' => '2026-12-01', 'departure' => '2026-12-05']);
$this->assertSame(BookingProgress::PORTAL_BOOKING, BookingProgress::kind($booking));
$this->assertStringContainsString('FeWo-direkt', BookingProgress::kindLabel($booking));
$this->assertSame('Vertrag senden', BookingProgress::next($booking)['title']);
$this->assertContains('Zahlung', array_column(BookingProgress::steps($booking), 'label'));
$this->assertNotContains('Anzahlung', array_column(BookingProgress::steps($booking), 'label'));
}
public function test_every_booking_mail_lands_in_the_communication_log_with_its_audience(): void
{
$booking = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['arrival' => '2026-11-07', 'departure' => '2026-11-10']);
app(SendOffer::class)->handle($booking);
$this->assertSame(['test'], BookingMessage::query()->pluck('audience')->unique()->all());
Settings::tenant()->update(['operating_mode' => Settings::MODE_LIVE]);
Settings::applyMailRouting();
BookingMessage::query()->delete();
$other = Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['arrival' => '2026-12-01', 'departure' => '2026-12-04']);
app(SendOffer::class)->handle($other);
$message = BookingMessage::query()->where('booking_id', $other->id)->sole();
$this->assertSame('guest', $message->audience);
$this->assertStringContainsString('Angebot', $message->subject);
}
public function test_cancellation_by_the_landlord_informs_guest_and_landlord_and_leaves_a_cost_task(): void
{
$booking = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-12-01', 'departure' => '2026-12-05']);
BookingTask::query()->create(['booking_id' => $booking->id, 'kind' => BookingTaskKind::CancellationRequest, 'status' => BookingTaskStatus::Open]);
Mail::fake();
$this->assertTrue(app(CancelBooking::class)->handle($booking, notifyGuest: true));
$this->assertSame(BookingStatus::Cancelled, $booking->fresh()->status);
Mail::assertSent(BookingCancelled::class, fn (BookingCancelled $mail): bool => $mail->cancelled);
Mail::assertSent(CancellationRequested::class, 1);
$this->assertSame(BookingTaskStatus::Done, BookingTask::query()->where('kind', BookingTaskKind::CancellationRequest)->sole()->status);
$this->assertSame(BookingTaskStatus::Open, BookingTask::query()->where('kind', BookingTaskKind::CancellationFee)->sole()->status);
$this->assertSame(0, Booking::query()->occupying()->count());
}
public function test_cancellation_without_guest_mail_and_the_admin_action_offer_both_choices(): void
{
$booking = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-12-01', 'departure' => '2026-12-05']);
Mail::fake();
$this->actingAs(User::factory()->create());
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction(TestAction::make('cancel')->arguments([]), ['notify_guest' => false, 'costs_task' => false])
->assertNotified('Buchung storniert');
Mail::assertNotSent(BookingCancelled::class);
Mail::assertSent(CancellationRequested::class, 1);
$this->assertSame(0, BookingTask::query()->where('kind', BookingTaskKind::CancellationFee)->count());
}
public function test_cancelled_bookings_leave_the_main_list_but_stay_in_the_archive_filter(): void
{
$active = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Confirmed, 'arrival' => '2026-12-01', 'departure' => '2026-12-05']);
$cancelled = Booking::factory()->for($this->property)->create(['status' => BookingStatus::Cancelled, 'arrival' => '2026-12-10', 'departure' => '2026-12-12']);
$this->actingAs(User::factory()->create());
Livewire::test(ListBookings::class)
->assertSee($active->reference)
->assertDontSee($cancelled->reference)
->filterTable('show_closed', true)
->assertSee($cancelled->reference);
}
public function test_the_booking_page_shows_process_contract_tasks_and_mail_log(): void
{
Mail::fake();
$booking = app(SendOffer::class)->handle(Booking::factory()->for($this->property)->reserved(now()->addHours(48))->create(['arrival' => '2026-11-07', 'departure' => '2026-11-10']));
$this->actingAs(User::factory()->create());
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->assertSee('Nächster Schritt: Wartet auf den Gast')
->assertSee('Vertrag')
->assertSee('Angebot (PDF) Version 1');
Livewire::test(MessagesRelationManager::class, ['ownerRecord' => $booking, 'pageClass' => ViewBooking::class])->assertSuccessful();
}
}