1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 08:04:03 +00:00

Fix #1404: support universal routes in CheckTenantForMaintenanceMode

This commit also corrects an Event::fake() call in a separate test, as
general Event::fake() calls without specified events can lead to
incorrect (and difficult to debug) behavior in some cases, since
Tenancy depends on the event system being functional.
This commit is contained in:
Samuel Štancl 2025-10-14 17:11:47 +02:00
parent e806825f71
commit e1b8658414
No known key found for this signature in database
GPG key ID: BA146259A1E16C57
2 changed files with 42 additions and 6 deletions

View file

@ -14,7 +14,13 @@ class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
public function handle($request, Closure $next)
{
if (! tenant()) {
throw new TenancyNotInitializedException;
// If there's no tenant, there's no tenant to check for maintenance mode.
// Since tenant identification middleware has higher priority than this
// middleware, a missing tenant would have already lead to request termination.
// (And even if priority were misconfigured, the request would simply get
// terminated *after* this middleware.)
// Therefore, we are likely on a universal route, in central context.
return $next($request);
}
if (tenant('maintenance_mode')) {

View file

@ -6,6 +6,8 @@ use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Database\Concerns\MaintenanceMode;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Events\TenantMaintenanceModeDisabled;
use Stancl\Tenancy\Events\TenantMaintenanceModeEnabled;
use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use function Stancl\Tenancy\Tests\pest;
@ -38,18 +40,46 @@ test('tenants can be in maintenance mode', function () {
pest()->get('http://acme.localhost/foo')->assertStatus(200);
});
test('maintenance mode events are fired', function () {
$tenant = MaintenanceTenant::create();
test('maintenance mode middleware can be used with universal routes', function () {
Route::get('/foo', function () {
return 'bar';
})->middleware(['universal', InitializeTenancyByDomain::class, CheckTenantForMaintenanceMode::class]);
Event::fake();
$tenant = MaintenanceTenant::create();
$tenant->domains()->create([
'domain' => 'acme.localhost',
]);
// Revert to central context after each request so that the tenant context
// from the request doesn't persist
$run = function (Closure $callback) { $callback(); tenancy()->end(); };
$run(fn () => pest()->get('http://acme.localhost/foo')->assertStatus(200));
$run(fn () => pest()->get('http://localhost/foo')->assertStatus(200));
$tenant->putDownForMaintenance();
Event::assertDispatched(\Stancl\Tenancy\Events\TenantMaintenanceModeEnabled::class);
$run(fn () => pest()->get('http://acme.localhost/foo')->assertStatus(503));
$run(fn () => pest()->get('http://localhost/foo')->assertStatus(200)); // Not affected by a tenant's maintenance mode
$tenant->bringUpFromMaintenance();
Event::assertDispatched(\Stancl\Tenancy\Events\TenantMaintenanceModeDisabled::class);
$run(fn () => pest()->get('http://acme.localhost/foo')->assertStatus(200));
$run(fn () => pest()->get('http://localhost/foo')->assertStatus(200));
});
test('maintenance mode events are fired', function () {
$tenant = MaintenanceTenant::create();
Event::fake([TenantMaintenanceModeEnabled::class, TenantMaintenanceModeDisabled::class]);
$tenant->putDownForMaintenance();
Event::assertDispatched(TenantMaintenanceModeEnabled::class);
$tenant->bringUpFromMaintenance();
Event::assertDispatched(TenantMaintenanceModeDisabled::class);
});
test('tenants can be put into maintenance mode using artisan commands', function() {