Files
fewo-stommer/tests/Feature/TenantIsolationTest.php
T
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

98 lines
3.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Actions\CreateReservation;
use App\Models\Booking;
use App\Models\Guest;
use App\Models\Property;
use App\Models\Tenant;
use App\Pricing\StayRequest;
use App\Tenancy\TenantContext;
use App\Tenancy\TenantContextMissing;
use Carbon\CarbonImmutable;
use Database\Seeders\PropertySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class TenantIsolationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->travelTo(CarbonImmutable::parse('2026-10-22 10:00'));
Mail::fake();
}
public function test_the_same_email_address_is_a_separate_guest_for_each_landlord(): void
{
$this->seed(PropertySeeder::class);
$stommer = Tenant::query()->where('slug', 'stommer')->firstOrFail();
$other = Tenant::factory()->create();
$foreign = Guest::factory()->create([
'tenant_id' => $other->id,
'email' => 'anna@example.com',
'first_name' => 'Bert',
]);
$booking = app(CreateReservation::class)->handle(
Property::withoutGlobalScope('tenant')->where('tenant_id', $stommer->id)->firstOrFail(),
new StayRequest(CarbonImmutable::parse('2026-11-07'), CarbonImmutable::parse('2026-11-10'), 2),
['first_name' => 'Anna', 'last_name' => 'Muster', 'email' => 'anna@example.com'],
);
$this->assertNotSame($foreign->id, $booking->guest_id);
$this->assertSame('Anna', $booking->guest->first_name);
$this->assertSame($stommer->id, $booking->guest->tenant_id);
app(TenantContext::class)->set($stommer);
$this->assertSame(1, Guest::query()->where('email', 'anna@example.com')->count());
app(TenantContext::class)->set($other);
$this->assertSame('Bert', Guest::query()->where('email', 'anna@example.com')->firstOrFail()->first_name);
}
public function test_booking_numbers_continue_per_landlord_and_start_again_for_the_next_one(): void
{
$firstLandlord = Tenant::factory()->create();
$secondLandlord = Tenant::factory()->create();
$firstProperty = Property::factory()->create(['tenant_id' => $firstLandlord->id]);
$existing = Booking::factory()->for($firstProperty)->create();
$existing->forceFill(['reference' => '2026-0042'])->saveQuietly();
$next = Booking::factory()->for($firstProperty)->create();
$other = Booking::factory()->for(Property::factory()->create(['tenant_id' => $secondLandlord->id]))->create();
$this->assertSame('2026-0043', $next->reference);
$this->assertSame('2026-0001', $other->reference);
$this->assertSame($firstLandlord->id, $next->tenant_id);
$this->assertSame($secondLandlord->id, $other->tenant_id);
}
public function test_a_query_fails_when_a_landlord_is_required_and_none_is_current(): void
{
$context = app(TenantContext::class);
$context->forget();
$context->requirePresence();
$this->expectException(TenantContextMissing::class);
Guest::query()->count();
}
public function test_the_seeded_apartment_belongs_to_stommer(): void
{
$this->seed(PropertySeeder::class);
$property = Property::withoutGlobalScope('tenant')->sole();
$this->assertSame('stommer', $property->tenant->slug);
$this->assertSame($property->tenant_id, $property->seasons()->withoutGlobalScope('tenant')->firstOrFail()->tenant_id);
}
}