You are here

public function SimpleLdapRoleChangeDrupalUserRoleTestCase::testChangeDrupalUserRole in Simple LDAP 7

Same name and namespace in other branches
  1. 7.2 simple_ldap_role/simple_ldap_role.test \SimpleLdapRoleChangeDrupalUserRoleTestCase::testChangeDrupalUserRole()

Changes a user's drupal roles.

File

simple_ldap_role/simple_ldap_role.test, line 274
Tests for Simple LDAP Role module.

Class

SimpleLdapRoleChangeDrupalUserRoleTestCase

Code

public function testChangeDrupalUserRole() {

  // Load configuration variables.
  $user_attribute_name = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
  $role_attribute_name = simple_ldap_role_variable_get('simple_ldap_role_attribute_name');
  $role_attribute_member = simple_ldap_role_variable_get('simple_ldap_role_attribute_member');
  $role_attribute_member_format = simple_ldap_role_variable_get('simple_ldap_role_attribute_member_format');

  // Get the member attribute string to use as the comparison.
  switch ($role_attribute_member_format) {
    case 'name':
      $member = $this->ldapUser[1]->{$user_attribute_name}[0];
      break;
    case 'dn':
    default:
      $member = $this->ldapUser[1]->dn;
  }

  // Initialize a user account object.
  $account = new stdClass();
  $account->name = $this->ldapUser[1]->{$user_attribute_name}[0];
  $account->pass_raw = $this->userPassword[1];

  // Log in with the test user. This should create/sync an LDAP user.
  $this
    ->drupalLogin($account);

  // Run cron to sync the LDAP groups to Drupal roles.
  $this
    ->cronRun();

  // Load the Drupal user.
  $drupal_user = user_load_by_name($this->ldapUser[1]->{$user_attribute_name}[0]);
  $this
    ->assertNotIdentical($drupal_user, FALSE, t(':name user exists in Drupal', array(
    ':name' => $this->ldapUser[1]->{$user_attribute_name}[0],
  )));

  // Load the Drupal role.
  $drupal_role = user_role_load_by_name($this->ldapGroup->{$role_attribute_name}[0]);
  $this
    ->assertNotIdentical($drupal_role, FALSE, t(':name role exists in Drupal', array(
    ':name' => $this->ldapGroup->{$role_attribute_name}[0],
  )));

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

  // Populate the user edit form.
  $edit = array(
    'roles[' . $drupal_role->rid . ']' => TRUE,
  );

  // Submit the user edit form.
  $this
    ->drupalPost('user/' . $drupal_user->uid . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), t('The changes have been saved.'));

  // Reload the LDAP group.
  $ldap_group = new SimpleLdapRole($drupal_role->name);
  $members = $ldap_group->{$role_attribute_member};

  // Verify that the LDAP user is now a member of the LDAP group.
  $this
    ->assertTrue(in_array($member, $members, TRUE), t(':member is a member of the :group LDAP group', array(
    ':member' => $member,
    ':group' => $ldap_group->dn,
  )));

  // Repopulate the user edit form.
  $edit = array(
    'roles[' . $drupal_role->rid . ']' => FALSE,
  );

  // Submit the user edit form.
  $this
    ->drupalPost('user/' . $drupal_user->uid . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), t('The changes have been saved.'));

  // Reload the LDAP group.
  $ldap_group = new SimpleLdapRole($drupal_role->name);
  $members = $ldap_group->{$role_attribute_member};

  // Verify that the LDAP user is no longer a member of the LDAP group.
  $this
    ->assertFalse(in_array($member, $members, TRUE), t(':member was removed from the :group LDAP group', array(
    ':member' => $member,
    ':group' => $ldap_group->dn,
  )));

  // Repopulate the user edit form again.
  $edit = array(
    'roles[' . $drupal_role->rid . ']' => TRUE,
  );

  // Submit the user edit form.
  $this
    ->drupalPost('user/' . $drupal_user->uid . '/edit', $edit, t('Save'));
  $this
    ->assertText(t('The changes have been saved.'), t('The changes have been saved.'));

  // Reload the LDAP group.
  $ldap_group = new SimpleLdapRole($drupal_role->name);
  $members = $ldap_group->{$role_attribute_member};

  // Verify that the LDAP user is a member of the LDAP group again.
  $this
    ->assertTrue(in_array($member, $members, TRUE), t(':member is a member of the :group LDAP group', array(
    ':member' => $member,
    ':group' => $ldap_group->dn,
  )));
}