From 3b3436f02044547b112ff1f95feb0af087619970 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   |   70 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletions(-)

diff --git modules/user/user.module modules/user/user.module
index c0372e6..595b7ca 100644
--- modules/user/user.module
+++ modules/user/user.module
@@ -1933,7 +1933,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..0c7f99c 100644
--- modules/user/user.test
+++ modules/user/user.test
@@ -1899,3 +1899,73 @@ class UserRolesAssignmentTestCase extends DrupalWebTestCase {
   }
 }
 
+
+/**
+ * Test authmap assignment and retrieval.
+ */
+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);
+  }
+
+  /**
+   * Tests that a user can be assigned an authmap and that the authmap can be removed
+   * again.
+   */
+  function testAssignAndRemoveAuthmap()  {
+    $account = $this->drupalCreateUser();
+
+    $authmaps = array();
+    $authmaps['authname_' . $this->randomName()] = $this->randomName();
+    $authmaps['authname_' . $this->randomName()] = $this->randomName();
+
+    // Assign the authmaps to the user.
+    user_set_authmaps($account, $authmaps);
+    $this->userLoadAndCheckAuthmapsAssigned($account, $authmaps);
+
+    // Remove the authmaps from the user.
+    $unset_authmaps = array();
+    foreach ($authmaps as $key => $value) {
+      $unset_authmaps[$key] = NULL;
+    }
+    user_set_authmaps($account, $unset_authmaps);
+    $this->userLoadAndCheckAuthmapsAssigned($account, $authmaps, FALSE);
+  }
+
+  /**
+   * Check authmaps on user object.
+   *
+   * @param object $account User.
+   * @param array $authmaps Authmaps.
+   * @param bool $is_assigned True if the authmaps should be present on the account.
+   */
+  private function userLoadAndCheckAuthmapsAssigned($account, $authmaps, $is_assigned = TRUE) {
+    $account = user_load($account->uid, TRUE);
+    if ($is_assigned) {
+      foreach ($authmaps as $key => $value) {
+        $module = explode('_', $key, 2);
+        $module = $module[1];
+        $acct_authmap = user_get_authmaps($value);
+        $this->assertEqual($acct_authmap[$module], $value, t('The authmap %value for module %module is present for the user.', array('%value' => $value, '%module' => $module)));
+      }
+    }
+    else {
+      foreach ($authmaps as $key => $value) {
+        $acct_authmap = user_get_authmaps($value);
+        $this->assertFalse($acct_authmap, t('The authmap %value is not present for the user.', array('%value' => $value)));
+      }
+    }
+  }
+}
-- 
1.7.0.4

