Files
fewo-stommer/tests/Feature/TenantSettingsTest.php
Lars StommerandClaude Opus 5 03ad4667ac Betreiber richtet Mandanten vollständig ein: Adressen, Sperrseite, Systemwerte
Ein neu angelegter Mandant war eine Sackgasse. Er bekam keine Website, also
keinen Host, also keine Adresse – und der Host wird im Mandanten-Admin
eingetragen, den er ohne Adresse nicht erreicht. „Im Admin öffnen" half
nicht: ohne Domain landete es beim Mandanten des aktuellen Hosts.

Einsprungsadressen gehören jetzt dem Betreiber, weil Webspace und Hosting
ihm gehören und ein Mandant nicht die Adresse eines anderen auf sich
zeigen lassen darf. Im Mandanten-Formular: Websites mit ihren Adressen.
Beim Anlegen entstehen Website, Host-Einträge und eine leere Startseite –
ohne Startseite antwortet die Website mit 404. Im Mandanten-Admin sind die
Adressen nur noch sichtbar, nicht änderbar. Die Domain beim Hoster auf das
Verzeichnis zu legen bleibt ein Schritt von Hand.

Gesperrter Mandant: statt 404 ein Hinweis mit Status 503, Text je Mandant
einstellbar. 503 hält die Seiten im Suchindex, weil die Sperre vorübergehend
ist.

Behoben: `URL::forceRootUrl(APP_URL)` zwang **alle** Links auf einen festen
Host. Mit einem Mandanten unsichtbar; mit zwei Domains hätte jeder interne
Link der zweiten Website auf die erste gezeigt. Der Ursprung ist jetzt der
Host der aufgelösten Website.

Neu: systemweite Einstellungen sind im Betreiber-Bereich änderbar
(Löschfristen, Bot-Schutz, Flutgrenze, Sicherungen, Portal-Fristen,
Versandweg, SMTP, harte Mail-Umleitung, Tokens). Leer heißt: der Wert der
.env gilt wieder – und zwar sofort, nicht erst beim nächsten Start.
Geheimnisse liegen verschlüsselt. Datenbank-Zugang und APP_KEY bleiben in
der .env, weil sie gebraucht werden, um die Datenbank zu öffnen und das
Gespeicherte zu entschlüsseln.

253 Feature-Tests grün.

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

