fix: API tag add requires a Name (#8472)

This commit is contained in:
Harsh Singh authored and GitHub committed 2026-11-28 11:04:52 -05:00
1 parent 7397e5f593
commit 8a107cfef3
2 files changed
+55 -1

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
<?php
// Tags.Name is NOT NULL and unique, but the Tag model's notBlank rule only ran when a Name was
// sent. An API add with other fields and no Name (a body such as {"Tag":{"CreatedBy":1}}) therefore
// saved a tag with an empty name, and the next one failed on the unique index with a 500.
// The rule 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_tag_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/Tag.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 Tag();
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' => 'Dog'), 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' => 'Dog'), 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);
+3 -1
View File
@@ -37,7 +37,9 @@ class Tag extends AppModel {
public $validate = array(
'Name' => array(
'notBlank' => array(
'rule' => array('notBlank')),
'rule' => array('notBlank'),
'required' => 'create',
'message' => 'A tag needs a name'),
'unique' => array(
'rule' => array('uniqueIgnoringCase'),
'message' => 'A tag with that name already exists')),