diff --git a/tests/php/test_event_image_src_files.php b/tests/php/test_event_image_src_files.php new file mode 100644 index 0000000000..61b3b62f83 --- /dev/null +++ b/tests/php/test_event_image_src_files.php @@ -0,0 +1,152 @@ + $v) $this->$k = $v; + } + public static function find_one($params = array(), $options = array()) { + global $frameRows; + foreach ($frameRows as $row) { + $ok = true; + foreach ($params as $k => $v) { + if (!isset($row[$k]) or $row[$k] != $v) $ok = false; + } + if ($ok) return new Frame($row); + } + return null; + } +} + +class Storage { + public function __construct($id = null) {} + public static function find_one($params = array()) { return new Storage(); } + public function Path() { return ''; } + public function Scheme($s = null) { return 'Medium'; } +} + +require_once $eventFile; + +class TestEvent extends Event { + public $dir = ''; + public function Path() { return $this->dir; } + public function Storage($new = null) { return new Storage(); } + public function DefaultVideo() { return ''; } + public function find_virtual_frame($fid) { return null; } +} + +$dir = sys_get_temp_dir().'/zm_event_image_src_'.getmypid(); +@mkdir($dir, 0700, true); +// The bytes do not matter: getImageSrc() only checks existence and size. +file_put_contents($dir.'/00005-analyse.jpg', 'not-really-a-jpeg'); +file_put_contents($dir.'/00009-analyse.jpg', 'not-really-a-jpeg'); + +function run_get_image_src($dir, $frameId, $row, $captureOnly = false) { + global $frameRows, $errors; + $frameRows = $row === null ? array() : array($row); + $errors = array(); + $event = new TestEvent(); + $event->dir = $dir; + $imageData = $event->getImageSrc($frameId, SCALE_BASE, $captureOnly); + return array('data' => $imageData, 'errors' => $errors); +} + +function errors_mentioning($errors, $needle) { + $found = array(); + foreach ($errors as $e) { + if (stripos($e, $needle) !== false) $found[] = $e; + } + return $found; +} + +echo "case A: alarm frame saved with an analysis JPEG only\n"; +$r = run_get_image_src($dir, 5, array('Id' => 5, 'EventId' => 7, 'FrameId' => 5, 'Type' => 'Alarm', 'Delta' => 0.5)); +check('image path is the analysis jpeg', $r['data']['imagePath'], $dir.'/00005-analyse.jpg'); +check('image file is the analysis jpeg', $r['data']['imageFile'], $dir.'/00005-analyse.jpg'); +check('analysis jpeg is detected', $r['data']['hasAnalysisImage'], true); +check('reported as an analysis image', $r['data']['isAnalysisImage'], true); +check('no "Capture file does not exist" error', + errors_mentioning($r['errors'], 'Capture file does not exist'), array()); +check('no errors at all', $r['errors'], array()); + +echo "case A2: normal frame saved with an analysis JPEG only\n"; +$r = run_get_image_src($dir, 5, array('Id' => 5, 'EventId' => 7, 'FrameId' => 5, 'Type' => 'Normal', 'Delta' => 0.5)); +check('capture fallback is the analysis jpeg', $r['data']['imagePath'], $dir.'/00005-analyse.jpg'); +check('no "Capture file does not exist" error', + errors_mentioning($r['errors'], 'Capture file does not exist'), array()); + +echo "case B: no Frame row and no virtual frame\n"; +$r = run_get_image_src($dir, 9, null); +check('requested FrameId is kept', $r['data']['FrameId'], 9); +check('image path uses the requested FrameId', $r['data']['imagePath'], $dir.'/00009-analyse.jpg'); + +echo "\n$passes passed, $failures failed\n"; +array_map('unlink', glob($dir.'/*')); +@rmdir($dir); +array_map('unlink', glob($stubdir.'/*')); +@rmdir($stubdir); +exit($failures ? 1 : 0); diff --git a/web/includes/Event.php b/web/includes/Event.php index 55f4e181cc..c7cae15533 100644 --- a/web/includes/Event.php +++ b/web/includes/Event.php @@ -535,14 +535,15 @@ class Event extends ZM_Object { $eventPath = $Event->Path(); if ( $frame and !is_array($frame) ) { + $requestedFrameId = $frame; Debug("Assuming that $frame is an Id"); - $f = Frame::find_one(['Id'=>$frame]); + $f = Frame::find_one(['Id'=>$requestedFrameId]); if ($f) { $frame = (array)$f; } else { - $frame = $this->find_virtual_frame($frame); + $frame = $this->find_virtual_frame($requestedFrameId); if (!$frame) - $frame = array('FrameId'=>$frame, 'Type'=>'', 'Delta'=>0); + $frame = array('FrameId'=>$requestedFrameId, 'Type'=>'', 'Delta'=>0); } } @@ -552,7 +553,7 @@ class Event extends ZM_Object { Debug('Frame not specified, using snapshot'); $frame = array('FrameId'=>'snapshot', 'Type'=>'', 'Delta'=>0); } else { - $captureImage = sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d-analyze.jpg', $frame['FrameId']); + $captureImage = sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d-analyse.jpg', $frame['FrameId']); if ( ! file_exists( $eventPath.'/'.$captureImage ) ) { $captureImage = sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d-capture.jpg', $frame['FrameId']); if ( !file_exists($eventPath.'/'.$captureImage) ) {