You are here

public function SimpleLdapRoleCreateDrupalRoleTestCase::testCreateRole in Simple LDAP 7

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

Creates a drupal role, and verifies LDAP.

File

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

Class

SimpleLdapRoleCreateDrupalRoleTestCase

Code

public function testCreateRole() {

  // Get module configurations.
  $objectclass = simple_ldap_role_variable_get('simple_ldap_role_objectclass');
  $attribute_member = simple_ldap_role_variable_get('simple_ldap_role_attribute_member');
  $attribute_member_default = simple_ldap_role_variable_get('simple_ldap_role_attribute_member_default');

  // Generate a randome group name.
  $name = $this
    ->randomName();

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

  // Submit the form to add a new Drupal role.
  $edit = array(
    'name' => $name,
  );
  $this
    ->drupalPost('admin/people/permissions/roles', $edit, t('Add role'));
  $this
    ->assertText(t('The role has been added'), t('The @role role was added to Drupal', array(
    '@role' => $name,
  )));

  // Load the LDAP group.
  $ldap_group = new SimpleLdapRole($name);
  $members = $ldap_group->{$attribute_member};

  // Get a list of required attributes for the configured 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);
      }
    }
  }

  // The LDAP group should have only been created if either there is a default
  // member configured, or the member attribute is not required (allowing an
  // empty group).
  if (!in_array($attribute_member, $must)) {

    // Test that the LDAP entry was created.
    $this
      ->assertTrue($ldap_group->exists, t(':dn is present in the LDAP directory', array(
      ':dn' => $ldap_group->dn,
    )));
    if (!empty($attribute_member_default)) {

      // Test that the default user was added to the LDAP group.
      $this
        ->assertTrue(in_array($attribute_member_default, $members, TRUE), t('The default user, :default, was added as a member of :dn', array(
        ':default' => $attribute_member_default,
        ':dn' => $ldap_group->dn,
      )));
    }
  }
  else {

    // The member attribute is required, whether the group was created depends
    // on if there is a default group configured.
    if (!empty($attribute_member_default)) {

      // Test that the group was created, and the default user was added.
      $this
        ->assertTrue($ldap_group->exists, t(':dn is present in the LDAP directory', array(
        ':dn' => $ldap_group->dn,
      )));
      $this
        ->assertTrue(in_array($attribute_member_default, $members, TRUE), t('The default user, :default, was added as a member of :dn', array(
        ':default' => $attribute_member_default,
        ':dn' => $ldap_group->dn,
      )));
    }
    else {

      // Test that the group was not created.
      $this
        ->assertFalse($ldap_group->exists, t(':dn is not present in the LDAP directory', array(
        ':dn' => $ldap_group->dn,
      )));
    }
  }

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