Files
zoneminder/tests/php/test_console_footer_no_monitors.php
T

110 lines
4.8 KiB
PHP

<?php
// The console footer's event totals link to the events view for the monitors the console is
// listing. When a search or filter leaves no monitor, the Monitor term had no value and dropped
// out, so the "0 events" link opened every event of every monitor. It now names monitor 0, which
// no event has. With monitors listed the link is unchanged.
//
// Run as: php tests/php/test_console_footer_no_monitors.php [root] [case] (root defaults to the repo)
// The case is picked by the driver, since console.php defines functions at file scope.
$root = isset($argv[1]) ? $argv[1] : realpath(__DIR__.'/../..');
if ($argc < 3) {
$failures = 0;
foreach (array('none', 'one', 'two') as $case) {
$out = array();
$rc = 0;
exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' '.escapeshellarg($root).' '.escapeshellarg($case).' 2>&1', $out, $rc);
echo implode("\n", $out)."\n";
if ($rc !== 0) $failures++;
}
echo $failures ? "\n$failures case(s) FAILED\n" : "\nall cases passed\n";
exit($failures ? 1 : 0);
}
$case = $argv[2];
namespace_stubs();
function namespace_stubs() {
eval('namespace ZM {
function Error($m) {} function Warning($m) {} function Debug($m) {}
class Monitor { private $r; function __construct($r = null) { $this->r = $r; } function __call($n, $a) { return $n == "Id" ? $this->r["Id"] : 0; } }
class Storage { static function find() { return array(); } }
class Group_Monitor { static function find($a = null) { return array(); } }
}');
}
$stub = sys_get_temp_dir().'/zm_console_footer_'.getmypid();
mkdir($stub, 0777, true);
file_put_contents($stub.'/_monitor_filters.php', '<?php
function getFilterSelection($n) { return ""; }
function getFilterText($n) { return ""; }
function monitorNameRegexp($f) { return "/x/"; }
function monitorStatusFilterSql($s, &$v, $e) { return "1"; }');
foreach (array('Monitor', 'Group', 'Group_Monitor') as $f) {
@mkdir($stub.'/includes');
file_put_contents($stub.'/includes/'.$f.'.php', '<?php');
}
set_include_path($stub.PATH_SEPARATOR.get_include_path());
register_shutdown_function(function() use ($stub) { system('rm -rf '.escapeshellarg($stub)); });
function getSkinFile($f) { global $stub; return $stub.'/_monitor_filters.php'; }
function canView($a, $id = null) { return false; }
function visibleMonitor($id) { return true; }
function human_filesize($b) { return (string)$b; }
function validHtmlStr($s) { return htmlspecialchars((string)$s); }
define('ZM_WEB_LIST_THUMBS', false);
// The filter helpers the footer builds its links with; querystring is the terms, readable.
$fn = file_get_contents($root.'/web/includes/functions.php');
preg_match('/^function addFilterTerm\(.*?\n\}/ms', $fn, $m);
eval($m[0]);
function parseFilter(&$filter) { $filter['querystring'] = json_encode($filter['Query']['terms']); return $filter; }
$rows = array();
function mon($id) {
return array('Id' => $id, 'Name' => "M$id", 'Function' => 'Monitor', 'Enabled' => 1, 'Sequence' => $id, 'Deleted' => 0,
'Status' => 'Running', 'Type' => 'Ffmpeg', 'ServerId' => 0, 'StorageId' => 0, 'Capturing' => 'Always',
'Analysing' => 'None', 'Decoding' => 'None', 'Recording' => 'None', 'ZoneCount' => 0, 'Manufacturer' => null, 'Model' => null,
'Width' => 1, 'Height' => 1, 'TotalEvents' => 2, 'HourEvents' => 0, 'DayEvents' => 0, 'WeekEvents' => 0, 'MonthEvents' => 0, 'ArchivedEvents' => 0);
}
if ($case == 'one') $rows = array(mon(5));
if ($case == 'two') $rows = array(mon(5), mon(7));
function dbFetchAll($sql, $col = null, $params = null) { return $GLOBALS['rows']; }
$Servers = array();
$user = new stdClass();
$_REQUEST = array();
$_COOKIE = array();
function ajaxResponse($d = array()) { $GLOBALS['data'] = $d; }
function ajaxError($m) {}
$_REQUEST = array('task' => 'query');
require $root.'/web/ajax/console.php';
$data = $GLOBALS['data'];
$failures = 0;
function check($label, $got, $want) {
global $failures;
if ($got === $want) { echo "ok - $label\n"; return; }
$failures++;
echo "FAIL - $label\n got: ".var_export($got, true)."\n want: ".var_export($want, true)."\n";
}
function monitorTerm($querystring) {
foreach (json_decode($querystring, true) as $t) {
if ($t['attr'] == 'Monitor') return $t;
}
return null;
}
foreach (array('Total', 'Hour', 'Day', 'Week', 'Month', 'Archived') as $period) {
$t = monitorTerm($data['footer'][$period.'FilterQuery']);
if ($case == 'none') {
check("$period footer link is limited to a monitor no event has", $t ? $t['val'] : null, '0');
} else if ($case == 'one') {
check("$period footer link is limited to the listed monitor", $t ? $t['val'] : null, '5');
} else {
check("$period footer link is limited to the listed monitors", $t ? $t['val'] : null, '5,7');
}
}
if ($case == 'none') check('the footer still counts no monitors', $data['footer']['monitor_count'], 0);
exit($failures ? 1 : 0);