'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); }