Files
fewo-stommer/tests/Feature/Process/ChannelCapabilityTest.php
LarsandClaude Sonnet 5.5 e95ba4f986 Schablonenkomposition: Basisprozess external.booking mit Platz für Finanz- und Dokumentenschablonen
Vier Schablonen (Eigenes/Portal-Inkasso, Eigene/Externe Vertragsdokumentation) in
app/Process/Templates, TemplateComposer prüft Ergebnisse, Verantwortlichkeiten, Ausschlüsse
und harte Modulabhängigkeiten. Der Plan friert die Schablonenversionen ein. A3–A5 bleiben
als Voreinstellungen; die vierte Kombination (eigenes Inkasso + externe Unterlagen) ist
darstellbar. Konfigurator-Logik und ConfigureChannelProcess kennen die Schablonenwahl.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 10:48:49 +01:00

270 lines
17 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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\ConfigurationImpact;
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');
}
/** The order is shown from the module definitions: dependencies, independence, conditions and how a duty may be fulfilled. */
public function test_the_configurator_shows_dependencies_independence_and_allowed_fulfilment(): void
{
$ds = $this->channel('Destination Solution', []);
$this->actingAs(User::factory()->create());
$steps = collect(Livewire::test(ChannelProcess::class, ['record' => $ds->getRouteKey()])->set('intake', 'booking')->instance()->steps())->keyBy('module');
$this->assertNotEmpty($steps['documents.send']['needs'], 'the contract documents need something first');
$this->assertSame([], $steps['intake.record']['needs'], 'the intake depends on nothing');
$this->assertNotNull($steps['documents.booking_notice']['condition']);
foreach ($steps as $step) {
if ($step['toggle'] === 'mandatory' && $step['executions'] !== []) {
$this->assertNotNull($step['why'], $step['module'].': the restriction is explained');
}
}
$this->assertNotEmpty(collect($steps['documents.send']['parameters'])->firstWhere('key', 'contract_delivery')['options']);
}
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, ProcessSetting::GuestMessages], array_column($conflicts, 'setting'), 'fixed and no longer allowed responsibilities are both listed');
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->assertArrayNotHasKey('guest_messages', $channel->process_overrides['booking'], 'a responsibility the model does not allow is removed with it');
$this->assertStringContainsString('Bisher eingestellt', $channel->settings['process_history'][0]['removed_settings'][0]);
}
/**
* Regression: Destination Solutions with the old `payments = portal`, switched to A3 in the configurator with the
* confirmation. The page carries the stale value in its draft; saving used to fail with "Inkasso … nicht zulässig".
*/
public function test_confirmed_switch_from_old_portal_collection_to_a3_saves_through_the_page(): void
{
$ds = $this->channel('Destination Solution', ['booking' => ['payments' => 'portal', 'guest_messages' => 'portal', 'contract_delivery' => 'manual']]);
$this->actingAs(User::factory()->create());
$page = Livewire::test(ChannelProcess::class, ['record' => $ds->getRouteKey()])->set('intake', 'booking');
$this->assertSame('portal', $page->get('drafts')['booking']['settings']['payments'], 'the page carries the stale value');
$page->set('capabilityDraft.collects_payment', '0')->call('chooseModel', 'a3.portal_booking');
$this->assertSame([], $page->instance()->problems(), 'a stored value the confirmed switch removes is not an error');
$page->call('save')->assertNotified('Nicht gespeichert');
$this->assertSame('portal', $ds->fresh()->process_overrides['booking']['payments'], 'without the confirmation nothing changes');
$page->set('acknowledge', true)->call('save')->assertNotified('Ablauf gespeichert');
$ds->refresh();
$this->assertSame('a3.portal_booking', $ds->process_overrides['booking']['model']);
$this->assertArrayNotHasKey('payments', $ds->process_overrides['booking']);
$this->assertArrayNotHasKey('guest_messages', $ds->process_overrides['booking'], 'a responsibility the model does not allow goes with it');
$this->assertSame('manual', $ds->process_overrides['booking']['contract_delivery'], 'unrelated stored settings stay');
$this->assertSame([], $page->instance()->conflicts(), 'the stale setting does not return as an error or conflict');
$this->assertSame([], $page->instance()->problems());
$this->assertArrayNotHasKey('payments', $page->get('drafts')['booking']['settings']);
}
/** A3 → A4 → A3 keeps the stored values consistent; stored values the target model accepts are untouched. */
public function test_switching_between_a3_and_a4_keeps_settings_consistent(): void
{
$ds = $this->channel('Destination Solution', []);
$configuration = app(ChannelProcessConfiguration::class);
$save = app(ConfigureChannelProcess::class);
$save->handle($ds, IntakeKind::Booking, ['model' => 'a3.portal_booking', 'capabilities' => ['collects_payment' => '0'], 'settings' => ['guest_messages' => 'landlord']], by: 'Lars');
$this->assertSame('landlord', $ds->fresh()->process_overrides['booking']['guest_messages']);
$save->handle($ds->fresh(), IntakeKind::Booking, ['model' => 'a4.portal_booking', 'capabilities' => ['collects_payment' => '1']], by: 'Lars');
$this->assertSame('a4.portal_booking', $ds->fresh()->process_overrides['booking']['model']);
$this->assertSame([], $configuration->conflicts($ds->fresh(), IntakeKind::Booking, 'a4.portal_booking'));
$save->handle($ds->fresh(), IntakeKind::Booking, ['model' => 'a3.portal_booking', 'capabilities' => ['collects_payment' => '0']], by: 'Lars');
$this->assertSame('a3.portal_booking', $ds->fresh()->process_overrides['booking']['model']);
}
/** A running case keeps its frozen plan when the channel model changes; other channels and intake kinds stay untouched. */
public function test_a_confirmed_switch_does_not_touch_running_cases_or_other_channels(): void
{
$ds = $this->channel('Destination Solution', ['booking' => ['payments' => 'portal']]);
$other = $this->channel('Sonstiges Portal', ['booking' => ['payments' => 'portal'], 'inquiry' => ['hold_hours' => 12]]);
$ds->update(['process_overrides' => [...$ds->process_overrides, 'inquiry' => ['hold_hours' => 24]]]);
$booking = app(TakeOverExternalBooking::class)->handle($this->property, $ds->fresh(), CarbonImmutable::parse('2026-12-05'), CarbonImmutable::parse('2026-12-09'), ['portal_reference' => 'DS-ALT']);
$frozen = $booking->fresh()->profile;
app(ConfigureChannelProcess::class)->handle($ds->fresh(), IntakeKind::Booking, ['model' => 'a3.portal_booking', 'capabilities' => ['collects_payment' => '0'], 'settings' => ['payments' => 'portal']], by: 'Lars', acknowledgeConflicts: true);
$this->assertSame($frozen, $booking->fresh()->profile, 'the running case keeps its frozen plan');
$this->assertSame('portal', $other->fresh()->process_overrides['booking']['payments'], 'another channel is untouched');
$this->assertSame(['hold_hours' => 24], array_intersect_key($ds->fresh()->process_overrides['inquiry'], ['hold_hours' => 1]), 'the other intake kind is untouched');
}
/** Switching a step off never drops the duty it served: the impact names dependants, what stays and the admissible ways. */
public function test_switching_off_the_data_request_shows_what_stays_and_the_alternatives_without_changing_anything(): void
{
$ds = $this->channel('Destination Solution', []);
$impact = app(ConfigurationImpact::class);
$this->assertSame([], $impact->of($ds, IntakeKind::Booking, ['model' => 'a3.portal_booking']), 'nothing switched off, nothing to report');
$draft = ['model' => 'a3.portal_booking', 'steps' => ['guest.data_request' => ['on' => false]]];
$result = collect($impact->of($ds, IntakeKind::Booking, $draft))->firstWhere('module', 'guest.data_request');
$this->assertTrue($result['valid']);
$this->assertSame([], $result['affected'], 'no step of this model waits for the data request: they carry on');
$this->assertContains('von Ihnen von Hand', $result['alternatives'], 'the admissible ways to obtain the data remain offered');
$this->assertStringContainsString('Es wird nichts anderes automatisch geändert', $result['suggestion']);
$this->assertSame([], app(ChannelProcessConfiguration::class)->problems($ds, IntakeKind::Booking, $draft), 'the combination is valid');
$mandatory = collect($impact->of($ds, IntakeKind::Booking, ['model' => 'a3.portal_booking', 'steps' => ['documents.send' => ['on' => false]]]))->firstWhere('module', 'documents.send');
$this->assertFalse($mandatory['valid']);
$this->assertStringContainsString('Pflicht', $mandatory['suggestion']);
$dependants = collect($impact->of($ds, IntakeKind::Booking, ['model' => 'a3.portal_booking', 'steps' => ['portal.confirmation' => ['on' => false]]]))->firstWhere('module', 'portal.confirmation');
$this->assertContains('Buchungsunterlagen (Vertragsdokumentation)', $dependants['affected'], 'the documents depend hard on the confirmation step');
$this->assertNotEmpty($dependants['remains']);
}
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', 'external.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']]);
}
}