You are here

public function SimpleLdapUserDeleteUserTestCase::testDeleteUser in Simple LDAP 7

Same name and namespace in other branches
  1. 7.2 simple_ldap_user/simple_ldap_user.test \SimpleLdapUserDeleteUserTestCase::testDeleteUser()

Tests user deletion.

File

simple_ldap_user/simple_ldap_user.test, line 1105
Tests for Simple LDAP User module.

Class

SimpleLdapUserDeleteUserTestCase

Code

public function testDeleteUser() {

  // Disable email verification and admin approval.
  variable_set('user_email_verification', FALSE);
  variable_set('user_register', USER_REGISTER_VISITORS);

  // Make sure the system is set to delete from LDAP
  variable_set('simple_ldap_user_delete_from_ldap', 1);

  // Generate user registration formdata.
  $edit['name'] = $name = $this
    ->randomName();
  $edit['mail'] = $mail = $name . '@example.com';
  $edit['pass[pass1]'] = $pass = $this
    ->randomName();
  $edit['pass[pass2]'] = $pass;

  // Fill in required fields from the user attribute map.
  $server = SimpleLdapServer::singleton();
  $objectclass = simple_ldap_user_variable_get('simple_ldap_user_objectclass');
  $must = array();
  foreach ($objectclass as $o) {
    foreach ($server->schema
      ->must($o, TRUE) as $m) {
      if (!in_array($m, $must)) {
        $must[] = strtolower($m);
      }
    }
  }
  $mapObject = SimpleLdapUserMap::singleton();
  foreach ($mapObject->map as $attribute) {
    if (in_array($attribute['ldap'], $must)) {
      if (count($attribute['drupal']) > 1) {
        continue;
      }
      $drupal_attribute = reset($attribute['drupal']);
      if (substr($drupal_attribute, 0, 1) == '#') {
        $drupal_attribute = substr($drupal_attribute, 1);
      }
      $attributetype = $server->schema
        ->get('attributetypes', $attribute['ldap']);

      // A syntax type of 1.3.6.1.4.1.1466.115.121.1.27 is an integer.
      if (isset($attributetype['syntax']) && $attributetype['syntax'] == '1.3.6.1.4.1.1466.115.121.1.27') {
        $edit[$drupal_attribute . '[und][0][value]'] = rand(1000, 65535);
      }
      else {
        $edit[$drupal_attribute . '[und][0][value]'] = $this
          ->randomName();
      }
    }
  }

  // Submit the registration form to create the user.
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  $accounts = user_load_multiple(array(), array(
    'name' => $name,
    'mail' => $mail,
  ));
  $drupal_user = reset($accounts);
  $this
    ->assertText(t('Registration successful. You are now logged in.'), t('Users are logged in after registering.'));
  $this
    ->drupalLogout();

  // Verify that the user exists in LDAP.
  $ldap_user = new SimpleLdapUser($name);
  $this
    ->assertTrue($ldap_user->exists, t('The user was created in LDAP.'));

  // Log in as user1.
  $this
    ->drupalUser1Login();

  // Submit the delete-user form.
  $edit = array();
  $edit['user_cancel_method'] = 'user_cancel_delete';
  $this
    ->drupalPost('user/' . $drupal_user->uid . '/cancel', $edit, t('Cancel account'));
  $this
    ->assertText(t('@name has been deleted.', array(
    '@name' => $name,
  )), t('The user was deleted from Drupal.'));

  // Verify that the user was deleted from LDAP.
  $ldap_user = new SimpleLdapUser($name);
  $this
    ->assertFalse($ldap_user->exists, t('The user was deleted from LDAP.'));
}