Files
zoneminder/tests/php/test_group_save_refusals_shown.php
T
Isaac ConnorandClaude Opus 5.5 af00ba10c8 test: PHP test stubs know the helpers and include path merged code now uses (#8468)
* 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>

* test: filter text test lifts filterSelectionScalars() with getFilterSelection()

getFilterSelection() in _monitor_filters.php now passes request and cookie
values through filterSelectionScalars() (#8455), so the test has to
evaluate that function too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 18:43:21 -05:00

93 lines
3.7 KiB
PHP

<?php
// The group modal posts view=group, which has no view file. When the group save action
// refused a save (a parent that is the group itself or one of its children, or a monitor
// moved in or out that the user can't edit) it only logged a warning and returned, so
// index.php found no view "group" and sent the user to the console with no message; a
// failed save() set $error_message but was lost the same way. The action now sets the
// message and shows the Groups page.
//
// php tests/php/test_group_save_refusals_shown.php [action-file]
namespace ZM {
function Warning($s) {}
function Debug($s) {}
function Error($s) {}
function AuditAction() {}
class Rec {
public static $valid_parent = true;
public static $can_edit_membership = true;
public static $save_result = true;
}
class Group {
private $id;
public function __construct($id = null) { $this->id = ($id === null or $id === '') ? null : (int)$id; }
public static function validParent($group_id, $parent_id) { return Rec::$valid_parent; }
public static function canEditMembership($monitor_ids) { return Rec::$can_edit_membership; }
public function Id() { return $this->id; }
public function ParentId() { return null; }
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 'Data too long for column Name'; }
}
}
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 dbQuery($sql, $args = array()) { return true; }
$action_file = isset($argv[1]) ? $argv[1] : __DIR__.'/../../web/includes/actions/group.php';
$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"; }
}
function run($valid_parent, $can_edit_membership, $save_result) {
global $action_file, $action, $error_message, $redirect, $view;
ZM\Rec::$valid_parent = $valid_parent;
ZM\Rec::$can_edit_membership = $can_edit_membership;
ZM\Rec::$save_result = $save_result;
$action = 'save';
$view = 'group';
$error_message = '';
$redirect = null;
$_REQUEST = array('action' => 'save', 'gid' => '5',
'newGroup' => array('Name' => 'Front', 'ParentId' => '', 'MonitorIds' => array('7')));
include $action_file;
return array($error_message, $redirect, $view);
}
list($err, $redir, $v) = run(true, true, true);
check('a save that works redirects to Groups', $redir, '?view=groups');
list($err, $redir, $v) = run(false, true, true);
check('an invalid parent is reported', $err !== '', true);
check('an invalid parent shows the Groups page', array($redir, $v), array(null, 'groups'));
list($err, $redir, $v) = run(true, false, true);
check('a refused monitor move is reported', $err !== '', true);
check('a refused monitor move shows the Groups page', array($redir, $v), array(null, 'groups'));
list($err, $redir, $v) = run(true, true, false);
check('a failed save is reported', strpos($err, 'Data too long') !== false, true);
check('a failed save shows the Groups page', array($redir, $v), array(null, 'groups'));
echo $failures ? "\n$failures failed\n" : "\nall passed\n";
exit($failures ? 1 : 0);
}