You are here

function CasUserAdminTestCase::testCASUserHooks in CAS 7

Same name and namespace in other branches
  1. 6.3 cas.test \CasUserAdminTestCase::testCASUserHooks()

Registers, modifies, and deletes a CAS user using User API hooks.

File

./cas.test, line 246
Tests for cas.module.

Class

CasUserAdminTestCase

Code

function testCASUserHooks() {

  // Create a test account.
  $account = $this
    ->drupalCreateUser();
  $uid = $account->uid;

  // Add a CAS username.
  $cas_name = $this
    ->randomName();
  $edit = array(
    'cas_name' => $cas_name,
  );
  $account = user_save($account, $edit);
  $this
    ->assertEqual($cas_name, $account->cas_name, t('CAS username %cas_name successfully created.', array(
    '%cas_name' => $cas_name,
  )));

  // Reload the account and ensure the CAS name is still present.
  $account = user_load($uid);
  $this
    ->assertEqual($cas_name, $account->cas_name, t('CAS username %cas_name successfully saved.', array(
    '%cas_name' => $cas_name,
  )));

  // Load the account by the CAS username.
  $account = cas_user_load_by_name($cas_name);
  $this
    ->assertEqual($uid, $account->uid, t('Loaded the correct account with CAS username %cas_name.', array(
    '%cas_name' => $cas_name,
  )));

  // Change the CAS username.
  $cas_new_name = $this
    ->randomName();
  $account = user_load($uid);
  $edit = array(
    'cas_name' => $cas_new_name,
  );
  user_save($account, $edit);
  $account = user_load($uid);
  $this
    ->assertEqual($cas_new_name, $account->cas_name, t('CAS username %cas_name successfully updated.', array(
    '%cas_name' => $cas_new_name,
  )));
  $this
    ->assertEqual(count($account->cas_names), 1, t('Only one CAS username is present.'));
  $account = cas_user_load_by_name($cas_name);
  $this
    ->assertFalse($account, t('Could not load account using old CAS username.'));

  // Remove the CAS username.
  $account = user_load($uid);
  $edit = array(
    'cas_name' => NULL,
  );
  user_save($account, $edit);
  $account = user_load($uid);
  $this
    ->assertFalse($account->cas_name, t('CAS username successfully deleted.'));
  $this
    ->assertEqual(count($account->cas_names), 0, t('No CAS usernames are present.'));

  // Attempt to load by a non-existent CAS username.
  $account = cas_user_load_by_name($cas_new_name);
  $this
    ->assertFalse($account, t('Could not load account with non-existent CAS username.'));

  // Verify that all CAS usernames have been removed from {cas_user}.
  $cas_uid_count = db_select('cas_user')
    ->condition('cas_name', array(
    $cas_name,
    $cas_new_name,
  ), 'IN')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($cas_uid_count, 0, t('CAS usernames successfully removed from {cas_user}.'));
}