Modellwahl über Kanalfähigkeiten mit erklärten Einschränkungen, Abgleich alter Werte, Ausführungsvarianten je Modul, getrennte Vertrags-/Buchungsinformation, Warten auf die Portalbestätigung mit Eskalation und Wiederaufnahme (Fälle C und D getestet). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
240 lines
12 KiB
PHP
240 lines
12 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Process;
|
||
|
||
use App\Actions\ConfigureChannelProcess;
|
||
use App\Actions\ContributeCaseFacts;
|
||
use App\Actions\ProcessPortalMail;
|
||
use App\Actions\RecordPortalAmounts;
|
||
use App\Actions\SendBookingDocuments;
|
||
use App\Actions\SendBookingNotice;
|
||
use App\Actions\TakeOverExternalBooking;
|
||
use App\Channels\Mail\PortalMail;
|
||
use App\Documents\DocumentReleases;
|
||
use App\Enums\ActivityState;
|
||
use App\Enums\BookingTaskKind;
|
||
use App\Enums\BookingTaskStatus;
|
||
use App\Enums\DocumentKind;
|
||
use App\Enums\FactSource;
|
||
use App\Enums\IntakeKind;
|
||
use App\Mail\BookingConfirmed;
|
||
use App\Mail\BookingDocumentation;
|
||
use App\Mail\BookingNotice;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Models\Channel;
|
||
use App\Models\ContractTemplate;
|
||
use App\Models\Property;
|
||
use App\Process\CaseActivities;
|
||
use App\Process\PortalConfirmation;
|
||
use App\Process\ProcessRunner;
|
||
use App\Support\Content;
|
||
use App\Support\Settings;
|
||
use Carbon\CarbonImmutable;
|
||
use Database\Seeders\PropertySeeder;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use PHPUnit\Framework\Attributes\Group;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* V2.1 cases C and D (docs/Prozessarchitektur/V2.1/02 §4–5): the contract document is created, filed and sent
|
||
* independently; a short booking information stands on its own; guest-facing steps wait for the portal's confirmation mail
|
||
* while the occupation is protected from the start; a missing mail escalates once; the landlord can confirm or send by hand.
|
||
*/
|
||
#[Group('module:portal.confirmation')]
|
||
#[Group('module:documents.booking_notice')]
|
||
class DocumentsAndPortalConfirmationTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private Property $property;
|
||
|
||
private Channel $portal;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->travelTo(CarbonImmutable::parse('2026-10-23 10:00'));
|
||
$this->seed(PropertySeeder::class);
|
||
$this->property = Property::sole();
|
||
Storage::fake('local');
|
||
Mail::fake();
|
||
Content::save(['contract.terms' => '<p>Echte Bedingungen</p>', 'stay.cancellation' => '<p>Echtes Storno</p>']);
|
||
Settings::tenant()->update(['bank_iban' => 'DE02120300000000202051', 'process_defaults' => ['processing' => 'automatic']]);
|
||
DocumentReleases::set(DocumentKind::ExternalRecord, true, 'Test');
|
||
$template = ContractTemplate::query()->create(['name' => 'Portalunterlagen', 'version' => 1, 'body' => '<p>Mietvertrag {{ mieter }}</p>', 'released_at' => now(), 'portal_terms_confirmed_at' => now()]);
|
||
$this->portal = Channel::factory()->for($this->property)->create(['name' => 'Destination Solution', 'booking_model' => 'portal_booking', 'import_enabled' => false, 'case_intake' => 'create',
|
||
'process_overrides' => ['booking' => ['contract_template_id' => $template->id]],
|
||
'settings' => ['import_url' => 'https://ds.example/a.ics', 'email_senders' => 'ds.example']]);
|
||
}
|
||
|
||
/** Case D: the contract is filed internally; the guest gets only a booking information. */
|
||
public function test_case_d_contract_only_internal_and_the_guest_gets_only_a_booking_information(): void
|
||
{
|
||
$this->configure(['contract_delivery' => 'internal', 'booking_notice' => 'automatic']);
|
||
|
||
$booking = $this->booking();
|
||
|
||
$booking->refresh();
|
||
$this->assertNotNull($booking->contract_path, 'the contract was created and filed');
|
||
$this->assertNull($booking->confirmation_sent_at, 'but not sent');
|
||
Mail::assertNotSent(BookingDocumentation::class);
|
||
Mail::assertNotSent(BookingConfirmed::class);
|
||
Mail::assertSent(BookingNotice::class, 1);
|
||
$activities = collect(CaseActivities::for($booking))->keyBy('key');
|
||
$this->assertSame([ActivityState::Done, 'Vertrag intern abgelegt'], [$activities['documents']->state, $activities['documents']->label]);
|
||
$this->assertSame(ActivityState::Done, $activities['booking_notice']->state);
|
||
|
||
// Sending the contract later by hand stays possible and fills the usual record.
|
||
$this->assertTrue(app(SendBookingDocuments::class)->handle($booking->fresh()));
|
||
$this->assertNotNull($booking->fresh()->confirmation_sent_at);
|
||
Mail::assertSent(BookingDocumentation::class, 1);
|
||
}
|
||
|
||
public function test_the_default_setting_sends_the_contract_documents_as_before_and_no_notice(): void
|
||
{
|
||
$this->configure([]);
|
||
|
||
$booking = $this->booking();
|
||
|
||
Mail::assertSent(BookingDocumentation::class, 1);
|
||
Mail::assertNotSent(BookingNotice::class);
|
||
$this->assertNotNull($booking->fresh()->confirmation_sent_at);
|
||
$this->assertNull(collect(CaseActivities::for($booking->fresh()))->firstWhere('key', 'booking_notice'));
|
||
}
|
||
|
||
public function test_the_contract_after_release_by_hand_is_prepared_but_not_sent(): void
|
||
{
|
||
$this->configure(['contract_delivery' => 'manual']);
|
||
|
||
$booking = $this->booking();
|
||
|
||
Mail::assertNothingSent();
|
||
$this->assertNull($booking->fresh()->contract_path);
|
||
$this->assertSame('sendDocuments', collect(CaseActivities::for($booking->fresh()))->firstWhere('key', 'documents')->action);
|
||
}
|
||
|
||
public function test_the_portal_confirmation_is_not_needed_when_the_portal_provides_everything_so_no_own_notice_goes_out(): void
|
||
{
|
||
$this->configure(['booking_notice' => 'automatic_unless_portal_confirmed', 'contract_delivery' => 'internal'], capabilities: ['messages_guest' => '1']);
|
||
|
||
$booking = $this->bookingFromMail();
|
||
|
||
Mail::assertNotSent(BookingNotice::class);
|
||
$notice = collect(CaseActivities::for($booking->fresh()))->firstWhere('key', 'booking_notice');
|
||
$this->assertSame(ActivityState::NotApplicable, $notice->state);
|
||
$this->assertStringContainsString('Das Portal informiert den Gast selbst und hat die Buchung bestätigt', $notice->detail);
|
||
$this->assertFalse(SendBookingNotice::sent($booking->fresh()));
|
||
}
|
||
|
||
/** Case C: the occupation is protected at once; the guest-facing steps wait for the portal's mail. */
|
||
public function test_case_c_guest_facing_steps_wait_for_the_portal_confirmation_and_the_occupation_does_not(): void
|
||
{
|
||
$this->configure(['await_portal_confirmation' => '1', 'booking_notice' => 'automatic'], capabilities: ['sends_confirmation_mail' => '1']);
|
||
|
||
// The booking is recognised without the portal's mail (e.g. a calendar entry or a manual entry).
|
||
$booking = $this->booking(withGuest: true);
|
||
|
||
$booking->refresh();
|
||
$this->assertTrue(Booking::query()->occupying()->whereKey($booking->id)->exists(), 'the booking is binding and occupies from the start');
|
||
$this->assertNull($booking->confirmation_sent_at);
|
||
Mail::assertNothingSent();
|
||
$activities = collect(CaseActivities::for($booking))->keyBy('key');
|
||
$this->assertSame(ActivityState::Waiting, $activities['portal_confirmation']->state);
|
||
$this->assertSame(ActivityState::Waiting, $activities['documents']->state);
|
||
$this->assertStringContainsString('wartet auf die Bestätigungsmail des Portals', $activities['documents']->detail);
|
||
$this->assertSame(ActivityState::Waiting, $activities['booking_notice']->state);
|
||
$this->assertNotSame(ActivityState::Waiting, $activities['stay']->state === ActivityState::Waiting ? ActivityState::Open : ActivityState::Done, 'independent steps are not held up');
|
||
|
||
// The portal's mail arrives late and is assigned to the case by its reference: everything goes on.
|
||
$this->travel(5)->hours();
|
||
$this->portalMail('Neue Buchung', $this->bookingText('DS-WAIT'));
|
||
|
||
$booking->refresh();
|
||
$this->assertNotNull(PortalConfirmation::evidence($booking));
|
||
$this->assertNotNull($booking->confirmation_sent_at, 'the contract documents went out after the confirmation');
|
||
Mail::assertSent(BookingNotice::class, 1);
|
||
}
|
||
|
||
public function test_a_missing_confirmation_escalates_once_and_sends_nothing(): void
|
||
{
|
||
$this->configure(['await_portal_confirmation' => '1', 'portal_confirmation_wait_hours' => '24', 'booking_notice' => 'automatic']);
|
||
$booking = $this->booking(withGuest: true);
|
||
|
||
$this->artisan('bookings:advance-waiting')->assertSuccessful();
|
||
$this->assertSame(0, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::PortalConfirmationMissing)->count(), 'within the time: nothing');
|
||
|
||
$this->travel(25)->hours();
|
||
$this->artisan('bookings:advance-waiting')->assertSuccessful();
|
||
$this->artisan('bookings:advance-waiting')->assertSuccessful();
|
||
|
||
$task = BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::PortalConfirmationMissing)->sole();
|
||
$this->assertSame(BookingTaskStatus::Open, $task->status);
|
||
Mail::assertNothingSent();
|
||
$this->assertTrue(Booking::query()->occupying()->whereKey($booking->id)->exists());
|
||
$this->assertSame(ActivityState::Problem, collect(CaseActivities::for($booking->fresh()))->firstWhere('key', 'portal_confirmation')->state);
|
||
|
||
// The mail comes after all: the task closes and the waiting steps go on.
|
||
$this->portalMail('Neue Buchung', $this->bookingText('DS-WAIT'));
|
||
$this->artisan('bookings:advance-waiting')->assertSuccessful();
|
||
|
||
$this->assertSame(BookingTaskStatus::Done, $task->fresh()->status);
|
||
$this->assertNotNull($booking->fresh()->confirmation_sent_at);
|
||
}
|
||
|
||
public function test_the_landlord_confirms_in_the_case_and_the_waiting_steps_continue(): void
|
||
{
|
||
$this->configure(['await_portal_confirmation' => '1', 'booking_notice' => 'automatic']);
|
||
$booking = $this->booking(withGuest: true);
|
||
$this->assertNull($booking->fresh()->confirmation_sent_at);
|
||
|
||
PortalConfirmation::confirm($booking->fresh(), 'Ingrid', 'im Portal gesehen');
|
||
app(ProcessRunner::class)->advance($booking->fresh(), 'portal.mail');
|
||
|
||
$this->assertSame('landlord', PortalConfirmation::evidence($booking->fresh())['source']);
|
||
$this->assertNotNull($booking->fresh()->confirmation_sent_at);
|
||
Mail::assertSent(BookingNotice::class, 1);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, string> $settings
|
||
* @param array<string, string> $capabilities
|
||
*/
|
||
private function configure(array $settings, array $capabilities = []): void
|
||
{
|
||
app(ConfigureChannelProcess::class)->handle($this->portal, IntakeKind::Booking, ['model' => 'a3.portal_booking', 'settings' => $settings, 'capabilities' => $capabilities], by: 'Test');
|
||
$this->portal->refresh();
|
||
}
|
||
|
||
private function booking(bool $withGuest = true): Booking
|
||
{
|
||
return app(TakeOverExternalBooking::class)->handle($this->property, $this->portal, CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-09'), [
|
||
'portal_reference' => 'DS-WAIT', 'portal_total' => 98000, 'guests' => 2,
|
||
...($withGuest ? ['guest' => ['first_name' => 'Dora', 'last_name' => 'Gast', 'email' => 'dora@example.org', 'street' => 'Weg 1', 'postal_code' => '10115', 'city' => 'Berlin', 'country' => 'DE']] : []),
|
||
]);
|
||
}
|
||
|
||
private function bookingFromMail(): Booking
|
||
{
|
||
$this->portalMail('Neue Buchung', $this->bookingText('DS-MAIL'));
|
||
$booking = Booking::query()->where('portal_reference', 'DS-MAIL')->sole();
|
||
app(ContributeCaseFacts::class)->handle($booking, ['guest.first_name' => 'Dora', 'guest.last_name' => 'Gast', 'guest.email' => 'dora@example.org'], FactSource::PortalMail);
|
||
app(RecordPortalAmounts::class)->handle($booking->fresh(), 98000, [], [], 'Destination Solution');
|
||
|
||
return $booking->fresh();
|
||
}
|
||
|
||
private function bookingText(string $reference): string
|
||
{
|
||
return "Gast: Dora Gast\nAnreise: 05.12.2026\nAbreise: 09.12.2026\nBuchungsnummer: {$reference}";
|
||
}
|
||
|
||
private function portalMail(string $subject, string $text): void
|
||
{
|
||
app(ProcessPortalMail::class)->handle(new PortalMail('<'.md5($text.now()).'@ds.example>', 'buchung@ds.example', $subject, $text, CarbonImmutable::now()));
|
||
}
|
||
}
|