mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20: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>
142 lines
6 KiB
PHP
142 lines
6 KiB
PHP
<?php
|
||
|
||
use Illuminate\Broadcasting\BroadcastManager;
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Support\Facades\Event;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Stancl\JobPipeline\JobPipeline;
|
||
use Stancl\Tenancy\Bootstrappers\BroadcastChannelPrefixBootstrapper;
|
||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||
use Stancl\Tenancy\Events\TenancyEnded;
|
||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||
use Stancl\Tenancy\Events\TenantCreated;
|
||
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
|
||
|
||
beforeEach(function () {
|
||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||
|
||
Event::listen(
|
||
TenantCreated::class,
|
||
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||
return $event->tenant;
|
||
})->toListener()
|
||
);
|
||
});
|
||
|
||
test('BroadcastChannelPrefixBootstrapper prefixes the channels events are broadcast on while tenancy is initialized', function() {
|
||
config([
|
||
'broadcasting.default' => $driver = 'testing',
|
||
'broadcasting.connections.testing.driver' => $driver,
|
||
]);
|
||
|
||
// Use custom broadcaster
|
||
app(BroadcastManager::class)->extend($driver, fn () => new TestingBroadcaster('original broadcaster'));
|
||
|
||
config(['tenancy.bootstrappers' => [BroadcastChannelPrefixBootstrapper::class, DatabaseTenancyBootstrapper::class]]);
|
||
|
||
Schema::create('users', function (Blueprint $table) {
|
||
$table->increments('id');
|
||
$table->string('name');
|
||
$table->string('email')->unique();
|
||
$table->string('password');
|
||
$table->rememberToken();
|
||
$table->timestamps();
|
||
});
|
||
|
||
universal_channel('users.{userId}', function ($user, $userId) {
|
||
return User::find($userId)->is($user);
|
||
});
|
||
|
||
$broadcaster = app(BroadcastManager::class)->driver();
|
||
|
||
$tenant = Tenant::create();
|
||
$tenant2 = Tenant::create();
|
||
|
||
pest()->artisan('tenants:migrate');
|
||
|
||
// Set up the 'testing' broadcaster override
|
||
// Identical to the default Pusher override (BroadcastChannelPrefixBootstrapper::pusher())
|
||
// Except for the parent class (TestingBroadcaster instead of PusherBroadcaster)
|
||
BroadcastChannelPrefixBootstrapper::$broadcasterOverrides['testing'] = function (BroadcastManager $broadcastManager) {
|
||
$broadcastManager->extend('testing', function ($app, $config) {
|
||
return new class('tenant broadcaster') extends TestingBroadcaster {
|
||
protected function formatChannels(array $channels)
|
||
{
|
||
$formatChannel = function (string $channel) {
|
||
$prefixes = ['private-', 'presence-'];
|
||
$defaultPrefix = '';
|
||
|
||
foreach ($prefixes as $prefix) {
|
||
if (str($channel)->startsWith($prefix)) {
|
||
$defaultPrefix = $prefix;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// Skip prefixing channels flagged with the global channel prefix
|
||
if (! str($channel)->startsWith('global__')) {
|
||
$channel = str($channel)->after($defaultPrefix)->prepend($defaultPrefix . tenant()->getTenantKey() . '.');
|
||
}
|
||
|
||
return (string) $channel;
|
||
};
|
||
|
||
return array_map($formatChannel, parent::formatChannels($channels));
|
||
}
|
||
};
|
||
});
|
||
};
|
||
|
||
auth()->login($user = User::create(['name' => 'central', 'email' => 'test@central.cz', 'password' => 'test']));
|
||
|
||
// The channel names used for testing the formatChannels() method (not real channels)
|
||
$channelNames = [
|
||
'channel',
|
||
'global__channel', // Channels prefixed with 'global__' shouldn't get prefixed with the tenant key
|
||
'private-user.' . $user->id,
|
||
];
|
||
|
||
// formatChannels doesn't prefix the channel names until tenancy is initialized
|
||
expect(invade(app(BroadcastManager::class)->driver())->formatChannels($channelNames))->toEqual($channelNames);
|
||
|
||
tenancy()->initialize($tenant);
|
||
|
||
$tenantBroadcaster = app(BroadcastManager::class)->driver();
|
||
|
||
auth()->login($tenantUser = User::create(['name' => 'tenant', 'email' => 'test@tenant.cz', 'password' => 'test']));
|
||
|
||
// The current (tenant) broadcaster isn't the same as the central one
|
||
expect($tenantBroadcaster->message)->not()->toBe($broadcaster->message);
|
||
// Tenant broadcaster has the same channels as the central broadcaster
|
||
expect($tenantBroadcaster->getChannels())->toEqualCanonicalizing($broadcaster->getChannels());
|
||
// formatChannels prefixes the channel names now
|
||
expect(invade($tenantBroadcaster)->formatChannels($channelNames))->toEqualCanonicalizing([
|
||
'global__channel',
|
||
$tenant->getTenantKey() . '.channel',
|
||
'private-' . $tenant->getTenantKey() . '.user.' . $tenantUser->id,
|
||
]);
|
||
|
||
// Initialize another tenant
|
||
tenancy()->initialize($tenant2);
|
||
|
||
auth()->login($tenantUser = User::create(['name' => 'tenant', 'email' => 'test2@tenant.cz', 'password' => 'test']));
|
||
|
||
// formatChannels prefixes channels with the second tenant's key now
|
||
expect(invade(app(BroadcastManager::class)->driver())->formatChannels($channelNames))->toEqualCanonicalizing([
|
||
'global__channel',
|
||
$tenant2->getTenantKey() . '.channel',
|
||
'private-' . $tenant2->getTenantKey() . '.user.' . $tenantUser->id,
|
||
]);
|
||
|
||
// The bootstrapper reverts to the tenant context – the channel names won't be prefixed anymore
|
||
tenancy()->end();
|
||
|
||
// The current broadcaster is the same as the central one again
|
||
expect(app(BroadcastManager::class)->driver())->toBe($broadcaster);
|
||
expect(invade(app(BroadcastManager::class)->driver())->formatChannels($channelNames))->toEqual($channelNames);
|
||
});
|
||
|