Compare commits

...

1 Commits

Author SHA1 Message Date
Benjamin DALSASS
928cb4b214 N°9728 - improve config editor 2026-11-23 14:00:52 +01:00
2 changed files with 147 additions and 5 deletions

View File

@@ -2615,9 +2615,11 @@ class Config
* Write the configuration to a file (php format) that can be reloaded later
* By default write to the same file that was specified when constructing the object
*
* @since 3.2.3-2 3.2.4 3.3.0 N°9728
*
* @param string $sFileName string Name of the file to write to (emtpy to write to the same file)
*
* @return boolean True otherwise throws an Exception
* @return bool True otherwise throws an Exception
*
* @throws \ConfigException
*/
@@ -2733,7 +2735,7 @@ class Config
$aParserValue = null;
}
$sSeenAs = self::PrettyVarExport($aParserValue, $aSettingInfo['value'], "\t");
fwrite($hFile, "\t'$sPropCode' => $sSeenAs,\n");
fwrite($hFile, "\t".var_export((string) $sPropCode, true)." => $sSeenAs,\n");
}
}
fwrite($hFile, ");\n");
@@ -2742,10 +2744,10 @@ class Config
fwrite($hFile, "/**\n *\n * Modules specific settings\n *\n */\n");
fwrite($hFile, "\$MyModuleSettings = array(\n");
foreach ($this->m_aModuleSettings as $sModule => $aProperties) {
fwrite($hFile, "\t'$sModule' => array (\n");
fwrite($hFile, "\t".var_export((string) $sModule, true)." => array (\n");
foreach ($aProperties as $sProperty => $value) {
$sNiceExport = self::PrettyVarExport($this->oItopConfigParser->GetVarValue('MyModuleSettings', $sProperty), $value, "\t\t");
fwrite($hFile, "\t\t'$sProperty' => $sNiceExport,\n");
fwrite($hFile, "\t\t".var_export((string) $sProperty, true)." => $sNiceExport,\n");
}
fwrite($hFile, "\t),\n");
}
@@ -2763,7 +2765,7 @@ class Config
} else {
fwrite($hFile, "\t'addons' => array (\n");
foreach ($this->m_aAddons as $sKey => $sFile) {
fwrite($hFile, "\t\t'$sKey' => '$sFile',\n");
fwrite($hFile, "\t\t".var_export((string) $sKey, true).' => '.var_export($sFile, true).",\n");
}
fwrite($hFile, "\t),\n");
}

View File

