id = $id; } function Id() { return $this->id; } } $canViewEvents = true; function canView($what) { global $canViewEvents; return $what === 'Events' ? $canViewEvents : false; } function translate($text) { return $text; } function render($monitor, $canView) { global $url_line, $link_line, $canViewEvents; $canViewEvents = $canView; $events_url = $events_link = null; eval($url_line); eval($link_line); return $events_link; } $failures = 0; function check($name, $monitor, $canView, $want) { global $failures; $link = render($monitor, $canView); $present = $link !== ''; if ($present !== $want) { $failures++; echo "FAIL - $name (link ".($present ? 'present' : 'absent').")\n"; return; } if ($want) { if (strpos($link, 'view=events') === false) { $failures++; echo "FAIL - $name (does not point at the events view)\n"; } else if (!preg_match('/%5Bval%5D=(\d+)/', $link, $id) or $id[1] != $monitor->Id()) { $failures++; echo "FAIL - $name (does not filter to the monitor id)\n"; } else if (strpos($link, 'title="Events"') === false) { $failures++; echo "FAIL - $name (link is not labelled Events)\n"; } else { echo "ok - $name\n"; } } else { echo "ok - $name\n"; } } check('existing monitor links to its events', new StubMonitor(7), true, true); check('new monitor has no events link', new StubMonitor(0), true, false); check('user without Events view gets no events link', new StubMonitor(7), false, false); exit($failures ? 1 : 0);