From 931c76d697f40f4ab0c7e96957ddc083da3557bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 22 Aug 2022 17:59:43 +0200 Subject: [PATCH] Pull 3.x changes into master (#922) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * exclude master from CI * Add space after 'up' in 'docker-compose up-d' (#900) * Fix ArgumentCountError on the TenantAssetsController (#894) * Fix ArgumentCount exception on the TenantAssetsController when no `$path` is provided * CS * CS * Handle null case explicitly * code style Co-authored-by: Bram Wubs Co-authored-by: Samuel Ć tancl * Add support for nested tenant config override (#920) * feat: add support for nested tenant config override * test: ensure nested tenant values are mapped * Update TenantConfigTest.php Co-authored-by: lukinovec Co-authored-by: Bram Wubs Co-authored-by: Bram Wubs Co-authored-by: George Bishop Co-authored-by: Abrar Ahmad --- src/Controllers/TenantAssetsController.php | 7 +++++-- src/Features/TenantConfig.php | 3 ++- tests/Features/TenantConfigTest.php | 22 ++++++++++++++++++++++ tests/TenantAssetTest.php | 12 ++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/Controllers/TenantAssetsController.php b/src/Controllers/TenantAssetsController.php index 67478cf9..5549da0d 100644 --- a/src/Controllers/TenantAssetsController.php +++ b/src/Controllers/TenantAssetsController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Controllers; use Illuminate\Routing\Controller; +use Throwable; class TenantAssetsController extends Controller { @@ -15,11 +16,13 @@ class TenantAssetsController extends Controller $this->middleware(static::$tenancyMiddleware); } - public function asset($path) + public function asset($path = null) { + abort_if($path === null, 404); + try { return response()->file(storage_path("app/public/$path")); - } catch (\Throwable $th) { + } catch (Throwable $th) { abort(404); } } diff --git a/src/Features/TenantConfig.php b/src/Features/TenantConfig.php index fb09a871..95691f0d 100644 --- a/src/Features/TenantConfig.php +++ b/src/Features/TenantConfig.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Features; use Illuminate\Contracts\Config\Repository; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Event; use Stancl\Tenancy\Contracts\Feature; use Stancl\Tenancy\Contracts\Tenant; @@ -45,7 +46,7 @@ class TenantConfig implements Feature { /** @var Tenant|Model $tenant */ foreach (static::$storageToConfigMap as $storageKey => $configKey) { - $override = $tenant->getAttribute($storageKey); + $override = Arr::get($tenant, $storageKey); if (! is_null($override)) { if (is_array($configKey)) { diff --git a/tests/Features/TenantConfigTest.php b/tests/Features/TenantConfigTest.php index 35df35ed..5c12c5f0 100644 --- a/tests/Features/TenantConfigTest.php +++ b/tests/Features/TenantConfigTest.php @@ -14,6 +14,28 @@ afterEach(function () { TenantConfig::$storageToConfigMap = []; }); +test('nested tenant values are merged', function () { + expect(config('whitelabel.theme'))->toBeNull(); + config([ + 'tenancy.features' => [TenantConfig::class], + 'tenancy.bootstrappers' => [], + ]); + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); + + TenantConfig::$storageToConfigMap = [ + 'whitelabel.config.theme' => 'whitelabel.theme', + ]; + + $tenant = Tenant::create([ + 'whitelabel' => ['config' => ['theme' => 'dark']], + ]); + + tenancy()->initialize($tenant); + expect(config('whitelabel.theme'))->toBe('dark'); + tenancy()->end(); +}); + test('config is merged and removed', function () { expect(config('services.paypal'))->toBe(null); config([ diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 2c5000f1..d43b7989 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -94,6 +94,18 @@ test('asset helper tenancy can be disabled', function () { expect(asset('foo'))->toBe($original); }); +test('test asset controller returns a 404 when no path is provided', function () { + TenantAssetsController::$tenancyMiddleware = InitializeTenancyByRequestData::class; + + $tenant = Tenant::create(); + + tenancy()->initialize($tenant); + + pest()->get(tenant_asset(null), [ + 'X-Tenant' => $tenant->id, + ])->assertNotFound(); +}); + function getEnvironmentSetUp($app) { $app->booted(function () {