@@ -24,6 +24,10 @@ namespace Combodo\iTop\Test\UnitTest\Module\iTopConfig;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use Config;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node;
use PhpParser\ParserFactory;
use utils;
class ConfigTest extends ItopTestCase
{
@@ -91,4 +95,140 @@ class ConfigTest extends ItopTestCase
],
];
}
/**
* @since 3.2.3
* @dataProvider ProviderArrayKeys
*/
public function testWriteToFilePreservesModuleKeys(int|string $key): void
{
$oConfig = new Config();
$oConfig->SetModuleSetting('existing-module', 'existing.property', true);
$oConfig->SetModuleSetting($key, 'sample.property', 'value');
$this->AssertWriteToFilePreservesSettings($oConfig, [
'existing-module' => ['existing.property' => true],
$key => ['sample.property' => 'value'],
], $oConfig->GetAddons());
}
/**
* @since 3.2.3
* @dataProvider ProviderArrayKeys
*/
public function testWriteToFilePreservesPropertyKeys(int|string $key): void
{
$oConfig = new Config();
$oConfig->SetModuleSetting('sample-module', 'existing.property', true);
$oConfig->SetModuleSetting('sample-module', $key, [$key => 'value']);
$this->AssertWriteToFilePreservesSettings($oConfig, [
'sample-module' => [
'existing.property' => true,
$key => [$key => 'value'],
],
], $oConfig->GetAddons());
}
/**
* @since 3.2.3
* @dataProvider ProviderArrayKeys
*/
public function testWriteToFilePreservesAddons(int|string $key): void
{
$oConfig = new Config();
$aAddons = $oConfig->GetAddons();
$aAddons[$key] = 'addons/'.$key.'/module.php';
$oConfig->SetAddons($aAddons);
$this->AssertWriteToFilePreservesSettings($oConfig, [], $aAddons);
}
/**
* @since 3.2.3
* @dataProvider ProviderArrayKeys
*/
public function testWriteToFilePreservesIncrementalAddons(int|string $key): void
{
$aAddons = (new Config())->GetAddons();
$aAddons[$key] = 'addons/'.$key.'/module.php';
$sSourceFile = tempnam(sys_get_temp_dir(), 'config-source');
$this->aFileToClean[] = $sSourceFile;
$this->assertNotFalse(file_put_contents($sSourceFile, "<?php\n"
."\$MySettings = [];\n"
."\$MyModuleSettings = [];\n"
."\$MyModules = [];\n"
."\$MyModules['addons'] = ".var_export($aAddons, true).";\n"));
$this->AssertWriteToFilePreservesSettings(new Config($sSourceFile), [], $aAddons);
}
/**
* @since 3.2.3
* @return array<string, array{int|string}>
*/
public static function ProviderArrayKeys(): array
{
return [
'module and property separators' => ['sample-module.setting_1'],
'apostrophe' => ["O'Reilly"],
'double quote' => ['a"b'],
'backslash' => ['a\\b'],
'repeated backslashes' => ['a\\\\b'],
'trailing backslash' => ['trailing\\'],
'backslash before apostrophe' => ["a\\'b"],
'unicode' => ["caf\u{00E9}-\u{65E5}\u{672C}"],
'line breaks' => ["first\nsecond\rthird"],
'dollar sign' => ['$value'],
'empty string' => [''],
'zero' => [0],
'integer' => [7],
'negative integer' => [-7],
'numeric string' => ['7'],
'leading zero' => ['07'],
];
}
/**
* @since 3.2.3
* @param array<int|string, array<int|string, mixed>> $aModuleSettings
* @param array<int|string, string> $aAddons
*/
private function AssertWriteToFilePreservesSettings(Config $oConfig, array $aModuleSettings, array $aAddons): void
{
$oOriginalConfig = utils::GetConfig();
$oParser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$oEvaluator = new ConstExprEvaluator();
try {
for ($i = 0; $i < 2; $i++) {
$sTargetFile = tempnam(sys_get_temp_dir(), 'config-target');
$this->aFileToClean[] = $sTargetFile;
$this->assertTrue($oConfig->WriteToFile($sTargetFile));
$aNodes = $oParser->parse(file_get_contents($sTargetFile));
$this->assertCount(3, $aNodes);
$aValues = [];
foreach ($aNodes as $oNode) {
$this->assertInstanceOf(Node\Stmt\Expression::class, $oNode);
$this->assertInstanceOf(Node\Expr\Assign::class, $oNode->expr);
$this->assertInstanceOf(Node\Expr\Variable::class, $oNode->expr->var);
$aValues[$oNode->expr->var->name] = $oEvaluator->evaluateDirectly($oNode->expr->expr);
}
$this->assertSame(['MySettings', 'MyModuleSettings', 'MyModules'], array_keys($aValues));
$this->assertSame($aModuleSettings, $aValues['MyModuleSettings']);
$this->assertSame(['addons' => $aAddons], $aValues['MyModules']);
$oConfig = new Config($sTargetFile);
$this->assertSame($aAddons, $oConfig->GetAddons());
foreach ($aModuleSettings as $sModule => $aProperties) {
foreach ($aProperties as $sProperty => $value) {
$this->assertSame($value, $oConfig->GetModuleSetting($sModule, $sProperty));
}
}
}
} finally {
utils::SetConfig($oOriginalConfig);
}
}
}