From 21664664ddef9cdbd7c14795e712316345acb6e8 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 2 Sep 2022 14:32:20 +0200 Subject: [PATCH] Add test for deleting storage after tenant deletion --- assets/config.php | 2 ++ tests/BootstrapperTest.php | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/assets/config.php b/assets/config.php index 2a54e0b9..32b212b1 100644 --- a/assets/config.php +++ b/assets/config.php @@ -137,6 +137,8 @@ return [ * where you want to use tenant-specific assets (product images, avatars, etc). */ 'asset_helper_tenancy' => true, + + 'delete_storage_after_tenant_deletion' => false, ], /** diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 96afbc83..f68e43e9 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; use Stancl\JobPipeline\JobPipeline; +use Illuminate\Support\Facades\File; use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Event; @@ -14,6 +14,7 @@ use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Events\TenantCreated; +use Illuminate\Filesystem\FilesystemAdapter; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; @@ -21,6 +22,7 @@ use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; +use Stancl\Tenancy\Events\TenantDeleted; beforeEach(function () { $this->mockConsoleOutput = false; @@ -184,6 +186,33 @@ test('filesystem data is separated', function () { expect($new_storage_path)->toEqual($expected_storage_path); }); +test('tenant storage gets deleted after the tenant if filesystem.delete_storage_after_tenant_deletion is true', function () { + config([ + 'tenancy.bootstrappers' => [ + FilesystemTenancyBootstrapper::class, + ], + 'tenancy.filesystem.delete_storage_after_tenant_deletion' => true + ]); + + Event::listen(TenantDeleted::class, function(TenantDeleted $event) { + if (config('tenancy.filesystem.delete_storage_after_tenant_deletion')) { + File::deleteDirectory($event->tenant->run(fn () => storage_path())); + } + }); + + tenancy()->initialize(Tenant::create()); + + Storage::fake('test'); + + expect(File::isDirectory(storage_path()))->toBeTrue(); + + Storage::put('test.txt', 'testing file'); + + tenant()->delete(); + + expect(File::isDirectory(storage_path()))->toBeFalse(); +}); + function getDiskPrefix(string $disk): string { /** @var FilesystemAdapter $disk */