Dazu ein genauerer Testname: Links tragen die Hauptadresse ihrer Website, nicht die Adresse, über die der Besucher kam. Das ist der kanonische Host und war vorher falsch benannt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
170 lines
6.2 KiB
PHP
170 lines
6.2 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\SiteDomain;
|
||
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 carry the main address of the website they belong to – not APP_URL, which is a single value and
|
||
* would have sent every visitor of a second landlord to the first landlord's host. Visiting through a
|
||
* further address of the same website still yields the main address, which keeps one canonical host.
|
||
*/
|
||
public function test_links_carry_the_main_address_of_their_own_website(): 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 = (string) $this->get('http://zweiter.test/')->assertOk()->getContent();
|
||
|
||
$this->assertStringNotContainsString('erster.test', $html);
|
||
$this->assertStringContainsString('zweiter.test', $html);
|
||
|
||
// A further address of the same website keeps pointing at its main address.
|
||
SiteDomain::query()->create(['site_id' => Site::withoutGlobalScope('tenant')->where('tenant_id', $second->id)->value('id'), 'host' => 'www.zweiter.test']);
|
||
|
||
$viaWww = (string) $this->get('http://www.zweiter.test/')->assertOk()->getContent();
|
||
|
||
$this->assertStringContainsString('https://zweiter.test', $viaWww);
|
||
}
|
||
|
||
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;
|
||
});
|
||
}
|
||
}
|