Projet

Général

Profil

Paste
Télécharger (12 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_test / LdapTestCase.class.php @ 59ae487e

1
<?php
2

    
3

    
4

    
5
/**
6
 * @file
7
 * simpletest class for LDAP simpletests
8
 *
9
 */
10

    
11
module_load_include('php', 'ldap_test', 'LdapTestFunctions.class');
12

    
13
class LdapTestCase extends DrupalWebTestCase {
14

    
15
  public $testFunctions;
16
  public $module_name;
17

    
18
  // storage for test data
19
  public $useFeatureData;
20
  public $featurePath;
21
  public $featureName;
22
  public $consumerAdminConf;
23

    
24
  public $ldapTestId;
25
  public $authorizationData;
26
  public $authenticationData;
27
  public $testData = array();
28

    
29
  public $sid; // current, or only, sid
30

    
31
  function __construct($test_id = NULL) {
32
    parent::__construct($test_id);
33
    $this->testFunctions = new LdapTestFunctions();
34
  }
35

    
36
  function setUp() {
37
    $modules = func_get_args();
38
    if (isset($modules[0]) && is_array($modules[0])) {
39
      $modules = $modules[0];
40
    }
41
    parent::setUp($modules);
42
    variable_set('ldap_simpletest', 2);
43
    variable_set('ldap_help_watchdog_detail', 0);
44
    $this->createTestUserFields();
45

    
46
  }
47

    
48
  function tearDown() {
49
    parent::tearDown();
50
    variable_del('ldap_help_watchdog_detail');
51
    variable_del('ldap_simpletest');
52
  }
53

    
54
  /**
55
   * setup configuration and fake test data for all ldap modules
56
   *
57
   * @param  string $test_ldap_id name of directory in ldap_test where data is (e.g. hogwarts)
58
   *
59
   * the following params are ids that indicate what config data in /ldap_test/<module_name>.conf.inc to use
60
   * for example if $ldap_user_conf_id = 'ad_authentication', the array /ldap_test/ldap_user.conf.inc with the key
61
   *  'ad_authentication' will be used for the user module cofiguration
62
   *
63
   * @param array $sids to setup
64
   * @param string $ldap_user_conf_id
65
   * @param string $ldap_authentication_conf_id = NULL,
66
   * @param string $ldap_authorization_conf_id = NULL,
67
   * @param string $ldap_feeds_conf_id = NULL,
68
   * @param string $ldap_query_conf_id = NULL
69
   */
70
  function prepTestData(
71
      $test_ldap_id,
72
      $sids,
73
      $ldap_user_conf_id = NULL,
74
      $ldap_authentication_conf_id = NULL,
75
      $ldap_authorization_conf_id = NULL,
76
      $ldap_feeds_conf_id = NULL,
77
      $ldap_query_conf_id = NULL
78
    ) {
79

    
80
    $this->testFunctions->configureLdapServers($sids);
81

    
82
    foreach ($sids as $sid) {
83
      $this->testFunctions->populateFakeLdapServerData($test_ldap_id, $sid);
84
    }
85

    
86
    if ($ldap_user_conf_id) {
87
      $this->testFunctions->configureLdapUser($ldap_user_conf_id);
88
    }
89
    if ($ldap_authentication_conf_id) {
90
      $this->testFunctions->configureLdapAuthentication($ldap_authentication_conf_id, $sids);
91
    }
92

    
93
    if ($ldap_authorization_conf_id) {
94
      $authorization_data = ldap_test_ldap_authorization_data();
95
      if (!empty($authorization_data[$ldap_authorization_conf_id])) {
96
        $this->testFunctions->prepConsumerConf($authorization_data[$ldap_authorization_conf_id]);
97
        foreach ($authorization_data[$ldap_authorization_conf_id] as $consumer_type => $discard) {
98
          $this->consumerAdminConf[$consumer_type] = ldap_authorization_get_consumer_admin_object($consumer_type);
99
        }
100
      }
101
    }
102
  }
103

    
104
  /**
105
   * attempt to derive a testid from backtrace
106
   */
107
  public function testId($description = NULL, $method = NULL) {
108

    
109
    static $test_id;
110
    static $i;
111

    
112
    if ($description || $method) {
113
      $test_id = NULL;
114
      $i = 0;
115
    }
116
    elseif ($test_id)  { // default test id
117
      $i++;
118
      return $test_id . '.' . $i;
119
    }
120
    if (!$method) {
121
      $trace = debug_backtrace();
122

    
123
      $caller = array_shift($trace);
124
      $caller = array_shift($trace);
125
      $method = $caller['function'];
126
      $count = 1;
127
      $method = str_replace('test', '', $method, $count);
128
    }
129

    
130
    $test_id = join(".", array($this->module_name, $method, $description));
131
    return $test_id;
132

    
133
  }
134
  public function removeUserFromGroup(&$test_data, $user_dn, $group_dn, $domain = "dc=hogwarts,dc=edu") {
135

    
136
      $filter = "(&(objectClass=group)(member=$user_dn))";
137
      if (!empty($test_data['search_results'][$filter][$domain]) &&
138
            in_array($group_dn, $test_data['search_results'][$filter][$domain])) {
139
        $test_data['search_results'][$filter][$domain] = array_diff($test_data['search_results'][$filter][$domain], array($group_dn));
140
        $test_data['search_results'][$filter][$domain]['count'] = count($test_data['search_results'][$filter][$domain] - 1);
141
      }
142

    
143
      if (!empty($test_data['users'][$user_dn]['attr']['memberof']) && in_array($group_dn, $test_data['users'][$user_dn]['attr']['memberof'])) {
144
        $test_data['users'][$user_dn]['attr']['memberof'] = array_diff($test_data['users'][$user_dn]['attr']['memberof'], array($group_dn));
145
        $test_data['users'][$user_dn]['attr']['memberof']['count'] = count($test_data['users'][$user_dn]['attr']['memberof'] - 1);
146
      }
147

    
148
      if (!empty($test_data['ldap'][$user_dn]['memberof']) && in_array($group_dn, $test_data['ldap'][$user_dn]['memberof'])) {
149
        $test_data['ldap'][$user_dn]['memberof'] = array_diff($test_data['ldap'][$user_dn]['memberof'], array($group_dn));
150
        $test_data['ldap'][$user_dn]['memberof']['count'] = count($test_data['ldap'][$user_dn]['memberof']) - 1;
151
      }
152

    
153
      if (!empty($test_data['groups'][$group_dn]['attr']['member']) && in_array($group_dn, $test_data['groups'][$group_dn]['attr']['member']) ) {
154
        $members = array_diff($test_data['groups'][$group_dn]['attr']['member'], array($group_dn));
155
        $test_data['groups'][$group_dn]['attr']['member'] = $members;
156
        $test_data['groups'][$group_dn]['attr']['member'][$i]['count'] = count($members - 1);
157
      }
158
    }
159

    
160
  public function AttemptLogonNewUser($name, $goodpwd = TRUE) {
161

    
162
    $this->drupalLogout();
163

    
164
    $edit = array(
165
      'name' => $name,
166
      'pass' => ($goodpwd) ? "goodpwd" : "badpwd",
167
    );
168
    $user = user_load_by_name($name);
169
    if ($user) {
170
      user_delete($user->uid);
171
    }
172
    $this->drupalPost('user', $edit, t('Log in'));
173
  }
174

    
175
  /**
176
   * keep user entity fields function for ldap_user
177
   * in base class instead of user test class in case
178
   * module integration testing is needed
179
   */
180

    
181
  function createTestUserFields() {
182
    foreach ($this->ldap_user_test_entity_fields() as $field_id => $field_conf) {
183
      $field_info = field_info_field($field_id);
184
      if (!$field_info) {
185
        field_create_field($field_conf['field']);
186
        field_create_instance($field_conf['instance']);
187
      }
188
      $field_info = field_info_field($field_id);
189
    }
190
  }
191

    
192
  function ldap_user_test_entity_fields() {
193

    
194
    $fields = array();
195

    
196
    $fields['field_lname']['field'] = array(
197
      'field_name' => 'field_lname',
198
      'type' => 'text',
199
      'settings' => array(
200
        'max_length' => 64,
201
      )
202
    );
203

    
204
    $fields['field_lname']['instance'] = array(
205
      'field_name' => 'field_lname',
206
      'entity_type' => 'user',
207
      'label' => 'Last Name',
208
      'bundle' => 'user',
209
      'required' => FALSE,
210
      'widget' => array(
211
        'type' => 'text_textfield',
212
      ),
213
      'display' => array(
214
        'default' => array(
215
          'type' => 'text_default',
216
        ),
217
      ),
218
      'settings' => array('user_register_form' => FALSE)
219
    );
220

    
221
  $fields['field_department']['field'] = array(
222
      'field_name' => 'field_department',
223
      'type' => 'text',
224
      'settings' => array(
225
        'max_length' => 64,
226
      )
227
    );
228

    
229
    $fields['field_department']['instance'] = array(
230
      'field_name' => 'field_department',
231
      'entity_type' => 'user',
232
      'label' => 'Department',
233
      'bundle' => 'user',
234
      'required' => FALSE,
235
      'widget' => array(
236
        'type' => 'text_textfield',
237
      ),
238
      'display' => array(
239
        'default' => array(
240
          'type' => 'text_default',
241
        ),
242
      ),
243
      'settings' => array('user_register_form' => FALSE)
244
    );
245

    
246

    
247
    $fields['field_fname']['field'] = array(
248
      'field_name' => 'field_fname',
249
      'type' => 'text',
250
      'settings' => array(
251
        'max_length' => 64,
252
      )
253
    );
254

    
255
    $fields['field_fname']['instance'] = array(
256
      'field_name' => 'field_fname',
257
      'entity_type' => 'user',
258
      'label' => 'Last Name',
259
      'bundle' => 'user',
260
      'required' => FALSE,
261
      'widget' => array(
262
        'type' => 'text_textfield',
263
      ),
264
      'display' => array(
265
        'default' => array(
266
          'type' => 'text_default',
267
        ),
268
      ),
269
      'settings' => array('user_register_form' => FALSE)
270
    );
271

    
272
    // display name for testing compound tokens
273
    $fields['field_display_name']['field'] = array(
274
      'field_name' => 'field_display_name',
275
      'type' => 'text',
276
      'settings' => array(
277
        'max_length' => 64,
278
      )
279
    );
280

    
281
    $fields['field_display_name']['instance'] = array(
282
      'field_name' => 'field_display_name',
283
      'entity_type' => 'user',
284
      'label' => 'Display Name',
285
      'bundle' => 'user',
286
      'required' => FALSE,
287
      'widget' => array(
288
        'type' => 'text_textfield',
289
      ),
290
      'display' => array(
291
        'default' => array(
292
          'type' => 'text_default',
293
        ),
294
      ),
295
      'settings' => array('user_register_form' => FALSE)
296
    );
297

    
298
    // display name for testing compound tokens
299
    $fields['field_binary_test']['field'] = array(
300
      'field_name' => 'field_binary_test',
301
      'type' => 'text',
302
      'size' => 'big',
303
    );
304

    
305
    $fields['field_binary_test']['instance'] = array(
306
      'field_name' => 'field_binary_test',
307
      'entity_type' => 'user',
308
      'label' => 'Binary Field',
309
      'bundle' => 'user',
310
      'required' => FALSE,
311
      'widget' => array(
312
        'type' => 'text_textfield',
313
      ),
314
      'display' => array(
315
        'default' => array(
316
          'type' => 'text_default',
317
        ),
318
      ),
319
      'settings' => array('user_register_form' => FALSE)
320
    );
321

    
322
    return $fields;
323

    
324
  }
325

    
326
  public function checkConsumerConfSetup($conf_id) {
327

    
328
    $authorization_data = ldap_test_ldap_authorization_data();
329
    $props_set_correctly = TRUE;
330

    
331
    foreach ($authorization_data[$conf_id] as $consumer_type => $conf) {
332
      $props_set_display = array();
333
      foreach ($conf as $prop => $values) {
334
        if (!property_exists($this->consumerAdminConf[$consumer_type], $prop)) {
335
          $props_set_correctly = FALSE;
336
          $props_set_display[] = $prop . " $consumer_type property does not exist in consumer conf object";
337
        }
338
        elseif (is_scalar($this->consumerAdminConf[$consumer_type]->{$prop})) {
339
          if ($this->consumerAdminConf[$consumer_type]->{$prop} == $values) {
340
            $props_set_display[] = $prop . " $consumer_type set to $values correctly";
341
          }
342
          else {
343
            $props_set_correctly = FALSE;
344
            $props_set_display[] = $prop . " $consumer_type not set to $values correctly";
345
          }
346
        }
347
      }
348
    }
349

    
350
    return array($props_set_display, $props_set_correctly);
351
  }
352

    
353

    
354
  public function compareFormToProperties($object, $data, $item_id, $map, $lcase_transformed) {
355

    
356
    $mismatches = array();
357
    foreach ($data as $field_id => $values) {
358
      $field_id = drupal_strtolower($field_id);
359
      if (!isset($map[$field_id])) {
360
        continue;
361
      }
362
      $property = $map[$field_id];
363
      if (!is_object($object) || !property_exists($object, $property) && !property_exists($object, drupal_strtolower($property))) {
364
        continue;
365
      }
366
      $property_value = $object->{$property};
367

    
368
      $field_value = isset($values[$item_id + 2]) ? $values[$item_id + 2] : $values[$item_id]; // for cases where string input is not same as array.
369

    
370
      if (in_array($field_id, $lcase_transformed) && is_scalar($field_value)) {
371
        $field_value = drupal_strtolower($field_value);
372
      }
373
      $property_value_show = (is_scalar($property_value)) ? $property_value : serialize($property_value);
374
      $field_value_show = (is_scalar($field_value)) ? $field_value : serialize($field_value);
375

    
376
      if (is_array($property_value) && is_array($field_value)) {
377
        $pass = count(array_diff($property_value, $field_value)) == 0;
378
      }
379
      elseif (is_scalar($property_value) && is_scalar($field_value)) {
380
        $pass = ($property_value == $field_value);
381
      }
382
      else {
383
        $pass = FALSE;
384
      }
385

    
386
      if (!$pass) {
387
        $mismatches[] = "property $property ($property_value_show) does not match field $field_id value ($field_value_show)";
388
      }
389
    }
390

    
391
    return $mismatches;
392
  }
393

    
394
}