active === null ? array() : array('State' => array('Id' => $this->active)); } } class ClassRegistry { public static $state; static function init($name) { return self::$state; } } eval('class FakeEvent { public $alias = "Event"; public $id = false; public $data = array(); '.$method.' }'); ClassRegistry::$state = new FakeState(); function run($id, $data, $active) { ClassRegistry::$state->active = $active; $e = new FakeEvent(); $e->id = $id; $e->data = $data; $r = $e->beforeSave(); return array($r, $e->data); } list($r, $d) = run(false, array('Event' => array('MonitorId' => 1)), 3); check('new event takes the active state', $d['Event']['StateId'], 3); check('beforeSave lets the save go on', $r, true); list($r, $d) = run(false, array('Event' => array('MonitorId' => 1)), null); check('no active state falls back to 0', $d['Event']['StateId'], 0); list($r, $d) = run(false, array('Event' => array('MonitorId' => 1, 'StateId' => 7)), 3); check('explicit StateId is kept', $d['Event']['StateId'], 7); list($r, $d) = run(false, array('Event' => array('MonitorId' => 1, 'StateId' => 0)), 3); check('explicit StateId 0 is kept', $d['Event']['StateId'], 0); list($r, $d) = run(12, array('Event' => array('Name' => 'x')), 3); check('an edit does not add StateId', isset($d['Event']['StateId']), false); echo $failures ? "\n$failures failed\n" : "\nall passed\n"; exit($failures ? 1 : 0); ?>