Implement conditions for fixed-settings in config (#6475)

* Implement conditions for `fixed-settings` in config

* Implement callable function for `fixed-settings` in config

* Tests for fixed settings from config
This commit is contained in:
Yuriy Bakhtin 2023-08-03 20:04:37 +04:00 committed by GitHub
parent cb52feb331
commit b1c47c7c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 8 deletions

View File

@ -8,6 +8,7 @@ HumHub Changelog
- Fix #6468: Module Administration - Marketplace Links broken without Pretty URLs
- Enh #6469: Added Info text for Marketplace page
- Fix #112: Reorder Table Rows
- Enh #6469: Implement conditions for `fixed-settings` in config
1.15.0-beta.1 (July 31, 2023)
-----------------------------

View File

@ -118,13 +118,33 @@ class SettingsManager extends BaseSettingsManager
* Indicates this setting is fixed in configuration file and cannot be
* changed at runtime.
*
* @param string|int $name
*
* @param string $name
* @return boolean
*/
public function isFixed(string $name): bool
{
return isset(Yii::$app->params['fixed-settings'][$this->moduleId][$name]);
return $this->getFixed($name) !== null;
}
/**
* Get the fixed setting value from configuration file.
*
* @param string $name
* @return mixed
*/
public function getFixed(string $name)
{
if (!isset(Yii::$app->params['fixed-settings'][$this->moduleId][$name])) {
return null;
}
$value = Yii::$app->params['fixed-settings'][$this->moduleId][$name];
if (is_callable($value)) {
return call_user_func($value, $this);
}
return $value;
}
/**
@ -132,10 +152,8 @@ class SettingsManager extends BaseSettingsManager
*/
public function get(string $name, $default = null)
{
if ($this->isFixed($name)) {
return Yii::$app->params['fixed-settings'][$this->moduleId][$name];
}
$fixedValue = $this->getFixed($name);
return parent::get($name, $default);
return $fixedValue ?? parent::get($name, $default);
}
}

View File

@ -117,6 +117,7 @@ class MailingSettingsForm extends Model
{
$settingsManager = Yii::$app->settings;
$systemEmailAddressIsFixedBefore = $settingsManager->isFixed('mailer.systemEmailAddress');
$settingsManager->set('mailer.transportType', $this->transportType);
if ($this->transportType === self::TRANSPORT_SMTP) {
@ -132,7 +133,10 @@ class MailingSettingsForm extends Model
$settingsManager->set('mailer.dsn', $this->dsn);
}
$settingsManager->set('mailer.systemEmailAddress', $this->systemEmailAddress);
if (!$systemEmailAddressIsFixedBefore && !$settingsManager->isFixed('mailer.systemEmailAddress')) {
// Update it only when it was not fixed before and after current updating
$settingsManager->set('mailer.systemEmailAddress', $this->systemEmailAddress);
}
$settingsManager->set('mailer.systemEmailName', $this->systemEmailName);
$settingsManager->set('mailer.systemEmailReplyTo', $this->systemEmailReplyTo);

View File

@ -169,6 +169,44 @@ class SettingsManagerTest extends HumHubDbTestCase
$this->assertEquals($value, $sm->get($setting));
}
public function testSettingFixedValues()
{
$module = 'base';
$sm = new SettingsManager(['moduleId' => $module]);
// Test callable function for fixed settings
Yii::$app->params['fixed-settings'][$module]['test.first'] = function (SettingsManager $sm) {
if ($sm->get('test.second') === 'secondValueFixed1') {
return 'value1FromFixedConfig';
}
if ($sm->get('test.second') === 'secondValueFixed2') {
return 'value2FromFixedConfig';
}
return null;
};
$sm->set('test.first', 'firstValueDB');
$sm->set('test.second', 'secondValueDB');
$this->assertEquals($sm->get('test.first'), 'firstValueDB');
$this->assertEquals($sm->get('test.second'), 'secondValueDB');
// Set special value for second param in order to force the first param from fixed config
$sm->set('test.second', 'secondValueFixed1');
$this->assertEquals($sm->get('test.first'), 'value1FromFixedConfig');
$sm->set('test.second', 'secondValueFixed2');
$this->assertEquals($sm->get('test.first'), 'value2FromFixedConfig');
// Test simple value
Yii::$app->params['fixed-settings'][$module]['test.first'] = 'staticValueFromFixedConfig';
$this->assertEquals($sm->get('test.first'), 'staticValueFromFixedConfig');
// Reset fixed value
Yii::$app->params['fixed-settings'][$module]['test.first'] = null;
$this->assertEquals($sm->get('test.first'), 'firstValueDB');
}
public function testSerialized()
{
$module = 'base';