You are here

public function SimpleLdapRoleChangeLdapGroupMembership::testChangeLdapGroupMembership in Simple LDAP 7

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

Test changing group membership directly in LDAP.

File

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

Class

SimpleLdapRoleChangeLdapGroupMembership

Code

public function testChangeLdapGroupMembership() {

  // Load configuration variables.
  $user_attribute_name = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
  $role_basedn = simple_ldap_role_variable_get('simple_ldap_role_basedn');
  $role_objectclass = simple_ldap_role_variable_get('simple_ldap_role_objectclass');
  $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');

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

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

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

  // Verify that the user is assigned to the test role.
  $this
    ->assertTrue(in_array($this->ldapGroup->{$role_attribute_name}[0], $drupal_user->roles), t(':user is assigned to the :role Drupal role.', array(
    ':user' => $drupal_user->name,
    ':role' => $this->ldapGroup->{$role_attribute_name}[0],
  )));

  // Remove the user from the LDAP group.
  $this->ldapGroup
    ->deleteUser($this->ldapUser[0]);
  $this->ldapGroup
    ->save();

  // Reload the drupal user.
  $drupal_user = user_load_multiple(array(), array(
    'name' => $this->ldapUser[0]->{$user_attribute_name}[0],
  ), TRUE);
  $drupal_user = reset($drupal_user);

  // Verify that the user is not assigned to the test role.
  $this
    ->assertFalse(in_array($this->ldapGroup->{$role_attribute_name}[0], $drupal_user->roles), t(':user is no longer assigned to the :role Drupal role.', array(
    ':user' => $drupal_user->name,
    ':role' => $this->ldapGroup->{$role_attribute_name}[0],
  )));

  // Re-add the user to the LDAP group.
  $this->ldapGroup
    ->addUser($this->ldapUser[0]);
  $this->ldapGroup
    ->save();

  // Reload the drupal user.
  $drupal_user = user_load_multiple(array(), array(
    'name' => $this->ldapUser[0]->{$user_attribute_name}[0],
  ), TRUE);
  $drupal_user = reset($drupal_user);

  // Verify that the user is assigned to the test role.
  $this
    ->assertTrue(in_array($this->ldapGroup->{$role_attribute_name}[0], $drupal_user->roles), t(':user is assigned to the :role Drupal role.', array(
    ':user' => $drupal_user->name,
    ':role' => $this->ldapGroup->{$role_attribute_name}[0],
  )));
}