mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 01:34:03 +00:00
Take care of doubling tenant keys in TenancyUrlGenerator, add regression test for using UrlGenerator and RootUrl bootstrappers together
This commit is contained in:
parent
2266c47722
commit
42e7ec329c
2 changed files with 55 additions and 2 deletions
|
|
@ -53,7 +53,24 @@ class TenancyUrlGenerator extends UrlGenerator
|
|||
{
|
||||
[$name, $parameters] = $this->prepareRouteInputs($name, Arr::wrap($parameters));
|
||||
|
||||
return parent::route($name, $parameters, $absolute);
|
||||
$url = parent::route($name, $parameters, $absolute);
|
||||
|
||||
if (isset($parameters[PathTenantResolver::tenantParameterName()])) {
|
||||
// Ensure the tenant key is present in the URL just once
|
||||
// This is necessary when using UrlGeneratorBootstrapper with RootUrlBootstrapper
|
||||
$tenantId = $parameters[PathTenantResolver::tenantParameterName()];
|
||||
$afterTenant = str($url)->afterLast($tenantId)->toString();
|
||||
$beforeTenant = str($url)->before($tenantId)->toString();
|
||||
|
||||
if (! $absolute && str(url('/'))->contains($tenantId)) {
|
||||
// If the URL should be relative and the tenant key is already present in the full URL, don't add it again
|
||||
return $afterTenant;
|
||||
}
|
||||
|
||||
return $beforeTenant . $tenantId . $afterTenant;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -10,18 +10,23 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
|
||||
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
||||
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
|
||||
|
||||
beforeEach(function () {
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
RootUrlBootstrapper::$rootUrlOverride = null;
|
||||
RootUrlBootstrapper::$rootUrlOverrideInTests = true;
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
RootUrlBootstrapper::$rootUrlOverride = null;
|
||||
RootUrlBootstrapper::$rootUrlOverrideInTests = false;
|
||||
});
|
||||
|
||||
test('root url bootstrapper overrides the root url when tenancy gets initialized and reverts the url to the central one after tenancy ends', function() {
|
||||
test('root url bootstrapper overrides the root url when tenancy gets initialized and reverts the url to the central one when ending tenancy', function() {
|
||||
config(['tenancy.bootstrappers' => [RootUrlBootstrapper::class]]);
|
||||
|
||||
Route::group([
|
||||
|
|
@ -65,3 +70,34 @@ test('root url bootstrapper overrides the root url when tenancy gets initialized
|
|||
expect(URL::to('/'))->toBe($baseUrl);
|
||||
expect(config('app.url'))->toBe($baseUrl);
|
||||
});
|
||||
|
||||
test('root url bootstrapper can be used with url generator bootstrapper', function() {
|
||||
config(['tenancy.bootstrappers' => [RootUrlBootstrapper::class, UrlGeneratorBootstrapper::class]]);
|
||||
|
||||
TenancyUrlGenerator::$prefixRouteNames = true;
|
||||
TenancyUrlGenerator::$passTenantParameterToRoutes = true;
|
||||
|
||||
Route::get('/', function () {
|
||||
return true;
|
||||
})->name('home');
|
||||
|
||||
Route::get('/{tenant}', function () {
|
||||
return true;
|
||||
})->name('tenant.home')->middleware(InitializeTenancyByPath::class);
|
||||
|
||||
$rootUrlOverride = function (Tenant $tenant, string $originalRootUrl) {
|
||||
return str($originalRootUrl)->beforeLast($tenant->getTenantKey())->toString() . '/' . $tenant->getTenantKey();
|
||||
};
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
$tenantUrl = $rootUrlOverride($tenant, url('/'));
|
||||
|
||||
RootUrlBootstrapper::$rootUrlOverride = $rootUrlOverride;
|
||||
|
||||
expect(route('home'))->toBe(url('/'));
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
expect(route('home'))->toBe($tenantUrl);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue