diff --git a/tests/php/test_zone_save_deleted_monitor.php b/tests/php/test_zone_save_deleted_monitor.php new file mode 100644 index 0000000000..24acb03135 --- /dev/null +++ b/tests/php/test_zone_save_deleted_monitor.php @@ -0,0 +1,121 @@ +id = $id; } + public function Id() { return $this->id; } + public function Deleted() { return $this->id == 7; } + public function Type() { return 'Ffmpeg'; } + public function ViewWidth() { return 640; } + public function ViewHeight() { return 480; } + public function Controllable() { return false; } + public function zmcControl($m) {} + public function sendControlCommand($m) {} + } +} + +namespace { + // The action's class imports are relative require_once calls. + $dir = sys_get_temp_dir().'/zm_test_zone_action_'.getmypid(); + mkdir($dir.'/includes', 0755, true); + foreach (array('Zone', 'ZonePreset') as $f) { + file_put_contents("$dir/includes/$f.php", " $v) { + if (!array_key_exists($k, $old) or (string)$old[$k] !== (string)$v) $c[$k] = "`$k` = '$v'"; + } + return $c; + } + + $passes = 0; + $failures = 0; + function check($name, $actual, $expected) { + global $passes, $failures; + if ($actual === $expected) { + $passes++; + echo "ok - $name\n"; + } else { + $failures++; + echo "FAIL - $name: expected ".var_export($expected, true).', got '.var_export($actual, true)."\n"; + } + } + + function insertedZones($queries) { + $found = array(); + foreach ($queries as $q) { + if (stripos(ltrim($q[0]), 'INSERT INTO Zones') === 0) $found[] = $q; + } + return $found; + } + + function saveZone($mid, $newZone) { + global $action, $error_message, $redirect; + ZM\Rec::$queries = array(); + $_REQUEST = array( + 'mid' => (string)$mid, + 'action' => 'zone', + 'zid' => '0', + 'newZone' => $newZone, + ); + $action = 'zone'; + $error_message = ''; + $redirect = null; + ob_start(); + include __DIR__.'/../../web/includes/actions/zone.php'; + ob_end_clean(); + return $error_message; + } + + $newZone = array('Name' => 'Test zone', 'Type' => 'Active', 'MinAlarmPixels' => '5'); + + // Monitor 7 is deleted, its zone must not be written. + $error = saveZone(7, $newZone); + check('a zone is not inserted for a deleted monitor', insertedZones(ZM\Rec::$queries), array()); + check('saving a zone for a deleted monitor reports an error', $error !== '', true); + + // Monitor 8 is live, saving must still work. + saveZone(8, $newZone); + check('a zone is still inserted for a live monitor', count(insertedZones(ZM\Rec::$queries)), 1); + + system('rm -rf '.escapeshellarg($dir)); + + echo "\n$passes passed, $failures failed\n"; + exit($failures ? 1 : 0); +} diff --git a/web/includes/actions/zone.php b/web/includes/actions/zone.php index 399f7f3ff3..1291dcffa2 100644 --- a/web/includes/actions/zone.php +++ b/web/includes/actions/zone.php @@ -27,6 +27,10 @@ if ( !empty($_REQUEST['mid']) && canEdit('Monitors', $_REQUEST['mid']) ) { if ( ($action == 'zone') && isset($_REQUEST['zid']) ) { $zid = validInt($_REQUEST['zid']); $monitor = new ZM\Monitor($mid); + if ( !$monitor->Id() or $monitor->Deleted() ) { + $error_message .= 'Monitor not found.
'; + return; + } if ( !empty($zid) ) { $zone = dbFetchOne('SELECT * FROM Zones WHERE MonitorId=? AND Id=?', NULL, array($mid, $zid));