Files
zoneminder/tests/php/test_group_save_empty_name.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

136 lines
5.2 KiB
PHP

<?php
// Saving a group with an empty, whitespace-only, missing or array
// newGroup[Name] must be rejected before ZM\Group::save() is called
// (error message set, view back to 'groups'), and a padded name must be
// saved trimmed.
//
// Run as: php tests/php/test_group_save_empty_name.php [webroot]
// webroot defaults to this worktree's web/ directory.
namespace ZM {
class Group {
public static $save_calls = 0;
public static $saved_data = null;
public static $last_error = '';
public static $next_id = 7;
private $id;
public function __construct($id = null) {
$this->id = $id ? (int)$id : null;
}
public function Id($v = null) {
if (func_num_args()) $this->id = $v;
return $this->id;
}
public function ParentId() { return null; }
public function MonitorIds() { return array(); }
public function save($changes) {
self::$save_calls++;
self::$saved_data = $changes;
if (!$this->id) $this->id = self::$next_id;
return true;
}
public function get_last_error() { return self::$last_error; }
public static function validParent($id, $parent) { return true; }
public static function canEditMembership($monitor_ids) { return true; }
}
function Debug($s) {}
function Warning($s) {}
function Error($s) {}
function Info($s) {}
function AuditAction() {}
}
namespace {
$failures = 0;
$passes = 0;
function check($name, $got, $want) {
global $failures, $passes;
if ($got === $want) { $passes++; echo "ok - $name\n"; }
else { $failures++; echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; }
}
// The pre-fix action reads $_REQUEST['newGroup']['Name'] without a guard,
// which raises an undefined-index warning on the missing-name case.
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
function canEdit($area) { return true; }
function dbFetchAll($sql, $col = false, $params = array()) { return array(); }
function dbQuery($sql, $params = array()) { return true; }
function dbLastError() { return ''; }
function validHtmlStr($s) { return htmlspecialchars((string)$s); }
// Copied from web/includes/functions.php.
function validCardinal($input) {
if (!is_scalar($input)) return '';
return preg_replace('/\D/', '', $input);
}
$webroot = isset($argv[1]) ? rtrim($argv[1], '/') : __DIR__.'/../../web';
// Run the save action with the given newGroup POST fields. A truthy $gid
// edits an existing group (exercising the dbFetchAll of old monitor ids);
// null creates a new one.
function run_action($new_group, $gid = '3') {
global $action, $error_message, $view, $redirect;
ZM\Group::$save_calls = 0;
ZM\Group::$saved_data = null;
ZM\Group::$last_error = '';
ZM\Group::$next_id = 7;
$action = 'save';
$error_message = '';
$view = null;
$redirect = null;
$_REQUEST = array('object' => 'group', 'newGroup' => $new_group);
if ($gid) $_REQUEST['gid'] = $gid;
require($GLOBALS['webroot'].'/includes/actions/group.php');
return array(
'saves' => ZM\Group::$save_calls,
'saved' => ZM\Group::$saved_data,
'error' => $error_message,
'view' => $view,
'redirect' => $redirect,
);
}
// 1. Whitespace-only name on an existing group must not be saved.
$r = run_action(array('Name' => ' ', 'ParentId' => ''));
check('whitespace-only name does not save', $r['saves'], 0);
check('whitespace-only name reports an error', $r['error'] !== '', true);
check('whitespace-only name returns to groups view', $r['view'], 'groups');
check('whitespace-only name does not redirect', $r['redirect'], null);
// 2. Empty name on a new group must not be saved.
$r = run_action(array('Name' => '', 'ParentId' => ''), null);
check('empty name does not save', $r['saves'], 0);
check('empty name reports an error', $r['error'] !== '', true);
check('empty name returns to groups view', $r['view'], 'groups');
check('empty name does not redirect', $r['redirect'], null);
// 3. A missing Name field must not be saved either.
$r = run_action(array('ParentId' => ''), null);
check('missing name does not save', $r['saves'], 0);
check('missing name reports an error', $r['error'] !== '', true);
check('missing name returns to groups view', $r['view'], 'groups');
check('missing name does not redirect', $r['redirect'], null);
// 4. An array Name (crafted POST) must not be saved.
$r = run_action(array('Name' => array('x' => 'y'), 'ParentId' => ''));
check('array name does not save', $r['saves'], 0);
check('array name reports an error', $r['error'] !== '', true);
check('array name returns to groups view', $r['view'], 'groups');
check('array name does not redirect', $r['redirect'], null);
// 5. A padded real name must be saved once, trimmed.
$r = run_action(array('Name' => ' Front door ', 'ParentId' => ''));
check('padded name saves once', $r['saves'], 1);
check('padded name is saved trimmed', $r['saved']['Name'] ?? null, 'Front door');
check('padded name saves the parent', $r['saved']['ParentId'] ?? null, null);
check('padded name reports no error', $r['error'], '');
check('padded name redirects to groups', $r['redirect'], '?view=groups');
echo "\n$passes passed, $failures failed.\n";
exit($failures ? 1 : 0);
}