mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
[4.x] Cloning: addTenantParameter(bool), domain(string|null) (#1374)
* Add test for the new clone action addTenantParameter property * Add $addTenantParameter properyt to the clone action * Fix code style (php-cs-fixer) * Update clone action annotation * Make addTenantParameter(false) sound by adding domain() logic to route cloning --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
parent
0ef0104355
commit
b2f2669885
3 changed files with 122 additions and 22 deletions
|
|
@ -6,6 +6,7 @@ use Stancl\Tenancy\Actions\CloneRoutesAsTenant;
|
|||
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||
use Illuminate\Support\Facades\Route as RouteFacade;
|
||||
use function Stancl\Tenancy\Tests\pest;
|
||||
use Illuminate\Routing\Exceptions\UrlGenerationException;
|
||||
|
||||
test('CloneRoutesAsTenant action clones routes with clone middleware by default', function () {
|
||||
config(['tenancy.identification.resolvers.' . PathTenantResolver::class . '.tenant_parameter_name' => 'team']);
|
||||
|
|
@ -337,3 +338,54 @@ test('clone action can be used fluently', function() {
|
|||
expect(collect(RouteFacade::getRoutes()->get())->map->getName())
|
||||
->toContain('tenant.foo', 'tenant.bar', 'tenant.baz');
|
||||
});
|
||||
|
||||
test('the cloned route can be scoped to a specified domain', function () {
|
||||
RouteFacade::domain('foo.localhost')->get('/foo', fn () => in_array('tenant', request()->route()->middleware()) ? 'tenant' : 'central')->name('foo')->middleware('clone');
|
||||
|
||||
// Importantly, we CANNOT add a domain to the cloned route *if the original route didn't have a domain*.
|
||||
// This is due to the route registration order - the more strongly scoped route (= route with a domain)
|
||||
// must be registered first, so that Laravel tries that route first and only moves on if the domain check fails.
|
||||
$cloneAction = app(CloneRoutesAsTenant::class);
|
||||
// To keep the test simple we don't even need a tenant parameter
|
||||
$cloneAction->domain('bar.localhost')->addTenantParameter(false)->handle();
|
||||
|
||||
expect(route('foo'))->toBe('http://foo.localhost/foo');
|
||||
expect(route('tenant.foo'))->toBe('http://bar.localhost/foo');
|
||||
});
|
||||
|
||||
test('tenant parameter addition can be controlled by setting addTenantParameter', function (bool $addTenantParameter) {
|
||||
RouteFacade::domain('central.localhost')
|
||||
->get('/foo', fn () => in_array('tenant', request()->route()->middleware()) ? 'tenant' : 'central')
|
||||
->name('foo')
|
||||
->middleware('clone');
|
||||
|
||||
// By default this action also removes the domain
|
||||
$cloneAction = app(CloneRoutesAsTenant::class);
|
||||
$cloneAction->addTenantParameter($addTenantParameter)->handle();
|
||||
|
||||
$clonedRoute = RouteFacade::getRoutes()->getByName('tenant.foo');
|
||||
|
||||
// We only use the route() helper here, since once a request is made
|
||||
// the URL generator caches the request's domain and it affects route
|
||||
// generation for routes that do not have domain() specified (tenant.foo)
|
||||
expect(route('foo'))->toBe('http://central.localhost/foo');
|
||||
if ($addTenantParameter)
|
||||
expect(route('tenant.foo', ['tenant' => 'abc']))->toBe('http://localhost/abc/foo');
|
||||
else
|
||||
expect(route('tenant.foo'))->toBe('http://localhost/foo');
|
||||
|
||||
// Original route still works
|
||||
$this->withoutExceptionHandling()->get(route('foo'))->assertSee('central');
|
||||
|
||||
if ($addTenantParameter) {
|
||||
expect($clonedRoute->uri())->toContain('{tenant}');
|
||||
|
||||
$this->withoutExceptionHandling()->get('http://localhost/abc/foo')->assertSee('tenant');
|
||||
$this->withoutExceptionHandling()->get('http://central.localhost/foo')->assertSee('central');
|
||||
} else {
|
||||
expect($clonedRoute->uri())->not()->toContain('{tenant}');
|
||||
|
||||
$this->withoutExceptionHandling()->get('http://localhost/foo')->assertSee('tenant');
|
||||
$this->withoutExceptionHandling()->get('http://central.localhost/foo')->assertSee('central');
|
||||
}
|
||||
})->with([true, false]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue