You are here

function LdapAuthorizationOg1Tests::testBasicFunctionsAndApi in Lightweight Directory Access Protocol (LDAP) 8.2

just make sure install succeeds and og and ldap_authorization_og functions work as designed

File

ldap_authorization/tests/Og1Tests.test, line 149

Class

LdapAuthorizationOg1Tests

Code

function testBasicFunctionsAndApi() {
  $this->ldapTestId = $this->module_name . ': setup success';

  // just to give warning if setup doesn't succeed.  may want to take these out at some point.
  $setup_success = module_exists('ldap_authentication') && module_exists('ldap_servers') && module_exists('ldap_authorization') && module_exists('ldap_authorization_og') && config('ldap_test.settings')
    ->get('simpletest') == 2;
  $this
    ->assertTrue($setup_success, ' ldap_authorizations og setup successful', $this->ldapTestId);
  $this->ldapTestId = $this->module_name . ': cron test';
  $this
    ->assertTrue(drupal_cron_run(), t('Cron can run with ldap authorization og enabled.'), $this->ldapTestId);
  $this
    ->assertTrue(ldap_authorization_og_authorization_id(2, 3) == "2-3", t('ldap_authorization_og_authorization_id() function works.'), $this->ldapTestId);
  $this
    ->assertTrue(ldap_authorization_og_og_version() == 1, t('ldap_authorization_og_og_version() is 1'), $this->ldapTestId);

  /***
   * I. some basic tests to make sure og module's apis are working before testing ldap_authorization_og
   */
  $web_user = $this
    ->drupalCreateUser();
  $this->ldapTestId = $this->module_name . ': og functions';
  list($og_gryffindor_group, $og_gryffindor_node) = ldap_authorization_og1_get_group('gryffindor', 'group_name');
  list($og_students_group, $og_students_node) = ldap_authorization_og1_get_group('students', 'group_name');
  list($og_faculty_group, $og_faculty_node) = ldap_authorization_og1_get_group('faculty', 'group_name');
  list($og_users_group, $og_users_node) = ldap_authorization_og1_get_group('users', 'group_name');

  // debug('og_gryffindor_node');debug($og_gryffindor_node->nid); debug($og_gryffindor_group->gid);
  //  debug('og_students_node');debug($og_students_node->nid); debug($og_students_group->gid);
  //  debug('og_faculty_node');debug($og_faculty_node->nid); debug($og_faculty_group->gid);
  //   debug('og_users_node');debug($og_users_node->nid); debug($og_users_group->gid);
  $anonymous_rid = ldap_authorization_og_rid_from_role_name(OG_ANONYMOUS_ROLE);
  $member_rid = ldap_authorization_og_rid_from_role_name(OG_AUTHENTICATED_ROLE);
  $admin_rid = ldap_authorization_og_rid_from_role_name(OG_ADMINISTRATOR_ROLE);
  $dungeon_master_rid = ldap_authorization_og_rid_from_role_name('dungeon-master');
  $time_keeper = ldap_authorization_og_rid_from_role_name('time-keeper');
  $student_member_consumer_id = $og_students_group->gid . '-' . $member_rid;
  $gid = ldap_authorization_og1_entity_id_to_gid($og_gryffindor_node->nid);
  $this
    ->assertTrue($gid == $og_gryffindor_group->gid, t('ldap_authorization_og1_entity_id_to_gid() functions'), $this->ldapTestId);

  //   debug("anonymous_rid=$anonymous_rid, member_rid=$member_rid,admin_rid=$admin_rid, dungeon_master_rid=$dungeon_master_rid, time_keeper=$time_keeper");

  /**
   *   @todo api tests: ldap_authorization_og_rid_from_role_name(),
   *   and ldap_authorization_og_get_group()
   */
  $ids = array(
    $web_user->uid,
  );

  //  debug($ids);
  $user_entities = entity_load('user', $ids, array(), TRUE);
  $user_entity = $user_entities[$web_user->uid];

  ///***

  // * II. construct ldapauthorization og object and test methods (ignoring if ldap created or not...
  // * unit tests for methods and class without any ldap context.)
  // */
  $this->ldapTestId = $this->module_name . ': LdapAuthorizationConsumerOG class';
  $og_auth = new LdapAuthorizationConsumerOG('og_group');
  $this
    ->assertTrue(is_object($og_auth), 'Successfully instantiated LdapAuthorizationConsumerOG', $this->ldapTestId);

  /**
   * test basic functions with admin user logged in
   */
  $ldap_entry = NULL;
  $user_data = array();
  $user = user_load($web_user->uid, TRUE);
  $this
    ->assertFalse($og_auth
    ->hasAuthorization($user, $student_member_consumer_id), 'hasAuthorization() method works for non LDAP provisioned og authorization', $this->ldapTestId);
  $user_auth_data = array();
  $consumers = array(
    $student_member_consumer_id => $og_auth->emptyConsumer,
  );
  $og_auth
    ->authorizationGrant($user, $user_auth_data, $consumers, $ldap_entry, TRUE);
  $this
    ->assertTrue(ldap_authorization_og1_has_membership($og_students_group->gid, $user->uid), 'authorizationGrant function works', $this->ldapTestId);
  $this
    ->assertTrue(ldap_authorization_og1_has_role($og_students_group->gid, $user->uid, $member_rid), 'ldap_authorization_og1_has_role function works', $this->ldapTestId);
  $this
    ->assertTrue("member" == ldap_authorization_og1_role_name_from_rid($member_rid), 'ldap_authorization_og1_role_name_from_rid function works', $this->ldapTestId);
  $this
    ->assertTrue($member_rid == ldap_authorization_og1_role_name_to_role_id("member"), 'ldap_authorization_og1_role_name_to_role_id function works', $this->ldapTestId);
  $has_student_membership = ldap_authorization_og1_has_membership($og_students_group->gid, $user->uid);
  $this
    ->assertTrue($has_student_membership, 'ldap_authorization_og1_has_membership function', $this->ldapTestId);
  if ($has_student_membership) {
    $og_auth
      ->authorizationRevoke($user, $user_auth_data, $consumers, $ldap_entry, TRUE);
    $this
      ->assertFalse(ldap_authorization_og1_has_membership($og_students_group->gid, $user->uid), 'authorizationRevoke function works', $this->ldapTestId);
  }
  $parts = $og_auth
    ->og1ConsumerIdParts("3-2");
  $this
    ->assertTrue($parts[0] == 3 && $parts[1] == 2, 'LdapAuthorizationOgConsumerOG::og1ConsumerIdParts method works', $this->ldapTestId);
  $mappings = array(
    array(
      'cn=students,ou=groups,dc=hogwarts,dc=edu',
      'group-name=students,role-name=member',
    ),
    array(
      'cn=faculty,ou=groups,dc=hogwarts,dc=edu',
      'group-name=faculty,role-name=member',
    ),
    array(
      'cn=gryffindor,ou=groups,dc=hogwarts,dc=edu',
      'group-name=gryffindor,role-name=member',
    ),
    array(
      'cn=users,ou=groups,dc=hogwarts,dc=edu',
      'group-name=users,role-name=dungeon-master',
    ),
    array(
      'cn=users,ou=groups,dc=hogwarts,dc=edu',
      'gid=2,rid=2',
    ),
    array(
      'cn=users,ou=groups,dc=hogwarts,dc=edu',
      'gid=2,rid=4',
    ),
  );
  $normalized_mappings = $og_auth
    ->normalizeMappings($mappings);
  $correct_mappings = $normalized_mappings[0]['normalized'] = ldap_authorization_og_authorization_id($og_students_group->gid, $member_rid) && ($normalized_mappings[1]['normalized'] = ldap_authorization_og_authorization_id($og_faculty_group->gid, $member_rid) && ($normalized_mappings[2]['normalized'] = ldap_authorization_og_authorization_id($og_gryffindor_group->gid, $dungeon_master_rid) && ($normalized_mappings[3]['normalized'] = ldap_authorization_og_authorization_id($og_users_group->gid, $member_rid) && ($normalized_mappings[4]['normalized'] = ldap_authorization_og_authorization_id($og_students_group->gid, $member_rid) && ($normalized_mappings[4]['normalized'] = ldap_authorization_og_authorization_id($og_students_group->gid, 4))))));
  if (!$correct_mappings) {
    debug('normalized_mappings');
    debug($normalized_mappings);
  }
  $this
    ->assertTrue($correct_mappings, 'normalizeMappings method works', $this->ldapTestId);
}