Files
fewo-stommer/tests/Feature/GuestMailInboxTest.php
LarsandClaude Opus 5.5 ad63db78f3
Deploy FeWo mit PipelineManager / deploy (push) Successful in 5m6s
Posteingang: Antwortbezug ohne Klammern, Testadresse im Testbetrieb, sofortige Auswertung
- Antwortbezug (In-Reply-To/References) wird mit und ohne spitze Klammern
  verglichen – Strato liefert ihn ohne, gespeichert war er mit; Antworten
  blieben deshalb im Posteingang hängen.
- Testbetrieb: Die Testadresse steht zusammen mit der genannten
  Vorgangsnummer für den Gast dieses Vorgangs (alle Antworten kommen vom
  Testpostfach). Im Live-Betrieb gilt das nicht.
- Eine zugeordnete Nachricht wird sofort ausgewertet (automatisch und nach
  Zuordnung von Hand), nicht erst im Fünf-Minuten-Lauf; die Meldung nennt,
  was übernommen wurde. Neu: „Erneut auswerten“.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 17:39:11 +01:00

252 lines
13 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;
use App\Actions\CreateReservation;
use App\Actions\ReceiveInboundMessage;
use App\Channels\Mail\MailboxReader;
use App\Channels\Mail\PortalMail;
use App\Enums\BookingTaskKind;
use App\Filament\Pages\Operations;
use App\Filament\Resources\Bookings\Pages\ViewBooking;
use App\Filament\Resources\Bookings\RelationManagers\InboundMessagesRelationManager;
use App\Filament\Resources\InboundMessages\Pages\ListInboundMessages;
use App\Mail\DataRequested;
use App\Models\Booking;
use App\Models\BookingMessage;
use App\Models\BookingTask;
use App\Models\Channel;
use App\Models\DataRequest;
use App\Models\InboundMessage;
use App\Models\Mailbox;
use App\Models\PortalEmail;
use App\Models\Property;
use App\Models\User;
use App\Pricing\StayRequest;
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\Mail\MailManager;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Mockery;
use Tests\TestCase;
use Webklex\PHPIMAP\Client;
use Webklex\PHPIMAP\Folder;
use Webklex\PHPIMAP\Support\FolderCollection;
/**
* v0.11 package U5: the inbox of the guest communication mailbox – stored once, portal notifications through the
* unchanged portal path, deterministic assignment (CH-13) or clarification (M04), reply references (M07 part),
* test-mode mark (M10) and the landlord's clarification view.
*/
class GuestMailInboxTest 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();
Mail::fake();
Http::fake(['*' => Http::response('', 500)]);
}
public function test_a_reply_to_our_mail_is_assigned_and_stops_the_reminders(): void
{
Settings::tenant()->update(['process_defaults' => ['data_request_mode' => 'automatic']]);
$booking = $this->request();
BookingMessage::query()->create(['booking_id' => $booking->id, 'audience' => 'test', 'subject' => 'Rückfrage', 'message_id' => '<abc@fewo.test>', 'sent_at' => now()]);
$message = $this->receive(from: 'irgendwer@example.org', subject: 'Re: Ihre Anfrage', text: 'Strandweg 1, 18225 Kühlungsborn', inReplyTo: '<abc@fewo.test>');
$this->assertSame([InboundMessage::ASSIGNED, $booking->id, 'reply'], [$message->status, $message->booking_id, $message->match_reason]);
$this->assertNull($booking->openDataRequest()->next_reminder_at, 'the answer stops the reminder');
$this->assertSame(1, BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::InboundReview)->count());
$this->assertTrue($message->test_mode, 'arrived in test operation');
}
public function test_the_case_number_counts_only_with_the_guest_as_sender(): void
{
$booking = $this->request();
$own = $this->receive(from: 'gesa@example.org', subject: 'Frage zu '.$booking->reference, text: 'Hallo');
$foreign = $this->receive(from: 'fremd@example.org', subject: 'Frage zu '.$booking->reference, text: 'Hallo', id: '<2@x>');
$this->assertSame([InboundMessage::ASSIGNED, 'reference_and_sender'], [$own->status, $own->match_reason]);
$this->assertSame(InboundMessage::NEEDS_REVIEW, $foreign->status);
$this->assertSame($booking->id, $foreign->hints[0]['booking_id']);
}
/** CH-13 as specified: one open case of the same sender alone is not enough. */
public function test_a_direct_guest_mail_needs_sender_property_and_dates(): void
{
$booking = $this->request();
$vague = $this->receive(from: 'gesa@example.org', subject: 'Unsere Reise', text: 'Wir freuen uns schon!');
$precise = $this->receive(from: 'gesa@example.org', subject: 'Unsere Reise', text: 'Für den Aufenthalt vom 07.11.2026 bis 10.11.2026 noch eine Frage.', id: '<3@x>');
$this->assertSame(InboundMessage::NEEDS_REVIEW, $vague->status);
$this->assertSame('offener Vorgang dieses Absenders (07.11.–10.11.2026)', $vague->hints[0]['why']);
$this->assertSame([InboundMessage::ASSIGNED, $booking->id, 'sender_property_dates'], [$precise->status, $precise->booking_id, $precise->match_reason]);
}
public function test_dates_of_two_cases_of_the_same_guest_are_resolved_only_when_unambiguous(): void
{
$first = $this->request();
$second = $this->request(arrival: '2026-11-20', departure: '2026-11-23');
$one = $this->receive(from: 'gesa@example.org', subject: 'Frage', text: 'Zum 20.11.2026 – 23.11.2026: Bringen wir den Hund mit?');
$both = $this->receive(from: 'gesa@example.org', subject: 'Frage', text: '07.11.2026 bis 10.11.2026 und 20.11.2026 bis 23.11.2026', id: '<4@x>');
$this->assertSame($second->id, $one->booking_id);
$this->assertSame(InboundMessage::NEEDS_REVIEW, $both->status);
$this->assertNotSame($first->id, $one->booking_id);
}
public function test_a_message_is_stored_once_and_our_own_mails_are_ignored(): void
{
$this->receive(from: 'gast@example.org', subject: 'Hallo', text: 'Test');
$this->receive(from: 'gast@example.org', subject: 'Hallo', text: 'Test');
$own = $this->receive(from: (string) config('mail.from.address'), subject: 'Kopie', text: 'x', id: '<own@x>');
$this->assertSame(2, InboundMessage::query()->count());
$this->assertSame(InboundMessage::IGNORED, $own->status);
}
/** CH-08: the portal path stays as it is. */
public function test_a_portal_notification_goes_through_the_unchanged_portal_path(): void
{
Channel::factory()->for($this->property)->create(['name' => 'FeWo-direkt', 'settings' => ['email_senders' => 'fewo-direkt.de']]);
$message = $this->receive(from: 'noreply@fewo-direkt.de', subject: 'Neue Buchung', text: "Anreise: 07.12.2026\nAbreise: 14.12.2026\nBuchungsnummer: HA-1");
$portal = PortalEmail::query()->sole();
$this->assertSame([InboundMessage::PORTAL, $portal->id], [$message->status, $message->portal_email_id]);
$this->assertSame($message->id, $portal->inbound_message_id);
$this->assertNotNull($portal->blocked_period_id, 'the booking was blocked as before');
}
/** M04: a general message creates nothing; the landlord decides in the inbox. */
public function test_the_landlord_assigns_a_message_or_makes_a_new_request_of_it(): void
{
$this->actingAs(User::factory()->create());
$booking = $this->request();
$general = $this->receive(from: 'neu@example.org', subject: 'Haben Sie im Dezember frei?', text: 'Wir sind zwei Personen.');
$other = $this->receive(from: 'unbekannt@example.org', subject: 'Nachtrag', text: 'Bitte zuordnen', id: '<5@x>');
$this->assertSame(1, Booking::query()->count(), 'no case from a message by itself');
Livewire::test(ListInboundMessages::class)
->callAction(TestAction::make('assign')->table($other), ['booking_id' => $booking->id])
->callAction(TestAction::make('createInquiry')->table($general), ['property_id' => $this->property->id, 'arrival' => '2026-12-01', 'departure' => '2026-12-05', 'guests' => 2, 'first_name' => 'Nina', 'last_name' => 'Neu', 'email' => 'neu@example.org']);
$this->assertSame([InboundMessage::ASSIGNED, $booking->id], [$other->fresh()->status, $other->fresh()->booking_id]);
$created = Booking::query()->where('source', 'email')->sole();
$this->assertSame([InboundMessage::PROCESSED, $created->id], [$general->fresh()->status, $general->fresh()->booking_id]);
$this->assertNotNull($created->openDataRequest(), 'the new request asks for its missing address');
}
public function test_our_mails_keep_their_message_id_for_replies(): void
{
config(['mail.default' => 'array']);
Settings::tenant()->update(['test_recipient' => 'test@example.org']);
Mail::swap(new MailManager($this->app));
$booking = $this->request();
$request = $booking->dataRequests()->create(['tenant_id' => $booking->tenant_id, 'status' => DataRequest::OPEN, 'mode' => 'automatic', 'purpose' => 'offer', 'fields' => ['guest.street']]);
Mail::to('gesa@example.org')->send(new DataRequested($booking, $request, DataRequest::INITIAL, 'https://example.test'));
$this->assertMatchesRegularExpression('/^<.+@.+>$/', (string) BookingMessage::query()->where('booking_id', $booking->id)->latest('id')->value('message_id'));
}
public function test_the_mailbox_is_kept_as_a_record_and_full_mode_reads_every_message(): void
{
$this->actingAs(User::factory()->create());
Livewire::test(Operations::class)
->fillForm(['mailbox_host' => 'imap.example.org', 'mailbox_username' => 'anfragen@example.org', 'mailbox_password' => 'geheim', 'mailbox_mode' => Mailbox::FULL, 'mailbox_address' => 'anfragen@example.org'])
->call('save')->assertHasNoFormErrors();
$mailbox = Mailbox::query()->sole();
$this->assertSame([Mailbox::FULL, 'anfragen@example.org', 'geheim'], [$mailbox->mode, $mailbox->address, $mailbox->password]);
$this->assertSame('anfragen@example.org', DataRequested::replyAddress(), 'questions ask for answers to this mailbox');
$reader = Mockery::mock(MailboxReader::class);
$reader->shouldReceive('fetchAll')->once()->andReturn([new PortalMail('<9@x>', 'gast@example.org', 'Hallo', 'Text', CarbonImmutable::now())]);
$this->app->instance(MailboxReader::class, $reader);
$this->artisan('channels:fetch-mail')->assertSuccessful();
$this->assertSame(InboundMessage::NEEDS_REVIEW, InboundMessage::query()->sole()->status);
$this->assertNotNull($mailbox->fresh()->last_fetched_at);
}
/** Test operation: every answer comes from the test mailbox, which then stands for the guest of the named case. */
public function test_in_test_operation_the_test_address_counts_as_the_guest_of_the_named_case(): void
{
Settings::tenant()->update(['test_recipient' => 'test@example.org']);
$booking = $this->request();
$message = $this->receive(from: 'test@example.org', subject: 'Re: Ihre Anfrage '.$booking->reference, text: 'Strandweg 1');
$this->assertSame([InboundMessage::ASSIGNED, $booking->id, 'reference_test_sender'], [$message->status, $message->booking_id, $message->match_reason]);
Settings::tenant()->update(['operating_mode' => 'live']);
$live = $this->receive(from: 'test@example.org', subject: 'Re: Ihre Anfrage '.$booking->reference, text: 'Strandweg 1', id: '<live@x>');
$this->assertSame(InboundMessage::NEEDS_REVIEW, $live->status, 'in live operation the test address is nobody special');
}
public function test_the_mailbox_can_be_checked_and_incoming_mails_show_on_the_case(): void
{
$this->actingAs(User::factory()->create());
Mailbox::query()->create(['tenant_id' => Settings::tenant()->id, 'host' => 'imap.example.org', 'username' => 'anfragen@example.org', 'password' => 'x', 'mode' => Mailbox::FULL, 'is_active' => true]);
$reader = Mockery::mock(MailboxReader::class);
$reader->shouldReceive('probe')->once()->andReturn(7);
$this->app->instance(MailboxReader::class, $reader);
Livewire::test(Operations::class)->call('checkMailbox')->assertNotified('Verbindung in Ordnung');
$booking = $this->request();
$this->receive(from: 'gesa@example.org', subject: 'Frage zu '.$booking->reference, text: 'Hallo');
Livewire::test(InboundMessagesRelationManager::class, ['ownerRecord' => $booking, 'pageClass' => ViewBooking::class])
->assertSee('Frage zu '.$booking->reference);
}
public function test_the_inbox_is_found_under_its_web_mail_name_and_an_unknown_folder_is_explained(): void
{
$folder = function (string $path): Folder {
$folder = Mockery::mock(Folder::class);
$folder->path = $path;
$folder->name = $path;
return $folder;
};
$client = Mockery::mock(Client::class);
$client->shouldReceive('getFolders')->andReturn(FolderCollection::make([$folder('Trash'), $folder('Sent Items'), $folder('INBOX')]));
foreach (['Posteingang', '', 'inbox', 'INBOX'] as $name) {
$this->assertSame('INBOX', MailboxReader::folder($client, $name)->path);
}
$this->assertSame('Sent Items', MailboxReader::folder($client, 'sent items')->path);
$this->expectExceptionMessage('Vorhanden: Trash, Sent Items, INBOX');
MailboxReader::folder($client, 'Archiv');
}
private function request(string $arrival = '2026-11-07', string $departure = '2026-11-10'): Booking
{
return app(CreateReservation::class)->handle($this->property->fresh(), new StayRequest(CarbonImmutable::parse($arrival), CarbonImmutable::parse($departure), 2), ['first_name' => 'Gesa', 'last_name' => 'Gast', 'email' => 'gesa@example.org'])->fresh();
}
private function receive(string $from, string $subject, string $text, ?string $inReplyTo = null, string $id = '<1@x>'): InboundMessage
{
return app(ReceiveInboundMessage::class)->handle(new PortalMail($id, $from, $subject, $text, CarbonImmutable::now(), inReplyTo: $inReplyTo))->fresh();
}
}