diff --git a/modules/user/user.module b/modules/user/user.module
index 7547f3f98a2eb9750ef24c1905b54e6e41b9e58a..6f9354971677a14c6a5ba5ee7e294d52458f0e27 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -1929,7 +1929,7 @@ function user_page_title($account) {
  *   An associative array with module as key and username as value.
  */
 function user_get_authmaps($authname = NULL) {
-  $authmaps = db_query("SELECT authname, module FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchAllKeyed();
+  $authmaps = db_query("SELECT module, authname FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchAllKeyed();
   return count($authmaps) ? $authmaps : 0;
 }
 
diff --git a/modules/user/user.test b/modules/user/user.test
index 0d5ec3926e858452eba6fe92b67672ff9aa0a4e0..d576a75b5a08dff21eaa8ea557bbaf0d42653b06 100644
--- a/modules/user/user.test
+++ b/modules/user/user.test
@@ -1921,3 +1921,62 @@ class UserRolesAssignmentTestCase extends DrupalWebTestCase {
   }
 }
 
+
+/**
+ * Unit test for authmap assignment.
+ */
+class UserAuthmapAssignmentTestCase extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => t('Authmap assignment'),
+      'description' => t('Tests that users can be assigned and unassigned authmaps.'),
+      'group' => t('User')
+    );
+  }
+
+  /**
+   * Test authmap assignment and retrieval.
+   */
+  function testAuthmapAssignment()  {
+    $account = $this->drupalCreateUser();
+
+    // Assign authmaps to the user.
+    $authmaps = array(
+      'authname_poll' => 'external username one',
+      'authname_book' => 'external username two',
+    );
+    user_set_authmaps($account, $authmaps);
+
+    // Test for expected authmaps.
+    $expected_authmaps = array(
+      'external username one' => array(
+        'poll' => 'external username one',
+      ),
+      'external username two' => array(
+        'book' => 'external username two',
+      ),
+    );
+    foreach ($expected_authmaps as $authname => $expected_output) {
+      $this->assertIdentical(user_get_authmaps($authname), $expected_output, t('Authmap for authname %authname was set correctly.', array('%authname' => $authname)));
+    }
+
+    // Remove authmap for module poll, add authmap for module blog.
+    $authmaps = array(
+      'authname_poll' => NULL,
+      'authname_blog' => 'external username three',
+    );
+    user_set_authmaps($account, $authmaps);
+
+    // Assert that external username one does not have authmaps.
+    $remove_username = 'external username one';
+    unset($expected_authmaps[$remove_username]);
+    $this->assertFalse(user_get_authmaps($remove_username), t('Authmap for %authname was removed.', array('%authname' => $remove_username)));
+
+    // Assert that a new authmap was created for external username three, and
+    // existing authmaps for external username two were unchanged.
+    $expected_authmaps['external username three'] = array('blog' => 'external username three');
+    foreach ($expected_authmaps as $authname => $expected_output) {
+      $this->assertIdentical(user_get_authmaps($authname), $expected_output, t('Authmap for authname %authname was set correctly.', array('%authname' => $authname)));
+    }
+  }
+}