event_ids = $term['val']; return $this; } public function addTerms($terms, $options = null) { return $this; } public function sql() { if ($this->event_ids !== null) return 'E.Id IN ('.implode(',', $this->event_ids ?: array(0)).')'; return 'E.MonitorId IN ('.implode(',', $this->monitor_ids).')'.$this->extra_sql; } public function post_sql_conditions() { return array(); } public function test_pre_sql_conditions() { return true; } public function limit() { return $this->limit; } public function sort_asc() { return true; } public function sort_field() { return 'StartDateTime'; } } class Event { private $row; public function __construct($row) { $this->row = $row; } public function remove_from_cache() {} public function canView() { return true; } public function Id() { return $this->row['Id']; } public function Monitor() { return new Monitor($this->row['MonitorId']); } 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($args, $sep) { return ''; } public function getStreamSrc($args, $sep) { return ''; } } class Monitor { private $id; public function __construct($id) { $this->id = $id; } public function Deleted() { return (bool)dbFetchOne('SELECT Deleted FROM Monitors WHERE Id=?', 'Deleted', array($this->id)); } } class Storage { public static function find() { return array(); } } } namespace { class TestUser { public function Username() { return 'tester'; } public function unviewableMonitorIds() { return array(); } } $includes = getenv('ZM_WEB_INCLUDES') ?: '/usr/share/zoneminder/www/includes'; if (!is_readable($includes.'/config.php')) { echo "skip - no readable config.php in $includes\n"; exit(0); } // events.php requires includes/Filter.php and the skin's _monitor_filters.php; // the classes and getFilteredMonitorIds() they provide are defined here. $stubdir = sys_get_temp_dir().'/zm_events_paging_stubs_'.getmypid(); @mkdir($stubdir.'/includes', 0700, true); file_put_contents($stubdir.'/includes/Filter.php', " '', 'offset' => 0, 'limit' => 0, 'sort' => 'StartDateTime', 'order' => 'ASC'), $request); return queryRequest($GLOBALS['test_filter'], $r['search'], array(), $r['sort'], $r['offset'], $r['order'], $r['limit']); } function ids($data) { return array_map(function($row) { return (int)$row['Id']; }, $data['rows']); } $db = zmDbConn(); $db->beginTransaction(); try { // 7 events that all start at the same time and are 10 or 20 seconds long dbQuery("INSERT INTO Monitors (Name, Deleted) VALUES ('testpaging', false)"); $mid = (int)dbInsertId(); $ids = array(); for ($i = 0; $i < 7; $i++) { dbQuery('INSERT INTO Events (MonitorId, StateId, Name, StartDateTime, EndDateTime, Length) VALUES (?, 1, ?, ?, ?, ?)', array($mid, 'testpaging '.$i, '2001-01-01 00:00:00', '2001-01-01 00:00:10', $i % 2 ? 20 : 10)); $ids[] = (int)dbInsertId(); } dbQuery("INSERT INTO Tags (Name) VALUES ('testpaging b')"); $tag_b = (int)dbInsertId(); dbQuery("INSERT INTO Tags (Name) VALUES ('testpaging a')"); $tag_a = (int)dbInsertId(); foreach (array(array($tag_b, $ids[1]), array($tag_a, $ids[1]), array($tag_a, $ids[4])) as $et) dbQuery('INSERT INTO Events_Tags (TagId, EventId) VALUES (?, ?)', $et); $GLOBALS['test_filter'] = new ZM\Filter(); $GLOBALS['test_filter']->monitor_ids = array($mid); foreach (array('StartDateTime' => $ids, 'Length' => array($ids[1], $ids[3], $ids[5], $ids[0], $ids[2], $ids[4], $ids[6])) as $sort => $want) { $pages = array(); for ($offset = 0; $offset < 7; $offset += 2) { $page = query(array('sort' => $sort, 'order' => 'DESC', 'limit' => '2', 'offset' => (string)$offset)); $pages = array_merge($pages, ids($page)); check("$sort page at offset $offset reports the full total", $page['total'], 7); } check("$sort pages list every event once, ties by Id", $pages, $want); } $tags = query(array('sort' => 'Tags', 'order' => 'DESC', 'limit' => '2', 'offset' => '0')); check('tag sort puts the tagged events first', ids($tags), array($ids[1], $ids[4])); check('tags are listed by name', $tags['rows'][0]['Tags'], 'testpaging a, testpaging b'); $GLOBALS['test_filter']->extra_sql = ' AND T.Id = '.$tag_a; $tagged = query(array('limit' => '1', 'offset' => '1')); check('tag filter counts the tagged events', $tagged['total'], 2); check('tag filter pages the tagged events', ids($tagged), array($ids[4])); check('tag filter shows the matching tag', $tagged['rows'][0]['Tags'], 'testpaging a'); $GLOBALS['test_filter']->extra_sql = ' AND E.NoSuchColumn = 1'; $error = ''; try { query(array('limit' => '2')); } catch (Exception $e) { $error = $e->getMessage(); } check('a failing query is reported', $error, 'ajaxError: Database query failed'); } finally { $db->rollBack(); } echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }