diff --git a/tests/php/test_api_model_null_not_null.php b/tests/php/test_api_model_null_not_null.php new file mode 100644 index 0000000000..e5858505d2 --- /dev/null +++ b/tests/php/test_api_model_null_not_null.php @@ -0,0 +1,81 @@ + array('type' => 'integer', 'null' => false), + 'Height' => array('type' => 'integer', 'null' => false), + 'Width' => array('type' => 'integer', 'null' => true), + 'Seen' => array('type' => 'datetime', 'null' => false), + 'Ended' => array('type' => 'datetime', 'null' => true), + 'Stamp' => array('type' => 'timestamp', 'null' => false), + 'Method' => array('type' => "enum('Local','Remote')", 'null' => false), + 'Name' => array('type' => 'string', 'length' => 5, 'null' => false), + 'Alias' => array('type' => 'string', 'length' => 5, 'null' => true), + 'Notes' => array('type' => 'text', 'null' => false), + 'Enabled' => array('type' => 'boolean', 'null' => false), + 'Derived' => array('type' => 'integer', 'null' => false), + 'created' => array('type' => 'datetime', 'null' => false), + 'modified' => array('type' => 'datetime', 'null' => false), + ); + } + public function invalidate($field, $message) { $this->invalid[$field] = $message; } +} +require_once __DIR__.'/../../web/api/app/Model/AppModel.php'; +class Computed extends AppModel { public $serverSetColumns = array('Derived'); } + +$passes = 0; +$failures = 0; +function check($name, $got, $expected) { + global $passes, $failures; + if ($got === $expected) { $passes++; echo "ok - $name\n"; } + else { $failures++; echo "not ok - $name (got ".var_export($got, true)." expected ".var_export($expected, true).")\n"; } +} + +function accepted($field, $value, $class = 'AppModel') { + $m = new $class(); + $m->data = array('Thing' => array($field => $value)); + $m->beforeValidate(); + return !isset($m->invalid[$field]); +} + +foreach (array('Height', 'Seen', 'Method', 'Name', 'Notes', 'Enabled') as $field) { + check("null $field (NOT NULL) is refused", accepted($field, null), false); + check("empty list $field (NOT NULL) is refused", accepted($field, array()), false); +} +foreach (array('Width', 'Ended', 'Alias') as $field) { + check("null $field (nullable) is accepted", accepted($field, null), true); + check("empty list $field (nullable) is accepted", accepted($field, array()), true); +} +foreach (array('Id', 'created', 'modified', 'Stamp') as $field) { + check("null $field (key, Cake date or timestamp) is accepted", accepted($field, null), true); + check("empty list $field (key, Cake date or timestamp) is accepted", accepted($field, array()), true); +} +check('null Derived is refused when the model does not set it', accepted('Derived', null), false); +check('null Derived is accepted when the model sets it in beforeSave', accepted('Derived', null, 'Computed'), true); +check('empty list Derived is accepted when the model sets it in beforeSave', accepted('Derived', array(), 'Computed'), true); +check('Height null is still refused on that model', accepted('Height', null, 'Computed'), false); +check('Height 0 is accepted', accepted('Height', 0), true); +check('Name "" is still accepted (text)', accepted('Name', ''), true); +check('Height as a non-empty list is still refused', accepted('Height', array(1)), false); + +$m = new AppModel(); +$m->data = array('Thing' => array('Name' => null, 'Height' => 5)); +$m->beforeValidate(); +check('only the null NOT NULL field is flagged', array_keys($m->invalid), array('Name')); + +echo "\n$passes passed, $failures failed\n"; +exit($failures ? 1 : 0); +?> diff --git a/web/api/app/Model/AppModel.php b/web/api/app/Model/AppModel.php index a3e0a1cd11..0826185609 100644 --- a/web/api/app/Model/AppModel.php +++ b/web/api/app/Model/AppModel.php @@ -31,18 +31,32 @@ App::uses('Model', 'Model'); */ class AppModel extends Model { + // Columns the model's beforeSave() sets itself, so a null or empty list sent for one is not refused. + public $serverSetColumns = array(); + // Check numbers, dates, enums and string lengths against the schema so bad input is a validation error, not a 500. public function beforeValidate($options = array()) { if (empty($this->data[$this->alias])) return true; $bits = array('tinyinteger' => 8, 'smallinteger' => 16, 'integer' => 32); foreach ($this->schema() as $field => $column) { - if (!isset($this->data[$this->alias][$field])) continue; + if (!is_array($this->data[$this->alias]) or !array_key_exists($field, $this->data[$this->alias])) continue; $value = $this->data[$this->alias][$field]; if (is_array($value) and $value) { // Cake would put the word Array in the insert and MySQL would answer with an unknown column. $this->invalidate($field, $field.' must be a single value'); continue; } + if ($value === null or $value === array()) { + // Cake saves null and an empty list as NULL, which a NOT NULL column refuses with a 500. + // Cake drops an empty primary key and empty created/modified/updated before the insert, and a + // server may fill a NOT NULL timestamp with the current time. + if (isset($column['null']) and !$column['null'] and $column['type'] != 'timestamp' and + $field != $this->primaryKey and !in_array($field, array('created', 'modified', 'updated')) and + !in_array($field, $this->serverSetColumns)) { + $this->invalidate($field, $field.' must not be empty'); + } + continue; + } if (!is_scalar($value)) continue; $type = $column['type']; if ($value === '') { diff --git a/web/api/app/Model/Event.php b/web/api/app/Model/Event.php index 94f2f9a5fc..1559306583 100644 --- a/web/api/app/Model/Event.php +++ b/web/api/app/Model/Event.php @@ -10,6 +10,9 @@ App::uses('AppModel', 'Model'); */ class Event extends AppModel { + // Set by beforeSave(), so a null sent for them is not refused. + public $serverSetColumns = array('StateId'); + /** * Use table * diff --git a/web/api/app/Model/Snapshot.php b/web/api/app/Model/Snapshot.php index a6e6c41807..8f183d1ce1 100644 --- a/web/api/app/Model/Snapshot.php +++ b/web/api/app/Model/Snapshot.php @@ -8,6 +8,9 @@ App::uses('AppModel', 'Model'); */ class Snapshot extends AppModel { + // Set by beforeSave(), so a null sent for them is not refused. + public $serverSetColumns = array('CreatedBy', 'CreatedOn'); + /** * Use table * diff --git a/web/api/app/Model/User.php b/web/api/app/Model/User.php index d5a49b6200..2b69d345d4 100644 --- a/web/api/app/Model/User.php +++ b/web/api/app/Model/User.php @@ -6,6 +6,9 @@ App::uses('AppModel', 'Model'); */ class User extends AppModel { + // Set by beforeSave(), so a null sent for them is not refused. + public $serverSetColumns = array('TokenMinExpiry'); + public $validate = array( 'Username' => array( 'required' => array( diff --git a/web/api/app/Model/Zone.php b/web/api/app/Model/Zone.php index 500e6eb189..7911b21952 100644 --- a/web/api/app/Model/Zone.php +++ b/web/api/app/Model/Zone.php @@ -7,6 +7,9 @@ App::uses('AppModel', 'Model'); */ class Zone extends AppModel { + // Set by beforeSave(), so a null sent for them is not refused. + public $serverSetColumns = array('NumCoords', 'Area'); + /** * Use table *