mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
54 lines
2.4 KiB
PHP
54 lines
2.4 KiB
PHP
<?php
|
|
// Groups.Name is NOT NULL with a default of '', so an API add without a Name
|
|
// is saved as a group with no name; the web console answers 'A group needs a name'.
|
|
// The notBlank rule only ran when a Name was sent. It must be required on create
|
|
// (and only on create, so an edit may leave Name out).
|
|
// Cake is stubbed and the model's rules are applied the way CakeValidationSet
|
|
// does for the rule types used here (required, notBlank, custom); Cake's own
|
|
// Validation class does the checking.
|
|
//
|
|
// php tests/php/test_api_group_name_required.php [root]
|
|
|
|
class App { public static function uses($class, $package) {} }
|
|
class AppModel {}
|
|
|
|
$root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..';
|
|
require_once $root.'/web/api/lib/Cake/Utility/Validation.php';
|
|
require_once $root.'/web/api/app/Model/Group.php';
|
|
|
|
$failures = 0;
|
|
function check($ok, $what) {
|
|
global $failures;
|
|
if ($ok) echo "ok - $what\n"; else { $failures++; echo "not ok - $what\n"; }
|
|
}
|
|
|
|
// True when $data passes every notBlank/custom rule of $field, with a field
|
|
// that is missing failing only a rule that requires it (true, or 'create' on a create).
|
|
function accepts($model, $field, $data, $create) {
|
|
foreach ($model->validate[$field] as $rule) {
|
|
$required = isset($rule['required']) ? $rule['required'] : false;
|
|
if (!array_key_exists($field, $data)) {
|
|
if ($required === true or ($required === 'create' and $create)) return false;
|
|
continue;
|
|
}
|
|
$r = $rule['rule'];
|
|
$name = is_array($r) ? $r[0] : $r;
|
|
$args = is_array($r) ? array_slice($r, 1) : array();
|
|
if ($name !== 'notBlank' and $name !== 'custom') continue;
|
|
if (!call_user_func_array(array('Validation', $name), array_merge(array($data[$field]), $args))) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
$model = new Group();
|
|
|
|
check(!accepts($model, 'Name', array(), true), "add without Name is refused");
|
|
check(!accepts($model, 'Name', array('Name' => ''), true), "add with a blank Name is refused");
|
|
check(!accepts($model, 'Name', array('Name' => ' '), true), "add with only spaces in Name is refused");
|
|
check(accepts($model, 'Name', array('Name' => 'Lobby'), true), "add with a non-blank Name is accepted");
|
|
check(accepts($model, 'Name', array(), false), "an edit may leave Name out");
|
|
check(accepts($model, 'Name', array('Name' => 'Lobby'), false), "an edit may keep the stored Name");
|
|
check(!accepts($model, 'Name', array('Name' => ''), false), "an edit may not blank Name");
|
|
|
|
exit($failures ? 1 : 0);
|