Files
fewo-stommer/tests/Feature/PaymentWorkflowTest.php
LarsandClaude Sonnet 5.5 a73b164ea1
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m36s
Zahlungsaufgaben, Freigabe-/Storno-Link, Vertragsvorlagen, Anreise-Mail
- Zahlungen werden nicht mehr automatisch angemahnt: faellige Anzahlung/Restzahlung werden Aufgaben
  (Zahlungen & Wuensche, Tages-/Wochenuebersicht); Mail nur per Klick; je Portal und Buchungstyp abschaltbar
- Freigabe-Link in der Eingangsbestaetigung, Storno-Link in der Buchungsbestaetigung; je Wohnung
  Wunsch / sofort / sofort bis Frist; Stornowuensche und Kosten bleiben Aufgabe des Gastgebers
- Vertragsvorlagen mit Versionen (je Angebot, Portal oder Wohnung waehlbar), benutzte Versionen gesperrt;
  Angebot und finaler Vertrag als unveraenderliche PDF mit SHA-256, Vertrag wird nie neu geschrieben
- Anreise-Mail je Wohnung einstellbar (Standard 3 Tage), neue Textfelder Schluessel/Parken/Ankunftstag

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

156 lines
6.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\ConfirmBooking;
use App\Actions\CreateReservation;
use App\Actions\RecordPayment;
use App\Actions\SendBookingReminders;
use App\Actions\SendPaymentReminder;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Bookings\RelationManagers\PaymentsRelationManager;
use App\Mail\ArrivalInformation;
use App\Mail\BookingConfirmed;
use App\Mail\PaymentReminder;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
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;
class PaymentWorkflowTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
Storage::fake('local');
Mail::fake();
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
}
public function test_confirmation_fixes_due_dates_and_sends_the_contract(): void
{
$booking = $this->confirmed('2026-12-01', '2026-12-05');
$this->assertSame('2026-10-29', $booking->deposit_due_on->toDateString());
$this->assertFalse($booking->requiresFullPayment());
Storage::disk('local')->assertExists($booking->contract_path);
$this->assertStringStartsWith('%PDF', Storage::disk('local')->get($booking->contract_path));
Mail::assertSent(BookingConfirmed::class, fn (BookingConfirmed $mail): bool => count($mail->attachments()) === 1);
}
public function test_short_term_booking_requires_full_payment(): void
{
$booking = $this->confirmed('2026-11-07', '2026-11-10');
$this->assertTrue($booking->requiresFullPayment());
$this->assertStringContainsString('Gesamtbetrag', (new BookingConfirmed($booking))->render());
}
public function test_payments_move_the_booking_through_its_statuses(): void
{
$booking = $this->confirmed('2026-12-01', '2026-12-05');
$record = new RecordPayment;
$record->handle($booking, $booking->deposit, today());
$this->assertSame(BookingStatus::DepositPaid, $booking->fresh()->status);
$record->handle($booking->fresh(), $booking->total - $booking->deposit, today());
$this->assertSame(BookingStatus::Paid, $booking->fresh()->status);
$this->assertSame(0, $booking->fresh()->openAmount());
$record->handle($booking->fresh(), -5000, today(), note: 'Erstattung');
$this->assertSame(BookingStatus::DepositPaid, $booking->fresh()->status);
}
public function test_reminders_are_sent_once_at_the_right_time(): void
{
$booking = $this->confirmed('2026-12-01', '2026-12-05');
$reminders = new SendBookingReminders;
$this->travelTo(CarbonImmutable::parse('2026-10-30 09:00'));
$this->assertSame(1, $reminders->handle()['deposit_overdue']);
$this->assertSame(0, $reminders->handle()['deposit_overdue']);
Mail::assertNotSent(PaymentReminder::class);
$task = BookingTask::query()->where('kind', BookingTaskKind::DepositReminder)->sole();
$this->assertSame(BookingTaskStatus::Open, $task->status);
app(SendPaymentReminder::class)->handle($task, 'Test');
Mail::assertSent(PaymentReminder::class, 1);
Mail::assertSent(PaymentReminder::class, fn (PaymentReminder $mail): bool => $mail->kind === PaymentReminder::DEPOSIT_OVERDUE && $mail->hasBcc(config('fewo.notification_email')));
$this->assertSame(BookingTaskStatus::Done, $task->fresh()->status);
$this->assertNotNull($booking->fresh()->deposit_reminded_at);
(new RecordPayment)->handle($booking, $booking->deposit, today());
$this->travelTo(CarbonImmutable::parse('2026-10-26 09:00'));
$this->assertSame(0, $reminders->handle()['balance_request'], 'balance is due on 03.11.; request 7 days before');
$this->travelTo(CarbonImmutable::parse('2026-10-27 09:00'));
$this->assertSame(1, $reminders->handle()['balance_request']);
$this->travelTo(CarbonImmutable::parse('2026-11-04 09:00'));
$this->assertSame(1, $reminders->handle()['balance_overdue']);
$this->travelTo(CarbonImmutable::parse('2026-11-27 09:00'));
$this->assertSame(0, $reminders->handle()['arrival_info'], 'default: 3 days before arrival');
$this->travelTo(CarbonImmutable::parse('2026-11-28 09:00'));
$this->assertSame(1, $reminders->handle()['arrival_info']);
Mail::assertSent(ArrivalInformation::class);
(new RecordPayment)->handle($booking->fresh(), $booking->fresh()->openAmount(), today());
$this->travelTo(CarbonImmutable::parse('2026-12-06 09:00'));
$this->assertSame(1, $reminders->handle()['completed']);
$this->assertSame(BookingStatus::Completed, $booking->fresh()->status);
}
public function test_payment_is_recorded_and_contract_downloaded_in_the_admin(): void
{
$this->actingAs(User::factory()->create());
$booking = $this->confirmed('2026-12-01', '2026-12-05');
Livewire::test(PaymentsRelationManager::class, ['ownerRecord' => $booking, 'pageClass' => ViewBooking::class])
->callAction(TestAction::make('record')->table(), [
'amount' => number_format($booking->deposit / 100, 2, '.', ''),
'paid_on' => '2026-10-25',
'method' => 'transfer',
])
->assertHasNoFormErrors();
$this->assertSame(BookingStatus::DepositPaid, $booking->fresh()->status);
Livewire::test(ViewBooking::class, ['record' => $booking->getRouteKey()])
->callAction('contract')
->assertFileDownloaded("Mietvertrag-{$booking->reference}.pdf");
}
private function confirmed(string $arrival, string $departure): Booking
{
$booking = app(CreateReservation::class)->handle(
$this->property,
new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2),
['first_name' => 'Anna', 'last_name' => 'Muster', 'email' => 'anna@example.com', 'city' => 'Köln'],
);
app(ConfirmBooking::class)->handle($booking);
return $booking->fresh();
}
}