id = $id; } public function Id() { return $this->id; } public function canEdit() { return true; } } } namespace { $root = $argv[1] ?? __DIR__.'/../..'; function canEdit($area) { return true; } function validCardinal($v) { return preg_match('/^\d+$/', (string)$v) ? (int)$v : ''; } function validHtmlStr($s) { return htmlspecialchars((string)$s); } function localRedirectUrl($url, $fallback) { return $fallback; } function dbLastError() { return ''; } // Like PDO, which turns an array bound as a string into "Array" with a warning. function dbQuery($sql, $params = array()) { $GLOBALS['queries'][] = array($sql, array_map(function($p) { return is_array($p) ? 'Array' : $p; }, $params)); return true; } class StubConn { public function beginTransaction() {} public function commit() {} public function rollBack() {} } $passed = 0; $failed = 0; function check($label, $got, $want) { global $passed, $failed; if ($got === $want) { $passed++; echo " ok $label\n"; } else { $failed++; echo " FAIL $label\n"; echo " got: ".var_export($got, true)."\n"; echo " want: ".var_export($want, true)."\n"; } } // Returns the UPDATEs sent and whether an error was reported. function run($file, $action, $request) { global $root, $error_message, $refreshParent, $closePopup, $redirect; $GLOBALS['queries'] = array(); $GLOBALS['audits'] = array(); $error_message = ''; $refreshParent = $closePopup = false; $redirect = ''; $_REQUEST = $request; $_SERVER['HTTP_REFERER'] = ''; $dbConn = new StubConn(); try { include $root.'/web/includes/actions/'.$file; } catch (Throwable $e) { return array('threw: '.get_class($e), array()); } return array($error_message !== '' ? 'error' : 'ok', $GLOBALS['queries']); } echo "event.php rename\n"; foreach (array('Front gate', '', '0') as $name) { check("rename to '$name' is written", run('event.php', 'rename', array('eid' => '5', 'eventName' => $name)), array('ok', array(array('UPDATE Events SET Name=? WHERE Id=?', array($name, 5))))); } check('rename with a list writes nothing and reports it', run('event.php', 'rename', array('eid' => '5', 'eventName' => array('x'))), array('error', array())); check('rename with a nested list writes nothing and reports it', run('event.php', 'rename', array('eid' => '5', 'eventName' => array(array('x')))), array('error', array())); echo "event.php eventdetail\n"; check('text, empty and "0" are written', run('event.php', 'eventdetail', array('eid' => '5', 'newEvent' => array('Cause' => '', 'Notes' => '0'))), array('ok', array(array('UPDATE Events SET Cause=?, Notes=? WHERE Id=?', array('', '0', 5))))); check('a list as the cause writes nothing', run('event.php', 'eventdetail', array('eid' => '5', 'newEvent' => array('Cause' => array('x'), 'Notes' => 'n'))), array('error', array())); check('a list as the notes writes nothing', run('event.php', 'eventdetail', array('eid' => '5', 'newEvent' => array('Cause' => 'c', 'Notes' => array('x')))), array('error', array())); check('newEvent that is not an array writes nothing', run('event.php', 'eventdetail', array('eid' => '5', 'newEvent' => 'x')), array('error', array())); echo "eventdetail.php\n"; check('one event: text and blanks are written', run('eventdetail.php', 'eventdetail', array('markEids' => array('1'), 'newEvent' => array('Name' => 'a', 'Cause' => '', 'Notes' => '0'))), array('ok', array(array('UPDATE Events SET Name=?, Cause=?, Notes=? WHERE Id=?', array('a', '', '0', 1))))); foreach (array('Name', 'Cause', 'Notes') as $field) { $new = array('Name' => 'a', 'Cause' => 'c', 'Notes' => 'n'); $new[$field] = array('x'); check("a list as $field writes nothing for any event", run('eventdetail.php', 'eventdetail', array('markEids' => array('1', '2'), 'newEvent' => $new)), array('error', array())); } check('newEvent that is not an array writes nothing', run('eventdetail.php', 'eventdetail', array('markEids' => array('1'), 'newEvent' => 'x')), array('error', array())); echo "\n$passed passed, $failed failed\n"; exit($failed ? 1 : 0); }