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>
160 lines
5.5 KiB
PHP
160 lines
5.5 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Filament\Operator\Tenants\Pages\CreateTenant;
|
||
use App\Filament\Operator\Tenants\Pages\EditTenant;
|
||
use App\Models\Page;
|
||
use App\Models\Site;
|
||
use App\Models\Tenant;
|
||
use App\Models\User;
|
||
use App\Tenancy\TenantContext;
|
||
use Filament\Facades\Filament;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Livewire\Livewire;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Setting a landlord up is the operator's job: the hosting and the addresses are theirs. A landlord the
|
||
* operator created must be reachable straight away – otherwise nobody can get in to finish the job.
|
||
*/
|
||
class TenantProvisioningTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->withoutVite();
|
||
}
|
||
|
||
public function test_a_landlord_created_by_the_operator_is_reachable_at_once(): void
|
||
{
|
||
$this->asOperator();
|
||
|
||
Livewire::test(CreateTenant::class)
|
||
->fillForm([
|
||
'name' => 'Ferienhaus Düne',
|
||
'slug' => 'duene',
|
||
'is_active' => true,
|
||
'websites' => [['name' => 'Ferienhaus Düne', 'hosts' => ['duene.test', 'www.duene.test']]],
|
||
])
|
||
->call('create')
|
||
->assertHasNoErrors();
|
||
|
||
$tenant = Tenant::query()->where('slug', 'duene')->sole();
|
||
$site = Site::withoutGlobalScope('tenant')->where('tenant_id', $tenant->id)->sole();
|
||
|
||
$this->assertSame(['duene.test', 'www.duene.test'], $site->domains()->orderBy('host')->pluck('host')->all());
|
||
$this->assertSame('duene.test', $site->domain, 'Die erste Adresse ist die Hauptadresse.');
|
||
|
||
// Without a home page the website would answer 404 and the landlord could never get in.
|
||
$this->assertTrue(
|
||
app(TenantContext::class)->run($tenant, fn (): bool => $site->pages()->where('slug', Page::HOME)->exists()),
|
||
);
|
||
|
||
$this->get('http://duene.test/')->assertOk()->assertSee('Ferienhaus Düne');
|
||
$this->get('http://www.duene.test/')->assertOk();
|
||
}
|
||
|
||
public function test_a_suspended_landlord_shows_a_notice_instead_of_a_dead_link(): void
|
||
{
|
||
$tenant = Tenant::factory()->create(['is_active' => true]);
|
||
$site = $this->websiteFor($tenant, 'duene.test');
|
||
$this->assertNotNull($site);
|
||
|
||
Tenant::factory()->create(); // a second landlord, so hosts really decide
|
||
|
||
$this->get('http://duene.test/')->assertOk();
|
||
|
||
$tenant->update(['is_active' => false, 'suspended_message' => 'Die Vermietung pausiert bis Frühjahr.']);
|
||
|
||
$response = $this->get('http://duene.test/');
|
||
$response->assertStatus(503);
|
||
$response->assertSee('Die Vermietung pausiert bis Frühjahr.');
|
||
$response->assertSee('nicht erreichbar');
|
||
}
|
||
|
||
public function test_the_default_notice_applies_when_none_was_written(): void
|
||
{
|
||
$tenant = Tenant::factory()->create(['is_active' => false]);
|
||
$this->websiteFor($tenant, 'duene.test');
|
||
Tenant::factory()->create();
|
||
|
||
$this->get('http://duene.test/')->assertStatus(503)->assertSee('Bitte versuchen Sie es später noch einmal.');
|
||
}
|
||
|
||
/**
|
||
* Links on a website have to carry the address the visitor arrived on. APP_URL is a single value, so
|
||
* without this every link of a second landlord would point at the first landlord's host.
|
||
*/
|
||
public function test_links_carry_the_address_the_visitor_came_in_on(): void
|
||
{
|
||
config(['app.url' => 'https://erster.test']);
|
||
$first = Tenant::factory()->create();
|
||
$this->websiteFor($first, 'erster.test');
|
||
|
||
$second = Tenant::factory()->create();
|
||
$this->websiteFor($second, 'zweiter.test');
|
||
|
||
$html = $this->get('http://zweiter.test/')->assertOk()->getContent();
|
||
|
||
$this->assertStringNotContainsString('erster.test', (string) $html);
|
||
}
|
||
|
||
public function test_the_operator_adds_an_address_to_an_existing_landlord(): void
|
||
{
|
||
$tenant = Tenant::factory()->create();
|
||
$site = $this->websiteFor($tenant, 'duene.test');
|
||
Tenant::factory()->create();
|
||
|
||
$this->get('http://neu.duene.test/')->assertNotFound();
|
||
|
||
$this->asOperator();
|
||
|
||
Livewire::test(EditTenant::class, ['record' => $tenant->getRouteKey()])
|
||
->fillForm(['websites' => [[
|
||
'id' => $site->id,
|
||
'name' => $site->name,
|
||
'hosts' => ['duene.test', 'neu.duene.test'],
|
||
]]])
|
||
->call('save')
|
||
->assertHasNoErrors();
|
||
|
||
$this->get('http://neu.duene.test/')->assertOk();
|
||
$this->get('http://duene.test/')->assertOk();
|
||
}
|
||
|
||
private function asOperator(): void
|
||
{
|
||
$operator = User::factory()->create();
|
||
$operator->forceFill(['is_operator' => true])->saveQuietly();
|
||
$this->actingAs($operator);
|
||
Filament::setCurrentPanel('operator');
|
||
}
|
||
|
||
private function websiteFor(Tenant $tenant, string $host): Site
|
||
{
|
||
return app(TenantContext::class)->run($tenant, function () use ($tenant, $host): Site {
|
||
$site = Site::query()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'name' => 'Website von '.$tenant->name,
|
||
'domain' => $host,
|
||
'is_default' => true,
|
||
'theme' => config('themes.base'),
|
||
]);
|
||
|
||
Page::query()->create([
|
||
'site_id' => $site->id,
|
||
'slug' => Page::HOME,
|
||
'title' => $site->name,
|
||
'sections' => [],
|
||
'is_published' => true,
|
||
]);
|
||
|
||
return $site;
|
||
});
|
||
}
|
||
}
|