mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 12:14:36 -05:00
Merge pull request #6869 from AJ0070/fix-api-events-archive-result
fix: API event archive reports a failed write instead of answering archived
This commit is contained in:
2 files changed
+107
-1
No files matched your search
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
// POST /api/events/<id>/archive ignored the result of Event->saveField('Archived', ...)
|
||||
// and always answered {"archived": <val>}, even when the save failed. The API now answers
|
||||
// 500 InternalErrorException when saveField() returns false.
|
||||
//
|
||||
// EventsController can't be loaded without CakePHP, so archive() is taken from the source
|
||||
// and run against stubs.
|
||||
// Run as: php tests/php/test_api_events_archive_save.php [root]
|
||||
|
||||
$failures = 0;
|
||||
function check($name, $got, $want) {
|
||||
global $failures;
|
||||
if ($got === $want) echo "ok - $name\n";
|
||||
else { $failures++; echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; }
|
||||
}
|
||||
|
||||
$root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..';
|
||||
$source = file_get_contents($root.'/web/api/app/Controller/EventsController.php');
|
||||
if ($source === false) {
|
||||
echo "FAIL - cannot read controller source\n";
|
||||
exit(1);
|
||||
}
|
||||
$start = strpos($source, 'public function archive(');
|
||||
if ($start === false) {
|
||||
echo "FAIL - archive() not found in controller source\n";
|
||||
exit(1);
|
||||
}
|
||||
$open = strpos($source, '{', $start);
|
||||
$depth = 0;
|
||||
for ($i = $open; $i < strlen($source); $i++) {
|
||||
if ($source[$i] == '{') $depth++;
|
||||
else if ($source[$i] == '}' && --$depth == 0) break;
|
||||
}
|
||||
$method = substr($source, $start, $i - $start + 1);
|
||||
|
||||
class NotFoundException extends Exception {}
|
||||
class UnauthorizedException extends Exception {}
|
||||
class BadRequestException extends Exception {}
|
||||
class InternalErrorException extends Exception {}
|
||||
function __($s) { return $s; }
|
||||
|
||||
eval('namespace ZM; class Event {
|
||||
function __construct($row) {}
|
||||
function canView() { return true; }
|
||||
function canEdit() { return true; }
|
||||
}');
|
||||
|
||||
class FakeEventModel {
|
||||
public $id; public $recursive; public $primaryKey = 'Id';
|
||||
public $rows = array();
|
||||
public $saveFieldResult = true;
|
||||
function exists($id) { return isset($this->rows[$id]); }
|
||||
function find($type, $options) {
|
||||
$id = $this->optionsId($options);
|
||||
return array('Event' => $this->rows[$id]);
|
||||
}
|
||||
private function optionsId($options) {
|
||||
return $options['conditions']['Event.Id'];
|
||||
}
|
||||
function saveField($field, $value) { return $this->saveFieldResult; }
|
||||
}
|
||||
class FakeRequest { function allowMethod() {} }
|
||||
eval('class FakeController { public $Event; public $request; public $out; public $setCalls = 0;
|
||||
function set($a) { $this->setCalls++; $this->out = $a; }
|
||||
'.$method.' }');
|
||||
|
||||
function newController($saveFieldResult) {
|
||||
$c = new FakeController();
|
||||
$c->Event = new FakeEventModel();
|
||||
$c->Event->rows = array(
|
||||
1 => array('Id' => 1, 'Archived' => 0, 'MonitorId' => 1),
|
||||
);
|
||||
$c->Event->saveFieldResult = $saveFieldResult;
|
||||
$c->request = new FakeRequest();
|
||||
return $c;
|
||||
}
|
||||
|
||||
// saveField() succeeds: the response reports the new value, no exception.
|
||||
$c = newController(true);
|
||||
try {
|
||||
$c->archive(1);
|
||||
check('successful save sets archived', isset($c->out['archived']) ? $c->out['archived'] : null, 1);
|
||||
check('successful save calls set()', $c->setCalls, 1);
|
||||
} catch (Exception $e) {
|
||||
check('successful save sets archived', get_class($e), 1);
|
||||
}
|
||||
|
||||
// saveField() fails: 500 InternalErrorException, no success payload.
|
||||
$c = newController(false);
|
||||
try {
|
||||
$c->archive(1);
|
||||
check('failed save throws', 'answered', 'InternalErrorException');
|
||||
check('failed save does not call set()', $c->setCalls, 0);
|
||||
} catch (InternalErrorException $e) {
|
||||
check('failed save throws', 'InternalErrorException', 'InternalErrorException');
|
||||
check('failed save does not call set()', $c->setCalls, 0);
|
||||
} catch (Exception $e) {
|
||||
check('failed save throws', get_class($e), 'InternalErrorException');
|
||||
check('failed save does not call set()', $c->setCalls, 0);
|
||||
}
|
||||
|
||||
echo $failures ? "\n$failures failed\n" : "\nall passed\n";
|
||||
exit($failures ? 1 : 0);
|
||||
?>
|
||||
@@ -664,7 +664,9 @@ class EventsController extends AppController {
|
||||
|
||||
// Save the new value
|
||||
$this->Event->id = $id;
|
||||
$this->Event->saveField('Archived', $archiveVal);
|
||||
if (!$this->Event->saveField('Archived', $archiveVal)) {
|
||||
throw new InternalErrorException(__('Failed to update the event'));
|
||||
}
|
||||
|
||||
$this->set(array(
|
||||
'archived' => $archiveVal,
|
||||
|
||||
Reference in new issue
Block a user