190 lines
8.0 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\Enums\TenantServiceKey;
use App\Filament\Operator\Tenants\Pages\CreateTenant;
use App\Filament\Operator\Tenants\Pages\EditTenant;
use App\Models\Tenant;
use App\Models\User;
use App\Support\Content;
use App\Support\Settings;
use App\Tenancy\TenantConfig;
use App\Tenancy\TenantContext;
use App\Tenancy\TenantFeatures;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Phase 3: operating data and texts belong to the landlord, not to the installation.
*/
class TenantSettingsTest extends TestCase
{
use RefreshDatabase;
public function test_each_landlord_has_its_own_imprint_bank_and_operating_mode(): void
{
config(['fewo.mail_redirect_to' => null]);
$first = Tenant::factory()->create([
'bank_iban' => 'DE02120300000000202051',
'operating_mode' => Settings::MODE_LIVE,
'owner_name' => 'Brigitte Beispiel',
]);
$second = Tenant::factory()->create([
'bank_iban' => 'DE02500105170137075030',
'operating_mode' => Settings::MODE_TEST,
'test_recipient' => 'test@zweiter.example',
'owner_name' => 'Zweiter Vermieter',
]);
$context = app(TenantContext::class);
$context->set($first);
Content::save(['legal.imprint' => 'Impressum des ersten Vermieters']);
$this->assertSame('Impressum des ersten Vermieters', Content::get('legal.imprint'));
$this->assertSame('DE02120300000000202051', config('fewo.bank.iban'));
$this->assertSame('Brigitte Beispiel', config('fewo.owner.name'));
$this->assertTrue(Settings::isLive());
$this->assertNull(Settings::mailRedirect());
$context->set($second);
$this->assertNull(Content::get('legal.imprint'));
Content::save(['legal.imprint' => 'Impressum des zweiten Vermieters']);
$this->assertSame('DE02500105170137075030', config('fewo.bank.iban'));
$this->assertSame('Zweiter Vermieter', config('fewo.owner.name'));
$this->assertFalse(Settings::isLive());
$this->assertSame('test@zweiter.example', Settings::mailRedirect());
$context->set($first);
$this->assertSame('Impressum des ersten Vermieters', Content::get('legal.imprint'));
}
public function test_without_a_landlord_the_configuration_of_the_installation_applies(): void
{
config(['fewo.owner.name' => 'Aus der Konfiguration']);
$first = Tenant::factory()->create(['owner_name' => 'Brigitte Beispiel']);
Tenant::factory()->create();
$context = app(TenantContext::class);
$context->set($first);
$this->assertSame('Brigitte Beispiel', config('fewo.owner.name'));
$context->forget();
$this->assertSame('Aus der Konfiguration', config('fewo.owner.name'));
$this->assertNull(Settings::tenant());
}
public function test_only_services_the_application_really_asks_about_are_offered(): void
{
$this->assertSame(
[TenantServiceKey::PortalSync, TenantServiceKey::PortalMail],
TenantServiceKey::wired(),
);
foreach (TenantServiceKey::cases() as $service) {
if (! $service->isWired()) {
$this->assertFalse($service->enabledByDefault(), $service->value.' darf nicht vorgegeben an sein, solange nichts danach fragt.');
}
}
}
public function test_a_landlord_without_a_decision_keeps_the_portal_services(): void
{
$tenant = Tenant::factory()->create();
$this->assertSame(0, $tenant->services()->count());
$this->assertTrue(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $tenant));
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::AiOffers, $tenant));
}
public function test_the_operator_creates_a_landlord_in_the_operator_panel(): void
{
$existing = Tenant::factory()->create();
app(TenantContext::class)->set($existing);
$operator = User::factory()->create();
$operator->forceFill(['is_operator' => true])->saveQuietly();
$this->actingAs($operator);
Filament::setCurrentPanel('operator');
$this->get('/betreiber/tenants')->assertOk()->assertSee('Neuen Mandanten anlegen');
$this->get('/betreiber/tenants/create')->assertOk();
Livewire::test(CreateTenant::class)
->fillForm([
'name' => 'Ferienhaus Düne',
'slug' => 'duene',
'is_active' => true,
'websites' => [['name' => 'Ferienhaus Düne', 'hosts' => ['duene.example']]],
'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => true,
'services.'.TenantServiceKey::PortalMail->value.'.is_enabled' => false,
])
->call('create')
->assertHasNoErrors();
$created = Tenant::query()->where('slug', 'duene')->sole();
$this->assertSame('Ferienhaus Düne', $created->name);
$this->assertTrue(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $created));
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalMail, $created));
// A new landlord must not inherit anything from the one the operator happened to be looking at.
$this->assertNull($created->contact_email);
$this->assertSame(Settings::MODE_TEST, $created->operating_mode);
}
public function test_the_operator_switches_a_service_on_the_tenant_form(): void
{
$tenant = Tenant::factory()->create();
app(TenantContext::class)->set($tenant);
$operator = User::factory()->create();
$operator->forceFill(['is_operator' => true])->saveQuietly();
$this->actingAs($operator);
Filament::setCurrentPanel('operator');
Livewire::test(EditTenant::class, ['record' => $tenant->getRouteKey()])
->fillForm([
'services.'.TenantServiceKey::PortalSync->value.'.is_enabled' => false,
'services.'.TenantServiceKey::PortalMail->value.'.is_enabled' => true,
])
->call('save')
->assertHasNoErrors();
$this->assertFalse($tenant->services()->where('service', TenantServiceKey::PortalSync->value)->sole()->is_enabled);
$this->assertTrue($tenant->services()->where('service', TenantServiceKey::PortalMail->value)->sole()->is_enabled);
$this->assertSame(0, $tenant->services()->where('service', TenantServiceKey::AiOffers->value)->count());
$this->assertFalse(app(TenantFeatures::class)->enabled(TenantServiceKey::PortalSync, $tenant));
}
/**
* The data a new landlord starts with comes from the installation, never from the landlord that happens
* to be current – otherwise the migration would copy one landlord's address into the next one.
*/
public function test_the_defaults_of_a_new_landlord_come_from_the_configuration_not_from_the_current_one(): void
{
config(['fewo.owner.name' => 'Aus der Konfiguration', 'fewo.owner.email' => 'konfiguration@example.com']);
$current = Tenant::factory()->create(['owner_name' => 'Erster Vermieter', 'contact_email' => 'erster@example.com']);
app(TenantContext::class)->set($current);
$this->assertSame('Erster Vermieter', config('fewo.owner.name'));
$next = Tenant::factory()->create();
$this->assertSame('Aus der Konfiguration', $next->owner_name);
$this->assertSame('konfiguration@example.com', $next->contact_email);
}
public function test_the_landlord_of_the_context_decides_the_sender(): void
{
$tenant = Tenant::factory()->create(['sender_email' => 'versand@vermieter.example', 'sender_name' => 'Vermieter']);
app(TenantContext::class)->set($tenant);
app(TenantConfig::class)->refresh();
$this->assertSame(['versand@vermieter.example', 'Vermieter'], [config('mail.from.address'), config('mail.from.name')]);
}
}