commands = $commands; } function commands() { return $this->commands; } } class FakeMonitor { public static $rows = array(); public static $sent = array(); private $id; private $row; function __construct($id) { $this->id = (int)$id; $this->row = isset(self::$rows[$this->id]) ? self::$rows[$this->id] : null; } function Id() { return $this->row ? $this->id : 0; } function Controllable() { return $this->row ? $this->row['controllable'] : 0; } function Control() { return $this->row ? $this->row['control'] : new FakeControl(array()); } function sendControlCommand($command) { self::$sent[] = $command; return $this->row ? $this->row['send'] : false; } } } namespace { $failures = 0; $passes = 0; function check($name, $got, $want) { global $failures, $passes; if ($got === $want) { $passes++; echo "ok - $name\n"; } else { $failures++; echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; } } $root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..'; $file = $root.'/web/api/app/Controller/ControlsController.php'; if (!is_file($file)) { fwrite(STDERR, "no such controller: $file\n"); exit(2); } // Stubs for the includes control_functions.php pulls in. $stubdir = sys_get_temp_dir().'/zm_api_control_command_stubs_'.getmypid(); @mkdir($stubdir, 0700, true); file_put_contents($stubdir.'/logger.php', " array()); private $method; function __construct($method, $data = array()) { $this->method = $method; $this->data = $data; } function is($type) { return in_array(strtolower($this->method), array_map('strtolower', (array)$type)); } function allowMethod() { foreach (func_get_args() as $m) if ($this->is($m)) return true; throw new MethodNotAllowedException('Method Not Allowed'); } } class FakeResponse { public $code = null; function statusCode($c = null) { if ($c !== null) $this->code = $c; return $this->code; } } class AppController { public $request; public $response; public $viewVars = array(); public function beforeFilter() {} public function set($vars) { $this->viewVars = $vars; } } // Load the real controller, dropping its App::uses/require_once lines: the // dependencies are the stubs above. $source = file_get_contents($file); $source = preg_replace('/^<\?php/', '', $source); $source = preg_replace('/^\s*App::uses\(.*$/m', '', $source); $source = preg_replace('/^\s*require_once\b.*$/m', '', $source); eval($source); $commands = array('moveConUp', 'moveConDown', 'moveStop', 'presetGoto', 'presetHome'); ZM\FakeMonitor::$rows = array( 1 => array('controllable' => 1, 'control' => new ZM\FakeControl($commands), 'send' => true), 2 => array('controllable' => 0, 'control' => new ZM\FakeControl($commands), 'send' => true), ); function run($method, $id, $data) { ZM\FakeMonitor::$sent = array(); $controller = new ControlsController(); $controller->request = new FakeRequest($method, $data); $controller->response = new FakeResponse(); $result = 'ok'; try { $controller->command($id); } catch (Throwable $e) { $result = get_class($e); } return array($result, ZM\FakeMonitor::$sent, $controller->viewVars); } // A valid command for a controllable monitor reaches the sender unchanged. $GLOBALS['control_view'] = true; list($r, $sent, $vars) = run('POST', 1, array('control' => 'moveConUp')); check('valid command is accepted', $r, 'ok'); check('valid command is passed to sendControlCommand()', $sent, array(' --command=moveConUp')); check('valid command answers Success', isset($vars['message']) ? $vars['message'] : null, 'Success'); // An unknown command must not reach the sender. list($r, $sent, $vars) = run('POST', 1, array('control' => 'definitelyNotACommand')); check('unknown command is refused', $r, 'BadRequestException'); check('unknown command is not sent', $sent, array()); // A monitor with Controllable off cannot be driven. list($r, $sent, $vars) = run('POST', 2, array('control' => 'moveConUp')); check('non-controllable monitor is refused', $r, 'BadRequestException'); check('non-controllable monitor is not sent', $sent, array()); // A user without Control permission cannot drive any monitor. $GLOBALS['control_view'] = false; list($r, $sent, $vars) = run('POST', 1, array('control' => 'moveConUp')); check('user without Control permission is refused', $r, 'UnauthorizedException'); check('user without Control permission sends nothing', $sent, array()); // The command is a mutation, so a GET is refused before anything else. $GLOBALS['control_view'] = true; list($r, $sent, $vars) = run('GET', 1, array('control' => 'moveConUp')); check('GET is refused', $r, 'MethodNotAllowedException'); check('GET sends nothing', $sent, array()); array_map('unlink', glob($stubdir.'/*')); @rmdir($stubdir); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }