Skip to content
Snippets Groups Projects
Commit 452ce864 authored by megachriz's avatar megachriz Committed by MegaChriz
Browse files

Issue #1570544 by MegaChriz, gaurav.goyal, AndrewsizZ, hpbruna, arrrgh,...

Issue #1570544 by MegaChriz, gaurav.goyal, AndrewsizZ, hpbruna, arrrgh, vcrkid, firfin: Add UID target to user processor.
parent 44b9e9be
No related branches found
No related tags found
No related merge requests found
......@@ -65,6 +65,7 @@ class FeedsUserProcessor extends FeedsProcessor {
$account->uid = 0;
$account->roles = array_filter($this->config['roles']);
$account->status = $this->config['status'];
$account->is_new = TRUE;
return $account;
}
......@@ -105,6 +106,21 @@ class FeedsUserProcessor extends FeedsProcessor {
throw new FeedsValidationException(t('User name missing or email not valid.'));
}
// Check when an user ID gets set or changed during processing if that user
// ID is not already in use.
if (!empty($account->uid)) {
$is_new = !empty($account->feeds_item->is_new);
$different = !empty($account->feeds_item->entity_id) && $account->feeds_item->entity_id != $account->uid;
if ($is_new || $different) {
$exists = entity_load_unchanged('user', $account->uid);
if ($exists) {
throw new FeedsValidationException(t('Could not update user ID to @uid since that ID is already in use.', array(
'@uid' => $account->uid,
)));
}
}
}
// Timezone validation.
if (!empty($account->timezone) && !array_key_exists($account->timezone, system_time_zones())) {
throw new FeedsValidationException(t("Failed importing '@name'. User's timezone is not valid.", array('@name' => $account->name)));
......@@ -126,6 +142,17 @@ class FeedsUserProcessor extends FeedsProcessor {
unset($edit['pass']);
}
// Check if the user ID changed when updating users.
if (!empty($account->feeds_item->entity_id) && $account->feeds_item->entity_id != $account->uid) {
// The user ID of the existing user is different. Try to update the user ID.
db_update('users')
->fields(array(
'uid' => $account->uid,
))
->condition('uid', $account->feeds_item->entity_id)
->execute();
}
user_save($account, $edit);
// If an encrypted password was given, directly set this in the database.
......@@ -258,6 +285,11 @@ class FeedsUserProcessor extends FeedsProcessor {
public function getMappingTargets() {
$targets = parent::getMappingTargets();
$targets += array(
'uid' => array(
'name' => t('User ID'),
'description' => t('The uid of the user. NOTE: use this feature with care, user ids are usually assigned by Drupal.'),
'optional_unique' => TRUE,
),
'name' => array(
'name' => t('User name'),
'description' => t('Name of the user.'),
......@@ -322,6 +354,10 @@ class FeedsUserProcessor extends FeedsProcessor {
// target's value.
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
switch ($target) {
case 'uid':
$uid = db_query("SELECT uid FROM {users} WHERE uid = :uid", array(':uid' => $value))->fetchField();
break;
case 'name':
$uid = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $value))->fetchField();
break;
......
name,mail,since,password,password_md5,password_sha512,timezone
Morticia,morticia@example.com,1244347500,mort,e0108a7eb91670308fff8179a4785453,$S$DfuNE4ur7Jq8xVoJURGm8oMIYunKd366KQUE6akc3EXW/ym9ghpq,UTC
Fester,fester@example.com,1241865600,fest,c8cce3815094f01f0ab774fd4f7a77d4,$S$DjJPqmjlWTIen0nQrG3a.vA71Vc0DqCpKuB.g9zmBMnGzIV6JxqH,
Gomez,gomez@example.com,1228572000,gome,8a5346b9a510f1f698ab0062b71201ac,$S$Dv.EtHlTfnrxuWGLbe3cf31mD9MF6.4u2Z46M2o2dMGgQGzi7m/5,utc
Wednesday,wednesdayexample.com,1228347137,wedn,fefb673afaf531dbd78771976a150dc8,$S$DdPzksGh/c8UukipWagAhTzaqUp/eNHVPiC.x6URBQyA503Z41PI,America/New_York
Pugsley,pugsley@example,1228260225,pugs,09189568a8ee4d0addf53d2f6e4847cd,$S$D1oUihjrYXr.4iesN8Sfw1rVRLdo188v0NRGgcNR/V09oIyYPYmZ,Europe/Lisbon
uid,name,mail,since,password,password_md5,password_sha512,timezone
201,Morticia,morticia@example.com,1244347500,mort,e0108a7eb91670308fff8179a4785453,$S$DfuNE4ur7Jq8xVoJURGm8oMIYunKd366KQUE6akc3EXW/ym9ghpq,UTC
202,Fester,fester@example.com,1241865600,fest,c8cce3815094f01f0ab774fd4f7a77d4,$S$DjJPqmjlWTIen0nQrG3a.vA71Vc0DqCpKuB.g9zmBMnGzIV6JxqH,
203,Gomez,gomez@example.com,1228572000,gome,8a5346b9a510f1f698ab0062b71201ac,$S$Dv.EtHlTfnrxuWGLbe3cf31mD9MF6.4u2Z46M2o2dMGgQGzi7m/5,utc
204,Wednesday,wednesdayexample.com,1228347137,wedn,fefb673afaf531dbd78771976a150dc8,$S$DdPzksGh/c8UukipWagAhTzaqUp/eNHVPiC.x6URBQyA503Z41PI,America/New_York
205,Pugsley,pugsley@example,1228260225,pugs,09189568a8ee4d0addf53d2f6e4847cd,$S$D1oUihjrYXr.4iesN8Sfw1rVRLdo188v0NRGgcNR/V09oIyYPYmZ,Europe/Lisbon
......@@ -141,6 +141,124 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase {
$this->assertText('Failed importing 2 user');
}
/**
* Tests mapping to user ID.
*/
public function testUidTarget() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to user ID.
$this->addMappings('user_import', array(
4 => array(
'source' => 'uid',
'target' => 'uid',
'unique' => TRUE,
),
));
// Create account with uid 202. The username and mail address of this account
// should be updated.
user_save(drupal_anonymous_user(), array(
'uid' => 202,
'name' => 'Joe',
'mail' => 'joe@example.com',
'pass' => 'joe',
'status' => 1,
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Created 2 users');
$this->assertText('Updated 1 user');
// Assert user ID's.
$account = user_load_by_name('Morticia');
$this->assertEqual(201, $account->uid, 'Morticia got user ID 201.');
$account = user_load_by_name('Gomez');
$this->assertEqual(203, $account->uid, 'Gomez got user ID 203.');
// Assert that the username and mail address of account 202 were changed.
$account = user_load(202);
$values = array(
'name' => array(
'expected' => 'Fester',
'actual' => $account->name,
),
'mail' => array(
'expected' => 'fester@example.com',
'actual' => $account->mail,
),
);
$this->assertEqual($values['name']['expected'], $values['name']['actual'], format_string('Username of account 202 changed in @expected (actual: @actual).', array(
'@expected' => $values['name']['expected'],
'@actual' => $values['name']['actual'],
)));
$this->assertEqual($values['mail']['expected'], $values['mail']['actual'], format_string('Mail address of account 202 changed in @expected (actual: @actual).', array(
'@expected' => $values['mail']['expected'],
'@actual' => $values['mail']['actual'],
)));
// Assert that user Joe no longer exists in the system.
$this->assertFalse(user_load_by_name('Joe'), 'No user with username Joe exists.');
$this->assertFalse(user_load_by_mail('joe@example.com'), 'No user with mail address joe@example.com exists.');
}
/**
* Tests if user ID's can be changed using the user ID target.
*
* Also checks if a clear error is reported when trying to change the
* user ID to something that is already in use.
*/
public function testUidUpdating() {
// Set to update existing users.
$this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING));
// Add mapping to user ID, but do not mark target as unique.
$this->addMappings('user_import', array(
4 => array(
'source' => 'uid',
'target' => 'uid',
),
));
// Create an account which user ID should be updated.
user_save(drupal_anonymous_user(), array(
'uid' => 54,
'name' => 'Morticia',
'mail' => 'morticia@example.com',
'pass' => 'mort',
'status' => 1,
));
// Create account with uid 202. Importing an other account with uid 202
// should fail.
user_save(drupal_anonymous_user(), array(
'uid' => 202,
'name' => 'Joe',
'mail' => 'joe@example.com',
'pass' => 'joe',
'status' => 1,
));
// Import CSV file.
$this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
$this->assertText('Created 1 user');
$this->assertText('Updated 1 user');
$this->assertText('Failed importing 3 users.');
$this->assertText('Could not update user ID to 202 since that ID is already in use.');
// Assert Morticia's user ID got updated.
$account = user_load_by_name('Morticia');
$this->assertEqual(201, $account->uid, 'Morticia now got user ID 201.');
// Assert that Fester failed to import.
$this->assertFalse(user_load_by_name('Fester'), 'The account for Fester was not imported.');
// Assert that user 202 did not change.
$account = user_load(202);
$this->assertEqual('Joe', $account->name, 'The user name of account 202 is still Joe.');
$this->assertEqual('joe@example.com', $account->mail, 'The mail address of account 202 is still joe@example.com.');
}
/**
* Tests mapping to role without automatically creating new roles.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment