**Phase 4 – was im Hintergrund lief, kannte keinen Mandanten.** `App\Tenancy\TenantRunner` führt jeden Lauf einmal je aktivem Mandanten aus, mit gesetztem Kontext. Ohne ihn sah der Scheduler bei zwei Mandanten gar keinen: Abfragen filterten nicht mehr und der Absender fiel auf die .env zurück – eine Erinnerung des einen Vermieters wäre unter der Adresse des anderen verschickt worden. Seit gestern gibt es zwei Mandanten; nur weil der Cronjob auf Strato noch nicht eingerichtet ist, ist das bisher niemandem passiert. Betroffen: Vormerkungen, Portal-Abgleich, Portal-Postfach, Überwachung, Erinnerungen, Bewertungsbitten, Anonymisierung. Die Datenbank-Sicherung bleibt global – eine Datenbank, ein Dump. Ein Mandant, dessen Lauf scheitert, hält die anderen nicht auf. Das Postfach gehört jetzt dem Mandanten (`mailbox_*`, Passwort verschlüsselt, leerer Server = aus). Es steht im Mandanten-Admin und nur, wenn der Betreiber den Dienst freigeschaltet hat. Auch die Zustands- Schlüssel im Cache tragen den Mandanten – sonst meldeten zwei Mandanten einander ihre letzte erfolgreiche Abholung. **Phase 5 – Kündigung.** `App\Tenancy\TenantArchive` mit zwei Befehlen des Betreibers: `tenants:export <slug>` schreibt alle Zeilen und Dateien in ein Zip, `tenants:delete <slug>` exportiert erst, fragt nach und löscht dann in der Reihenfolge, die die Fremdschlüssel erlauben, Medien eingeschlossen. Konten, die nur zu diesem Mandanten gehörten, gehen mit – ein Konto, das nirgends mehr hineinkommt, ist personenbezogenes Datum ohne Zweck. Wer noch für einen anderen Mandanten arbeitet, und der Betreiber, bleiben. Der Export nennt die Konten mit Rolle, aber ohne Passwort-Hashes und ohne 2FA-Geheimnisse: er wird herausgegeben. Löschen ist bewusst keine Schaltfläche im Panel. `TenantSeparationTest` füllt jeden Bereich aus A1 für zwei Mandanten, exportiert und löscht einen und prüft, dass der andere Zeile für Zeile unverändert ist – auch die Hosts, die über die Website am Mandanten hängen und keine eigene tenant_id haben. Dieser Test merkt künftig, wenn eine neue Tabelle ihre tenant_id vergisst. 262 Feature-Tests grün. Der Export einmal auf echten Daten ausgeführt (2,3 MB mit Fotos); das Löschen nicht, weil es Dateien aus dem echten Speicher entfernt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
213 lines
9.6 KiB
PHP
213 lines
9.6 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Actions\MonitorChannels;
|
||
use App\Actions\ProcessPortalMail;
|
||
use App\Actions\SyncChannel;
|
||
use App\Channels\Mail\PortalMail;
|
||
use App\Channels\Mail\PortalMailParser;
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\ChannelTaskAction;
|
||
use App\Enums\PortalEmailStatus;
|
||
use App\Enums\PortalEmailType;
|
||
use App\Filament\Resources\PortalEmails\Pages\ListPortalEmails;
|
||
use App\Mail\ChannelConflict;
|
||
use App\Mail\MailboxAlert;
|
||
use App\Mail\PortalMailNotification;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use App\Models\ChannelTask;
|
||
use App\Models\PortalEmail;
|
||
use App\Models\Property;
|
||
use App\Models\User;
|
||
use App\Support\Settings;
|
||
use Carbon\CarbonImmutable;
|
||
use Database\Seeders\PropertySeeder;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Livewire\Livewire;
|
||
use PHPUnit\Framework\Attributes\DataProvider;
|
||
use Tests\TestCase;
|
||
|
||
class PortalMailTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private const BOOKING_MAIL = <<<'TXT'
|
||
Hallo Brigitte,
|
||
gute Nachrichten! Sie haben eine neue Buchung erhalten.
|
||
Gast: Max Mustermann
|
||
Anreise: Sa., 7. Nov. 2026
|
||
Abreise: Di., 10. Nov. 2026
|
||
Reisende: 2 Erwachsene
|
||
Buchungsnummer: HA-7X3K9Q
|
||
Ihr FeWo-direkt Team
|
||
TXT;
|
||
|
||
private Property $property;
|
||
|
||
private Channel $fewo;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
|
||
$this->seed(PropertySeeder::class);
|
||
$this->property = Property::sole();
|
||
$this->fewo = Channel::factory()->for($this->property)->create([
|
||
'name' => 'FeWo-direkt',
|
||
'settings' => ['import_url' => 'https://www.fewo-direkt.de/icalendar/x.ics', 'email_senders' => 'fewo-direkt.de, vrbo.com'],
|
||
]);
|
||
Mail::fake();
|
||
}
|
||
|
||
/**
|
||
* @return array<string, array{string, string, PortalEmailType, ?string, ?string, ?string}>
|
||
*/
|
||
public static function mailProvider(): array
|
||
{
|
||
return [
|
||
'deutsche Buchung' => ['Neue Buchung: Ferienwohnung Stommer', self::BOOKING_MAIL, PortalEmailType::Booking, '2026-11-07', '2026-11-10', 'HA-7X3K9Q'],
|
||
'englische Buchung' => ['Booking confirmed – Instant Booking', "Check-in: Nov 7, 2026\nCheck-out: Nov 10, 2026\nReservation ID: HA-ABCDE1", PortalEmailType::Booking, '2026-11-07', '2026-11-10', 'HA-ABCDE1'],
|
||
'numerische Daten' => ['Buchungsbestätigung', 'Ihr Gast reist vom 07.11.2026 bis zum 10.11.2026 an.', PortalEmailType::Booking, '2026-11-07', '2026-11-10', null],
|
||
'Anfrage ist keine Buchung' => ['Neue Buchungsanfrage von Max', self::BOOKING_MAIL, PortalEmailType::Inquiry, '2026-11-07', '2026-11-10', 'HA-7X3K9Q'],
|
||
'Stornierung' => ['Buchung storniert: HA-7X3K9Q', 'Die Buchung vom 07.11.2026 bis 10.11.2026 wurde storniert.', PortalEmailType::Cancellation, '2026-11-07', '2026-11-10', 'HA-7X3K9Q'],
|
||
'unbekannt' => ['Ihr monatlicher Bericht', 'Statistik für Oktober 2026', PortalEmailType::Unknown, null, null, null],
|
||
];
|
||
}
|
||
|
||
#[DataProvider('mailProvider')]
|
||
public function test_parser_recognises_portal_mails(string $subject, string $text, PortalEmailType $type, ?string $arrival, ?string $departure, ?string $reference): void
|
||
{
|
||
$parsed = (new PortalMailParser)->parse($subject, $text);
|
||
|
||
$this->assertSame([$type, $arrival, $departure, $reference], [$parsed->type, $parsed->startsOn?->toDateString(), $parsed->endsOn?->toDateString(), $parsed->reference]);
|
||
}
|
||
|
||
public function test_booking_mail_blocks_immediately_and_informs_other_manual_portals(): void
|
||
{
|
||
$ostseeklar = Channel::factory()->for($this->property)->create(['name' => 'Ostseeklar', 'requires_manual_blocking' => true]);
|
||
|
||
$record = $this->process('Neue Buchung', self::BOOKING_MAIL);
|
||
|
||
$this->assertSame(PortalEmailStatus::Blocked, $record->status);
|
||
$block = $this->fewo->blockedPeriods()->sole();
|
||
$this->assertSame(['email', '2026-11-07', '2026-11-10', 'email:HA-7X3K9Q'], [$block->source, $block->starts_on->toDateString(), $block->ends_on->toDateString(), $block->external_uid]);
|
||
$this->assertSame(ChannelTaskAction::Block, $ostseeklar->tasks()->sole()->action);
|
||
Mail::assertSent(PortalMailNotification::class);
|
||
|
||
$this->assertNull($this->process('Neue Buchung', self::BOOKING_MAIL), 'same message is processed only once');
|
||
$this->assertSame(PortalEmailStatus::AlreadyKnown, $this->process('Neue Buchung', self::BOOKING_MAIL, 'other-id')->status);
|
||
$this->assertNull($this->process('Neue Buchung', self::BOOKING_MAIL, 'x', 'news@unbekannt.de'));
|
||
}
|
||
|
||
public function test_overlap_with_an_existing_booking_raises_a_conflict(): void
|
||
{
|
||
Booking::factory()->for($this->property)->create(['arrival' => '2026-11-09', 'departure' => '2026-11-12', 'status' => BookingStatus::Confirmed]);
|
||
|
||
$this->process('Neue Buchung', self::BOOKING_MAIL);
|
||
|
||
Mail::assertSent(ChannelConflict::class);
|
||
}
|
||
|
||
public function test_calendar_sync_confirms_the_mail_block_without_creating_new_tasks(): void
|
||
{
|
||
Channel::factory()->for($this->property)->create(['name' => 'Ostseeklar', 'requires_manual_blocking' => true]);
|
||
$this->process('Neue Buchung', self::BOOKING_MAIL);
|
||
$tasksBefore = ChannelTask::count();
|
||
|
||
$this->fakeFeed("UID:vrbo-123\r\nDTSTART;VALUE=DATE:20261107\r\nDTEND;VALUE=DATE:20261110");
|
||
(new SyncChannel)->handle($this->fewo);
|
||
|
||
$block = $this->fewo->blockedPeriods()->sole();
|
||
$this->assertSame(['channel', 'vrbo-123'], [$block->source, $block->external_uid]);
|
||
$this->assertSame($tasksBefore, ChannelTask::count());
|
||
}
|
||
|
||
public function test_calendar_entry_with_other_dates_replaces_the_mail_block(): void
|
||
{
|
||
$this->process('Neue Buchung', self::BOOKING_MAIL);
|
||
|
||
$this->fakeFeed("UID:vrbo-123\r\nDTSTART;VALUE=DATE:20261107\r\nDTEND;VALUE=DATE:20261111");
|
||
(new SyncChannel)->handle($this->fewo);
|
||
|
||
$this->assertSame(['channel', '2026-11-11'], [$this->fewo->blockedPeriods()->sole()->source, $this->fewo->blockedPeriods()->sole()->ends_on->toDateString()]);
|
||
}
|
||
|
||
public function test_mail_blocks_survive_a_calendar_sync_that_does_not_know_them_yet(): void
|
||
{
|
||
$this->process('Neue Buchung', self::BOOKING_MAIL);
|
||
|
||
$this->fakeFeed('');
|
||
(new SyncChannel)->handle($this->fewo);
|
||
|
||
$this->assertSame(1, $this->fewo->blockedPeriods()->count());
|
||
}
|
||
|
||
public function test_cancellation_releases_the_mail_block(): void
|
||
{
|
||
$this->process('Neue Buchung', self::BOOKING_MAIL);
|
||
|
||
$record = $this->process('Buchung storniert: HA-7X3K9Q', 'Die Buchung wurde storniert.', 'cancel-1');
|
||
|
||
$this->assertSame(PortalEmailStatus::Released, $record->status);
|
||
$this->assertSame(0, $this->fewo->blockedPeriods()->count());
|
||
}
|
||
|
||
public function test_booking_without_readable_dates_needs_review_and_can_be_blocked_in_the_admin(): void
|
||
{
|
||
$record = $this->process('Neue Buchung', 'Details finden Sie in Ihrem Vermieterkonto.');
|
||
|
||
$this->assertSame(PortalEmailStatus::NeedsReview, $record->status);
|
||
$this->assertSame(0, $this->fewo->blockedPeriods()->count());
|
||
Mail::assertSent(PortalMailNotification::class);
|
||
|
||
$this->actingAs(User::factory()->create());
|
||
Livewire::test(ListPortalEmails::class)
|
||
->assertCanSeeTableRecords([$record])
|
||
->callTableAction('block', $record, ['starts_on' => '2026-11-20', 'ends_on' => '2026-11-23']);
|
||
|
||
$this->assertSame(PortalEmailStatus::Blocked, $record->fresh()->status);
|
||
$this->assertSame('2026-11-20', $this->fewo->blockedPeriods()->sole()->starts_on->toDateString());
|
||
}
|
||
|
||
public function test_n8n_can_push_mails_with_a_signed_request(): void
|
||
{
|
||
config(['fewo.automation.secret' => 'geheim']);
|
||
$body = json_encode(['message_id' => 'n8n-1', 'from' => 'noreply@fewo-direkt.de', 'subject' => 'Neue Buchung', 'text' => self::BOOKING_MAIL]);
|
||
|
||
$this->call('POST', route('channels.portal-mail'), [], [], [], ['HTTP_X-Signature' => 'falsch', 'CONTENT_TYPE' => 'application/json'], $body)->assertForbidden();
|
||
$this->call('POST', route('channels.portal-mail'), [], [], [], ['HTTP_X-Signature' => hash_hmac('sha256', $body, 'geheim'), 'CONTENT_TYPE' => 'application/json'], $body)
|
||
->assertOk()
|
||
->assertJson(['processed' => true, 'type' => 'booking', 'status' => 'blocked']);
|
||
|
||
$this->assertSame(1, PortalEmail::count());
|
||
}
|
||
|
||
public function test_mailbox_outage_is_alerted_once(): void
|
||
{
|
||
// The mailbox belongs to the landlord now, not to the server's .env.
|
||
Settings::tenant()?->update(['mailbox_host' => 'imap.strato.de', 'mailbox_username' => 'fewo@stommer.de']);
|
||
|
||
$this->fewo->update(['settings' => ['email_senders' => 'fewo-direkt.de']]);
|
||
|
||
$this->assertSame(1, (new MonitorChannels)->handle());
|
||
$this->assertSame(0, (new MonitorChannels)->handle());
|
||
Mail::assertSent(MailboxAlert::class, 1);
|
||
}
|
||
|
||
private function process(string $subject, string $text, string $messageId = 'msg-1', string $from = 'noreply@fewo-direkt.de'): ?PortalEmail
|
||
{
|
||
return app(ProcessPortalMail::class)->handle(new PortalMail($messageId, $from, $subject, $text, CarbonImmutable::now()));
|
||
}
|
||
|
||
private function fakeFeed(string $event): void
|
||
{
|
||
$body = "BEGIN:VCALENDAR\r\n".($event !== '' ? "BEGIN:VEVENT\r\n{$event}\r\nEND:VEVENT\r\n" : '').'END:VCALENDAR';
|
||
Http::fake(['*' => Http::response($body)]);
|
||
}
|
||
}
|