diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-04-25 17:52:43 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-04-25 17:52:43 +0000 |
commit | fb5d44bc2c1c2d3ea79c4c9d19ea0d8c7d1f6950 (patch) | |
tree | 9d4c006c79db10896660c0cbf98ce43faee0f654 /modules/user | |
parent | 9a59cf76742669f4e7a26d1ef2737179db8cf7b5 (diff) | |
download | brdo-fb5d44bc2c1c2d3ea79c4c9d19ea0d8c7d1f6950.tar.gz brdo-fb5d44bc2c1c2d3ea79c4c9d19ea0d8c7d1f6950.tar.bz2 |
#303965 by moshe weitzman and snufkin: Allow data import scripts to set /->is_new programmatically.
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/user.module | 11 | ||||
-rw-r--r-- | modules/user/user.test | 43 |
2 files changed, 50 insertions, 4 deletions
diff --git a/modules/user/user.module b/modules/user/user.module index db8977921..64bebc656 100644 --- a/modules/user/user.module +++ b/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 a/modules/user/user.test b/modules/user/user.test index 055f7c261..0baa1cfb6 100644 --- a/modules/user/user.test +++ b/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.')); + } +} |