V2.1: Kanalfähigkeiten, Ausführungsvarianten, Dokumentenentkopplung, Portalbestätigung

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>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-05 07:54:11 +01:00
1 parent ef1635e59a
commit 8442d0a3d8
62 files changed
+1818 -69

No files matched your search

@@ -0,0 +1,160 @@
<?php
namespace Tests\Feature\Process;
use App\Actions\ConfigureChannelProcess;
use App\Actions\TakeOverExternalBooking;
use App\Enums\CaseType;
use App\Enums\IntakeKind;
use App\Enums\ProcessSetting;
use App\Filament\Resources\Channels\Pages\ChannelProcess;
use App\Models\Channel;
use App\Models\Property;
use App\Models\User;
use App\Process\Capability;
use App\Process\ChannelCapabilities;
use App\Process\ChannelProcessConfiguration;
use App\Process\ProcessProfile;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;
use Livewire\Livewire;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
/**
* V2.1 cases A and B (docs/Prozessarchitektur/V2.1/02 §3): the admissible models follow from the confirmed capabilities of
* the channel, not from a fixed list of portals; old stored settings and the draft are told apart; a model fixing a
* stored setting differently is never switched silently.
*/
#[Group('configurator')]
class ChannelCapabilityTest extends TestCase
{
use RefreshDatabase;
private Property $property;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-23 10:00'));
$this->seed(PropertySeeder::class);
$this->property = Property::sole();
Mail::fake();
}
/** Case A: Destination Solutions today – we collect, we document (E-016 for the described account). */
public function test_case_a_destination_solutions_today_is_a3(): void
{
$ds = $this->channel('Destination Solution', []);
$options = app(ChannelProcessConfiguration::class)->modelOptions($ds, IntakeKind::Booking);
$this->assertSame(Capability::SUGGESTED, app(ChannelCapabilities::class)->resolve($ds)['collects_payment']->source);
$this->assertTrue($options['a3.portal_booking']['admissible']);
$this->assertFalse($options['a4.portal_booking']['admissible']);
$restriction = $options['a4.portal_booking']['restrictions'][0];
$this->assertSame('Kanalfähigkeit', $restriction->kind);
$this->assertStringContainsString('Zieht das Portal das Geld des Gastes ein?', $restriction->reason);
$this->assertSame('Portalbuchung, eigene Abwicklung', $restriction->alternative, 'the allowed alternative is named');
$this->assertStringContainsString('Inkasso', (string) $restriction->change);
}
/** Case B: the account changes – the portal collects. The existing A4 model fits; no new logic. */
public function test_case_b_destination_solutions_with_portal_collection_switches_to_the_existing_a4_model(): void
{
$ds = $this->channel('Destination Solution', []);
$configuration = app(ChannelProcessConfiguration::class);
$before = $configuration->modelOptions($ds, IntakeKind::Booking);
$this->assertFalse($before['a4.portal_booking']['admissible']);
// The unsaved draft already changes what fits.
$draft = ['collects_payment' => '1'];
$this->assertTrue($configuration->modelOptions($ds, IntakeKind::Booking, $draft)['a4.portal_booking']['admissible']);
$this->assertFalse($configuration->modelOptions($ds, IntakeKind::Booking, $draft)['a3.portal_booking']['admissible']);
app(ConfigureChannelProcess::class)->handle($ds, IntakeKind::Booking, ['model' => 'a4.portal_booking', 'capabilities' => $draft], by: 'Lars');
$ds->refresh();
$capability = app(ChannelCapabilities::class)->resolve($ds)['collects_payment'];
$this->assertSame([true, Capability::CONFIRMED, 'Lars'], [$capability->value, $capability->source, $capability->confirmedBy]);
$this->assertSame('Inkasso: ja', $ds->settings['process_history'][0]['capabilities'][0]);
$booking = app(TakeOverExternalBooking::class)->handle($this->property, $ds, CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-09'), ['portal_reference' => 'DS-NEU']);
$this->assertSame([CaseType::A4, 'a4.portal_booking'], [$booking->case_type, $booking->profile['plan']['model']]);
$this->assertSame(ProcessProfile::MODEL, $booking->process()->source(ProcessSetting::Payments));
}
/** The old stored value `payments = portal` is a derived capability and is shown as such – not overwritten unnoticed. */
public function test_a_stored_old_setting_becomes_a_derived_capability_and_is_not_silently_overridden(): void
{
$ds = $this->channel('Destination Solution', ['booking' => ['payments' => 'portal']]);
$this->actingAs(User::factory()->create());
$capability = app(ChannelCapabilities::class)->resolve($ds)['collects_payment'];
$this->assertSame([true, Capability::DERIVED], [$capability->value, $capability->source], 'the stored setting wins over the profile suggestion');
// The derived model of this channel is A4 – the page keeps it instead of switching to A3.
$page = Livewire::test(ChannelProcess::class, ['record' => $ds->getRouteKey()])->set('intake', 'booking');
$this->assertSame('a4.portal_booking', $page->get('drafts')['booking']['model']);
$page->assertSee('abgeleitet aus Ihrer bisherigen Einstellung');
}
public function test_saving_a_model_that_overrides_a_stored_setting_needs_the_confirmation_and_is_recorded(): void
{
$channel = $this->channel('Sonstiges Portal', ['booking' => ['payments' => 'portal', 'guest_messages' => 'portal']]);
$channel->update(['settings' => [...(array) $channel->settings, 'capabilities' => ['collects_payment' => ['value' => false, 'at' => now()->toIso8601String(), 'by' => 'Lars']]]]);
$configuration = app(ChannelProcessConfiguration::class);
$conflicts = $configuration->conflicts($channel->refresh(), IntakeKind::Booking, 'a3.portal_booking');
$this->assertSame([ProcessSetting::Payments], array_column($conflicts, 'setting'));
try {
app(ConfigureChannelProcess::class)->handle($channel, IntakeKind::Booking, ['model' => 'a3.portal_booking'], by: 'Lars');
$this->fail('saving without the confirmation must be refused');
} catch (ValidationException $exception) {
$this->assertStringContainsString('ausdrücklich bestätigen', $exception->errors()['process'][0]);
$this->assertStringContainsString('Bisher eingestellt: „Das Portal“', $exception->errors()['process'][1]);
}
$this->assertSame('portal', $channel->fresh()->process_overrides['booking']['payments'], 'nothing was changed');
app(ConfigureChannelProcess::class)->handle($channel, IntakeKind::Booking, ['model' => 'a3.portal_booking'], by: 'Lars', acknowledgeConflicts: true);
$channel->refresh();
$this->assertArrayNotHasKey('payments', $channel->process_overrides['booking']);
$this->assertSame('portal', $channel->process_overrides['booking']['guest_messages'], 'what the model does not fix stays');
$this->assertStringContainsString('Bisher eingestellt', $channel->settings['process_history'][0]['removed_settings'][0]);
}
public function test_unknown_capabilities_block_nothing_but_ask_for_confirmation(): void
{
$generic = $this->channel('Irgendein Portal', []);
$options = app(ChannelProcessConfiguration::class)->modelOptions($generic, IntakeKind::Booking);
$this->assertSame(['a3.portal_booking', 'a4.portal_booking', 'a5.portal_booking'], array_keys(array_filter($options, fn (array $option): bool => $option['admissible'])));
$this->assertNotSame([], $options['a4.portal_booking']['hints']);
}
public function test_a5_needs_both_capabilities_and_says_which_is_missing(): void
{
$fewo = $this->channel('FeWo-direkt', []);
$a5 = app(ChannelProcessConfiguration::class)->modelOptions($fewo, IntakeKind::Booking)['a5.portal_booking'];
$this->assertFalse($a5['admissible']);
$this->assertStringContainsString('Vertragsunterlagen', $a5['restrictions'][0]->change);
$this->assertTrue(app(ChannelProcessConfiguration::class)->modelOptions($fewo, IntakeKind::Booking, ['provides_contract_documents' => '1'])['a5.portal_booking']['admissible'], 'O2 is a suggestion: with a confirmed capability A5 becomes available');
}
/**
* @param array<string, mixed> $overrides
*/
private function channel(string $name, array $overrides): Channel
{
return Channel::factory()->for($this->property)->create(['name' => $name, 'booking_model' => 'portal_booking', 'process_overrides' => $overrides, 'import_enabled' => false, 'settings' => ['import_url' => 'https://portal.example/'.md5($name).'.ics']]);
}
}
@@ -0,0 +1,239 @@
<?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()));
}
}