Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions src/Fieldtypes/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ public function rules(Field $field, $value = null)
];
}

// Fields stored in a column still need a rule, otherwise their
// value is left out of the validated data and never saved.
if ($this->hasColumn()) {
return [
$field->handle => 'sometimes',
];
}

return [];
}

Expand Down Expand Up @@ -405,6 +413,19 @@ public function getResource($model, Field $field)
return $this->getValue($model, $field);
}

/**
* Returns value of field in the format expected by
* the config value a setting overrides.
*
* @param mixed $value
*
* @return mixed
*/
public function getConfigValue($value)
{
return $value;
}

/**
* Destroy relationship data in storage.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Fieldtypes/ListFieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ class ListFieldtype extends Fieldtype
* @var array
*/
public $validation = []; // no validation

/**
* Returns value of field in the format expected by
* the config value a setting overrides.
*
* List items are saved as `{ _id, value }` pairs,
* config expects a plain list of values.
*
* @param mixed $value
*
* @return array
*/
public function getConfigValue($value)
{
return collect($value)
->map(function ($item) {
return is_array($item) ? ($item['value'] ?? null) : $item;
})
->values()
->all();
}
}
4 changes: 4 additions & 0 deletions src/Providers/SettingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ private function bootConfigOverrides()
$value = $value->all();
}

if ($fieldtype = $field->type()) {
$value = $fieldtype->getConfigValue($value);
}

// For file types we want to pass the path of the file
// through to override config values.
//
Expand Down
4 changes: 3 additions & 1 deletion src/Services/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function set($key, $value = null)
$group->blueprint->fields->each(function ($field) use ($group, $key, $value) {
if ($key == "{$group->handle}.{$field->handle}") {
if ($field->settings['override'] !== false) {
config([$field->settings['override'] => $value]);
$fieldtype = $field->type();

config([$field->settings['override'] => $fieldtype ? $fieldtype->getConfigValue($value) : $value]);
}
}
});
Expand Down
109 changes: 109 additions & 0 deletions tests/src/Feature/Settings/OptionalSettingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Fusion\Tests\Feature\Settings;

use Fusion\Fieldtypes\ListFieldtype;
use Fusion\Providers\SettingServiceProvider;
use Fusion\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class OptionalSettingTest extends TestCase
{
use RefreshDatabase;

/**
* Called before each test is run...
*
* @return void
*/
public function setUp(): void
{
parent::setUp();

$this->handleValidationExceptions();
}

/** @test */
public function a_setting_without_validation_rules_will_be_saved()
{
$this
->be($this->owner, 'api')
->json('PATCH', 'api/settings/cors', [
'cors_max_age' => 60,
'cors_supports_credentials' => true,
])->assertStatus(200);

$this->assertDatabaseHas('settings_cors', [
'cors_max_age' => 60,
'cors_supports_credentials' => true,
]);

$this->assertTrue(setting('cors.cors_supports_credentials'));
$this->assertTrue(config('cors.supports_credentials'));
}

/** @test */
public function a_list_setting_without_validation_rules_will_be_saved()
{
$paths = [
['_id' => 'a1', 'value' => 'api/*'],
['_id' => 'b2', 'value' => 'feeds/*'],
];

$this
->be($this->owner, 'api')
->json('PATCH', 'api/settings/cors', [
'cors_max_age' => 0,
'cors_paths' => $paths,
])->assertStatus(200);

$this->assertEquals($paths, setting('cors.cors_paths'));
}

/** @test */
public function a_list_setting_will_override_config_with_plain_values()
{
$this
->be($this->owner, 'api')
->json('PATCH', 'api/settings/cors', [
'cors_max_age' => 0,
'cors_paths' => [
['_id' => 'a1', 'value' => 'api/*'],
['_id' => 'b2', 'value' => 'feeds/*'],
],
])->assertStatus(200);

$this->assertSame(['api/*', 'feeds/*'], config('cors.paths'));

// Overrides applied when settings are loaded on boot..
config(['cors.paths' => []]);
$this->app->forgetInstance('setting');
(new SettingServiceProvider($this->app))->boot();

$this->assertSame(['api/*', 'feeds/*'], config('cors.paths'));
}

/** @test */
public function a_list_setting_default_will_override_config_unchanged()
{
$this->assertSame(['api/*'], setting('cors.cors_paths'));
$this->assertSame(['api/*'], (new ListFieldtype())->getConfigValue(setting('cors.cors_paths')));
}

/** @test */
public function optional_settings_left_out_of_the_request_keep_their_value()
{
setting(['cors.cors_supports_credentials' => true]);

$this
->be($this->owner, 'api')
->json('PATCH', 'api/settings/cors', [
'cors_max_age' => 30,
])->assertStatus(200);

$this->assertDatabaseHas('settings_cors', [
'cors_max_age' => 30,
'cors_supports_credentials' => true,
]);
}
}
15 changes: 0 additions & 15 deletions tests/stubs/laravel/config/fusion.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,4 @@

],

/*
|--------------------------------------------------------------------------
| Content Structures
|--------------------------------------------------------------------------
|
*/
'structures' => [

'forms' => 'Forms',
'matrix' => 'Matrix',
'menus' => 'Menus',
'users' => 'Users',

],

];