mirror of
https://github.com/flarum/framework.git
synced 2024-12-02 06:53:47 +08:00
ae280016e7
Previously, the `prepareDatabase` method would directly modify the database, booting the app in the process. This would prevent any extenders from being applied, since `->extend()` has no effect once the app is booted. Since the new implementation of `prepareDatabase` simply registers seed data to be applied during app boot, the workaround of sticking this seed data into `prepDb` is no longer necessary, and seed data common to all test cases in a class can be provided in `setUp`. When needed, app boot is explicitly triggered in individual test cases by calling `$this->app()`.
176 lines
5.0 KiB
PHP
176 lines
5.0 KiB
PHP
<?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;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->prepareDatabase([
|
|
'users' => [
|
|
$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()
|
|
{
|
|
$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')
|
|
);
|
|
|
|
$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';
|
|
})
|
|
);
|
|
|
|
$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)
|
|
);
|
|
|
|
$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_falls_back_to_default()
|
|
{
|
|
$this->extend(
|
|
(new Extend\Settings())
|
|
->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', null, 'customDefault')
|
|
);
|
|
|
|
$response = $this->send(
|
|
$this->request('GET', '/api', [
|
|
'authenticatedAs' => 1,
|
|
])
|
|
);
|
|
|
|
$payload = json_decode($response->getBody(), true);
|
|
|
|
$this->assertArrayHasKey('customPrefix.noCustomSetting', $payload['data']['attributes']);
|
|
$this->assertEquals('customDefault', $payload['data']['attributes']['customPrefix.noCustomSetting']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function custom_setting_default_passed_to_callback()
|
|
{
|
|
$this->extend(
|
|
(new Extend\Settings())
|
|
->serializeToForum('customPrefix.noCustomSetting', 'custom-prefix.no_custom_setting', function ($value) {
|
|
return $value.'Modified2';
|
|
}, 'customDefault')
|
|
);
|
|
|
|
$response = $this->send(
|
|
$this->request('GET', '/api', [
|
|
'authenticatedAs' => 1,
|
|
])
|
|
);
|
|
|
|
$payload = json_decode($response->getBody(), true);
|
|
|
|
$this->assertArrayHasKey('customPrefix.noCustomSetting', $payload['data']['attributes']);
|
|
$this->assertEquals('customDefaultModified2', $payload['data']['attributes']['customPrefix.noCustomSetting']);
|
|
}
|
|
}
|
|
|
|
class CustomInvokableClass
|
|
{
|
|
public function __invoke($value)
|
|
{
|
|
return $value.'ModifiedByInvokable';
|
|
}
|
|
}
|