diff --git a/tests/php/test_monitors_bulk_view_editable_only.php b/tests/php/test_monitors_bulk_view_editable_only.php new file mode 100644 index 0000000000..57a7bdea29 --- /dev/null +++ b/tests/php/test_monitors_bulk_view_editable_only.php @@ -0,0 +1,103 @@ +id = $id; } + function Id() { return $this->id; } + function Deleted() { return $this->id == 3; } + function __call($name, $args) { return ''; } + static function find($q) { + $out = array(); + foreach ($q['Id'] as $id) $out[] = new Monitor($id); + return $out; + } + } + class Server { + static function find() { return array(); } + } + class Storage { + static function find() { return array(); } + } +} + +namespace { + function canEdit($a, $id = null) { + if ($a != 'Monitors') return true; + return $id != 2; + } + function validCardinal($v) { return (int)$v; } + function translate($s) { return $s; } + function xhtmlHeaders($f = null, $t = null) { throw new Exception('STOP'); } + + $failures = 0; + function check($name, $got, $want) { + global $failures; + if ($got === $want) echo "ok - $name\n"; + else { $failures++; echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; } + } + + $path = isset($argv[1]) ? $argv[1] : __DIR__.'/../../web/skins/classic/views/monitors.php'; + $case = isset($argv[2]) ? $argv[2] : null; + + if ($case === null) { + // Parent: run every case in its own process and aggregate the results. + $cases = array('A', 'B', 'C'); + $failed = false; + foreach ($cases as $c) { + $cmd = escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' ' + .escapeshellarg($path).' '.escapeshellarg($c); + $out = array(); + exec($cmd, $out, $rc); + echo "== case $c ==\n".implode("\n", $out)."\n"; + if ($rc !== 0) $failed = true; + } + echo $failed ? "\nFAILED\n" : "\nall passed\n"; + exit($failed ? 1 : 0); + } + + function run_case($path, $case) { + global $monitors, $view, $failures; + $mids = array( + 'A' => array(1, 2, 3), + 'B' => array(2), + 'C' => array(3), + ); + $_REQUEST = array('mids' => $mids[$case]); + $monitors = null; + $view = 'monitors'; + + ob_start(); + try { + include $path; + } catch (Throwable $e) { + // The view calls xhtmlHeaders() right after loading the monitors; the stub throws + // STOP so the form markup is never rendered. + } + ob_end_clean(); + + if ($case === 'A') { + $ids = is_array($monitors) ? array_map(function($m) { return $m->Id(); }, $monitors) : array(); + check('keeps only the monitor the user may edit', $ids, array(1)); + check('does not fall back to the error page when an editable monitor remains', $view, 'monitors'); + } else if ($case === 'B') { + check('falls back to the error page when the only monitor is not editable', $view, 'error'); + } else { + check('falls back to the error page when the only monitor is deleted', $view, 'error'); + } + + exit($failures ? 1 : 0); + } + + run_case($path, $case); +} diff --git a/web/skins/classic/views/monitors.php b/web/skins/classic/views/monitors.php index 1b6834cb9f..c3f0d2bc08 100644 --- a/web/skins/classic/views/monitors.php +++ b/web/skins/classic/views/monitors.php @@ -25,6 +25,7 @@ if ( !canEdit('Monitors') ) { $mids = (isset($_REQUEST['mids']) and is_array($_REQUEST['mids'])) ? array_filter(array_map('validCardinal', $_REQUEST['mids'])) : array(); $monitors = $mids ? ZM\Monitor::find(array('Id' => $mids)) : array(); +$monitors = array_values(array_filter($monitors, function($m) { return !$m->Deleted() && canEdit('Monitors', $m->Id()); })); if ( !count($monitors) ) { $view = 'error'; return;