Files
fewo-stommer/tests/Feature/OperationsModeTest.php
T
Lars StommerandClaude Opus 5 e6d4d89141 Mandanten Phase 3: Einstellungen, Texte und Dienste gehören dem Mandanten
Vermieterdaten, Bank, Steuerangabe, Adressen, Betriebsart und Fristen sind
Spalten von `tenants`. `App\Tenancy\TenantConfig` legt sie über
`config('fewo.*')` und `mail.from`, sobald der Mandantenkontext wechselt –
so lesen Vorlagen, Mails und der Mietvertrag weiter dieselben Schlüssel und
`resources/themes/**` bleibt unangetastet.

Die `.env` ist die Grundlinie: Rückfall, solange kein Mandant feststeht, und
Grenze für Löschfristen und Bot-Schutz (nur verschärfen, nicht lockern). Die
Flutgrenze bleibt global. Bank, Steuerangabe und eigene Dienst-Schlüssel
liegen verschlüsselt.

Die Seite System › Betrieb schreibt jetzt den Mandanten und enthält
zusätzlich Vermieterdaten, Bank und die Fristen. Tabelle `settings` und
Modell `Setting` entfallen; die Migration übernimmt die bisherigen Werte
aus `settings` und der Konfiguration in den Mandanten Stommer.

Neu: `tenant_services` (Freischaltung je Dienst mit optionalem eigenem
API-Schlüssel, nur im Betreiber-Bereich schaltbar) und `service_usages`.
Es zählt noch nichts, weil kein Dienst aufruft.

235 Feature-Tests grün. Der Umbauweg wurde zusätzlich auf einer Kopie der
lokalen Datenbank durchgespielt.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-10-23 20:49:22 +02:00

