From c1223678a7f2452b7494bd39361337ee4fae48fb Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Tue, 27 Sep 2022 13:31:14 +0200 Subject: [PATCH] Add SettingsServiceProvider (#8) * Move config to SettingsServiceProvider * WIP * WIP * WIP --- env/codecov/testbench.yaml | 3 + env/mysql/testbench.yaml | 3 + env/postgres/testbench.yaml | 3 + src/EscolaLmsJitsiServiceProvider.php | 13 +- src/Providers/SettingsServiceProvider.php | 27 +++++ testbench.yaml | 3 + tests/Api/SettingsApiTest.php | 140 ++++++++++++++++++++++ 7 files changed, 182 insertions(+), 10 deletions(-) create mode 100644 src/Providers/SettingsServiceProvider.php create mode 100644 tests/Api/SettingsApiTest.php diff --git a/env/codecov/testbench.yaml b/env/codecov/testbench.yaml index 5b4263c..7859cbe 100644 --- a/env/codecov/testbench.yaml +++ b/env/codecov/testbench.yaml @@ -11,3 +11,6 @@ providers: - Spatie\Permission\PermissionServiceProvider - EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider - EscolaLms\Settings\EscolaLmsSettingsServiceProvider + - EscolaLms\Core\EscolaLmsServiceProvider + - EscolaLms\Auth\EscolaLmsAuthServiceProvider + - EscolaLms\Categories\EscolaLmsCategoriesServiceProvider diff --git a/env/mysql/testbench.yaml b/env/mysql/testbench.yaml index 1170e74..946a48e 100644 --- a/env/mysql/testbench.yaml +++ b/env/mysql/testbench.yaml @@ -12,3 +12,6 @@ providers: - Spatie\Permission\PermissionServiceProvider - EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider - EscolaLms\Settings\EscolaLmsSettingsServiceProvider + - EscolaLms\Core\EscolaLmsServiceProvider + - EscolaLms\Auth\EscolaLmsAuthServiceProvider + - EscolaLms\Categories\EscolaLmsCategoriesServiceProvider \ No newline at end of file diff --git a/env/postgres/testbench.yaml b/env/postgres/testbench.yaml index cd07ad3..f01997b 100644 --- a/env/postgres/testbench.yaml +++ b/env/postgres/testbench.yaml @@ -11,3 +11,6 @@ providers: - Spatie\Permission\PermissionServiceProvider - EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider - EscolaLms\Settings\EscolaLmsSettingsServiceProvider + - EscolaLms\Core\EscolaLmsServiceProvider + - EscolaLms\Auth\EscolaLmsAuthServiceProvider + - EscolaLms\Categories\EscolaLmsCategoriesServiceProvider \ No newline at end of file diff --git a/src/EscolaLmsJitsiServiceProvider.php b/src/EscolaLmsJitsiServiceProvider.php index 89d0b54..9af2794 100644 --- a/src/EscolaLmsJitsiServiceProvider.php +++ b/src/EscolaLmsJitsiServiceProvider.php @@ -2,11 +2,10 @@ namespace EscolaLms\Jitsi; +use EscolaLms\Jitsi\Providers\SettingsServiceProvider; use Illuminate\Support\ServiceProvider; use EscolaLms\Jitsi\Services\Contracts\JitsiServiceContract; use EscolaLms\Jitsi\Services\JitsiService; -use EscolaLms\Settings\Facades\AdministrableConfig; -use EscolaLms\Jitsi\Enum\PackageStatusEnum; /** * SWAGGER_VERSION @@ -14,9 +13,6 @@ class EscolaLmsJitsiServiceProvider extends ServiceProvider { - - const CONFIG_KEY = 'jitsi'; - public $singletons = [ JitsiServiceContract::class => JitsiService::class, ]; @@ -28,18 +24,15 @@ class EscolaLmsJitsiServiceProvider extends ServiceProvider */ public function boot() { - AdministrableConfig::registerConfig(self::CONFIG_KEY . '.package_status', ['required', 'string', 'in:' . implode(',', PackageStatusEnum::getValues())], false); - AdministrableConfig::registerConfig(self::CONFIG_KEY . '.host', ['required', 'string'], true); - AdministrableConfig::registerConfig(self::CONFIG_KEY . '.app_id', ['nullable', 'string'], false); - AdministrableConfig::registerConfig(self::CONFIG_KEY . '.secret', ['nullable', 'string'], false); } - public function register() { $this->mergeConfigFrom( __DIR__ . '/../config/jitsi.php', 'jitsi' ); + + $this->app->register(SettingsServiceProvider::class); } } diff --git a/src/Providers/SettingsServiceProvider.php b/src/Providers/SettingsServiceProvider.php new file mode 100644 index 0000000..1762edb --- /dev/null +++ b/src/Providers/SettingsServiceProvider.php @@ -0,0 +1,27 @@ +app->getProviders(EscolaLmsSettingsServiceProvider::class)) { + $this->app->register(EscolaLmsSettingsServiceProvider::class); + } + + AdministrableConfig::registerConfig(self::CONFIG_KEY . '.package_status', ['required', 'string', 'in:' . implode(',', PackageStatusEnum::getValues())], false); + AdministrableConfig::registerConfig(self::CONFIG_KEY . '.host', ['required', 'string']); + AdministrableConfig::registerConfig(self::CONFIG_KEY . '.app_id', ['nullable', 'string'], false); + AdministrableConfig::registerConfig(self::CONFIG_KEY . '.secret', ['nullable', 'string'], false); + } + } +} \ No newline at end of file diff --git a/testbench.yaml b/testbench.yaml index 7d2d04a..7e527ab 100644 --- a/testbench.yaml +++ b/testbench.yaml @@ -11,3 +11,6 @@ providers: - Spatie\Permission\PermissionServiceProvider - EscolaLms\Jitsi\EscolaLmsJitsiServiceProvider - EscolaLms\Settings\EscolaLmsSettingsServiceProvider + - EscolaLms\Core\EscolaLmsServiceProvider + - EscolaLms\Auth\EscolaLmsAuthServiceProvider + - EscolaLms\Categories\EscolaLmsCategoriesServiceProvider \ No newline at end of file diff --git a/tests/Api/SettingsApiTest.php b/tests/Api/SettingsApiTest.php new file mode 100644 index 0000000..24b5c55 --- /dev/null +++ b/tests/Api/SettingsApiTest.php @@ -0,0 +1,140 @@ +markTestSkipped('Settings package not installed'); + } + + $this->seed(PermissionTableSeeder::class); + Config::set('escola_settings.use_database', true); + + $this->user = config('auth.providers.users.model')::factory()->create(); + $this->user->guard_name = 'api'; + $this->user->assignRole('admin'); + } + + public function testAdministrableConfigApi(): void + { + $configKey = SettingsServiceProvider::CONFIG_KEY; + + $packageStatus = $this->faker->randomElement(PackageStatusEnum::getValues()); + $host = $this->faker->domainWord; + $appId = $this->faker->uuid; + $secret = $this->faker->uuid; + + $this->response = $this->actingAs($this->user, 'api')->json( + 'POST', + '/api/admin/config', + [ + 'config' => [ + [ + 'key' => "$configKey.package_status", + 'value' => $packageStatus, + ], + [ + 'key' => "$configKey.host", + 'value' => $host, + ], + [ + 'key' => "$configKey.app_id", + 'value' => $appId, + ], + [ + 'key' => "$configKey.secret", + 'value' => $secret, + ], + ] + ] + ); + $this->response->assertOk(); + + $this->response = $this->actingAs($this->user, 'api')->json( + 'GET', + '/api/admin/config' + ); + + $this->response->assertOk(); + + $this->response->assertJsonFragment([ + $configKey => [ + 'package_status' => [ + 'full_key' => "$configKey.package_status", + 'key' => 'package_status', + 'rules' => [ + 'required', + 'string', + 'in:' . implode(',', PackageStatusEnum::getValues()), + ], + 'public' => false, + 'readonly' => false, + 'value' => $packageStatus, + ], + 'host' => [ + 'full_key' => "$configKey.host", + 'key' => 'host', + 'rules' => [ + 'required', + 'string' + ], + 'public' => true, + 'readonly' => false, + 'value' => $host, + ], + 'app_id' => [ + 'full_key' => "$configKey.app_id", + 'key' => 'app_id', + 'rules' => [ + 'nullable', + 'string', + ], + 'public' => false, + 'value' => $appId, + 'readonly' => false, + ], + 'secret' => [ + 'full_key' => "$configKey.secret", + 'key' => 'secret', + 'rules' => [ + 'nullable', + 'string' + ], + 'public' => false, + 'readonly' => false, + 'value' => $secret, + ], + ], + ]); + + $this->response = $this->json( + 'GET', + '/api/config' + ); + + $this->response->assertOk(); + + $this->response->assertJsonFragment([ + $configKey => [ + 'host' => $host, + ], + ]); + } +} \ No newline at end of file