diff --git a/tests/php/test_api_configs_index_confd.php b/tests/php/test_api_configs_index_confd.php new file mode 100644 index 0000000000..1714e9442a --- /dev/null +++ b/tests/php/test_api_configs_index_confd.php @@ -0,0 +1,65 @@ +rows; } + function schema() { return array('Name' => 1, 'Value' => 1); } +} +class FakeRequest { public $params = array('named' => array()); } + +eval('class ConfigsHarness { public $request, $Config, $vars; + function set($v) { $this->vars = $v; } + function requireSystemView() {} + function filterReferencesValue($p) { return false; } + function isSecretConfig($c, $n = null) { return false; } + function redactSecret($c, $n = null) { return $c; } + '.$method.' }'); + +$failures = 0; +function check($n, $got, $want) { + global $failures; + if ($got === $want) echo "ok - $n\n"; + else { $failures++; echo "FAIL - $n (got ".var_export($got, true).", want ".var_export($want, true).")\n"; } +} + +$zm_config = array( + 'ZM_WEB_TITLE' => array('Name' => 'ZM_WEB_TITLE', 'Value' => 'From conf.d'), + 'ZM_LANG_DEFAULT' => array('Name' => 'ZM_LANG_DEFAULT', 'Value' => 'en_gb'), +); +$h = new ConfigsHarness(); +$h->request = new FakeRequest(); +$h->Config = new FakeConfig(); +$h->Config->rows = array( + array('Config' => array('Name' => 'ZM_WEB_TITLE', 'Value' => 'From the database')), + array('Config' => array('Name' => 'ZM_LANG_DEFAULT', 'Value' => 'en_gb')), +); +$h->index(); +$byName = array(); +foreach ($h->vars['configs'] as $c) $byName[$c['Config']['Name']] = $c['Config']['Value']; + +check('an option overridden in conf.d is answered with the conf.d value', $byName['ZM_WEB_TITLE'] ?? null, 'From conf.d'); +check('an option not overridden keeps its value', $byName['ZM_LANG_DEFAULT'] ?? null, 'en_gb'); +check('each option is answered once', count($h->vars['configs']), 2); + +echo $failures ? "\n$failures failed\n" : "\nall passed\n"; +exit($failures ? 1 : 0); diff --git a/web/api/app/Controller/ConfigsController.php b/web/api/app/Controller/ConfigsController.php index ac62090992..3f5b8655b9 100644 --- a/web/api/app/Controller/ConfigsController.php +++ b/web/api/app/Controller/ConfigsController.php @@ -116,9 +116,11 @@ class ConfigsController extends AppController { $configs = $visible; } + # References into $configs itself: a reference to the loop's copy $c is lost + # once the loop moves on, and the conf.d values below never reached the response. $config_by_name = []; - foreach ($configs as $c) { - $config_by_name[$c['Config']['Name']] = &$c['Config']; + foreach (array_keys($configs) as $i) { + $config_by_name[$configs[$i]['Config']['Name']] = &$configs[$i]['Config']; } global $zm_config;