From 5af8633ca70f22ee6e081d6ed054daad3bd3de64 Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <bfroehle@math.berkeley.edu>
Date: Thu, 28 Oct 2010 11:12:35 -0700
Subject: [PATCH] user.test: Test getting and setting authmaps.

---
 modules/user/user.test |   68 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)

diff --git modules/user/user.test modules/user/user.test
index 2d54f70..4059634 100644
--- modules/user/user.test
+++ modules/user/user.test
@@ -1899,3 +1899,71 @@ 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('authname_mappingone' => '540393', 'authname_TWO' => 'http://drupal.simpletest/');
+
+    // 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 is present in the user object.'));
+      }
+    }
+    else {
+      foreach ($authmaps as $key => $value) {
+        $acct_authmap = user_get_authmaps($value);
+        $this->assertEqual($acct_authmap, 0, t('The authmap is not present in the user object.'));
+      }
+    }
+  }
+}
-- 
1.7.3.1

