From 8bd010c2062953a4289da76c8daf3af5be10654c Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <bfroehle@lbl.gov>
Date: Fri, 29 Oct 2010 09:51:32 -0700
Subject: [PATCH] 955414: (user_get_authmaps) Fix key value pair ordering.

The comments for user_get_authmaps describe the function as
returning a key-value pair '%module' => '%authname'.  This agrees
with functionality in past releases.

In [#394594], the key value pair ordering was (inadvertently)
reversed to be '%authname' => '%module'.

Included also is a unit test for user_get_authmaps and
user_set_authmaps which verifies that authmaps can be set and
removed, and their output has the proper key-value pair ordering.
---
 modules/user/user.module |    2 +-
 modules/user/user.test   |   67 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletions(-)

diff --git modules/user/user.module modules/user/user.module
index a9f7462..a9c5edd 100644
--- modules/user/user.module
+++ modules/user/user.module
@@ -1922,7 +1922,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 authname, module FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchAllKeyed(1,0);
   return count($authmaps) ? $authmaps : 0;
 }
 
diff --git modules/user/user.test modules/user/user.test
index 2d54f70..f085038 100644
--- modules/user/user.test
+++ modules/user/user.test
@@ -1899,3 +1899,70 @@ class UserRolesAssignmentTestCase extends DrupalWebTestCase {
   }
 }
 
+
+/**
+ * Unit test for authmap assignment.
+ */
+class UserAuthmapAssignmentTestCase extends DrupalWebTestCase {
+  protected $admin_user;
+
+  public static function getInfo() {
+    return array(
+      'name' => t('Authmap assignment'),
+      'description' => t('Tests that users can be assigned and unassigned authmaps.'),
+      'group' => t('User')
+    );
+  }
+
+  function setUp() {
+    parent::setUp();
+    $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'administer users'));
+    $this->drupalLogin($this->admin_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)));
+    }
+  }
+}
-- 
1.7.3.1

