travelTo(CarbonImmutable::parse('2026-11-01 09:00')); Mail::fake(); } public function test_the_runner_visits_every_active_landlord_with_that_landlord_current(): void { $first = Tenant::factory()->create(['name' => 'Erster']); $second = Tenant::factory()->create(['name' => 'Zweiter']); Tenant::factory()->create(['name' => 'Gesperrt', 'is_active' => false]); $seen = []; $succeeded = app(TenantRunner::class)->each(function (Tenant $tenant) use (&$seen): void { $seen[] = [$tenant->id, app(TenantContext::class)->id()]; }); $this->assertSame(2, $succeeded); $this->assertSame([[$first->id, $first->id], [$second->id, $second->id]], $seen); $this->assertNull(app(TenantContext::class)->id(), 'Nach dem Lauf ist kein Mandant mehr gesetzt.'); } public function test_a_failing_landlord_does_not_stop_the_others(): void { Tenant::factory()->create(); Tenant::factory()->create(); $visited = 0; $succeeded = app(TenantRunner::class)->each(function () use (&$visited): void { $visited++; if ($visited === 1) { throw new \RuntimeException('Postfach nicht erreichbar'); } }); $this->assertSame(2, $visited); $this->assertSame(1, $succeeded); } /** * The test the plan asks for: a run for A must not write to B's guests, nor under B's sender. */ public function test_a_run_touches_only_its_own_landlord_and_uses_their_sender(): void { [$a, $bookingA] = $this->landlordWithExpiredHold('Nordhaus', 'nord@example.com', 'Absender Nord '); [$b, $bookingB] = $this->landlordWithExpiredHold('Südheim', 'sued@example.com', 'Absender Süd '); $senders = []; app(TenantRunner::class)->each(function (Tenant $tenant) use (&$senders): void { $senders[$tenant->name] = [config('mail.from.address'), config('fewo.owner.name')]; app(ExpireReservations::class)->handle(); }); $this->assertSame(['nord@example.com', 'Nordhaus'], $senders['Nordhaus']); $this->assertSame(['sued@example.com', 'Südheim'], $senders['Südheim']); // Each landlord is told about their own expired hold only. Mail::assertSent(ReservationExpired::class, 2); Mail::assertSent(ReservationExpired::class, fn (ReservationExpired $mail): bool => $mail->hasTo('nord@example.com')); Mail::assertSent(ReservationExpired::class, fn (ReservationExpired $mail): bool => $mail->hasTo('sued@example.com')); $this->assertSame('expired', $bookingA->fresh()->status->value); $this->assertSame('expired', $bookingB->fresh()->status->value); $this->assertNotSame($a->id, $b->id); } public function test_the_mailbox_belongs_to_the_landlord_and_is_unreadable_from_another_one(): void { $withBox = Tenant::factory()->create([ 'mailbox_host' => 'imap.nord.example', 'mailbox_username' => 'post@nord.example', 'mailbox_password' => 'geheim', ]); $without = Tenant::factory()->create(); app(TenantContext::class)->set($withBox); $this->assertTrue(MailboxReader::isConfigured()); $this->assertSame('imap.nord.example', MailboxReader::configuration()['host']); $this->assertSame('geheim', MailboxReader::configuration()['password']); app(TenantContext::class)->set($without); $this->assertFalse(MailboxReader::isConfigured(), 'Ein Mandant ohne Postfach sieht keines – auch nicht das fremde.'); $this->assertNull(MailboxReader::configuration()); // Stored encrypted, like the bank details. $this->assertNotSame('geheim', DB::table('tenants')->where('id', $withBox->id)->value('mailbox_password')); } public function test_the_ical_token_of_one_landlord_does_not_serve_another_ones_occupancy(): void { $nord = $this->websiteFor(Tenant::factory()->create(['name' => 'Nordhaus']), 'nord.test'); $sued = $this->websiteFor(Tenant::factory()->create(['name' => 'Südheim']), 'sued.test'); $channel = app(TenantContext::class)->run($nord->tenant, fn (): Channel => Channel::factory()->create([ 'property_id' => Property::factory()->create(['site_id' => $nord->id])->id, ])); // On its own address the feed answers … $this->get('http://nord.test'.route('channels.feed', ['token' => $channel->export_token], absolute: false))->assertOk(); // … on the other landlord's address it does not exist. $this->get('http://sued.test'.route('channels.feed', ['token' => $channel->export_token], absolute: false))->assertNotFound(); $this->assertNotNull($sued); } /** * @return array{Tenant, Booking} */ private function landlordWithExpiredHold(string $name, string $email, string $sender): array { $tenant = Tenant::factory()->create([ 'name' => $name, 'owner_name' => $name, 'notification_emails' => $email, 'sender_email' => $email, 'sender_name' => 'Absender '.$name, 'operating_mode' => 'live', ]); $booking = app(TenantContext::class)->run($tenant, fn (): Booking => Booking::factory() ->for(Property::factory()->create()) ->reserved(now()->subHour()) ->create()); return [$tenant, $booking]; } private function websiteFor(Tenant $tenant, string $host): Site { return app(TenantContext::class)->run($tenant, fn (): Site => Site::factory()->create([ 'tenant_id' => $tenant->id, 'name' => $tenant->name, 'domain' => $host, 'is_default' => false, ])); } }