From db50dc5b595675dcb468be88e5930fa74cd6a195 Mon Sep 17 00:00:00 2001
From: Chris Leppanen <chris.leppanen@gmail.com>
Date: Fri, 5 Oct 2012 08:35:34 -0700
Subject: [PATCH] Issue #1567508 by kruser, beansboxchrispang: Fixed User
 import is incorrectly changing passwords.

---
 plugins/FeedsUserProcessor.inc  | 15 ++++++++++++--
 tests/feeds_processor_user.test | 35 +++++++++++++++++++++++++++++----
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/plugins/FeedsUserProcessor.inc b/plugins/FeedsUserProcessor.inc
index 49e3acd0..b067c414 100644
--- a/plugins/FeedsUserProcessor.inc
+++ b/plugins/FeedsUserProcessor.inc
@@ -40,7 +40,10 @@ class FeedsUserProcessor extends FeedsProcessor {
    * Loads an existing user.
    */
   protected function entityLoad(FeedsSource $source, $uid) {
-    return user_load($uid);
+    // Copy the password so that we can compare it again at save.
+    $user = user_load($uid);
+    $user->feeds_original_pass = $user->pass;
+    return $user;
   }
 
   /**
@@ -59,7 +62,15 @@ class FeedsUserProcessor extends FeedsProcessor {
     if ($this->config['defuse_mail']) {
       $account->mail = $account->mail . '_test';
     }
-    user_save($account, (array) $account);
+
+    $edit = (array) $account;
+
+    // Remove pass from $edit if the password is unchanged.
+    if (isset($account->feeds_original_pass) && $account->pass == $account->feeds_original_pass) {
+      unset($edit['pass']);
+    }
+
+    user_save($account, $edit);
     if ($account->uid && !empty($account->openid)) {
       $authmap = array(
         'uid' => $account->uid,
diff --git a/tests/feeds_processor_user.test b/tests/feeds_processor_user.test
index 8b3bd6d6..73f8d06d 100644
--- a/tests/feeds_processor_user.test
+++ b/tests/feeds_processor_user.test
@@ -34,12 +34,12 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase {
       0 => array(
         'source' => 'name',
         'target' => 'name',
-        'unique' => 0,
+        'unique' => FALSE,
       ),
       1 => array(
         'source' => 'mail',
         'target' => 'mail',
-        'unique' => 1,
+        'unique' => TRUE,
       ),
       2 => array(
         'source' => 'since',
@@ -74,7 +74,7 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase {
     $this->assertText('Created 3 users');
     // 1 user has an invalid email address, all users should be assigned
     // the manager role.
-    $this->assertText('Failed importing 2 user');
+    $this->assertText('Failed importing 2 users.');
     $this->drupalGet('admin/people');
     $this->assertText('Morticia');
     $this->assertText('Fester');
@@ -84,12 +84,39 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase {
     $count = db_query("SELECT count(*) FROM {users_roles} WHERE rid = :rid", array(':rid' => $admin_rid))->fetchField();
     $this->assertEqual($count, 0, t('No imported user was assigned the administrator role.'));
 
-    // @todo Test status setting, update existing and role settings.
+    // Run import again, verify no new users.
+    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
+    $this->assertText('Failed importing 2 users.');
 
     // Attempt to log in as one of the imported users.
     $account = user_load_by_name('Morticia');
     $this->assertTrue($account, 'Imported user account loaded.');
     $account->pass_raw = 'mort';
     $this->drupalLogin($account);
+
+    // Login as admin.
+    $this->drupalLogin($this->admin_user);
+
+    // Removing a mapping forces updating without needing a different file.
+    // We are also testing that if we don't map anything to the user's password
+    // that it will keep its existing one.
+    $mappings = array(
+      3 => array(
+        'source' => 'password',
+        'target' => 'pass',
+      ),
+    );
+    $this->removeMappings('user_import', $mappings);
+    $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => 2));
+    $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv');
+    // Assert result.
+    $this->assertText('Updated 3 users');
+    $this->assertText('Failed importing 2 user');
+
+    // Attempt to log in as one of the imported users.
+    $account = user_load_by_name('Fester');
+    $this->assertTrue($account, 'Imported user account loaded.');
+    $account->pass_raw = 'fest';
+    $this->drupalLogin($account);
   }
 }
-- 
GitLab