Files
zoneminder/tests/php/test_controlcaps_delete_ids.php
T

58 lines
2.4 KiB
PHP

<?php
// Deleting control capabilities took any numeric-looking cid and then stripped it down to its
// digits, so '-3', '1.5' or '1e1' deleted capability 3, 15 or 11, and updated the monitors
// using it. Only a plain whole number names a capability; anything else is skipped.
//
// Run as: php tests/php/test_controlcaps_delete_ids.php [root] (root defaults to the repo)
class Done extends Exception {}
function canEdit($a) { return true; }
function validCardinal($v) { return is_scalar($v) ? preg_replace('/\D/', '', $v) : ''; }
function ajaxError($m) { $GLOBALS['out'] = 'error: '.$m; throw new Done(); }
function ajaxResponse($r = array()) { $GLOBALS['out'] = 'ok'; throw new Done(); }
function dbLastError() { return ''; }
function dbQuery($sql, $values = array()) {
$GLOBALS['queries'][] = array(trim($sql), $values);
return true;
}
$root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..';
function deleted($cids) {
global $root;
$GLOBALS['out'] = 'nothing';
$GLOBALS['queries'] = array();
$_REQUEST = array('action' => 'delete', 'cids' => $cids);
try {
include $root.'/web/ajax/controlcaps.php';
} catch (Done $e) {
}
$ids = array();
foreach ($GLOBALS['queries'] as $q) {
if (strpos($q[0], 'DELETE FROM Controls') === 0) $ids[] = $q[1][0];
}
return $ids;
}
$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"; }
}
check('a negative id deletes nothing', deleted(array('-3')), array());
check('a decimal id deletes nothing', deleted(array('1.5')), array());
check('an exponent id deletes nothing', deleted(array('1e1')), array());
check('a signed id deletes nothing', deleted(array('+3')), array());
check('an id with spaces deletes nothing', deleted(array(' 3')), array());
check('plain ids are deleted', deleted(array('3', '12')), array('3', '12'));
check('an integer id is deleted', deleted(array(7)), array('7'));
check('a single id given as a string is deleted', deleted('5'), array('5'));
check('zero is still passed through', deleted(array('0')), array('0'));
check('empty and list entries are skipped', deleted(array('', array('4'), '8')), array('8'));
check('a bad id beside a good one leaves the good one', deleted(array('-3', '9')), array('9'));
echo $failures ? "\n$failures failed\n" : "\nall passed\n";
exit($failures ? 1 : 0);