184 lines
7.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Enums\UserRole;
use App\Filament\Pages\Operations;
use App\Mail\NewReservation;
use App\Mail\ReservationReceived;
use App\Models\Property;
use App\Models\Tenant;
use App\Models\User;
use App\Pricing\StayRequest;
use App\Support\Settings;
use App\Tenancy\TenantConfig;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class OperationsModeTest extends TestCase
{
use RefreshDatabase;
public function test_new_installations_start_in_test_mode_with_the_notification_address(): void
{
config(['fewo.mail_redirect_to' => null, 'fewo.notification_email' => 'fewo@example.com']);
$this->landlord(['notification_emails' => null, 'test_recipient' => null]);
$this->assertFalse(Settings::isLive());
$this->assertSame('fewo@example.com', Settings::mailRedirect());
}
public function test_in_test_mode_every_mail_goes_to_the_test_recipient_only(): void
{
config(['fewo.mail_redirect_to' => null, 'mail.default' => 'array']);
$this->landlord(['operating_mode' => Settings::MODE_TEST, 'test_recipient' => 'test@example.com']);
Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->cc('vermieterin@example.com')->subject('Angebot'));
$sent = app(MailManager::class)->mailer('array')->getSymfonyTransport()->messages()->last();
$this->assertSame(['test@example.com'], array_map(fn ($address) => $address->getAddress(), $sent->getEnvelope()->getRecipients()));
}
public function test_switched_live_mails_reach_their_real_recipients(): void
{
config(['fewo.mail_redirect_to' => null, 'mail.default' => 'array']);
$this->landlord(['operating_mode' => Settings::MODE_LIVE]);
Mail::raw('Hallo', fn ($message) => $message->to('gast@example.com')->subject('Angebot'));
$sent = app(MailManager::class)->mailer('array')->getSymfonyTransport()->messages()->last();
$this->assertSame(['gast@example.com'], array_map(fn ($address) => $address->getAddress(), $sent->getEnvelope()->getRecipients()));
$this->assertNull(Settings::mailRedirect());
}
public function test_addresses_and_landlord_data_of_the_internal_communication_are_set_in_the_admin(): void
{
config(['fewo.mail_redirect_to' => null]);
$this->seed(PropertySeeder::class);
$tenant = Tenant::query()->where('slug', 'stommer')->firstOrFail();
app(TenantContext::class)->set($tenant);
$this->actingAs(User::factory()->create());
Livewire::test(Operations::class)
->set('data.mode', Settings::MODE_LIVE)
->set('data.notification_recipients', ['brigitte@example.com', 'lars@example.com'])
->set('data.contact_email', 'info@example.com')
->set('data.sender_name', 'FeWo Test')
->set('data.sender_email', 'versand@example.com')
->set('data.owner_name', 'Brigitte Beispiel')
->set('data.bank_iban', 'DE02120300000000202051')
->call('save')
->assertHasNoErrors();
$this->assertSame(['brigitte@example.com', 'lars@example.com'], Settings::notificationRecipients());
$this->assertSame(['info@example.com', 'versand@example.com', 'FeWo Test'], [config('fewo.owner.email'), config('mail.from.address'), config('mail.from.name')]);
$this->assertSame(['Brigitte Beispiel', 'DE02120300000000202051'], [config('fewo.owner.name'), config('fewo.bank.iban')]);
Mail::fake();
$booking = app(CreateReservation::class)->handle(Property::sole(), new StayRequest(CarbonImmutable::parse('2026-12-01'), CarbonImmutable::parse('2026-12-05'), 2), ['first_name' => 'Erika', 'last_name' => 'Muster', 'email' => 'erika@example.com']);
Mail::assertSent(NewReservation::class, fn (NewReservation $mail): bool => $mail->hasTo('brigitte@example.com') && $mail->hasTo('lars@example.com'));
Mail::assertSent(ReservationReceived::class, fn (ReservationReceived $mail): bool => $mail->hasReplyTo('info@example.com'));
$this->assertNotNull($booking);
}
public function test_the_bank_details_are_stored_encrypted(): void
{
$tenant = $this->landlord(['bank_iban' => 'DE02120300000000202051']);
$stored = DB::table('tenants')->where('id', $tenant->id)->value('bank_iban');
$this->assertNotSame('DE02120300000000202051', $stored);
$this->assertSame('DE02120300000000202051', $tenant->fresh()->bank_iban);
}
public function test_a_landlord_cannot_relax_a_retention_period_or_the_bot_protection(): void
{
config(['fewo.guest_retention_years' => 10, 'fewo.spam.per_ip_per_hour' => 5, 'fewo.spam.min_seconds' => 4]);
$this->landlord([
'guest_retention_years' => 25,
'spam_per_ip_per_hour' => 50,
'spam_min_seconds' => 1,
'deposit_due_days' => 21,
]);
$this->assertSame(10, config('fewo.guest_retention_years'));
$this->assertSame(5, config('fewo.spam.per_ip_per_hour'));
$this->assertSame(4, config('fewo.spam.min_seconds'));
$this->assertSame(21, config('fewo.deposit_due_days'));
}
public function test_a_landlord_may_tighten_a_retention_period(): void
{
config(['fewo.guest_retention_years' => 10, 'fewo.spam.min_seconds' => 4]);
$this->landlord(['guest_retention_years' => 3, 'spam_min_seconds' => 9]);
$this->assertSame(3, config('fewo.guest_retention_years'));
$this->assertSame(9, config('fewo.spam.min_seconds'));
}
public function test_invalid_notification_addresses_are_rejected(): void
{
$this->landlord();
$this->actingAs(User::factory()->create());
Livewire::test(Operations::class)
->set('data.notification_recipients', ['keine-adresse'])
->call('save')
->assertHasErrors();
}
public function test_server_override_always_wins(): void
{
config(['fewo.mail_redirect_to' => 'server@example.com']);
$this->landlord(['operating_mode' => Settings::MODE_LIVE]);
$this->assertSame('server@example.com', Settings::mailRedirect());
}
public function test_administrator_switches_the_mode_in_the_admin_and_sees_the_banner(): void
{
config(['fewo.mail_redirect_to' => null]);
$this->landlord();
$this->actingAs(User::factory()->create());
$this->get(Operations::getUrl())->assertOk()->assertSee('Testbetrieb:');
Livewire::test(Operations::class)
->set('data.mode', Settings::MODE_TEST)
->set('data.test_recipient', 'lars@example.com')
->call('save')
->assertHasNoErrors();
$this->assertSame('lars@example.com', Settings::mailRedirect());
Livewire::test(Operations::class)->set('data.mode', Settings::MODE_LIVE)->call('save')->assertNotified('Scharf geschaltet');
$this->assertTrue(Settings::isLive());
$this->actingAs(User::factory()->role(UserRole::Rentals)->create());
$this->get(Operations::getUrl())->assertForbidden();
}
/**
* The only landlord, with the operating data under test applied to the configuration.
*
* @param array<string, mixed> $attributes
*/
private function landlord(array $attributes = []): Tenant
{
$tenant = Tenant::factory()->create($attributes);
app(TenantContext::class)->set($tenant);
app(TenantConfig::class)->refresh();
return $tenant;
}
}