f = Rec::$db[$id]; } public function __call($name, $args) { if (count($args)) $this->f[$name] = $args[0]; return isset($this->f[$name]) ? $this->f[$name] : null; } public function Id($v = null) { return isset($this->f['Id']) ? $this->f['Id'] : null; } public function Storage() { return new Storage(); } public function zmcControl($m) {} public function sendControlCommand($m) {} public function commandFieldChangesNotAllowed($d) { return array(); } public function changes($new) { $c = array(); foreach ($new as $k => $v) { if (!array_key_exists($k, $this->f) or (string)$this->f[$k] !== (string)$v) $c[$k] = $v; } return $c; } public function save($changes) { Rec::$saved[] = $changes; foreach ($changes as $k => $v) $this->f[$k] = $v; return true; } public function insert($changes) { Rec::$inserted[] = $changes; $this->f = $changes + array('Id' => 99); return true; } public function get_last_error() { return ''; } } class Zone { public function save($d) { return true; } public function get_last_error() { return ''; } } } namespace { define('ZM_OPT_X10', false); define('ZM_OPT_CONTROL', false); function canEdit($p, $id = null) { return true; } function canView($p, $id = null) { return true; } function canCreate($p) { return true; } function validInt($v) { return preg_replace('/[^\-\d]/', '', $v); } function validCardinal($v) { return preg_replace('/\D/', '', $v); } function validDevicePath($v) { return $v; } function validHtmlStr($v) { return $v; } function dbFetchOne($sql, $col = null, $args = array()) { return (strpos($sql, 'MAX(Sequence)') !== false) ? 7 : null; } function dbFetchAll($sql, $col = null, $args = array()) { return (strpos($sql, 'FROM Zones') !== false) ? ZM\Rec::$zones : array(); } function dbQuery($sql, $args = array()) { if (strpos($sql, 'UPDATE Zones') === 0) ZM\Rec::$queries[] = array($sql, $args); } function coordsToPoints($c) { return array(); } function pointsToCoords($p) { return ''; } function getFormChanges($old, $new, $types = false) { $c = array(); foreach ($new as $k => $v) { if (!array_key_exists($k, $old) or (string)$old[$k] !== (string)$v) $c[$k] = "`$k` = '$v'"; } return $c; } // The action takes its checkbox defaults from this helper where it exists. $src = file_get_contents(__DIR__.'/../../web/includes/functions.php'); if (preg_match('/^function monitorFormUncheckedDefaults\(.*?^}\n/ms', $src, $m)) { eval($m[0]); } else { function monitorFormUncheckedDefaults() { return array(); } } // The action pulls its classes in with relative require_once calls. $dir = sys_get_temp_dir().'/zm_test_action_'.getmypid(); mkdir($dir.'/includes', 0755, true); mkdir(sys_get_temp_dir().'/zm_test_storage', 0755, true); foreach (array('Monitor', 'Zone', 'Storage', 'Manufacturer', 'Model') as $f) { file_put_contents("$dir/includes/$f.php", " '', 'Manufacturer' => '', 'ModelId' => '', 'Model' => '', 'Name' => 'Door', 'Type' => 'Ffmpeg', 'Width' => '640', 'Height' => '480', ); $existing = array('Id' => 5, 'Name' => 'Garage', 'Type' => 'Ffmpeg', 'Deleted' => 0, 'Width' => 640, 'Height' => 480, 'Capturing' => 'None', 'Controllable' => 0); // Saving still works $err = save(array('newMonitor' => $form + array('Id' => '5')), array(5 => array('Deleted' => 1) + $existing)); check('a deleted monitor id is reused', count(ZM\Rec::$saved), 1); check('reusing a deleted id reports no error', $err, ''); save(array('newMonitor' => $form + array('Id' => '')), array()); check('a new monitor with no id is inserted', count(ZM\Rec::$inserted), 1); // Zone thresholds on a resolution change $zone = array('Id' => 1, 'Name' => 'All', 'Units' => 'Percent', 'Coords' => '0.00,0.00 100.00,0.00 100.00,100.00 0.00,100.00', 'MinAlarmPixels' => 0.05, 'MaxAlarmPixels' => 75, 'MinFilterPixels' => 0.05, 'MaxFilterPixels' => 75, 'MinBlobPixels' => 0.05, 'MaxBlobPixels' => 75); $big = array('Width' => '1280', 'Height' => '960'); save(array('mid' => '5', 'newMonitor' => $big + $form), array(5 => $existing), array($zone)); check('the monitor itself is resized', count(ZM\Rec::$saved), 1); check('percentage zone thresholds are left alone', ZM\Rec::$queries, array()); $legacy = array('Id' => 2, 'Name' => 'Old', 'Units' => 'Pixels', // As the zone editor stores them: every coordinate gets two decimals 'Coords' => '0.00,0.00 639.00,0.00 639.00,479.00 0.00,479.00', 'MinAlarmPixels' => 100, 'MaxAlarmPixels' => 1000, 'MinFilterPixels' => 0, 'MaxFilterPixels' => 0, 'MinBlobPixels' => 0, 'MaxBlobPixels' => 0); save(array('mid' => '5', 'newMonitor' => $big + $form), array(5 => $existing), array($legacy)); check('pixel zone thresholds are still rescaled', count(ZM\Rec::$queries), 1); check('pixel zone MaxAlarmPixels scales with the area', strpos(ZM\Rec::$queries[0][0] ?? '', "`MaxAlarmPixels` = '4000'") !== false, true); system('rm -rf '.escapeshellarg($dir).' '.escapeshellarg(sys_get_temp_dir().'/zm_test_storage')); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }