View source
<?php
require_once drupal_get_path('module', 'ldap_authorization') . '/tests/LdapAuthorizationTestCase.class.php';
class LdapAuthorizationBasicTests extends LdapAuthorizationTestCase {
public function setUp($addl_modules = array()) {
parent::setUp(array());
}
public static function getInfo() {
return array(
'group' => 'LDAP Authorization',
'name' => 'LDAP Authorization: Basic Tests',
'description' => 'Basic functionality tests.',
);
}
function testSimpleStuff() {
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') && variable_get('ldap_simpletest', 0) == 1;
$this
->assertTrue($setup_success, ' ldap_authorizations setup successful', $this->ldapTestId);
$this->ldapTestId = $this->module_name . ': test for api functions';
$api_functions = array(
'ldap_authorization_get_consumer_object' => array(
1,
1,
),
'ldap_authorization_get_consumers' => array(
3,
0,
),
'ldap_authorizations_user_authorizations' => array(
4,
1,
),
);
foreach ($api_functions as $api_function_name => $param_count) {
$reflector = new ReflectionFunction($api_function_name);
$this
->assertTrue(function_exists($api_function_name) && $param_count[1] == $reflector
->getNumberOfRequiredParameters() && $param_count[0] == $reflector
->getNumberOfParameters(), ' api function ' . $api_function_name . ' parameters and required parameters count unchanged.', $this->ldapTestId);
}
$this->ldapTestId = $this->module_name . ': cron test';
$this
->assertTrue(drupal_cron_run(), t('Cron can run with ldap authorization enabled.'), $this->ldapTestId);
$this->ldapTestId = 'BasicTests';
$this->serversData = 'BasicTests/ldap_servers.inc';
$this->authorizationData = 'BasicTests/ldap_authorization.simple.inc';
$this->authenticationData = 'BasicTests/ldap_authentication.inc';
$this->consumerType = 'drupal_role';
$this
->prepTestData();
$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');
$correct_roles = in_array('content editors', array_values($verykool->roles)) && in_array('content approvers', array_values($verykool->roles));
if (!$correct_roles) {
debug('verykool roles');
debug($verykool->roles);
}
$this
->assertTrue($correct_roles, 'verykool granted 2 roles on actual logon "content editors" and "content approvers" drupal roles ', $this->ldapTestId . '.duplicate_entry');
$this
->drupalGet('user/logout');
$ctools_originally_enabled = module_exists('ctools');
foreach (array(
'no_ctools',
'ctools',
) as $mode) {
if ($mode == 'ctools') {
module_enable(array(
'ctools',
));
$label_text = "ctool enabled";
}
elseif ($mode == 'no_ctools') {
module_disable(array(
'ctools',
), TRUE);
$label_text = "ctools disabled";
}
$rows = db_query('select * from ldap_authorization')
->fetchAllAssoc('numeric_consumer_conf_id');
$initial_count = count(array_keys($rows));
$consumer_conf_admin = ldap_authorization_get_consumer_admin_object($this->consumerType);
$consumer_conf_admin->status = 0;
$consumer_conf_admin
->save();
$consumer_conf_admin = ldap_authorization_get_consumer_admin_object($this->consumerType);
$rows = db_query('select * from ldap_authorization')
->fetchAllAssoc('numeric_consumer_conf_id');
$second_count = count(array_keys($rows));
$this->ldapTestId = $this->module_name . ": {$label_text} save doesn't break record count";
$this
->assertTrue($initial_count == $second_count, t("ldap_authorization record count consistent with {$label_text}, intiial count = {$initial_count}, final count={$second_count}"), $this->ldapTestId);
$this->ldapTestId = $this->module_name . ": {$label_text} saves status correctly";
$this
->assertTrue($consumer_conf_admin->status === 0, t("ldap_authorization status saved correctly."), $this->ldapTestId);
$this->ldapTestId = $this->module_name . ": {$label_text} loads numericConsumerConfId correctly";
$this
->assertTrue($consumer_conf_admin->numericConsumerConfId > 0, t("ldap_authorization loaded numericConsumerConfId correctly.(" . $consumer_conf_admin->numericConsumerConfId . ")"), $this->ldapTestId);
$consumer_conf_admin->status = 1;
$consumer_conf_admin
->save();
$consumer_conf_admin = ldap_authorization_get_consumer_admin_object($this->consumerType);
}
if ($ctools_originally_enabled) {
module_enable(array(
'ctools',
));
}
else {
module_disable(array(
'ctools',
), TRUE);
}
}
function testFlags() {
return;
$this->ldapTestId = 'Flags';
$this->serversData = 'BasicTests/ldap_servers.inc';
$this->authorizationData = 'BasicTests/ldap_authorization.flags.inc';
$this->authenticationData = 'BasicTests/ldap_authentication.inc';
$this->consumerType = 'drupal_role';
$this
->prepTestData();
$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');
$roles1 = $new_authorizations[$this->consumerType];
$consumer_conf_admin = ldap_authorization_get_consumer_admin_object($this->consumerType);
$consumer_conf_admin->status = 0;
$consumer_conf_admin
->save();
$consumer_conf_admin = ldap_authorization_get_consumer_admin_object($this->consumerType);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'query', $this->consumerType);
$roles2 = isset($new_authorizations[$this->consumerType]) ? $new_authorizations[$this->consumerType] : array();
$this
->assertTrue(count($roles1) == 1 && count($roles2) == 0, 'disable consumer configuration disallows authorizations.', $this->ldapTestId . '.enable');
$consumer_conf_admin->onlyApplyToLdapAuthenticated = 1;
$consumer_conf_admin->status = 1;
$consumer_conf_admin
->save();
$jkool = user_save($user, array(
'name' => 'jkool',
'mail' => 'jkool@guests.myuniversity.edu',
));
user_set_authmaps($jkool, array(
'authname_ldap_authentication' => NULL,
));
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'query');
$roles = isset($new_authorizations['drupal_role']) ? $new_authorizations['drupal_role'] : array();
$this
->assertTrue(count($roles) == 0, ' only apply to ldap authenticated grants no roles for non ldap user.', $this->ldapTestId . '.enable');
user_set_authmaps($jkool, array(
'authname_ldap_authentication' => 'jkool',
));
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'set');
$consumer_conf_admin->onlyApplyToLdapAuthenticated = 0;
$consumer_conf_admin->revokeLdapProvisioned = 1;
$consumer_conf_admin
->save();
$jkool = user_load($jkool->uid);
$data = array(
'roles' => array(
3 => 'administrator',
),
'data' => array(
'ldap_authorizations' => array(
'drupal_role' => array(
'administrator' => array(
'date_granted' => 1304216778,
),
),
),
),
);
$jkool = user_save($jkool, $data);
$was_set = isset($jkool->roles[3]);
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'set', $this->consumerType, 'logon');
$jkool = user_load($jkool->uid);
$this
->assertTrue($was_set && !isset($jkool->roles[3]), ' revoke ldap granted roles when no longer deserved.', $this->ldapTestId . '.revokeRoles');
$jkool = user_load($jkool->uid);
$data = array(
'roles' => array(),
'data' => array(
'ldap_authorizations' => array(),
),
);
$jkool = user_save($jkool, $data);
$consumer_conf_admin->createConsumers = 1;
$consumer_conf_admin
->save();
if (in_array('guests', array_values(user_roles()))) {
user_role_delete('guests');
}
$guest_role_deleted = !in_array('guests', array_values(user_roles()));
list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($jkool, 'set', 'drupal_role', 'logon');
$jkool = user_load($jkool->uid);
$guest_role_recreated = in_array('guests', array_values(user_roles()));
$roles_by_name = array_flip(user_roles());
$jkool_granted_guest = isset($jkool->roles[$roles_by_name['guests']]);
$this
->assertTrue($guest_role_deleted && $guest_role_recreated && $jkool_granted_guest, ' create consumers (e.g. roles)', $this->ldapTestId . '.createRoles');
}
}