You are here

public function SimpleLdapUserRegistrationTestCase::testRegistrationValues in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_user/simple_ldap_user.test \SimpleLdapUserRegistrationTestCase::testRegistrationValues()

Tests that the values used to register are properly saved.

File

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

Class

SimpleLdapUserRegistrationTestCase

Code

public function testRegistrationValues() {

  // Get simple_ldap_user config.
  $objectclass = simple_ldap_user_variable_get('simple_ldap_user_objectclass');
  $attribute_name = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
  $attribute_mail = simple_ldap_user_variable_get('simple_ldap_user_attribute_mail');
  $attribute_map = simple_ldap_user_variable_get('simple_ldap_user_attribute_map');

  // Get a list of required LDAP attributes for the user objectclass.
  $server = SimpleLdapServer::singleton();
  $must = array();
  foreach ($objectclass as $o) {
    foreach ($server->schema
      ->must($o, TRUE) as $m) {
      if (!in_array($m, $must)) {
        $must[] = strtolower($m);
      }
    }
  }

  // Allow registration by site visitors without administrator approval.
  variable_set('user_register', USER_REGISTER_VISITORS);

  // Don't require e-mail verification.
  variable_set('user_email_verification', FALSE);

  // Set the default timezone to Brussels.
  variable_set('configurable_timezones', 1);
  variable_set('date_default_timezone', 'Europe/Brussels');

  // Verify that the required mapped fields show up on the registration form.
  $this
    ->drupalGet('user/register');
  foreach ($attribute_map as $field) {
    if (count($field['drupal']) > 1) {
      continue;
    }
    $drupal_attribute = reset($field['drupal']);
    $type = substr($drupal_attribute, 0, 1);
    if (in_array($field['ldap'], $must) && $type == '#') {
      $drupal_attribute = substr($drupal_attribute, 1);
      $this
        ->assertText($drupal_attribute, t(':field appears on the user registration form', array(
        ':field' => $drupal_attribute,
      )));
    }
  }

  // Submit registration form.
  $edit = $this
    ->formData($name, $mail);
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomName();
  $edit['pass[pass2]'] = $new_pass;
  $this
    ->drupalPost(NULL, $edit, t('Create new account'));

  // Load the Drupal user.
  $accounts = user_load_multiple(array(), array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);

  // Load the LDAP user.
  $ldap_user = new SimpleLdapUser($name);

  // Check the LDAP objectclass(es).
  foreach ($ldap_user->objectclass as $o) {
    $this
      ->assertTrue(in_array($o, $objectclass), t('The LDAP entry has the :o objectclass', array(
      ':o' => $o,
    )));
  }

  // Check user fields.
  $this
    ->assertEqual($new_user->name, $name, t('Drupal username matches.'));
  $this
    ->assertEqual($ldap_user->{$attribute_name}[0], $name, t('LDAP username matches.'));
  $this
    ->assertEqual($new_user->mail, $mail, t('Drupal e-mail address matches.'));
  $this
    ->assertEqual($ldap_user->{$attribute_mail}[0], $mail, t('LDAP e-mail address matches.'));
  foreach ($attribute_map as $field) {
    if (count($field['drupal']) > 1) {
      continue;
    }
    $drupal_attribute = reset($field['drupal']);
    $type = substr($drupal_attribute, 0, 1);
    if (in_array($field['ldap'], $must) && $type == '#') {
      $drupal_attribute = substr($drupal_attribute, 1);
      $this
        ->assertEqual($new_user->{$drupal_attribute}[LANGUAGE_NONE][0]['value'], $edit[$drupal_attribute . '[und][0][value]'], t('The :field Drupal field value was correctly saved.', array(
        ':field' => $drupal_attribute,
      )));
      $this
        ->assertEqual($ldap_user->{$field['ldap']}[0], $edit[$drupal_attribute . '[und][0][value]'], t('The :field LDAP attribute was correctly saved.', array(
        ':field' => $field['ldap'],
      )));
    }
  }

  // Cleanup.
  $ldap_user
    ->delete();
}