public function SimpleLdapUserTestCase::setUp in Simple LDAP 7
Same name and namespace in other branches
- 7.2 simple_ldap_user/simple_ldap_user.test \SimpleLdapUserTestCase::setUp()
Inherited parent::setUp().
Overrides SimpleLdapServerTestCase::setUp
2 calls to SimpleLdapUserTestCase::setUp()
- SimpleLdapRoleTestCase::setUp in simple_ldap_role/
simple_ldap_role.test - Inherited parent::setUp().
- SimpleLdapSSOTestCase::setUp in simple_ldap_sso/
simple_ldap_sso.test - Set up the modules and any other.
2 methods override SimpleLdapUserTestCase::setUp()
- SimpleLdapRoleTestCase::setUp in simple_ldap_role/
simple_ldap_role.test - Inherited parent::setUp().
- SimpleLdapSSOTestCase::setUp in simple_ldap_sso/
simple_ldap_sso.test - Set up the modules and any other.
File
- simple_ldap_user/
simple_ldap_user.test, line 16 - Tests for Simple LDAP User module.
Class
- SimpleLdapUserTestCase
- @file Tests for Simple LDAP User module.
Code
public function setUp() {
// Get the live simple_ldap_user configuration.
$basedn = simple_ldap_user_variable_get('simple_ldap_user_basedn');
$scope = simple_ldap_user_variable_get('simple_ldap_user_scope');
$objectclass = simple_ldap_user_variable_get('simple_ldap_user_objectclass');
$attribute_name = simple_ldap_user_variable_get('simple_ldap_user_attribute_name');
$attribute_mail = simple_ldap_user_variable_get('simple_ldap_user_attribute_mail');
$attribute_pass = simple_ldap_user_variable_get('simple_ldap_user_attribute_pass');
$attribute_rdn = simple_ldap_user_variable_get('simple_ldap_user_attribute_rdn');
$attribute_map = simple_ldap_user_variable_get('simple_ldap_user_attribute_map');
$password_hash = simple_ldap_user_variable_get('simple_ldap_user_password_hash');
$filter = simple_ldap_user_variable_get('simple_ldap_user_filter');
$source = simple_ldap_user_variable_get('simple_ldap_user_source');
$sync = simple_ldap_user_variable_get('simple_ldap_user_sync');
$delete_ldap_user = simple_ldap_user_variable_get('simple_ldap_user_delete_from_ldap');
// Make sure the variables from settings.php are written to the database.
// Otherwise subsequent tests will not be able to read them. These are
// cleaned up in $this->tearDown().
variable_set('simple_ldap_user_attribute_map', $attribute_map);
// Create the simpletest sandbox.
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
parent::setUp($modules);
// Initialize an LDAP server object.
$server = SimpleLdapServer::singleton();
// Create a set of drupal-only users before enabling Simple LDAP User so
// that they are only present in Drupal.
for ($i = 0; $i < 5; $i++) {
$this->drupalUser[$i] = $this
->drupalCreateUser();
}
// Get a list of required attributes for the configured objectclass(es).
$must = array();
foreach ($objectclass as $o) {
foreach ($server->schema
->must($o, TRUE) as $m) {
if (!in_array($m, $must)) {
$must[] = strtolower($m);
}
}
}
// Add mapped fields to user entity.
foreach ($attribute_map as $attribute) {
// Clean up the attribute. We can't use simple_ldap_user_variable_get()
// yet because simple_ldap_user is not enabled yet. The module can't be
// enabled until after the Drupal users are initialized, or they would
// sync to LDAP, and invalidate the tests. Chicken and egg problem here.
$attribute['ldap'] = strtolower($attribute['ldap']);
if (!is_array($attribute['drupal'])) {
$attribute['drupal'] = array(
$attribute['drupal'],
);
}
// Skip one-to-many mappings.
if (count($attribute['drupal']) > 1) {
continue;
}
// Get the drupal name and type.
$drupal_attribute = reset($attribute['drupal']);
$type = substr($drupal_attribute, 0, 1);
// Create user fields.
if ($type == '#') {
$drupal_attribute = substr($drupal_attribute, 1);
$required = in_array($attribute['ldap'], $must);
$attributetype = $server->schema
->get('attributetypes', $attribute['ldap']);
// A syntax type of 1.3.6.1.4.1.1466.115.121.1.27 is an integer.
if (isset($attributetype['syntax']) && $attributetype['syntax'] == '1.3.6.1.4.1.1466.115.121.1.27') {
$type = 'number_integer';
}
else {
$type = 'text';
}
field_create_field(array(
'field_name' => $drupal_attribute,
'type' => $type,
'cardinality' => 1,
));
field_create_instance(array(
'field_name' => $drupal_attribute,
'entity_type' => 'user',
'label' => $drupal_attribute,
'bundle' => 'user',
'required' => $required,
'settings' => array(
'user_register_form' => $required,
),
));
// Populate the field value of each of the test Drupal users.
foreach ($this->drupalUser as $drupal_user) {
if ($type == 'number_integer') {
$edit[$drupal_attribute]['und'][0]['value'] = $drupal_user->uid + 1000;
}
else {
$edit[$drupal_attribute]['und'][0]['value'] = $this
->randomName();
}
$drupal_user = user_save($drupal_user, $edit);
}
}
}
// Enable the Simple LDAP User module.
$modules = array(
'simple_ldap_user',
);
$success = module_enable($modules);
$this
->assertTrue($success, t('Enabled modules: %modules', array(
'%modules' => implode(', ', $modules),
)));
// Configure the sandboxed simple_ldap_user.
variable_set('simple_ldap_user_basedn', $basedn);
variable_set('simple_ldap_user_scope', $scope);
variable_set('simple_ldap_user_objectclass', $objectclass);
variable_set('simple_ldap_user_attribute_name', $attribute_name);
variable_set('simple_ldap_user_attribute_mail', $attribute_mail);
variable_set('simple_ldap_user_attribute_pass', $attribute_pass);
variable_set('simple_ldap_user_attribute_rdn', $attribute_rdn);
variable_set('simple_ldap_user_attribute_map', $attribute_map);
variable_set('simple_ldap_user_password_hash', $password_hash);
variable_set('simple_ldap_user_filter', $filter);
variable_set('simple_ldap_user_source', $source);
variable_set('simple_ldap_user_sync', $sync);
variable_set('simple_ldap_user_delete_from_ldap', $delete_ldap_user);
// Get the fully qualified attribute map.
$mapObject = SimpleLdapUserMap::singleton();
// Create a set of LDAP entries to use during testing.
for ($i = 0; $i < 5; $i++) {
// Create a new LDAP User object, and set the attributes.
$name = $this
->randomName();
$this->ldapUser[$i] = new SimpleLdapUser($name);
$this->ldapUser[$i]->{$attribute_mail} = $this
->randomName() . '@example.com';
foreach ($mapObject->map as $attribute) {
$attributetype = $server->schema
->get('attributetypes', $attribute['ldap']);
// A syntax type of 1.3.6.1.4.1.1466.115.121.1.27 is an integer.
if (isset($attributetype['syntax']) && $attributetype['syntax'] == '1.3.6.1.4.1.1466.115.121.1.27') {
$this->ldapUser[$i]->{$attribute['ldap']} = 1000 + $i;
}
else {
$this->ldapUser[$i]->{$attribute['ldap']} = $this
->randomName();
}
}
// Set the DN.
if (empty($attribute_rdn)) {
$this->ldapUser[$i]->dn = $attribute_name . '=' . $name . ',' . $basedn;
}
else {
$this->ldapUser[$i]->dn = $attribute_rdn . '=' . $this->ldapUser[$i]->{$attribute_rdn}[0] . ',' . $basedn;
}
// Set the user's password.
$this->userPassword[$i] = $this
->randomName();
SimpleLdapUser::hash($this->userPassword[$i], $this->userPassword[$i]);
$this->ldapUser[$i]->{$attribute_pass} = $this->userPassword[$i];
// Save the entry to LDAP.
$this->ldapUser[$i]
->save();
// If no exception is thrown by save() then the save was successful, but
// show the DN to make the tester feel better.
$this
->assertTrue($this->ldapUser[$i]->exists, t(':dn was added to LDAP', array(
':dn' => $this->ldapUser[$i]->dn,
)));
// Verify that the LDAP user can bind.
$result = $server
->bind($this->ldapUser[$i]->dn, $this->userPassword[$i]);
$this
->assertTrue($result, t('Successful bind with :dn using password :pw', array(
':dn' => $this->ldapUser[$i]->dn,
':pw' => $this->userPassword[$i],
)));
}
}