|
2 | 2 |
|
3 | 3 | use Illuminate\Database\Schema\Blueprint; |
4 | 4 | use Illuminate\Routing\UrlGenerator; |
| 5 | +use Illuminate\Support\Facades\URL; |
5 | 6 | use Stancl\Tenancy\Tests\Etc\Tenant; |
6 | 7 | use Illuminate\Support\Facades\Event; |
7 | 8 | use Illuminate\Support\Facades\Route; |
|
25 | 26 | Event::listen(TenancyEnded::class, RevertToCentralContext::class); |
26 | 27 | TenancyUrlGenerator::$prefixRouteNames = false; |
27 | 28 | TenancyUrlGenerator::$passTenantParameterToRoutes = false; |
| 29 | + TenancyUrlGenerator::$overrides = []; |
| 30 | + TenancyUrlGenerator::$bypassParameter = 'central'; |
28 | 31 | UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false; |
29 | 32 | }); |
30 | 33 |
|
31 | 34 | afterEach(function () { |
32 | 35 | TenancyUrlGenerator::$prefixRouteNames = false; |
33 | 36 | TenancyUrlGenerator::$passTenantParameterToRoutes = false; |
| 37 | + TenancyUrlGenerator::$overrides = []; |
| 38 | + TenancyUrlGenerator::$bypassParameter = 'central'; |
34 | 39 | UrlGeneratorBootstrapper::$addTenantParameterToDefaults = false; |
35 | 40 | }); |
36 | 41 |
|
|
359 | 364 | expect(route('home', ['bypassParameter' => false, 'tenant' => $tenant->getTenantKey()]))->toBe($tenantRouteUrl) |
360 | 365 | ->not()->toContain('bypassParameter'); |
361 | 366 | }); |
| 367 | + |
| 368 | +test('the temporarySignedRoute method can automatically prefix the passed route name', function() { |
| 369 | + config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]); |
| 370 | + |
| 371 | + Route::get('/{tenant}/foo', fn () => 'foo')->name('tenant.foo')->middleware([InitializeTenancyByPath::class]); |
| 372 | + |
| 373 | + TenancyUrlGenerator::$prefixRouteNames = true; |
| 374 | + |
| 375 | + $tenant = Tenant::create(); |
| 376 | + |
| 377 | + tenancy()->initialize($tenant); |
| 378 | + |
| 379 | + // Route name ('foo') gets prefixed automatically (will be 'tenant.foo') |
| 380 | + $tenantSignedUrl = URL::temporarySignedRoute('foo', now()->addMinutes(2), ['tenant' => $tenantKey = $tenant->getTenantKey()]); |
| 381 | + |
| 382 | + expect($tenantSignedUrl)->toContain("localhost/{$tenantKey}/foo"); |
| 383 | +}); |
| 384 | + |
| 385 | +test('the bypass parameter works correctly with temporarySignedRoute', function() { |
| 386 | + config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]); |
| 387 | + |
| 388 | + Route::get('/foo', fn () => 'foo')->name('central.foo'); |
| 389 | + |
| 390 | + TenancyUrlGenerator::$prefixRouteNames = true; |
| 391 | + TenancyUrlGenerator::$bypassParameter = 'central'; |
| 392 | + |
| 393 | + $tenant = Tenant::create(); |
| 394 | + |
| 395 | + tenancy()->initialize($tenant); |
| 396 | + |
| 397 | + // Bypass parameter allows us to generate URL for the 'central.foo' route in tenant context |
| 398 | + $centralSignedUrl = URL::temporarySignedRoute('central.foo', now()->addMinutes(2), ['central' => true]); |
| 399 | + |
| 400 | + expect($centralSignedUrl) |
| 401 | + ->toContain('localhost/foo') |
| 402 | + ->not()->toContain('central='); // Bypass parameter gets removed from the generated URL |
| 403 | +}); |
0 commit comments