row = $row; } public function remove_from_cache() {} public function Id() { return $this->row['Id']; } public function canView() { return true; } public function Width() { return 0; } public function Height() { return 0; } public function ThumbnailWidth() { return 0; } public function ThumbnailHeight() { return 0; } public function DefaultVideo() { return ''; } public function DiskSpace() { return 0; } public function getThumbnailSrc($a, $s) { return ''; } public function getStreamSrc($a, $s) { return ''; } } class Storage { public static function find() { return array(); } } } namespace { class Done extends \Exception {} $scenarios = array('no_monitor_matches', 'search_with_no_events', 'search_with_events'); if (!isset($argv[1])) { $failures = 0; foreach ($scenarios as $scenario) { passthru(escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' '.$scenario, $rc); if ($rc) $failures++; } exit($failures ? 1 : 0); } $scenario = $argv[1]; $web = __DIR__.'/../../web'; $stubdir = sys_get_temp_dir().'/zm_events_empty_match_'.getmypid(); @mkdir($stubdir, 0700, true); foreach (array('database.php', 'Monitor.php', 'mf.php') as $stub) file_put_contents($stubdir.'/'.$stub, " 1, 2 => 1, 3 => 2) as $id => $mid) { $events[] = array('Id' => $id, 'MonitorId' => $mid, 'StorageId' => 0, 'Name' => 'e'.$id, 'Cause' => '', 'Tags' => '', 'Archived' => 0, 'Emailed' => 0, 'Notes' => '', 'Length' => 1, 'DiskSpace' => 0); } function matching($sql, $params = array()) { $rows = $GLOBALS['events']; if (isset($GLOBALS['base_ids']) and strpos($sql, 'LIKE') === false) { $base = $GLOBALS['base_ids']; $rows = array_filter($rows, function($r) use ($base) { return in_array($r['Id'], $base); }); } foreach (array('Id', 'MonitorId') as $col) { if (preg_match_all('/E\.`?'.$col.'`?\s+IN\s*\(([^)]*)\)/i', $sql, $m)) { foreach ($m[1] as $list) { $ids = (trim($list) !== '' and trim(str_replace(array('?', ',', ' '), '', $list)) === '') ? array_map('intval', (array)$params) : array_map('intval', array_map(function($v) { return trim($v, " '\""); }, explode(',', $list))); $rows = array_filter($rows, function($r) use ($col, $ids) { return in_array($r[$col], $ids); }); } } } return array_values($rows); } function dbQuery($sql, $params = array()) { $GLOBALS['pending'] = matching($sql, $params); return true; } function dbFetchNext($q) { return array_shift($GLOBALS['pending']); } function dbFetchAll($sql, $col = false, $params = null) { $rows = matching($sql, $params); return $col ? array_column($rows, $col) : $rows; } function dbFetchOne($sql, $col = false, $params = null) { return count(matching($sql, $params)); } function dbError($sql) { return ''; } $attr_monitor_ids = null; $_REQUEST = array('task' => 'query', 'sort' => 'Id', 'order' => 'asc'); if ($scenario == 'no_monitor_matches') { // The Status filter matched no monitor $attr_monitor_ids = array(); $want = array(); } else if ($scenario == 'search_with_no_events') { $base_ids = array(); $_REQUEST['search'] = 'e'; $want = array(); } else { $base_ids = array(1); $_REQUEST['search'] = 'e'; $want = array(1); } $out = null; try { include $web.'/ajax/events.php'; } catch (Done $e) { } $got = is_array($out) ? array_map(function($r) { return (int)$r['Id']; }, $out['rows']) : $out; if ($got === $want) { echo "ok - $scenario lists ".json_encode($want)."\n"; exit(0); } echo "FAIL - $scenario listed ".json_encode($got).", want ".json_encode($want)."\n"; exit(1); }