diff --git a/plugins/FeedsUserProcessor.inc b/plugins/FeedsUserProcessor.inc index 5143913..87a08f7 100644 --- a/plugins/FeedsUserProcessor.inc +++ b/plugins/FeedsUserProcessor.inc @@ -107,23 +107,22 @@ class FeedsUserProcessor extends FeedsProcessor { } user_save($account, $edit); + + // If an encrypted password was given, directly set this in the database. if ($account->uid && !empty($account->pass_crypted)) { db_update('users') ->fields(array('pass' => $account->pass_crypted)) ->condition('uid', $account->uid) ->execute(); } + if ($account->uid && !empty($account->openid)) { $authmap = array( 'uid' => $account->uid, 'module' => 'openid', 'authname' => $account->openid, ); - if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array( - 'uid', - 'module' - )) - ) { + if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array('uid', 'module'))) { drupal_write_record('authmap', $authmap); } } @@ -208,6 +207,7 @@ class FeedsUserProcessor extends FeedsProcessor { case 'pass': $this->setPassTarget($source, $target_user, $target_element, $value, $mapping); break; + case 'created': $target_user->created = feeds_to_unixtime($value, REQUEST_TIME); break; @@ -251,8 +251,8 @@ class FeedsUserProcessor extends FeedsProcessor { 'pass' => array( 'name' => t('Password'), 'description' => t('The user password.'), - 'summary_callback' => array($this, 'passSummaryCallback'), - 'form_callback' => array($this, 'passFormCallback'), + 'summary_callbacks' => array(array($this, 'passSummaryCallback')), + 'form_callbacks' => array(array($this, 'passFormCallback')), ), 'status' => array( 'name' => t('Account status'), @@ -512,22 +512,7 @@ class FeedsUserProcessor extends FeedsProcessor { } /** - * Summary callback for pass mapper. - * - * @param array $mapping - * Associative array of the mapping settings. - * @param array $target - * Array of target settings, as defined by the processor. - * @param array $form - * The whole mapping form. - * @param array $form_state - * The form state of the mapping form. - * - * @return string - * Returns, as a string that may contain HTML, the summary to display while - * the full form isn't visible. - * If the return value is empty, no summary and no option to view the form - * will be displayed. + * Mapping configuration summary callback for target "pass". */ public function passSummaryCallback($mapping, $target, $form, $form_state) { $options = $this->passSummaryCallbackOptions(); @@ -544,6 +529,7 @@ class FeedsUserProcessor extends FeedsProcessor { * * @return array * An array of password encryption option titles. + * * @see passSummaryCallback() */ protected function passSummaryCallbackOptions() { @@ -555,19 +541,7 @@ class FeedsUserProcessor extends FeedsProcessor { } /** - * Settings form callback for pass mapper. - * - * @param array $mapping - * Associative array of the mapping settings. - * @param array $target - * Array of target settings, as defined by the processor. - * @param array $form - * The whole mapping form. - * @param array $form_state - * The form state of the mapping form. - * - * @return array - * Mapping configuration form for pass mapper. + * Mapping configuration form callback for target "pass". */ public function passFormCallback($mapping, $target, $form, $form_state) { return array( @@ -587,6 +561,7 @@ class FeedsUserProcessor extends FeedsProcessor { * * @return array * An array of password encryption option titles. + * * @see passFormCallback() */ protected function passFormCallbackOptions() { @@ -620,9 +595,11 @@ class FeedsUserProcessor extends FeedsProcessor { $target_user->pass = $target_user->pass_crypted = $new_hash; } break; + case self::PASS_SHA512: $target_user->pass = $target_user->pass_crypted = $value; break; + default: $target_user->pass = $value; break; diff --git a/tests/feeds_processor_user.test b/tests/feeds_processor_user.test index b7a69da..2c7c7b7 100644 --- a/tests/feeds_processor_user.test +++ b/tests/feeds_processor_user.test @@ -602,49 +602,40 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { * Test if users with md5 passwords can login after import. */ public function testMD5() { - // Create an importer. - $this->createImporterConfiguration('User import', 'user_import'); - - // Set and configure plugins. - $this->setPlugin('user_import', 'FeedsFileFetcher'); - $this->setPlugin('user_import', 'FeedsCSVParser'); - $this->setPlugin('user_import', 'FeedsUserProcessor'); + // Set to update existing users. + $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING)); - // Go to mapping page and create a couple of mappings. - $mappings = array( - 0 => array( - 'source' => 'name', - 'target' => 'name', - 'unique' => FALSE, - ), - 1 => array( - 'source' => 'mail', - 'target' => 'mail', - 'unique' => TRUE, - ), - 2 => array( - 'source' => 'since', - 'target' => 'created', + // Replace password mapper. + $this->removeMappings('user_import', array( + 3 => array( + 'source' => 'password', + 'target' => 'pass', ), + )); + $this->addMappings('user_import', array( 3 => array( 'source' => 'password_md5', 'target' => 'pass', 'pass_encryption' => 'md5', ), - ); - $this->addMappings('user_import', $mappings); + )); - // Use standalone form. - $edit = array( - 'content_type' => '', - ); - $this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save'); + // Create an account for Gomez, to ensure passwords can also be imported for + // existing users. Give Gomez a password different from the one that gets + // imported to ensure that his password gets updated. + user_save(drupal_anonymous_user(), array( + 'name' => 'Gomez', + 'mail' => 'gomez@example.com', + 'pass' => 'temporary', + 'status' => 1, + )); // Import CSV file. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv'); // Assert result. - $this->assertText('Created 3 users'); + $this->assertText('Created 2 users'); + $this->assertText('Updated 1 user'); // Try to login as each successful imported user. $this->feedsLoginUser('Morticia', 'mort'); @@ -656,49 +647,40 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { * Test if users with sha512 passwords can login after import. */ public function testSha512() { - // Create an importer. - $this->createImporterConfiguration('User import', 'user_import'); - - // Set and configure plugins. - $this->setPlugin('user_import', 'FeedsFileFetcher'); - $this->setPlugin('user_import', 'FeedsCSVParser'); - $this->setPlugin('user_import', 'FeedsUserProcessor'); + // Set to update existing users. + $this->setSettings('user_import', 'FeedsUserProcessor', array('update_existing' => FEEDS_UPDATE_EXISTING)); - // Go to mapping page and create a couple of mappings. - $mappings = array( - 0 => array( - 'source' => 'name', - 'target' => 'name', - 'unique' => FALSE, - ), - 1 => array( - 'source' => 'mail', - 'target' => 'mail', - 'unique' => TRUE, - ), - 2 => array( - 'source' => 'since', - 'target' => 'created', + // Replace password mapper. + $this->removeMappings('user_import', array( + 3 => array( + 'source' => 'password', + 'target' => 'pass', ), + )); + $this->addMappings('user_import', array( 3 => array( 'source' => 'password_sha512', 'target' => 'pass', 'pass_encryption' => 'sha512', ), - ); - $this->addMappings('user_import', $mappings); + )); - // Use standalone form. - $edit = array( - 'content_type' => '', - ); - $this->drupalPost('admin/structure/feeds/user_import/settings', $edit, 'Save'); + // Create an account for Gomez, to ensure passwords can also be imported for + // existing users. Give Gomez a password different from the one that gets + // imported to ensure that his password gets updated. + user_save(drupal_anonymous_user(), array( + 'name' => 'Gomez', + 'mail' => 'gomez@example.com', + 'pass' => 'temporary', + 'status' => 1, + )); // Import CSV file. $this->importFile('user_import', $this->absolutePath() . '/tests/feeds/users.csv'); // Assert result. - $this->assertText('Created 3 users'); + $this->assertText('Created 2 users'); + $this->assertText('Updated 1 user'); // Try to login as each successful imported user. $this->feedsLoginUser('Morticia', 'mort'); @@ -716,7 +698,7 @@ class FeedsCSVtoUsersTest extends FeedsWebTestCase { * * @return void */ - public function feedsLoginUser($username, $password) { + protected function feedsLoginUser($username, $password) { $account = user_load_by_name($username); $this->assertTrue($account, 'Imported user account loaded.'); $account->pass_raw = $password;