From cd3645af1a807e7cf25d53e3c76c243be7eb0136 Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Sun, 21 Nov 2010 08:02:30 +0000
Subject: [PATCH] #955414 by bfroehle: Fixedd user_get_authmaps: respect proper
 key-value ordering as in description

---
 modules/user/user.module |  2 +-
 modules/user/user.test   | 59 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/modules/user/user.module b/modules/user/user.module
index 7547f3f98a2e..6f9354971677 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 0d5ec3926e85..d576a75b5a08 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)));
+    }
+  }
+}
-- 
GitLab