data = $data; } function is($t) { return $t == 'post'; } } class FakeModel { public $id = null; public $saved = null; public $validationErrors = array(); public $row = array('Notification' => array('Id' => 7, 'UserId' => 5, 'Token' => 'T')); function create() { $this->id = null; } function find($type, $o = array()) { return $this->row; } function save($d) { $this->saved = $d; if (!$this->id) $this->id = 8; return true; } } class Resp { function statusCode($c) {} } $source = file_get_contents("$root/web/api/app/Controller/NotificationsController.php"); $start = strpos($source, 'public function add('); $depth = 0; for ($i = strpos($source, '{', $start); $i < strlen($source); $i++) { if ($source[$i] == '{') $depth++; else if ($source[$i] == '}' && --$depth == 0) break; } $code = substr($source, $start, $i - $start + 1); eval('class Ctl { public $request; public $Notification; public $response; public $vars; public $admin = true; function _isAdmin() { return $this->admin; } function _userId() { return 1; } function set($v) { $this->vars = $v; } '.str_replace(array('$this->_isAdmin()', '$this->_userId()'), array('$this->admin', '1'), $code).' }'); function run($admin, $data) { $c = new Ctl; $c->admin = $admin; $c->request = new FakeRequest($data); $c->Notification = new FakeModel; $c->response = new Resp; $c->add(); return $c->Notification->saved['Notification']; } $r = run(true, array('Token' => 'T', 'MonitorList' => '1,2')); check('admin re-register without UserId keeps owner', array_key_exists('UserId', $r), false); $r = run(true, array('Token' => 'T', 'UserId' => 9)); check('admin naming UserId sets it', $r['UserId'], 9); exit($failures ? 1 : 0);