$sql, 'params' => $params); return true; } function deletePath($path) { global $calls; $calls[] = array('sql' => 'deletePath', 'params' => $path); } class StubDbConn { public function beginTransaction() { global $calls; $calls[] = array('sql' => 'begin', 'params' => null); } public function commit() { global $calls; $calls[] = array('sql' => 'commit', 'params' => null); } public function rollback() { global $calls; $calls[] = array('sql' => 'rollback', 'params' => null); } } function zmDbConn() { return new StubDbConn(); } class Storage { private $id; private $scheme; public function __construct($id = null) { $this->id = $id; } public static function find_one($params) { return new Storage($params['Id']); } public function Id() { return $this->id; } public function Path() { return '/var/cache/zoneminder/events'; } public function DoDelete() { return 1; } public function Scheme($s = null) { if ($s) $this->scheme = $s; return $this->scheme; } } class Monitor {} require_once __DIR__.'/../../web/includes/Event.php'; function run_delete($fields) { global $calls, $row; $calls = array(); $row = $fields; $event = new Event($fields); $event->delete(); return $calls; } function index_of($calls, $prefix) { foreach ($calls as $i => $c) { if (strpos($c['sql'], $prefix) === 0) return $i; } return false; } $base = array('Id' => 77, 'MonitorId' => 1, 'StorageId' => 3, 'DiskSpace' => 12345, 'Archived' => 0, 'StartDateTime' => null); foreach (array('Deep', 'Medium') as $scheme) { $event = new Event(array_merge($base, array('Scheme' => $scheme))); check("$scheme: no relative path", $event->Relative_Path(), ''); check("$scheme: no path", $event->Path(), ''); check("$scheme: no link path", $event->Link_Path(), ''); $calls = run_delete(array_merge($base, array('Scheme' => $scheme))); check("$scheme: no files deleted", index_of($calls, 'deletePath'), false); check("$scheme: event row kept", index_of($calls, 'DELETE FROM Events'), false); check("$scheme: rolled back", index_of($calls, 'rollback') !== false, true); check("$scheme: no storage update", index_of($calls, 'UPDATE Storage'), false); $event = new Event(array_merge($base, array('Scheme' => $scheme, 'StartDateTime' => ''))); check("$scheme: empty StartDateTime has no relative path", $event->Relative_Path(), ''); } $event = new Event(array_merge($base, array('Scheme' => 'Shallow'))); check('Shallow: relative path', $event->Relative_Path(), '1/77'); $calls = run_delete(array_merge($base, array('Scheme' => 'Shallow'))); $p = index_of($calls, 'deletePath'); check('Shallow: event files deleted', $p !== false ? $calls[$p]['params'] : null, '/var/cache/zoneminder/events/1/77'); check('Shallow: event row deleted', index_of($calls, 'DELETE FROM Events') !== false, true); $calls = run_delete(array_merge($base, array('Scheme' => 'Medium', 'StartDateTime' => '2024-05-06 07:08:09'))); $p = index_of($calls, 'deletePath'); check('Medium with start: files deleted', $p !== false ? strpos($calls[$p]['params'], '/var/cache/zoneminder/events/1/2024-05-0') === 0 : false, true); echo "\n$passes passed, $failures failed\n"; array_map('unlink', glob($stubdir.'/*')); @rmdir($stubdir); exit($failures ? 1 : 0);