id = $id; $this->name = $name; $this->editable = $editable; $this->type = $type; } public function Id() { return $this->id; } public function Type() { return $this->type; } public function Name() { return $this->name; } public function canEdit() { return $this->editable; } public function zmcControl($mode) { $GLOBALS['restarted'][] = array($this->id, $mode); } } $root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..'; $path = $root.'/web/includes/actions/console.php'; $src = file_get_contents($path); if (!preg_match('/^function restartMonitors\(.*?\n\}/ms', $src, $m)) { echo "Could not find restartMonitors() in $path\n"; exit(1); } eval($m[0]); function reset_state($method) { $_SERVER['REQUEST_METHOD'] = $method; $GLOBALS['error_message'] = ''; $GLOBALS['restarted'] = array(); $GLOBALS['audits'] = array(); \ZM\Monitor::$byId = array( 1 => new FakeMonitor(1, 'Front Door', true), 2 => new FakeMonitor(2, 'Garage', false), 3 => new FakeMonitor(3, 'Porch', true), 4 => new FakeMonitor(4, 'Status page', true, 'WebSite'), ); } echo "editable monitors restart, the rest are refused\n"; reset_state('POST'); restartMonitors(array('1', '2', '3')); check('only editable monitors restarted', $GLOBALS['restarted'], array(array(1, 'restart'), array(3, 'restart'))); check('refused monitor reported', strpos($GLOBALS['error_message'], 'Garage') !== false, true); check('audit entry per restarted monitor', $GLOBALS['audits'], array( array('restart', 'monitor', 1), array('restart', 'monitor', 3), )); echo "\na WebSite monitor has no capture daemon to restart\n"; reset_state('POST'); restartMonitors(array('1', '4', '3')); check('WebSite monitor skipped, the others restarted', $GLOBALS['restarted'], array(array(1, 'restart'), array(3, 'restart'))); check('no audit entry for the skipped monitor', $GLOBALS['audits'], array( array('restart', 'monitor', 1), array('restart', 'monitor', 3), )); check('skipping it is not an error', $GLOBALS['error_message'], ''); echo "\nnon-POST requests are refused\n"; reset_state('GET'); restartMonitors(array('1', '3')); check('nothing restarted', $GLOBALS['restarted'], array()); check('reported as a POST-only action', strpos($GLOBALS['error_message'], 'POST') !== false, true); echo "\nunknown ids are reported\n"; reset_state('POST'); restartMonitors(array('99')); check('nothing restarted', $GLOBALS['restarted'], array()); check('not found reported', strpos($GLOBALS['error_message'], '99') !== false, true); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }