mirror of
https://github.com/flarum/core.git
synced 2025-07-30 21:20:24 +02:00
Add Settings Extender (#2452)
This commit is contained in:
@@ -45,15 +45,6 @@ class ApiSerializerTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
protected function prepSettingsDb()
|
||||
{
|
||||
$this->prepareDatabase([
|
||||
'settings' => [
|
||||
['key' => 'customPrefix.customSetting', 'value' => 'customValue']
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
133
tests/integration/extenders/SettingsTest.php
Normal file
133
tests/integration/extenders/SettingsTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?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_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';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user