null,'Name'=>''); }"); } // Monitor::canEdit() asks whether the id is editable; every monitor in the test is. function editableMonitor($id) { return true; } function visibleMonitor($id) { return true; } set_include_path($stub_dir.PATH_SEPARATOR.$root.'/web/includes'.PATH_SEPARATOR.get_include_path()); require_once($root.'/web/includes/Object.php'); require_once($root.'/web/includes/Monitor.php'); // Cake, the controller base and the model base are stubbed. class App { public static function uses($class, $package) {} } class AppModel { public $useTable; public $primaryKey = 'Id'; } function __($s) { return $s; } // Cake's Set::extract('/Monitor/.', $result) as used by daemonStatus(). class Set { public static function extract($path, $data) { return array($data['Monitor']); } } class BadRequestException extends Exception {} class NotFoundException extends Exception {} class UnauthorizedException extends Exception {} $GLOBALS['monitor_row'] = array( 'Id' => 1, 'Type' => 'Ffmpeg', 'Capturing' => 'Modect', 'Device' => '', 'ServerId' => 1, ); require_once $model_file; class StubMonitorModel extends Monitor { public function exists($id = null) { return true; } public function find($type = 'first', $options = array()) { return array('Monitor' => $GLOBALS['monitor_row']); } } class AppController { public $Monitor; public $request; public $viewVars = array(); public function __construct() { $this->Monitor = new StubMonitorModel(); } public function set($vars) { $this->viewVars = $vars; } } require_once $ctrl_file; // A zmdc.pl that records that it ran, then does $body. function stub_zmdc($body) { global $bin; $marker = escapeshellarg($bin.'/ran'); file_put_contents($bin.'/zmdc.pl', "#!/bin/sh\necho ran > $marker\n$body\n"); chmod($bin.'/zmdc.pl', 0755); @unlink($bin.'/ran'); } function zmdc_ran() { global $bin; return file_exists($bin.'/ran'); } function control($row, $command, $daemon = 'zmc') { $GLOBALS['monitor_row'] = $row; $controller = new MonitorsController(); $controller->daemonControl($row['Id'], $command, $daemon); return $controller->viewVars; } function status_of($row) { $GLOBALS['monitor_row'] = $row; $c = new MonitorsController(); $c->request = new stdClass(); $c->request->params = array('named' => array('id' => $row['Id'], 'daemon' => 'zmc')); $c->daemonStatus(); return $c->viewVars; } $running = "echo \"'zmc -m 1' running since 26/11/16 10:00:00, pid = 100\""; stub_zmdc($running); $r = status_of(array('Id' => 1, 'Type' => 'Ffmpeg', 'Capturing' => 'Modect', 'Device' => '', 'ServerId' => 1)); check('monitor of this server running', $r['status'], true); check('zmdc.pl ran for this server', zmdc_ran(), true); stub_zmdc($running); $r = status_of(array('Id' => 1, 'Type' => 'Ffmpeg', 'Capturing' => 'Modect', 'Device' => '', 'ServerId' => 2)); check('monitor of another server is not reported from local zmdc', $r['status'], false); check('other server is explained', $r['statustext'] ?? null, 'Monitor is assigned to a different server'); check('other server does not run zmdc.pl', zmdc_ran(), false); echo "$passes passed, $failures failed\n"; exit($failures ? 1 : 0);