Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Settings Extender #2452

Merged
merged 7 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/Extend/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Extend;

use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;

class Settings implements ExtenderInterface
{
private $settings = [];
private $defaults = [];
private $modifiers = [];

/**
* Serialize a setting value to the ForumSerializer attributes.
*
* @param string $attributeName: The attribute name to be used in the ForumSerializer attributes array.
* @param string $key: The key of the setting.
* @return $this
*/
public function serializeToForum(string $attributeName, string $key)
{
$this->settings[$key] = $attributeName;

return $this;
}

/**
* Set a default value for a setting selected for serialization.
*
* @param string $key
* @param $value
* @return $this
*/
public function default(string $key, $value)
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->defaults[$key] = $value;

return $this;
}

/**
* Modify a setting's value.
*
* @param string $key
* @param $callback
* @return $this
*/
public function modifier(string $key, $callback)
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->modifiers[$key] = $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 => $attributeName) {
$value = $settings->get($key, $this->defaults[$key] ?? null);

if (isset($this->modifiers[$key])) {
$callback = ContainerUtil::wrapCallback($this->modifiers[$key], $container);
$value = $callback($value);
}

$attributes[$attributeName] = $value;
}

return $attributes;
}
);
}
}
}
9 changes: 0 additions & 9 deletions tests/integration/extenders/ApiSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ protected function prepDb()
]);
}

protected function prepSettingsDb()
{
$this->prepareDatabase([
'settings' => [
['key' => 'customPrefix.customSetting', 'value' => 'customValue']
],
]);
}

/**
* @test
*/
Expand Down
188 changes: 188 additions & 0 deletions tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tests\integration\extenders;

use Flarum\Extend;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;

class SettingsTest extends TestCase
{
use RetrievesAuthorizedUsers;

protected function prepDb()
{
$this->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_doesnt_exist_by_default()
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
{
$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_exists_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_falls_back_to_default_value()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting', 'custom-prefix.unavailable_custom_setting')
->default('custom-prefix.unavailable_custom_setting', 'default')
);

$this->prepDb();

$response = $this->send(
$this->request('GET', '/api', [
'authenticatedAs' => 1,
])
);

$payload = json_decode($response->getBody(), true);

$this->assertArrayHasKey('customPrefix.unavailableCustomSetting', $payload['data']['attributes']);
$this->assertEquals('default', $payload['data']['attributes']['customPrefix.unavailableCustomSetting']);
}

/**
* @test
*/
public function custom_setting_modifier_works_if_added()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting')
->modifier('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_modifier_works_with_invokable_class()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2')
->modifier('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']);
}

/**
* @test
*/
public function custom_setting_modifier_works_on_default_value()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting2', 'custom-prefix.unavailable_custom_setting2')
->default('custom-prefix.unavailable_custom_setting2', 'default')
->modifier('custom-prefix.unavailable_custom_setting2', 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.unavailableCustomSetting2', $payload['data']['attributes']);
$this->assertEquals('defaultModified', $payload['data']['attributes']['customPrefix.unavailableCustomSetting2']);
}
}

class CustomInvokableClass
{
public function __invoke($value)
{
return $value.'ModifiedByInvokable';
}
}