canView()) { $view = 'error'; return; }`, matching frames.php, frame.php // and stats.php. // // The views are run with an eid that does not exist and an eid that does. The stub // getNavBarHTML() throws 'STOP' so a view that does not refuse is caught as a behaviour // failure instead of rendering the rest of the page. // // Run as: php tests/php/test_event_video_missing_event.php [views dir] namespace ZM { function Warning($m) {} function Error($m) {} function Debug($m) {} function Info($m) {} class Event { public $id; function __construct($id = null) { $this->id = $id; } // Only the existing event under test (7) has an Id; any other eid is a failed lookup. function Id() { return $this->id == 7 ? 7 : 0; } function MonitorId() { return 1; } function Monitor() { static $m = null; if ($m === null) $m = new Monitor(); return $m; } function Path() { return sys_get_temp_dir(); } function canView() { return $this->Id() > 0; } function __call($name, $args) { return null; } } class Monitor { function canView($u = null) { return true; } function __call($name, $args) { return null; } } class Filter { static function parse($q) { return new FilterStub(); } } class FilterStub { function has_term($a) { return false; } function terms($t = null) { return array(array('attr' => 'MonitorId')); } function addTerm($t) { return $this; } function querystring() { return ''; } } } namespace { $root = isset($argv[1]) ? $argv[1] : __DIR__.'/../../web/skins/classic/views'; $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"; } } error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED); // The views require these; give them empty files so the real includes are not loaded. $stub = sys_get_temp_dir().'/zm_event_video_missing_'.getmypid(); mkdir($stub.'/includes', 0777, true); foreach (array('Event', 'Event_Data', 'Filter', 'Zone') as $f) file_put_contents($stub."/includes/$f.php", " $eid); $view = 'view'; $thrown = null; ob_start(); try { include $path; } catch (\Throwable $ex) { $thrown = $ex->getMessage(); } ob_end_clean(); return array($view, $thrown); } // A missing event must be refused by each view. foreach (array('event', 'video') as $name) { list($view, $thrown) = run_view($root.'/'.$name.'.php', 999); if ($thrown !== null) { $failures++; echo "FAIL - $name.php did not refuse the missing event (threw $thrown)\n"; } else { echo "ok - $name.php returned before getNavBarHTML for the missing event\n"; } check("$name.php sets view to error for the missing event", $view, 'error'); } // An existing event must still get past the guard. It may throw 'STOP' later. foreach (array('event', 'video') as $name) { list($view, $thrown) = run_view($root.'/'.$name.'.php', 7); check("$name.php keeps the view for an existing event", $view === 'error', false); } system('rm -rf '.escapeshellarg($stub)); echo $failures ? "\n$failures failed\n" : "\nall passed\n"; exit($failures ? 1 : 0); }