includes/common.inc | 2 - modules/node/node.module | 16 +++++++++++-- modules/node/node.test | 52 ++++++++++++++++++++++++++++++++++++++++++++++ modules/user/user.module | 11 ++++++--- modules/user/user.test | 43 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 9 deletions(-) diff --git includes/common.inc includes/common.inc index 2fd7984..30baddd 100644 --- includes/common.inc +++ includes/common.inc @@ -3971,8 +3971,6 @@ function drupal_write_record($table, &$object, $primary_keys = array()) { // NOTE: Each table should come with one serial field only. if ($info['type'] == 'serial') { $serial = $field; - // Ignore values for serial when inserting data. Unsupported. - unset($object->$field); } // Build arrays for the fields and values in our query. diff --git modules/node/node.module modules/node/node.module index 889c2c9..0a1b146 100644 --- modules/node/node.module +++ modules/node/node.module @@ -1046,7 +1046,11 @@ function node_submit($node) { } /** - * Save a node object into the database. + * Save changes to a node or add a new node. + * + * @param $node + * The $node object to be saved. If $node->nid is + * omitted or $node->is_new is TRUE, a new node will be added. */ function node_save(&$node) { field_attach_presave('node', $node); @@ -1054,10 +1058,12 @@ function node_save(&$node) { node_invoke_node($node, 'presave'); global $user; - $node->is_new = FALSE; + if (!isset($node->is_new)) { + $node->is_new = empty($node->nid); + } // Apply filters to some default node fields: - if (empty($node->nid)) { + if ($node->is_new) { // Insert a new node. $node->is_new = TRUE; @@ -1082,6 +1088,7 @@ function node_save(&$node) { } elseif (!empty($node->revision)) { $node->old_vid = $node->vid; + unset($node->vid); } else { // When updating a node, avoid clobbering an existing log entry with an empty one. @@ -1134,6 +1141,9 @@ function node_save(&$node) { // Update the node access table for this node. node_access_acquire_grants($node); + + // Clear internal properties. + unset($node->is_new); // Clear the page and block caches. cache_clear_all(); diff --git modules/node/node.test modules/node/node.test index 9628f63..950fc5a 100644 --- modules/node/node.test +++ modules/node/node.test @@ -664,3 +664,55 @@ class NodeRSSContentTestCase extends DrupalWebTestCase { $this->assertText($test_text, t('Extra node content appears in RSS feed.')); } } + +/** + * Test case to check node save related functionality, including import-save + */ +class NodeSaveTestCase extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('Node save'), + 'description' => t('Test node_save() for saving content.'), + 'group' => t('Node'), + ); + } + + function setUp() { + parent::setUp(); + // Create a user that is allowed to post; we'll use this to test the submission. + $web_user = $this->drupalCreateUser(array('create article content')); + $this->drupalLogin($web_user); + $this->web_user = $web_user; + } + + /** + * Import test, to check if custom node ids are saved properly. + * Workflow: + * - first create a piece of content + * - save the content + * - check if node exists + */ + function testImport() { + // Node ID must be a number that is not in the database. + $max_nid = db_result(db_query('SELECT MAX(nid) FROM {node}')); + $test_nid = $max_nid + mt_rand(1000, 1000000); + $title = $this->randomName(8); + $node = array( + 'title' => $title, + 'body' => $this->randomName(32), + 'uid' => $this->web_user->uid, + 'type' => 'article', + 'nid' => $test_nid, + 'is_new' => TRUE, + ); + $node = (object)$node; + node_save($node); + // Test the import. + $node_by_nid = node_load($test_nid); + $this->assertTrue($node_by_nid, t('Node load by node ID.')); + + $node_by_title = $this->drupalGetNodeByTitle($title); + $this->assertTrue($node_by_title, t('Node load by node title.')); + } +} diff --git modules/user/user.module modules/user/user.module index 3728b72..09b0411 100644 --- modules/user/user.module +++ modules/user/user.module @@ -353,7 +353,7 @@ function user_load_by_name($name) { * * @param $account * The $user object for the user to modify or add. If $user->uid is - * omitted, a new user will be added. + * omitted (or $user->is_new == TRUE), a new user will be added. * * @param $edit * An array of fields and values to save. For example array('name' @@ -398,7 +398,10 @@ function user_save($account, $edit = array(), $category = 'account') { $edit = (array) $edit; - if (is_object($account) && $account->uid) { + if (!isset($account->is_new)) { + $account->is_new = empty($account->uid); + } + if (is_object($account) && !$account->is_new) { user_module_invoke('update', $edit, $account, $category); $data = unserialize(db_query('SELECT data FROM {users} WHERE uid = :uid', array(':uid' => $account->uid))->fetchField()); // Consider users edited by an administrator as logged in, if they haven't @@ -410,7 +413,7 @@ function user_save($account, $edit = array(), $category = 'account') { // Form fields that don't pertain to the users, user_roles, or // Field API are automatically serialized into the users.data // column. - if ($key != 'roles' && empty($user_fields[$key]) && empty($field_form[$key])) { + if (!in_array($key, array('roles', 'is_new')) && empty($user_fields[$key]) && empty($field_form[$key])) { if ($value === NULL) { unset($data[$key]); } @@ -538,7 +541,7 @@ function user_save($account, $edit = array(), $category = 'account') { // Form fields that don't pertain to the users, user_roles, or // Field API are automatically serialized into the user.data // column. - if (($key != 'roles') && (empty($user_fields[$key]) && empty($field_form[$key])) && ($value !== NULL)) { + if ((!in_array($key, array('roles', 'is_new'))) && (empty($user_fields[$key]) && empty($field_form[$key])) && ($value !== NULL)) { $data[$key] = $value; } } diff --git modules/user/user.test modules/user/user.test index 74d830b..f9b5639 100644 --- modules/user/user.test +++ modules/user/user.test @@ -973,3 +973,46 @@ class UserBlocksUnitTests extends DrupalWebTestCase { $this->assertEqual(db_query("SELECT COUNT(*) FROM {sessions} WHERE uid = :uid AND sid = :sid AND timestamp = :timestamp", array(':uid' => $fields['uid'], ':sid' => $fields['sid'], ':timestamp' => $fields['timestamp']))->fetchField(), 1, t('Session record inserted.')); } } + +/** + * Test case to test user_save() behaviour. + */ +class UserSaveTestCase extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('User save test'), + 'description' => t('Test user_save() for arbitrary new uid.'), + 'group' => t('User'), + ); + } + + /** + * Test creating a user with arbitrary uid. + */ + function testUserImport() { + // User ID must be a number that is not in the database. + $max_uid = db_result(db_query('SELECT MAX(uid) FROM {users}')); + $test_uid = $max_uid + mt_rand(1000, 1000000); + $test_name = $this->randomName(); + + // Create the base user, based on drupalCreateUser(). + $user = array( + 'name' => $test_name, + 'uid' => $test_uid, + 'mail' => $test_name . '@example.com', + 'is_new' => TRUE, + 'pass' => user_password(), + 'status' => 1, + ); + $user_by_return = user_save('', $user); + $this->assertTrue($user_by_return, t('Loading user by return of user_save().')); + + // Test if created user exists. + $user_by_uid = user_load($test_uid); + $this->assertTrue($user_by_uid, t('Loading user by uid.')); + + $user_by_name = user_load_by_name($test_name); + $this->assertTrue($user_by_name, t('Loading user by name.')); + } +}