You are here

function LdapUserIntegrationTests::testDrupalAccountsOrphaned in Lightweight Directory Access Protocol (LDAP) 8.2

Same name and namespace in other branches
  1. 7.2 ldap_user/tests/ldap_user.test \LdapUserIntegrationTests::testDrupalAccountsOrphaned()

test cron function for dealing with ldap associated users who no longer have ldap entries

  • fix search in fake server to deal with general or queries

simpletest approach:

  • loop through all options for user_cancel ldap_user_orphan_email user_cancel_block, user_cancel_block_unpublish, user_cancel_reassign, user_cancel_delete

    • automatically generate 70 ldap users with cns hpotter1-hpotter300
    • create 75 corresponding drupal uses that are ldap identified
    • delete 10 of the ldap entries
    • run cron
    • test for drupal accounts being dealt with correctly and or email sent

File

ldap_user/tests/ldap_user.test, line 1027

Class

LdapUserIntegrationTests

Code

function testDrupalAccountsOrphaned() {

  // just to give warning if setup doesn't succeed.  may want to take these out at some point.
  $setup_success = module_exists('ldap_user') && module_exists('ldap_servers') && config('ldap_test.settings')
    ->get('simpletest') > 0;
  $this
    ->assertTrue($setup_success, ' ldap_user setup successful', $this
    ->testId('orphaned entries tests'));
  $sids = array(
    'activedirectory1',
  );
  $this
    ->prepTestData('hogwarts', $sids, 'provisionToDrupal', 'default');
  $ldap_user_conf = ldap_user_conf('admin');
  $drupal_form = $ldap_user_conf
    ->drupalForm();
  $account_options = $drupal_form['basic_to_drupal']['orphanedDrupalAcctBehavior']['#options'];
  $cn_to_account = array();
  $ldap_server = ldap_servers_get_servers('activedirectory1', NULL, TRUE, TRUE);

  // debug("ldap_server"); debug(is_object($ldap_server));
  foreach ($account_options as $account_option => $account_option_text) {

    // debug("$account_option - $account_option_text");
    $sids = array(
      'activedirectory1',
    );
    $this
      ->prepTestData('hogwarts', $sids, 'provisionToDrupal', 'default');
    $ldap_user_conf->orphanedDrupalAcctBehavior = $account_option;
    $ldap_user_conf
      ->save();
    $test_id = "ldap_user.orphans.{$account_option}";
    $test_text = "Test of orphaned Drupal account option: {$account_option_text}";
    $success = FALSE;

    // create 70 drupal accounts (clone0 to clone69) based on corresponding ldap entries
    $first_clone_username = 'clone0';
    $last_clone_username = 'clone' . (LDAP_TEST_USER_ORPHAN_CLONE_COUNT - 1);
    for ($i = 0; $i < LDAP_TEST_USER_ORPHAN_CLONE_COUNT; $i++) {

      // 70
      $name = "clone" . $i;

      //debug("create clone $name, activedirectory1");
      $account = $this
        ->createLdapIdentifiedDrupalAccount($ldap_user_conf, $name, 'activedirectory1');
      $cn_to_account[$name] = $account;

      //debug("new account"); debug($account);
    }

    //  debug($cn_to_account['clone0']);
    //  debug($cn_to_account[$last_clone_username]);
    // delete 10 ldap entries
    $clone_first_uid = $cn_to_account[$first_clone_username]->uid;
    $clone_last_uid = $cn_to_account[$last_clone_username]->uid;
    $clone_first = user_load($clone_first_uid, TRUE);
    $clone_last = user_load($clone_last_uid, TRUE);

    //debug("pre ldap delete, clone0 and cloneN $first_clone_username and $last_clone_username"); debug($clone_first);debug($clone_last); //debug($ldap_server->entries);
    $delete = LDAP_TEST_USER_ORPHAN_CLONE_COUNT - LDAP_TEST_USER_ORPHAN_CLONE_REMOVE_COUNT;
    for ($i = 0; $i < $delete; $i++) {
      $name = "clone" . $i;
      $account = $cn_to_account[$name];

      //debug("delete ldap entry: ". $account->ldap_user_current_dn['und'][0]['value']);

      //  ?? is it possible the ldap delete hook is causing the drupal user to get populated with empty values?
      $ldap_server
        ->delete($account->ldap_user_current_dn['und'][0]['value']);
    }
    $clone_first = user_load($clone_first_uid, TRUE);
    $clone_last = user_load($clone_last_uid, TRUE);

    //debug("post ldap delete and pre cron, clone0 and cloneN"); debug($clone_first->status);debug($clone_last->status);// debug($ldap_server->entries);
    drupal_cron_run();
    $clone_first = user_load($clone_first_uid, TRUE);
    $clone_last = user_load($clone_last_uid, TRUE);

    //debug("post cron, clone0 and cloneN"); debug($clone_first->status);debug($clone_last->status); //debug($ldap_server->entries);
    switch ($account_option) {
      case 'ldap_user_orphan_do_not_check':
        $test_uids = array();
        for ($i = 0; $i < LDAP_TEST_USER_ORPHAN_CLONE_COUNT; $i++) {

          // 70
          $name = "clone" . $i;
          $test_uids[] = @$cn_to_account[$name]->uid;

          //debug($account);
        }
        $success = TRUE;
        $accounts = user_load_multiple($test_uids);

        // debug("accounts for $test_id"); debug($accounts);
        foreach ($accounts as $uid => $account) {
          if ($account->status != 1) {
            $success = FALSE;
            break;
          }
        }
        if ($success) {
          $success = $clone_last && $clone_last->status == 1;
          if (!$success) {

            // debug("success = $success, status=" . $clone_last->status);
          }
        }
        break;
      case 'ldap_user_orphan_email':

        // debug('ldap_user_orphan_email');
        // test is if email has 10 users and was sent
        $emails = $this
          ->drupalGetMails();
        if (count($emails)) {
          $email_body = $emails[count($emails) - 1]['body'];

          // most recent email is the one of interest
          $success = strpos($email_body, "The following {$delete} Drupal users") !== FALSE;
        }
        else {
          $success = FALSE;
        }
        break;
      case 'user_cancel_block':
      case 'user_cancel_block_unpublish':

        //debug('user_cancel_block');

        // test is if clone0-clone9 have a status of 0
        // and clone12,11... have a status of 1
        $test_uids = array();
        for ($i = 0; $i < $delete; $i++) {

          // 70
          $name = "clone" . $i;
          $test_uids[] = @$cn_to_account[$name]->uid;
        }
        $success = TRUE;
        $accounts = user_load_multiple($test_uids);
        foreach ($accounts as $uid => $account) {
          if ($account->status != 0) {
            $success = FALSE;
            break;
          }
        }
        if ($success) {
          $clone_last = user_load($clone_last_uid, TRUE);
          $success = $clone_last && $clone_last->status == 1;
        }
        break;
      case 'user_cancel_reassign':
      case 'user_cancel_delete':

        // test is if clone0-clone9 are deleted
        // and clone12,11... have a status of 1
        $test_uids = array();
        for ($i = 0; $i < $delete; $i++) {

          // 70
          $name = "clone" . $i;
          $test_uids[] = @$cn_to_account[$name]->uid;

          //debug($account);
        }
        $success = TRUE;
        $accounts = user_load_multiple($test_uids);
        $success = count($accounts) == LDAP_TEST_USER_ORPHAN_CLONE_COUNT;
        if ($success) {
          $clone_last = user_load($clone_last_uid, TRUE);
          $success = $clone_last && $clone_last->status == 1;
        }
        break;
    }
    $this
      ->assertTrue($success, $test_id, $test_text);

    // remove all drupal users except 1 for next test
    foreach ($cn_to_account as $cn => $account) {
      @user_delete($account->uid);
    }
  }
}