View source
<?php
require_once drupal_get_path('module', 'ldap_authorization') . '/tests/LdapAuthorizationTestCase.class.php';
require_once drupal_get_path('module', 'ldap_authorization_og') . '/LdapAuthorizationConsumerOG.class.php';
class LdapAuthorizationOg2Tests extends LdapAuthorizationTestCase {
public $group_type = NULL;
public $group_content_type = NULL;
public $group_nodes = array();
public $user1;
public static function getInfo() {
return array(
'group' => 'LDAP Authorization',
'name' => 'OG 7.x-2.x Tests.',
'description' => 'Test ldap authorization og 2.',
);
}
public $consumerType = 'og_group';
public function setUp($addl_modules = array()) {
parent::setUp(array());
}
function brokensetUp($addl_modules = array()) {
parent::setUp(array(
'ldap_authorization_og',
'og_example',
));
if (ldap_authorization_og_og_version() != 2) {
debug('LdapAuthorizationOg2Tests must be run with OG 7.x-2.x');
return;
}
return;
$this->user1 = $this
->drupalCreateUser();
$this->groups = array();
require 'ldap_authorization_og2.inc';
foreach ($og_roles as $og_role_name => $og_role) {
$role = new stdClass();
$role->gid = 0;
$role->group_type = $og_role['entity_type'];
$role->group_bundle = $og_role['bundle_type'];
$role->name = $og_role_name;
$status = og_role_save($role);
}
$this->group_type = $this
->drupalCreateContentType()->type;
og_create_field(OG_GROUP_FIELD, 'node', $this->group_type);
$this->group_content_type = $this
->drupalCreateContentType()->type;
og_create_field(OG_AUDIENCE_FIELD, 'node', $this->group_content_type);
foreach ($og_groups as $og_name => $og_conf) {
$label = $og_conf['label'];
if ($og_conf['entity_type'] == 'node') {
$settings = array();
$settings['type'] = $this->group_type;
$settings[OG_GROUP_FIELD][LANGUAGE_NONE][0]['value'] = 1;
$settings['uid'] = $this->user1->uid;
$settings['title'] = $og_conf['label'];
$settings['type'] = $og_conf['bundle'];
$this->group_nodes[$og_name] = $this
->drupalCreateNode($settings);
}
}
}
function testBasicFunctionsAndApi() {
return;
if (ldap_authorization_og_og_version() != 2) {
debug('LdapAuthorizationOg2Tests must be run with OG 7.x-2.x');
return;
}
$this->ldapTestId = $this->module_name . ': setup success';
$setup_success = module_exists('ldap_authentication') && module_exists('ldap_servers') && module_exists('ldap_authorization') && module_exists('ldap_authorization_drupal_role') && module_exists('ldap_authorization_og') && variable_get('ldap_simpletest', 0) == 1;
$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);
$web_user = $this
->drupalCreateUser();
$this->ldapTestId = $this->module_name . ': og functions';
list($og_knitters, $og_knitters_node) = ldap_authorization_og2_get_group('node', 'knitters', 'group_name', 'object');
list($og_bakers, $og_bakers_node) = ldap_authorization_og2_get_group('node', 'bakers', 'group_name', 'object');
list($og_butchers, $og_butchers_node) = ldap_authorization_og2_get_group('node', 'butchers', 'group_name', 'object');
$anonymous_rid = ldap_authorization_og2_rid_from_role_name('node', $og_knitters_node->nid, OG_ANONYMOUS_ROLE);
$member_rid = ldap_authorization_og2_rid_from_role_name('node', $og_bakers_node->nid, OG_AUTHENTICATED_ROLE);
$admin_rid = ldap_authorization_og2_rid_from_role_name('node', $og_butchers_node->nid, OG_ADMINISTRATOR_ROLE);
$values = array(
'entity_type' => 'user',
'entity' => $web_user->uid,
'field_name' => FALSE,
'state' => OG_STATE_ACTIVE,
);
$og_membership = og_group('node', $og_knitters_node->nid, $values);
$og_membership = og_group('node', $og_bakers_node->nid, $values);
$og_membership = og_group('node', $og_butchers_node->nid, $values);
og_role_grant('node', $og_knitters_node->nid, $web_user->uid, $member_rid);
og_role_grant('node', $og_bakers_node->nid, $web_user->uid, $member_rid);
og_role_grant('node', $og_bakers_node->nid, $web_user->uid, $admin_rid);
$web_user = user_load($web_user->uid, TRUE);
$ids = array(
$web_user->uid,
);
$user_entity = entity_load('user', $ids);
$this
->assertTrue(og_is_member('node', $og_knitters_node->nid, 'user', $web_user), 'User is member of Group og_knitters without LDAP (based on og_is_member() function)', $this->ldapTestId);
$this
->assertTrue(ldap_authorization_og2_has_role($og_knitters_node->nid, $web_user->uid, OG_AUTHENTICATED_ROLE), 'User is member of Group og_knitters without LDAP (based on ldap_authorization_og2_has_role() function)', $this->ldapTestId);
$this
->assertTrue(ldap_authorization_og2_has_role($og_bakers_node->nid, $web_user->uid, OG_AUTHENTICATED_ROLE), 'User is member of Group og_bakers without LDAP (based on dap_authorization_og_has_role() function)', $this->ldapTestId);
$this
->assertTrue(ldap_authorization_og2_has_role($og_bakers_node->nid, $web_user->uid, OG_ADMINISTRATOR_ROLE), 'User is administrator member of Group og_bakers without LDAP (based on dap_authorization_og_has_role() function)', $this->ldapTestId);
$this->ldapTestId = $this->module_name . ': LdapAuthorizationConsumerOG class';
$og_auth = new LdapAuthorizationConsumerOG('og_group');
$this
->assertTrue(is_object($og_auth), 'Successfully instantiated LdapAuthorizationConsumerOG', $this->ldapTestId);
$this
->assertTrue($og_auth
->hasAuthorization($web_user, ldap_authorization_og_authorization_id($og_bakers_node->nid, $admin_rid, 'node')), 'hasAuthorization() method works for non LDAP provisioned og authorization', $this->ldapTestId);
$this
->assertTrue($og_auth->consumerType == 'og_group', 'LdapAuthorizationConsumerOG ConsumerType set properly', $this->ldapTestId);
$consumer_ids = $og_auth
->availableConsumerIDs();
$should_haves = array();
require_once drupal_get_path('module', 'ldap_authorization_og') . '/LdapAuthorizationConsumerOG.class.php';
list($groups, $availableConsumerIDs) = LdapAuthorizationConsumerOG::og2Groups();
foreach ($groups['node'] as $gid => $group) {
foreach ($group['roles'] as $rid => $role) {
$should_haves[] = ldap_authorization_og_authorization_id($gid, $rid, 'node');
}
}
$match = (bool) (count(array_intersect($consumer_ids, $should_haves)) == count($should_haves));
$this
->assertTrue($match, 'LdapAuthorizationConsumerOG availableConsumerIDs()', $this->ldapTestId);
$should_haves = array(
ldap_authorization_og_authorization_id($og_knitters_node->nid, $member_rid, 'node'),
ldap_authorization_og_authorization_id($og_bakers_node->nid, $member_rid, 'node'),
ldap_authorization_og_authorization_id($og_bakers_node->nid, $admin_rid, 'node'),
ldap_authorization_og_authorization_id($og_butchers_node->nid, $member_rid, 'node'),
);
$web_user_authorizations = $og_auth
->usersAuthorizations($web_user);
$match = (bool) (count(array_intersect($web_user_authorizations, $should_haves)) == count($should_haves));
$this
->assertTrue($match, 'LdapAuthorizationConsumerOG usersAuthorizations()', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$baker_member_id = ldap_authorization_og_authorization_id($og_bakers_node->nid, $member_rid, 'node');
$og_auth
->authorizationRevoke($web_user, $web_user->data['ldap_authorizations']['og_groups'], array(
$baker_member_id,
), NULL, TRUE);
$web_user_authorizations = $og_auth
->usersAuthorizations($web_user);
$this
->assertTrue(in_array($baker_member_id, $web_user_authorizations), 'LdapAuthorizationConsumerOG authorizationRevoke() test revoke on member role', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$butcher_member_id = ldap_authorization_og_authorization_id($og_butchers_node->nid, $member_rid, 'node');
$og_auth
->authorizationGrant($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
$butcher_member_id,
), NULL, TRUE);
$web_user = user_load($web_user->uid, TRUE);
$web_user_authorizations = $og_auth
->usersAuthorizations($web_user);
$this
->assertTrue(in_array($butcher_member_id, array_values($web_user_authorizations)), 'LdapAuthorizationConsumerOG authorizationGrant()', $this->ldapTestId);
$this
->assertTrue($og_auth
->hasLdapGrantedAuthorization($web_user, $butcher_member_id), 'hasLdapGrantedAuthorization() method works for non LDAP provisioned og authorization', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$og_auth
->authorizationRevoke($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
$butcher_member_id,
), NULL, TRUE);
$web_user_authorizations = $og_auth
->usersAuthorizations($web_user);
$this
->assertFalse(in_array($butcher_member_id, $web_user_authorizations), 'LdapAuthorizationConsumerOG authorizationRevoke()', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$og_auth
->authorizationRevoke($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
$butcher_member_id,
), NULL, TRUE);
$web_user_authorizations = $og_auth
->usersAuthorizations($web_user);
$this
->assertFalse(in_array($butcher_member_id, $web_user_authorizations), 'LdapAuthorizationConsumerOG authorizationRevoke() attempt to revoke role that user doesnt have', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$result = $og_auth
->authorizationRevoke($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
'node:454:44334',
), NULL, TRUE);
$this
->assertFalse($result, 'LdapAuthorizationConsumerOG authorizationRevoke() test revoke of bogus authorization', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$result = $og_auth
->authorizationGrant($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
'node:454:44334',
), NULL, TRUE);
$this
->assertFalse($result, 'LdapAuthorizationConsumerOG authorizationGrant() test grant of bogus authorization', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$result = $og_auth
->authorizationRevoke($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
'bogusformat',
), NULL, TRUE);
$this
->assertFalse($result, 'LdapAuthorizationConsumerOG authorizationRevoke() test revoke malformed params', $this->ldapTestId);
$web_user = user_load($web_user->uid, TRUE);
$result = $og_auth
->authorizationGrant($web_user, $web_user->data['ldap_authorizations']['og_group'], array(
'bogusformat',
), NULL, TRUE);
$this
->assertFalse($result, 'LdapAuthorizationConsumerOG authorizationGrant() test grant malformed params', $this->ldapTestId);
$bakers_label = ldap_authorization_og2_get_group('node', 'bakers', 'group_name', 'label');
$this
->assertTrue($bakers_label == 'bakers', 'ldap_authorization_og2_get_group() function label return works with query type gid', $this->ldapTestId);
$test = ldap_authorization_og2_has_role($og_bakers_node->nid, $web_user->uid, OG_ADMINISTRATOR_ROLE);
$this
->assertTrue($test, 'ldap_authorization_og2_has_role() function works', $this->ldapTestId);
$test = ldap_authorization_og2_has_role($og_knitters_node->nid, $web_user->uid, OG_ADMINISTRATOR_ROLE);
$this
->assertTrue($test === FALSE, 'ldap_authorization_og2_has_role() function fails with FALSE', $this->ldapTestId);
}
function testAuthorizationsWithoutLogon() {
return;
if (ldap_authorization_og_og_version() != 2) {
debug('LdapAuthorizationOg2Tests must be run with OG 7.x-2.x');
return;
}
$this->ldapTestId = $this->module_name . ': og authorizations on logon';
$setup_success = module_exists('ldap_authentication') && module_exists('ldap_servers') && module_exists('ldap_authorization') && module_exists('ldap_authorization_drupal_role') && module_exists('ldap_authorization_og') && variable_get('ldap_simpletest', 0) == 1;
$this
->assertTrue($setup_success, ' ldap_authorizations og setup successful', $this->ldapTestId);
$web_user = $this
->drupalCreateUser();
$this->ldapTestId = 'OgWithoutLogon';
$this->serversData = 'Og/ldap_servers.inc';
$this->authorizationData = 'Og/ldap_authorization_og2.inc';
$this->authenticationData = 'Og/ldap_authentication.inc';
$this->consumerType = 'og_group';
$this
->prepTestData();
$og_auth = new LdapAuthorizationConsumerOG('og_group');
$this
->assertTrue(is_object($og_auth), 'Successfully instantiated LdapAuthorizationConsumerOG', $this->ldapTestId);
list($og_knitters, $og_knitters_node) = ldap_authorization_og2_get_group('node', 'knitters', 'group_name', 'object');
list($og_bakers, $og_bakers_node) = ldap_authorization_og2_get_group('node', 'bakers', 'group_name', 'object');
list($og_butchers, $og_butchers_node) = ldap_authorization_og2_get_group('node', 'butchers', 'group_name', 'object');
$anonymous_rid = ldap_authorization_og2_rid_from_role_name('node', $og_knitters_node->nid, OG_ANONYMOUS_ROLE);
$member_rid = ldap_authorization_og2_rid_from_role_name('node', $og_bakers_node->nid, OG_AUTHENTICATED_ROLE);
$admin_rid = ldap_authorization_og2_rid_from_role_name('node', $og_butchers_node->nid, OG_ADMINISTRATOR_ROLE);
$knitters_nonmember_id = ldap_authorization_og_authorization_id($og_knitters_node->nid, $anonymous_rid, 'node');
$knitters_member_id = ldap_authorization_og_authorization_id($og_knitters_node->nid, $member_rid, 'node');
$bakers_nonmember_id = ldap_authorization_og_authorization_id($og_bakers_node->nid, $anonymous_rid, 'node');
$bakers_member_id = ldap_authorization_og_authorization_id($og_bakers_node->nid, $member_rid, 'node');
$butcher_member_id = ldap_authorization_og_authorization_id($og_butchers_node->nid, $member_rid, 'node');
$butcher_admin_id = ldap_authorization_og_authorization_id($og_butchers_node->nid, $admin_rid, 'node');
debug("butcher_member_id={$butcher_member_id},\n butcher_admin_id={$butcher_admin_id},\n knitters_nonmember_id={$knitters_nonmember_id},\n knitters_member_id={$knitters_member_id}\n ");
$user = $this
->drupalCreateUser(array());
$unkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'unkool',
'mail' => 'unkool@nowhere.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($unkool, 'query');
if (count($new_authorizations['og_group']) != 0) {
debug('new authorizations');
debug($new_authorizations);
}
$this
->assertTrue(count($new_authorizations['og_group']) == 0, 'user account unkool tested for granting no drupal roles ', $this->ldapTestId . '.nomatch');
$user = $this
->drupalCreateUser(array());
$jkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'jkool',
'mail' => 'jkool@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'query');
$correct_roles = (bool) (isset($new_authorizations['og_group']) && in_array($butcher_member_id, $new_authorizations['og_group']) && in_array($bakers_member_id, $new_authorizations['og_group']));
if (!$correct_roles) {
debug('jkool og ldap authorizations');
debug($new_authorizations);
debug($new_authorizations);
}
$this
->assertTrue($correct_roles, "user account jkool tested for granting og butchers member and admin ({$butcher_member_id} and {$butcher_admin_id})", $this->ldapTestId . '.onematch');
$user = $this
->drupalCreateUser(array());
$verykool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'verykool',
'mail' => 'verykool@myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($verykool, 'query');
$correct_roles = (bool) (isset($new_authorizations['og_group']) && in_array($butcher_member_id, $new_authorizations[$this->consumerType]) && in_array($bakers_member_id, $new_authorizations[$this->consumerType]));
if (!$correct_roles) {
debug('verykool og ldap authorizations');
debug($new_authorizations);
debug($new_authorizations);
}
$this
->assertTrue($correct_roles, "user account verykool tested for granting og knitters member ({$knitters_member_id}) and og butchers member ({$butcher_member_id}) ", $this->ldapTestId . '.manymatch');
$this
->assertTrue($correct_roles, 'user account verykool tested for case insensitivity ', $this->ldapTestId . '.caseinsensitive');
}
function testAuthorizationsOnLogon() {
return;
if (ldap_authorization_og_og_version() != 2) {
debug('LdapAuthorizationOg2Tests must be run with OG 7.x-2.x');
return;
}
$this->ldapTestId = $this->module_name . ': og authorizations on logon';
$setup_success = module_exists('ldap_authentication') && module_exists('ldap_servers') && module_exists('ldap_authorization') && module_exists('ldap_authorization_drupal_role') && module_exists('ldap_authorization_og') && variable_get('ldap_simpletest', 0) == 1;
$this
->assertTrue($setup_success, ' ldap_authorizations og setup successful', $this->ldapTestId);
$web_user = $this
->drupalCreateUser();
$this->ldapTestId = 'OgLogon';
$this->serversData = 'Og/ldap_servers.inc';
$this->authorizationData = 'Og/ldap_authorization_og2.inc';
$this->authenticationData = 'Og/ldap_authentication.inc';
$this->consumerType = 'og_group';
$this
->prepTestData();
$og_auth = new LdapAuthorizationConsumerOG('og_group');
$this
->assertTrue(is_object($og_auth), 'Successfully instantiated LdapAuthorizationConsumerOG', $this->ldapTestId);
list($og_knitters, $og_knitters_node) = ldap_authorization_og2_get_group('node', 'knitters', 'group_name', 'object');
list($og_bakers, $og_bakers_node) = ldap_authorization_og2_get_group('node', 'bakers', 'group_name', 'object');
list($og_butchers, $og_butchers_node) = ldap_authorization_og2_get_group('node', 'butchers', 'group_name', 'object');
$anonymous_rid = ldap_authorization_og2_rid_from_role_name('node', $og_knitters_node->nid, OG_ANONYMOUS_ROLE);
$member_rid = ldap_authorization_og2_rid_from_role_name('node', $og_knitters_node->nid, OG_AUTHENTICATED_ROLE);
$admin_rid = ldap_authorization_og2_rid_from_role_name('node', $og_knitters_node->nid, OG_ADMINISTRATOR_ROLE);
$knitters_nonmember_id = ldap_authorization_og_authorization_id($og_knitters_node->nid, $anonymous_rid, 'node');
$knitters_member_id = ldap_authorization_og_authorization_id($og_knitters_node->nid, $member_rid, 'node');
$bakers_nonmember_id = ldap_authorization_og_authorization_id($og_bakers_node->nid, $anonymous_rid, 'node');
$bakers_member_id = ldap_authorization_og_authorization_id($og_bakers_node->nid, $member_rid, 'node');
$butcher_member_id = ldap_authorization_og_authorization_id($og_butchers_node->nid, $member_rid, 'node');
$butcher_admin_id = ldap_authorization_og_authorization_id($og_butchers_node->nid, $admin_rid, 'node');
debug("\n butcher_member_id={$butcher_member_id},<br/>\n butcher_admin_id={$butcher_admin_id},<br/>\n bakers_nonmember_id={$bakers_nonmember_id},<br/>\n bakers_member_id={$bakers_member_id},<br/>\n knitters_nonmember_id={$knitters_nonmember_id},<br/>\n knitters_member_id={$knitters_member_id}<br/>\n ");
$verykool = user_load_by_name('verykool');
if (is_object($verykool)) {
user_delete($verykool->uid);
}
$edit = array(
'name' => 'verykool',
'pass' => 'goodpwd',
);
$this
->drupalPost('user', $edit, t('Log in'));
$this
->assertText(t('Member for'), 'New Ldap user with good password authenticated.', $this->ldapTestId);
$this
->assertTrue($this->testFunctions
->ldapUserIsAuthmapped('verykool'), 'Ldap user properly authmapped.', $this->ldapTestId);
$verykool = user_load_by_name('verykool');
$existing_authorizations = $og_auth
->usersAuthorizations($verykool);
$correct_roles = in_array($butcher_member_id, $existing_authorizations) && in_array($bakers_member_id, $existing_authorizations);
if (!$correct_roles) {
debug('verykool og authorizations');
debug($existing_authorizations);
}
$this
->assertTrue($correct_roles, 'verykool granted butcher and knitter memberships', $this->ldapTestId);
$this
->drupalGet('user/logout');
$user = $this
->drupalCreateUser(array());
$jkool = $this->testFunctions
->drupalLdapUpdateUser(array(
'name' => 'jkool',
'mail' => 'jkool@guests.myuniversity.edu',
), TRUE, $user);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'query');
user_delete($jkool->uid);
$edit = array(
'name' => 'jkool',
'pass' => 'goodpwd',
);
$this
->drupalPost('user', $edit, t('Log in'));
$this
->assertText(t('Member for'), 'New Ldap user with good password authenticated.', $this->ldapTestId);
$this
->assertTrue($this->testFunctions
->ldapUserIsAuthmapped('jkool'), 'Ldap user properly authmapped.', $this->ldapTestId);
$jkool = user_load_by_name('jkool');
$existing_authorizations = $og_auth
->usersAuthorizations($jkool);
$correct_roles = in_array($butcher_member_id, $existing_authorizations);
if (!$correct_roles) {
debug('jkool og authorizations');
debug($existing_authorizations);
}
$this
->assertTrue($correct_roles, 'jkool granted admin role', $this->ldapTestId);
$this
->drupalGet('user/logout');
}
}