-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
$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'; | ||
} | ||
} |