From f2c64088ed950f85787e7489b7d56cf40f02c96c Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Fri, 2 Sep 2022 22:04:00 +0500 Subject: [PATCH] [4.x] Set tenant as a default parameter for the URLs when using Path identification (#925) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * set tenant as default url parameter * Update PathIdentificationTest.php * assertion * test rename * fix tests * fix string Co-authored-by: Samuel Ć tancl --- src/Middleware/InitializeTenancyByPath.php | 8 +++++++ tests/PathIdentificationTest.php | 26 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Middleware/InitializeTenancyByPath.php b/src/Middleware/InitializeTenancyByPath.php index e66400c5..ae15323c 100644 --- a/src/Middleware/InitializeTenancyByPath.php +++ b/src/Middleware/InitializeTenancyByPath.php @@ -7,6 +7,9 @@ namespace Stancl\Tenancy\Middleware; use Closure; use Illuminate\Http\Request; use Illuminate\Routing\Route; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\URL; +use Stancl\Tenancy\Events\InitializingTenancy; use Stancl\Tenancy\Exceptions\RouteIsMissingTenantParameterException; use Stancl\Tenancy\Resolvers\PathTenantResolver; use Stancl\Tenancy\Tenancy; @@ -37,6 +40,11 @@ class InitializeTenancyByPath extends IdentificationMiddleware // We don't want to initialize tenancy if the tenant is // simply injected into some route controller action. if ($route->parameterNames()[0] === PathTenantResolver::$tenantParameterName) { + // Set tenant as a default parameter for the URLs in the current request + Event::listen(InitializingTenancy::class, function (InitializingTenancy $event) { + URL::defaults([PathTenantResolver::$tenantParameterName => $event->tenancy->tenant->getTenantKey()]); + }); + return $this->initializeTenancy( $request, $next, diff --git a/tests/PathIdentificationTest.php b/tests/PathIdentificationTest.php index bda0cfcb..bfa8f8ad 100644 --- a/tests/PathIdentificationTest.php +++ b/tests/PathIdentificationTest.php @@ -18,7 +18,11 @@ beforeEach(function () { ], function () { Route::get('/foo/{a}/{b}', function ($a, $b) { return "$a + $b"; - }); + })->name('foo'); + + Route::get('/baz/{a}/{b}', function ($a, $b) { + return "$a - $b"; + })->name('baz'); }); }); @@ -123,3 +127,23 @@ test('tenant parameter name can be customized', function () { ->withoutExceptionHandling() ->get('/acme/foo/abc/xyz'); }); + +test('tenant parameter is set for all routes as the default parameter once the tenancy initialized', function () { + Tenant::create([ + 'id' => 'acme', + ]); + + expect(tenancy()->initialized)->toBeFalse(); + + // make a request that will initialize tenancy + pest()->get(route('foo', ['tenant' => 'acme', 'a' => 1, 'b' => 2])); + + expect(tenancy()->initialized)->toBeTrue(); + expect(tenant('id'))->toBe('acme'); + + // assert that the route WITHOUT the tenant parameter matches the route WITH the tenant parameter + expect(route('baz', ['a' => 1, 'b' => 2]))->toBe(route('baz', ['tenant' => 'acme', 'a' => 1, 'b' => 2])); + + expect(route('baz', ['a' => 1, 'b' => 2]))->toBe('http://localhost/acme/baz/1/2'); // assert the full route string + pest()->get(route('baz', ['a' => 1, 'b' => 2]))->assertOk(); // Assert route don't need tenant parameter +});