diff --git a/tests/php/test_api_frames_save_json.php b/tests/php/test_api_frames_save_json.php new file mode 100644 index 0000000000..c998a692a1 --- /dev/null +++ b/tests/php/test_api_frames_save_json.php @@ -0,0 +1,128 @@ +method = $method; $this->data = $data; } + function is($type) { return in_array(strtolower($this->method), array_map('strtolower', (array)$type)); } +} + +class FakeSource { + public $model; + function create($model, $fields, $values) { return $model->saveResult; } + function value($v, $type) { return "'".$v."'"; } +} + +class FakeEvent { + function find($type, $options = array()) { return array(); } +} + +class FakeFrame { + public $saveResult = true; + public $validationErrors = array(); + public $Event; + private $source; + function __construct() { $this->Event = new FakeEvent(); $this->source = new FakeSource(); } + function set($columns) {} + function validates() { + if (!$this->saveResult) $this->validationErrors = array('Score' => array('invalid')); + return $this->saveResult; + } + function getDataSource() { return $this->source; } + function getColumnType($field) { return 'integer'; } + function updateAll($fields, $conditions) { return $this->saveResult; } +} + +function extract_method($source, $name) { + $start = strpos($source, "public function $name("); + if ($start === false) return null; + $depth = 0; + for ($i = strpos($source, '{', $start); $i < strlen($source); $i++) { + if ($source[$i] == '{') $depth++; + else if ($source[$i] == '}' && --$depth == 0) break; + } + return substr($source, $start, $i - $start + 1); +} + +$source = file_get_contents($file); +$add = extract_method($source, 'add'); +$edit = extract_method($source, 'edit'); +if ($add === null or $edit === null) { + fwrite(STDERR, "no add()/edit() in $file\n"); + exit(1); +} + +eval('class FramesHarness { public $request, $Frame, $vars = array(), $flashed = array(), $columns = array(); + function set($v, $value = null) { $this->vars = array_merge($this->vars, is_array($v) ? $v : array($v => $value)); } + function flash($message, $url = null) { $this->flashed[] = $message; return "FLASH"; } + function requireRequestEventEdit() {} + function requireFrameEdit($eventId, $frameId) {} + function findFrame($eventId, $frameId) { return array(); } + function requestColumns($exclude = array()) { return array_diff_key($this->columns, array_flip($exclude)); } + '.$add.' '.$edit.' }'); + +function run($action, $method, $save_result, $columns = array('EventId' => 5, 'FrameId' => 3, 'Score' => 10)) { + global $user; + $user = null; + $h = new FramesHarness(); + $h->request = new FakeRequest($method, array('Frame' => $columns)); + $h->Frame = new FakeFrame(); + $h->Frame->saveResult = $save_result; + $h->columns = $columns; + try { + if ($action == 'add') $h->add(); else $h->edit(5, 3); + } catch (Throwable $e) { + return array(get_class($e), $h); + } + return array('ok', $h); +} + +list($r, $h) = run('add', 'POST', true); +check('add: POST save answers Saved', isset($h->vars['message']) ? $h->vars['message'] : null, 'Saved'); +check('add: POST save serializes message', isset($h->vars['_serialize']) ? $h->vars['_serialize'] : null, array('message')); +check('add: POST save does not flash an HTML page', $h->flashed, array()); +list($r, $h) = run('add', 'POST', false); +check('add: failed save answers the errors', isset($h->vars['message']) ? $h->vars['message'] : null, array('Score' => array('invalid'))); +check('add: failed save serializes message', isset($h->vars['_serialize']) ? $h->vars['_serialize'] : null, array('message')); +list($r, $h) = run('add', 'GET', true); +check('add: GET is a BadRequestException', $r, 'BadRequestException'); + +list($r, $h) = run('edit', 'PUT', true); +check('edit: PUT save answers Saved', isset($h->vars['message']) ? $h->vars['message'] : null, 'Saved'); +check('edit: PUT save does not flash an HTML page', $h->flashed, array()); +list($r, $h) = run('edit', 'PUT', false); +check('edit: failed save answers Error', isset($h->vars['message']) ? $h->vars['message'] : null, 'Error'); +check('edit: failed save serializes message', isset($h->vars['_serialize']) ? $h->vars['_serialize'] : null, array('message')); +list($r, $h) = run('edit', 'PUT', true, array('EventId' => 5, 'FrameId' => 3)); +check('edit: PUT with only key fields says nothing was saved', isset($h->vars['message']) ? $h->vars['message'] : null, 'No fields to save'); +list($r, $h) = run('edit', 'GET', true); +check('edit: GET is a BadRequestException', $r, 'BadRequestException'); + +echo $failures ? "\n$failures failed\n" : "\nall passed\n"; +exit($failures ? 1 : 0); diff --git a/web/api/app/Controller/FramesController.php b/web/api/app/Controller/FramesController.php index e2f6f1d2a3..9d88567aee 100644 --- a/web/api/app/Controller/FramesController.php +++ b/web/api/app/Controller/FramesController.php @@ -161,21 +161,26 @@ class FramesController extends AppController { * @return void */ public function add() { - if ($this->request->is('post')) { - global $user; - if ($user and (!$user->canEdit('Events'))) { - throw new UnauthorizedException(__('Insufficient Privileges')); - } - $this->requireRequestEventEdit(); - $columns = $this->requestColumns(); - $this->Frame->set($columns); - if ($this->Frame->validates() and - $this->Frame->getDataSource()->create($this->Frame, array_keys($columns), array_values($columns))) { - return $this->flash(__('The frame has been saved.'), array('action' => 'index')); - } + if (!$this->request->is('post')) { + throw new BadRequestException(__('Invalid method. Should be post')); } - $events = $this->Frame->Event->find('list'); - $this->set(compact('events')); + global $user; + if ($user and (!$user->canEdit('Events'))) { + throw new UnauthorizedException(__('Insufficient Privileges')); + } + $this->requireRequestEventEdit(); + $columns = $this->requestColumns(); + $this->Frame->set($columns); + if ($this->Frame->validates() and + $this->Frame->getDataSource()->create($this->Frame, array_keys($columns), array_values($columns))) { + $message = 'Saved'; + } else { + $message = $this->Frame->validationErrors ? $this->Frame->validationErrors : 'Error'; + } + $this->set(array( + 'message' => $message, + '_serialize' => array('message') + )); } /** @@ -199,13 +204,17 @@ class FramesController extends AppController { } if (count($columns) and $this->Frame->updateAll($columns, array('Frame.EventId' => $eventId, 'Frame.FrameId' => $frameId))) { - return $this->flash(__('The frame has been saved.'), array('action' => 'index')); + $message = 'Saved'; + } else { + $message = count($columns) ? 'Error' : 'No fields to save'; } + $this->set(array( + 'message' => $message, + '_serialize' => array('message') + )); } else { - $this->request->data = $this->findFrame($eventId, $frameId); + throw new BadRequestException(__('Invalid method. Should be post or put')); } - $events = $this->Frame->Event->find('list'); - $this->set(compact('events')); } /**