diff --git a/tests/php/test_monitors_bulk_save_unchanged.php b/tests/php/test_monitors_bulk_save_unchanged.php new file mode 100644 index 0000000000..070d755a93 --- /dev/null +++ b/tests/php/test_monitors_bulk_save_unchanged.php @@ -0,0 +1,71 @@ +id = $id; } + function Type() { return 'Ffmpeg'; } + function Capturing() { return 'Always'; } + function commandFieldChangesNotAllowed($v) { return array(); } + function changes($new) { + $changes = array(); + foreach ($new as $k => $v) if ((self::$current[$this->id][$k] ?? null) != $v) $changes[$k] = $v; + return $changes; + } + function save($v) { self::$calls[] = "save $this->id"; return true; } + function get_last_error() { return ''; } + function zmcControl($m) { self::$calls[] = "zmc $m $this->id"; } + function zmtrackControl($m) { self::$calls[] = "zmtrack $m $this->id"; } + } +} + +namespace { + function canEdit($p, $id = null) { return true; } + function ValidInt($v) { return (int)$v; } + + $action_file = isset($argv[1]) ? $argv[1] : __DIR__.'/../../web/includes/actions/monitors.php'; + + $failures = 0; + function check($n, $got, $want) { + global $failures; + if ($got === $want) echo "ok - $n\n"; + else { $failures++; echo "FAIL - $n (got ".var_export($got, true).", want ".var_export($want, true).")\n"; } + } + + function run($newMonitor) { + global $action_file, $action, $error_message, $redirect, $view; + ZM\Monitor::$calls = array(); + ZM\Monitor::$current = array( + 1 => array('Capturing' => 'Always', 'Importance' => 'Normal'), + 2 => array('Capturing' => 'Always', 'Importance' => 'Less'), + ); + $action = 'save'; + $error_message = ''; + $redirect = null; + $_REQUEST = array('action' => 'save', 'mids' => array('1', '2'), 'newMonitor' => $newMonitor); + include $action_file; + return ZM\Monitor::$calls; + } + + $calls = run(array('Capturing' => 'Always', 'Importance' => 'unchanged')); + check('a save that changes nothing restarts no monitor', $calls, array()); + check('a save that changes nothing still returns to the console', $redirect, '?view=console'); + + $calls = run(array('Capturing' => 'Always', 'Importance' => 'Normal')); + check('only the monitor that changes is stopped, saved and started', + $calls, array('zmc stop 2', 'save 2', 'zmc start 2', 'zmtrack start 2')); + + echo $failures ? "\n$failures failed\n" : "\nall passed\n"; + exit($failures ? 1 : 0); +} diff --git a/web/includes/actions/monitors.php b/web/includes/actions/monitors.php index f195ba2b81..774874eeb6 100644 --- a/web/includes/actions/monitors.php +++ b/web/includes/actions/monitors.php @@ -43,6 +43,9 @@ if ($action == 'save') { $error_message .= 'Changing '.implode(', ', $refused).' requires System edit permission.
'; continue; } + // The bulk form sends every field for every monitor; a monitor it changes + // nothing on is left alone instead of having its capture stopped and restarted. + if (!count($Monitor->changes($newMonitor))) continue; if ($Monitor->Type() != 'WebSite') { $Monitor->zmcControl('stop'); }