diff --git a/tests/php/test_device_save_missing_device.php b/tests/php/test_device_save_missing_device.php new file mode 100644 index 0000000000..8ef005730e --- /dev/null +++ b/tests/php/test_device_save_missing_device.php @@ -0,0 +1,62 @@ + $did, 'newDevice' => array('Name' => 'Porch', 'KeyString' => 'A1')); + include $root.'/web/includes/actions/device.php'; + return array($error_message, $redirect, $view, $GLOBALS['queries']); +} + +list($err, $redir, $v, $queries) = save('9'); +check('a deleted device runs no write', $queries, array()); +check('a deleted device reports that it is gone', $err, 'Device not found
'); +check('a deleted device does not redirect', $redir, null); +check('a deleted device shows the Devices view', $v, 'devices'); + +list($err, $redir, $v, $queries) = save('4'); +check('an existing device is updated', $queries, array('UPDATE Devices SET Name=?, KeyString=? WHERE Id=?')); +check('an existing device reports nothing', $err, ''); +check('an existing device redirects to the list', $redir, '?view=devices'); + +list($err, $redir, $v, $queries) = save(''); +check('a new device is inserted', $queries, array('INSERT INTO Devices SET Name=?, KeyString=?')); +check('a new device reports nothing', $err, ''); +check('a new device redirects to the list', $redir, '?view=devices'); + +echo $failures ? "\n$failures failed\n" : "\nall passed\n"; +exit($failures ? 1 : 0); diff --git a/web/includes/actions/device.php b/web/includes/actions/device.php index a57fc9f0fc..036171e8e5 100644 --- a/web/includes/actions/device.php +++ b/web/includes/actions/device.php @@ -35,8 +35,13 @@ if ( $action == 'device' ) { if ( !is_scalar($name) or !is_scalar($keyString) ) { $error_message .= 'A device needs a Name and a KeyString
'; } else if ( isset($_REQUEST['did']) && $_REQUEST['did'] ) { - $saved = (dbQuery('UPDATE Devices SET Name=?, KeyString=? WHERE Id=?', - array($name, $keyString, $_REQUEST['did']) ) !== null); + if ( !dbFetchOne('SELECT Id FROM Devices WHERE Id=?', 'Id', array($_REQUEST['did'])) ) { + // Deleted in another tab: the UPDATE would match no row and still report a save + $error_message .= 'Device not found
'; + } else { + $saved = (dbQuery('UPDATE Devices SET Name=?, KeyString=? WHERE Id=?', + array($name, $keyString, $_REQUEST['did']) ) !== null); + } } else { $saved = (dbQuery('INSERT INTO Devices SET Name=?, KeyString=?', array($name, $keyString) ) !== null);