Id = $params['Id']; return $m; } public function canView($u = null) { // Real behaviour: per-monitor row for 2 is None; no row for null, so the // global Monitors permission decides. if ($this->Id == 2) return false; return $u->Monitors() != 'None'; } } class TestUser { private $perms; public function __construct($perms) { $this->perms = $perms; } public function Events() { return $this->perms['Events']; } public function Snapshots() { return $this->perms['Snapshots']; } public function Monitors() { return $this->perms['Monitors']; } // Same answers as User::canView()/canEdit() for a user without a role, for // an Event::canView() that asks the user rather than reading the field. public function canView($area) { return $this->perms[$area] != 'None'; } public function canEdit($area) { return in_array($this->perms[$area], array('Edit', 'Create')); } } require_once __DIR__.'/../../web/includes/Event.php'; $editor = new TestUser(array('Events' => 'Edit', 'Snapshots' => 'None', 'Monitors' => 'View')); $viewer = new TestUser(array('Events' => 'View', 'Snapshots' => 'None', 'Monitors' => 'View')); $allowed = new Event(array('Id' => 1, 'MonitorId' => 1)); $hidden = new Event(array('Id' => 2, 'MonitorId' => 2)); check('event on a viewable monitor: canView', $allowed->canView($viewer), true); check('event on a viewable monitor: canEdit for Events=Edit', $allowed->canEdit($editor), true); check('event on a viewable monitor: no canEdit for Events=View', $allowed->canEdit($viewer), false); check('event on a denied monitor: no canView', $hidden->canView($viewer), false); check('event on a denied monitor: no canEdit', $hidden->canEdit($editor), false); $empties = array( 'new Event()' => new Event(), 'new Event(array())' => new Event(array()), 'new Event()' => new Event(999), 'new Event(row without Id)' => new Event(array('Name' => 'x')), ); foreach ($empties as $label => $empty) { check("$label: no canView", $empty->canView($viewer), false); check("$label: no canEdit", $empty->canEdit($editor), false); } echo "\n$passes passed, $failures failed\n"; array_map('unlink', glob($stubdir.'/*')); @rmdir($stubdir); exit($failures ? 1 : 0);