Files
zoneminder/tests/php/test_group_save_membership_result.php
Isaac ConnorandClaude Opus 5.5 83c734d473 test: PHP test stubs know the helpers and include path merged code now uses
Twenty tests in tests/php failed on master after PRs that each passed on
their own landed together: code under test started calling a helper, or
loading a file, that these tests' stand-ins didn't provide.

- validCardinal(): actions/group.php, actions/controlcap.php and
  ajax/reports.php deleteRequest() reduce request ids with it (#7800).
  The five group save tests, the controlcap test and the reports delete
  test now carry a copy of it.
- ZM\scalar_values_differ(): Object.php changes() compares with it
  (#7774). The group, storage, user and zmtrack tests that load
  Object.php now carry a copy.
- authHashTtl(): generateAuthHash() takes its freshness limit from it
  (#8367). test_auth_hash_slot_age lifts it out of auth.php alongside
  generateAuthHash().
- includes/log_redact.php: ajax/status.php requires it relative to web/
  (#7930). The five status tests put web/ on the include path before
  including status.php, as the web server's working directory does.
- test_reports_ajax_forged_id: deleteRequest() now reports a delete()
  that returns false (#7820), and the stub Report::delete() returned
  nothing, so it returns true.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 18:38:08 -05:00

130 lines
4.9 KiB
PHP

<?php
// A group save replaced Groups_Monitors with a DELETE and one INSERT per monitor
// and ignored every result, so a failed INSERT (e.g. a monitor deleted meanwhile)
// silently dropped members and the save still audited and redirected as success.
// A failed DELETE or INSERT is now reported and does not redirect.
//
// php tests/php/test_group_save_membership_result.php [action-file]
namespace ZM {
function Warning($s) {}
function Debug($s) {}
function Error($s) {}
class Rec {
public static $queries = array();
public static $audit = array();
public static $save_result = true;
public static $last_error = '';
public static $rows = array();
public static $fail = array();
public static $fail_mid = null;
}
function AuditAction($action = null, $type = null, $id = null, $note = null) {
Rec::$audit[] = array($action, $type, $id, $note);
}
class Group {
private $id = null;
private $parent = null;
public function __construct($id = null) {
$this->id = ($id === null or $id === '') ? null : (int)$id;
if ($this->id) {
$this->parent = isset(Rec::$rows[$this->id]) ? Rec::$rows[$this->id] : null;
}
}
public static function validParent($group_id, $parent_id) { return true; }
public static function canEditMembership($monitor_ids) { return true; }
public function Id($v = null) {
if ($v !== null) $this->id = $v;
return $this->id;
}
public function ParentId() { return $this->parent; }
public function MonitorIds() { return array(); }
public function save($values) {
if (!Rec::$save_result) return false;
if (!$this->id) $this->id = 42;
return true;
}
public function get_last_error() { return Rec::$last_error; }
}
}
namespace {
function canEdit($p, $id = null) { return true; }
function validHtmlStr($v) { return htmlspecialchars((string)$v, ENT_QUOTES); }
// Copied from web/includes/functions.php.
function validCardinal($input) {
if (!is_scalar($input)) return '';
return preg_replace('/\D/', '', $input);
}
function dbFetchAll($sql, $col = null, $args = array()) { return array(); }
function dbLastError() { return 'fk <fail>'; }
function dbQuery($sql, $args = array()) {
ZM\Rec::$queries[] = array($sql, $args);
foreach (ZM\Rec::$fail as $needle) {
if (strpos($sql, $needle) === 0) {
if ($needle !== 'INSERT' or ZM\Rec::$fail_mid === null or $args[1] === ZM\Rec::$fail_mid) return null;
}
}
return true;
}
$action_file = isset($argv[1]) ? $argv[1] : __DIR__.'/../../web/includes/actions/group.php';
if (!preg_match('#^/#', $action_file)) $action_file = getcwd().'/'.$action_file;
if (!is_file($action_file)) { fwrite(STDERR, "no such action file: $action_file\n"); exit(1); }
$failures = 0;
function check($name, $got, $want = true) {
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"; }
}
function run_action($action_file, $request, $fail, $fail_mid = null) {
ZM\Rec::$queries = array();
ZM\Rec::$audit = array();
ZM\Rec::$save_result = true;
ZM\Rec::$rows = array(3 => null);
ZM\Rec::$fail = $fail;
ZM\Rec::$fail_mid = $fail_mid;
$GLOBALS['error_message'] = '';
$_REQUEST = $request;
$action = 'save';
$redirect = 'SENTINEL';
ob_start();
include $action_file;
ob_end_clean();
return array($GLOBALS['error_message'], $redirect);
}
$req = array('gid' => '3', 'newGroup' => array('Name' => 'Front', 'MonitorIds' => array('1', '2', '3')));
list($msg, $redirect) = run_action($action_file, $req, array());
check('a good save redirects', $redirect, '?view=groups');
check('a good save reports nothing', $msg, '');
check('a good save audits', count(ZM\Rec::$audit), 1);
list($msg, $redirect) = run_action($action_file, $req, array('INSERT'), 2);
check('a failed insert does not redirect', $redirect, 'SENTINEL');
check('a failed insert is reported', $msg !== '', true);
check('the report names the monitor', strpos($msg, '2') !== false, true);
check('the report is html escaped', strpos($msg, '&lt;fail&gt;') !== false, true);
check('a failed insert is not audited as success', count(ZM\Rec::$audit), 0);
list($msg, $redirect) = run_action($action_file, $req, array('DELETE'));
check('a failed delete does not redirect', $redirect, 'SENTINEL');
check('a failed delete is reported', $msg !== '', true);
check('a failed delete adds no members', count(ZM\Rec::$queries), 1);
check('a failed delete is not audited as success', count(ZM\Rec::$audit), 0);
list($msg, $redirect) = run_action($action_file,
array('newGroup' => array('Name' => 'Back', 'MonitorIds' => array('4'))), array());
check('a new group clears membership by its own id',
ZM\Rec::$queries[0][1] === array(42), true);
echo $failures ? "\n$failures failed\n" : "\nall passed\n";
exit($failures ? 1 : 0);
}