From cfa533ebd6aafacc04b2393850b75f1fa097d259 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 4 Dec 2020 23:20:06 +0100 Subject: [PATCH] Add Settings Extender (#2452) --- src/Extend/Settings.php | 63 +++++++++ .../extenders/ApiSerializerTest.php | 9 -- tests/integration/extenders/SettingsTest.php | 133 ++++++++++++++++++ 3 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 src/Extend/Settings.php create mode 100644 tests/integration/extenders/SettingsTest.php diff --git a/src/Extend/Settings.php b/src/Extend/Settings.php new file mode 100644 index 000000000..a2c66b219 --- /dev/null +++ b/src/Extend/Settings.php @@ -0,0 +1,63 @@ +settings[$key] = compact('attributeName', 'callback'); + + return $this; + } + + public function extend(Container $container, Extension $extension = null) + { + if (! empty($this->settings)) { + AbstractSerializer::addMutator( + ForumSerializer::class, + function () use ($container) { + $settings = $container->make(SettingsRepositoryInterface::class); + $attributes = []; + + foreach ($this->settings as $key => $setting) { + $value = $settings->get($key, null); + + if (isset($setting['callback'])) { + $callback = ContainerUtil::wrapCallback($setting['callback'], $container); + $value = $callback($value); + } + + $attributes[$setting['attributeName']] = $value; + } + + return $attributes; + } + ); + } + } +} diff --git a/tests/integration/extenders/ApiSerializerTest.php b/tests/integration/extenders/ApiSerializerTest.php index 6e08ae9ea..2bee2bafd 100644 --- a/tests/integration/extenders/ApiSerializerTest.php +++ b/tests/integration/extenders/ApiSerializerTest.php @@ -45,15 +45,6 @@ class ApiSerializerTest extends TestCase ]); } - protected function prepSettingsDb() - { - $this->prepareDatabase([ - 'settings' => [ - ['key' => 'customPrefix.customSetting', 'value' => 'customValue'] - ], - ]); - } - /** * @test */ diff --git a/tests/integration/extenders/SettingsTest.php b/tests/integration/extenders/SettingsTest.php new file mode 100644 index 000000000..854c01465 --- /dev/null +++ b/tests/integration/extenders/SettingsTest.php @@ -0,0 +1,133 @@ +prepareDatabase([ + 'users' => [ + $this->adminUser(), + $this->normalUser() + ], + 'settings' => [ + ['key' => 'custom-prefix.custom_setting', 'value' => 'customValue'], + ['key' => 'custom-prefix.custom_setting2', 'value' => 'customValue'] + ] + ]); + } + + /** + * @test + */ + public function custom_setting_isnt_serialized_by_default() + { + $this->prepDb(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody(), true); + + $this->assertArrayNotHasKey('customPrefix.customSetting', $payload['data']['attributes']); + } + + /** + * @test + */ + public function custom_setting_serialized_if_added() + { + $this->extend( + (new Extend\Settings()) + ->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting') + ); + + $this->prepDb(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody(), true); + + $this->assertArrayHasKey('customPrefix.customSetting', $payload['data']['attributes']); + $this->assertEquals('customValue', $payload['data']['attributes']['customPrefix.customSetting']); + } + + /** + * @test + */ + public function custom_setting_callback_works_if_added() + { + $this->extend( + (new Extend\Settings()) + ->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting', function ($value) { + return $value.'Modified'; + }) + ); + + $this->prepDb(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody(), true); + + $this->assertArrayHasKey('customPrefix.customSetting', $payload['data']['attributes']); + $this->assertEquals('customValueModified', $payload['data']['attributes']['customPrefix.customSetting']); + } + + /** + * @test + */ + public function custom_setting_callback_works_with_invokable_class() + { + $this->extend( + (new Extend\Settings()) + ->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class) + ); + + $this->prepDb(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody(), true); + + $this->assertArrayHasKey('customPrefix.customSetting2', $payload['data']['attributes']); + $this->assertEquals('customValueModifiedByInvokable', $payload['data']['attributes']['customPrefix.customSetting2']); + } +} + +class CustomInvokableClass +{ + public function __invoke($value) + { + return $value.'ModifiedByInvokable'; + } +}