Files
zoneminder/tests/php/test_options_nested_values.php
T

154 lines
7.0 KiB
PHP

<?php
// Options save bound whatever the request held for an option to the Config
// UPDATE. A list for a string or boolean option was converted to the text
// "Array" and stored, and for a number option preg_match() threw a TypeError.
// Adding a menu entry with a list for its label, icon or link also threw a
// TypeError, after the existing entries had already been saved. A list is
// refused and nothing is written; text, an empty string, "0", an unchecked
// checkbox and an option that was not sent behave as before.
//
// Includes the real web/includes/actions/options.php (and config_values.php) from
// a repo root given as argv[1] (default __DIR__.'/../..'), with the database layer
// stubbed, so it can be pointed at a copy of the original file.
//
// Run as: php tests/php/test_options_nested_values.php [root]
namespace ZM {
function Warning($s) {}
function Error($s) {}
function Debug($s) {}
function AuditAction($a = null, $t = null, $i = null, $n = null) {}
class MenuItem {
public static $saved = array();
public static function find($p = array(), $o = array()) { return array(); }
public function save($v) { self::$saved[] = $v; return true; }
public function get_last_error() { return ''; }
}
}
namespace {
$root = $argv[1] ?? __DIR__.'/../..';
$stub = sys_get_temp_dir().'/zm_options_nested_'.getmypid();
@mkdir($stub.'/includes', 0777, true);
copy($root.'/web/includes/config_values.php', $stub.'/includes/config_values.php');
file_put_contents($stub.'/includes/MenuItem.php', '<?php');
set_include_path($stub.PATH_SEPARATOR.get_include_path());
$passes = 0;
$failures = 0;
function check($name, $got, $want) {
global $passes, $failures;
if ($got === $want) { $passes++; echo " ok $name\n"; }
else { $failures++; echo " FAIL $name\n got: ".var_export($got, true)."\n want: ".var_export($want, true)."\n"; }
}
$rows = array(
array('Name' => 'ZM_TEXT', 'Value' => 'old', 'Type' => 'string', 'DefaultValue' => 'old', 'Requires' => '', 'System' => 0, 'Readonly' => 0),
array('Name' => 'ZM_COUNT', 'Value' => '5', 'Type' => 'integer', 'DefaultValue' => '5', 'Requires' => '', 'System' => 0, 'Readonly' => 0),
array('Name' => 'ZM_FLAG', 'Value' => '1', 'Type' => 'boolean', 'DefaultValue' => '1', 'Requires' => '', 'System' => 0, 'Readonly' => 0),
);
$GLOBALS['queries'] = array();
function canEdit($a) { return true; }
function validHtmlStr($s) { return htmlspecialchars((string)$s); }
function dbLastError() { return ''; }
function dbQuery($sql, $params = array()) {
$GLOBALS['queries'][] = array($sql, $params);
if (strpos($sql, 'SELECT Name,Value,Type,DefaultValue') === 0) {
$GLOBALS['cursor'] = $GLOBALS['rows'];
return new stdClass();
}
return true;
}
function dbFetchNext($result) { return array_shift($GLOBALS['cursor']); }
function dbFetchAll($sql) { return $GLOBALS['rows']; }
function loadConfigFiles() { return array(); }
function configFileValue($v, $t) { return $v; }
function loadConfig($define = true) {}
function authHashSecretValid($s) { return true; }
function save_options($newConfig) {
global $error_message, $action, $redirect, $restartWarning;
$GLOBALS['rows'] = $GLOBALS['rowsDef'];
$GLOBALS['queries'] = array();
$error_message = '';
$redirect = '';
$restartWarning = false;
$action = 'options';
$_REQUEST = array('tab' => 'system', 'newConfig' => $newConfig);
try {
include $GLOBALS['root'].'/web/includes/actions/options.php';
} catch (Throwable $e) {
return array('threw '.get_class($e), array());
}
$updates = array();
foreach ($GLOBALS['queries'] as $q) {
if (strpos($q[0], 'UPDATE Config') === 0) $updates[] = $q[1];
}
return array($error_message === '' ? 'ok' : 'error', $updates);
}
$GLOBALS['rowsDef'] = $rows;
echo "option values\n";
check('text, empty text and "0" are saved',
save_options(array('ZM_TEXT' => 'new', 'ZM_COUNT' => '7', 'ZM_FLAG' => '1')),
array('ok', array(array('new', 'ZM_TEXT'), array('7', 'ZM_COUNT'))));
check('an empty string is saved', save_options(array('ZM_TEXT' => '', 'ZM_COUNT' => '5', 'ZM_FLAG' => '1')),
array('ok', array(array('', 'ZM_TEXT'))));
check('"0" is saved', save_options(array('ZM_TEXT' => '0', 'ZM_COUNT' => '5', 'ZM_FLAG' => '1')),
array('ok', array(array('0', 'ZM_TEXT'))));
check('an unchecked checkbox is saved as 0', save_options(array('ZM_TEXT' => 'old', 'ZM_COUNT' => '5')),
array('ok', array(array(0, 'ZM_FLAG'))));
check('an option that was not sent is left alone', save_options(array('ZM_FLAG' => '1')),
array('ok', array()));
echo "a list is refused\n";
check('a list for a string option writes nothing', save_options(array('ZM_TEXT' => array('x'), 'ZM_FLAG' => '1')),
array('error', array()));
check('a nested list for a string option writes nothing', save_options(array('ZM_TEXT' => array(array('x')), 'ZM_FLAG' => '1')),
array('error', array()));
check('a list for a number option writes nothing', save_options(array('ZM_COUNT' => array('7'), 'ZM_FLAG' => '1')),
array('error', array()));
check('a list for a boolean option writes nothing and does not turn it off', save_options(array('ZM_FLAG' => array('1'))),
array('error', array()));
check('the other options in the same request are still saved',
save_options(array('ZM_TEXT' => array('x'), 'ZM_COUNT' => '9', 'ZM_FLAG' => '1')),
array('error', array(array('9', 'ZM_COUNT'))));
function add_items($newItems) {
global $error_message, $action, $redirect;
ZM\MenuItem::$saved = array();
$error_message = '';
$redirect = '';
$action = 'menuitems';
$_REQUEST = array('items' => array(), 'newItems' => $newItems);
try {
include $GLOBALS['root'].'/web/includes/actions/options.php';
} catch (Throwable $e) {
return array('threw '.get_class($e), array());
}
return array($error_message === '' ? 'ok' : 'error', array_map(function($v) { return $v['MenuKey']; }, ZM\MenuItem::$saved));
}
echo "new menu entries\n";
check('text, an empty label and a missing field are added',
add_items(array(
array('MenuKey' => 'A', 'Label' => 'Label', 'Icon' => 'home', 'Link' => '?view=a'),
array('MenuKey' => 'B', 'Label' => '', 'Icon' => '', 'Link' => ''),
array('MenuKey' => 'C'),
)), array('ok', array('A', 'B', 'C')));
check('a blank key is skipped as before', add_items(array(array('MenuKey' => ' ', 'Label' => 'x'))), array('ok', array()));
foreach (array('Label', 'Icon', 'Link') as $field) {
check("a list for $field is not added, the next entry is",
add_items(array(array('MenuKey' => 'A', $field => array('x')), array('MenuKey' => 'B'))),
array('error', array('B')));
}
check('a list for the key is skipped', add_items(array(array('MenuKey' => array('x'), 'Label' => 'x'), array('MenuKey' => 'B'))),
array('ok', array('B')));
check('an entry that is not an array is skipped', add_items(array('x', array('MenuKey' => 'B'))), array('ok', array('B')));
echo "\n$passes passed, $failures failed\n";
exit($failures ? 1 : 0);
}