mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:34:03 +00:00
* Run cache tests on all supported drivers * update ci healthcheck for memcached * remove memcached healthcheck * fix typos in test comments, expand internal.md [ci skip] * add empty line [ci skip] * switch to using $store->setPrefix() * add dynamodb * refactor try-finally to try-catch * remove unnecessary clearResolvedInstances() call * add dual Cache:: and cache() assertions * add apc * Flush APCu cache in test setup * Revert "add dual Cache:: and cache() assertions" This reverts commit a0bab162fbe2dd0d25e7056ceca4fb7ce54efc77. * phpstan fix * Add logic for scoping 'file' disks to FilesystemTenancyBootstrapper * minor changes, add todos * refactor how the session.connection is used in the DB session bootstrapper * add session forgery prevention logic to the db session bootstrapper * only use the fs bootstrapper for file disk in 'cache data is separated' dataset * minor session scoping test changes * Add session scoping logic to FilesystemTenancyBootstrapper, correctly update disk roots even with storage_path_tenancy disabled * Fix code style (php-cs-fixer) * update docblock * make not-null check more explicit * separate bootstrapper tests, fix swapped test names for two tests * refactor cache bootstrapper tests * resolve global cache todo * expand tests: session separation tests, more filesystem separation assertions; change prefix_base-type config keys to templates/formats * add apc session scoping test, various session separation bugfixes * phpstan + minor logic fixes * prefix_format -> prefix * fix database session separation test * revert composer.json changes, update laravel dependencies to expected next release * only run session scoping logic in cache bootstrapper for redis, memcached, dynamodb, apc; update gitattributes * tenancy.central_domains -> tenancy.identification.central_domains * db session separation test: add datasets --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
157 lines
7.4 KiB
PHP
157 lines
7.4 KiB
PHP
<?php
|
||
|
||
use Illuminate\Routing\Exceptions\UrlGenerationException;
|
||
use Illuminate\Routing\UrlGenerator;
|
||
use Illuminate\Support\Facades\Event;
|
||
use Illuminate\Support\Facades\Route;
|
||
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
|
||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
|
||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||
use Stancl\Tenancy\Events\TenancyEnded;
|
||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||
|
||
beforeEach(function () {
|
||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||
TenancyUrlGenerator::$prefixRouteNames = false;
|
||
TenancyUrlGenerator::$passTenantParameterToRoutes = true;
|
||
});
|
||
|
||
afterEach(function () {
|
||
TenancyUrlGenerator::$prefixRouteNames = false;
|
||
TenancyUrlGenerator::$passTenantParameterToRoutes = true;
|
||
});
|
||
|
||
test('url generator bootstrapper swaps the url generator instance correctly', function() {
|
||
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||
|
||
tenancy()->initialize(Tenant::create());
|
||
expect(app('url'))->toBeInstanceOf(TenancyUrlGenerator::class);
|
||
expect(url())->toBeInstanceOf(TenancyUrlGenerator::class);
|
||
|
||
tenancy()->end();
|
||
expect(app('url'))->toBeInstanceOf(UrlGenerator::class)
|
||
->not()->toBeInstanceOf(TenancyUrlGenerator::class);
|
||
expect(url())->toBeInstanceOf(UrlGenerator::class)
|
||
->not()->toBeInstanceOf(TenancyUrlGenerator::class);
|
||
});
|
||
|
||
test('url generator bootstrapper can prefix route names passed to the route helper', function() {
|
||
Route::get('/central/home', fn () => route('home'))->name('home');
|
||
// Tenant route name prefix is 'tenant.' by default
|
||
Route::get('/{tenant}/home', fn () => route('tenant.home'))->name('tenant.home')->middleware(['tenant', InitializeTenancyByPath::class]);
|
||
|
||
$tenant = Tenant::create();
|
||
$tenantKey = $tenant->getTenantKey();
|
||
$centralRouteUrl = route('home');
|
||
$tenantRouteUrl = route('tenant.home', ['tenant' => $tenantKey]);
|
||
TenancyUrlGenerator::$bypassParameter = 'bypassParameter';
|
||
|
||
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||
|
||
tenancy()->initialize($tenant);
|
||
|
||
// Route names don't get prefixed when TenancyUrlGenerator::$prefixRouteNames is false
|
||
expect(route('home'))->not()->toBe($centralRouteUrl);
|
||
// When TenancyUrlGenerator::$passTenantParameterToRoutes is true (default)
|
||
// The route helper receives the tenant parameter
|
||
// So in order to generate central URL, we have to pass the bypass parameter
|
||
expect(route('home', ['bypassParameter' => true]))->toBe($centralRouteUrl);
|
||
|
||
|
||
TenancyUrlGenerator::$prefixRouteNames = true;
|
||
// The $prefixRouteNames property is true
|
||
// The route name passed to the route() helper ('home') gets prefixed prefixed with 'tenant.' automatically
|
||
expect(route('home'))->toBe($tenantRouteUrl);
|
||
|
||
// The 'tenant.home' route name doesn't get prefixed because it is already prefixed with 'tenant.'
|
||
// Also, the route receives the tenant parameter automatically
|
||
expect(route('tenant.home'))->toBe($tenantRouteUrl);
|
||
|
||
// Ending tenancy reverts route() behavior changes
|
||
tenancy()->end();
|
||
|
||
expect(route('home'))->toBe($centralRouteUrl);
|
||
});
|
||
|
||
test('both the name prefixing and the tenant parameter logic gets skipped when bypass parameter is used', function () {
|
||
$tenantParameterName = PathTenantResolver::tenantParameterName();
|
||
|
||
Route::get('/central/home', fn () => route('home'))->name('home');
|
||
// Tenant route name prefix is 'tenant.' by default
|
||
Route::get('/{tenant}/home', fn () => route('tenant.home'))->name('tenant.home')->middleware(['tenant', InitializeTenancyByPath::class]);
|
||
|
||
$tenant = Tenant::create();
|
||
$centralRouteUrl = route('home');
|
||
$tenantRouteUrl = route('tenant.home', ['tenant' => $tenant->getTenantKey()]);
|
||
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||
|
||
TenancyUrlGenerator::$prefixRouteNames = true;
|
||
TenancyUrlGenerator::$bypassParameter = 'bypassParameter';
|
||
|
||
tenancy()->initialize($tenant);
|
||
|
||
// The $bypassParameter parameter ('central' by default) can bypass the route name prefixing
|
||
// When the bypass parameter is true, the generated route URL points to the route named 'home'
|
||
expect(route('home', ['bypassParameter' => true]))->toBe($centralRouteUrl)
|
||
// Bypass parameter prevents passing the tenant parameter directly
|
||
->not()->toContain($tenantParameterName . '=')
|
||
// Bypass parameter gets removed from the generated URL automatically
|
||
->not()->toContain('bypassParameter');
|
||
|
||
// When the bypass parameter is false, the generated route URL points to the prefixed route ('tenant.home')
|
||
expect(route('home', ['bypassParameter' => false]))->toBe($tenantRouteUrl)
|
||
->not()->toContain('bypassParameter');
|
||
});
|
||
|
||
test('url generator bootstrapper can make route helper generate links with the tenant parameter', function() {
|
||
Route::get('/query_string', fn () => route('query_string'))->name('query_string')->middleware(['universal', InitializeTenancyByRequestData::class]);
|
||
Route::get('/path', fn () => route('path'))->name('path');
|
||
Route::get('/{tenant}/path', fn () => route('tenant.path'))->name('tenant.path')->middleware([InitializeTenancyByPath::class]);
|
||
|
||
$tenant = Tenant::create();
|
||
$tenantKey = $tenant->getTenantKey();
|
||
$queryStringCentralUrl = route('query_string');
|
||
$queryStringTenantUrl = route('query_string', ['tenant' => $tenantKey]);
|
||
$pathCentralUrl = route('path');
|
||
$pathTenantUrl = route('tenant.path', ['tenant' => $tenantKey]);
|
||
|
||
// Makes the route helper receive the tenant parameter whenever available
|
||
// Unless the bypass parameter is true
|
||
TenancyUrlGenerator::$passTenantParameterToRoutes = true;
|
||
|
||
TenancyUrlGenerator::$bypassParameter = 'bypassParameter';
|
||
|
||
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||
|
||
expect(route('path'))->toBe($pathCentralUrl);
|
||
// Tenant parameter required, but not passed since tenancy wasn't initialized
|
||
expect(fn () => route('tenant.path'))->toThrow(UrlGenerationException::class);
|
||
|
||
tenancy()->initialize($tenant);
|
||
|
||
// Tenant parameter is passed automatically
|
||
expect(route('path'))->not()->toBe($pathCentralUrl); // Parameter added as query string – bypassParameter needed
|
||
expect(route('path', ['bypassParameter' => true]))->toBe($pathCentralUrl);
|
||
expect(route('tenant.path'))->toBe($pathTenantUrl);
|
||
|
||
expect(route('query_string'))->toBe($queryStringTenantUrl)->toContain('tenant=');
|
||
expect(route('query_string', ['bypassParameter' => 'true']))->toBe($queryStringCentralUrl)->not()->toContain('tenant=');
|
||
|
||
tenancy()->end();
|
||
|
||
expect(route('query_string'))->toBe($queryStringCentralUrl);
|
||
|
||
// Tenant parameter required, but shouldn't be passed since tenancy isn't initialized
|
||
expect(fn () => route('tenant.path'))->toThrow(UrlGenerationException::class);
|
||
|
||
// Route-level identification
|
||
pest()->get("http://localhost/query_string")->assertSee($queryStringCentralUrl);
|
||
pest()->get("http://localhost/query_string?tenant=$tenantKey")->assertSee($queryStringTenantUrl);
|
||
pest()->get("http://localhost/path")->assertSee($pathCentralUrl);
|
||
pest()->get("http://localhost/$tenantKey/path")->assertSee($pathTenantUrl);
|
||
});
|