Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ldap / ldap_test / LdapTestCase.class.php @ 5136ce55

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
      //debug("removeUserFromGroup:debug test_data[search_results][$filter]"); debug($test_data['search_results']['(&(objectClass=group)(member=$user_dn))']);
143

    
144

    
145
      if (!empty($test_data['users'][$user_dn]['attr']['memberof']) && in_array($group_dn, $test_data['users'][$user_dn]['attr']['memberof'])) {
146
        $test_data['users'][$user_dn]['attr']['memberof'] = array_diff($test_data['users'][$user_dn]['attr']['memberof'], array($group_dn));
147
        $test_data['users'][$user_dn]['attr']['memberof']['count'] = count($test_data['users'][$user_dn]['attr']['memberof'] - 1);
148
      }
149
      //debug("removeUserFromGroup:debug test_data[users][$user_dn]"); debug($test_data['users'][$user_dn]);
150

    
151

    
152
      if (!empty($test_data['ldap'][$user_dn]['memberof']) && in_array($group_dn, $test_data['ldap'][$user_dn]['memberof'])) {
153
        $test_data['ldap'][$user_dn]['memberof'] = array_diff($test_data['ldap'][$user_dn]['memberof'], array($group_dn));
154
        $test_data['ldap'][$user_dn]['memberof']['count'] = count($test_data['ldap'][$user_dn]['memberof']) - 1;
155
      }
156
      //debug("removeUserFromGroup:debug test_data[ldap][$user_dn]"); debug($test_data['ldap'][$user_dn]);
157

    
158

    
159
      if (!empty($test_data['groups'][$group_dn]['attr']['member']) && in_array($group_dn, $test_data['groups'][$group_dn]['attr']['member']) ) {
160
        $members = array_diff($test_data['groups'][$group_dn]['attr']['member'], array($group_dn));
161
        $test_data['groups'][$group_dn]['attr']['member'] = $members;
162
        $test_data['groups'][$group_dn]['attr']['member'][$i]['count'] = count($members - 1);
163
      }
164
      //debug("removeUserFromGroup:debug test_data[groups][$group_dn]"); debug($test_data['groups'][$group_dn]);
165
    }
166

    
167
  public function AttemptLogonNewUser($name, $goodpwd = TRUE) {
168

    
169
    $this->drupalLogout();
170

    
171
    $edit = array(
172
      'name' => $name,
173
      'pass' => ($goodpwd) ? "goodpwd" : "badpwd",
174
    );
175
    $user = user_load_by_name($name);
176
    if ($user) {
177
      user_delete($user->uid);
178
    }
179
    $this->drupalPost('user', $edit, t('Log in'));
180
  }
181

    
182
  /**
183
   * keep user entity fields function for ldap_user
184
   * in base class instead of user test class in case
185
   * module integration testing is needed
186
   */
187

    
188
  function createTestUserFields() {
189
    foreach ($this->ldap_user_test_entity_fields() as $field_id => $field_conf) {
190
      $field_info = field_info_field($field_id);
191
      if (!$field_info) {
192
        field_create_field($field_conf['field']);
193
        field_create_instance($field_conf['instance']);
194
      }
195
      $field_info = field_info_field($field_id);
196
    }
197
  }
198

    
199
  function ldap_user_test_entity_fields() {
200

    
201
    $fields = array();
202

    
203
    $fields['field_lname']['field'] = array(
204
      'field_name' => 'field_lname',
205
      'type' => 'text',
206
      'settings' => array(
207
        'max_length' => 64,
208
      )
209
    );
210

    
211
    $fields['field_lname']['instance'] = array(
212
      'field_name' => 'field_lname',
213
      'entity_type' => 'user',
214
      'label' => 'Last Name',
215
      'bundle' => 'user',
216
      'required' => FALSE,
217
      'widget' => array(
218
        'type' => 'text_textfield',
219
      ),
220
      'display' => array(
221
        'default' => array(
222
          'type' => 'text_default',
223
        ),
224
      ),
225
      'settings' => array('user_register_form' => FALSE)
226
    );
227

    
228
  $fields['field_department']['field'] = array(
229
      'field_name' => 'field_department',
230
      'type' => 'text',
231
      'settings' => array(
232
        'max_length' => 64,
233
      )
234
    );
235

    
236
    $fields['field_department']['instance'] = array(
237
      'field_name' => 'field_department',
238
      'entity_type' => 'user',
239
      'label' => 'Department',
240
      'bundle' => 'user',
241
      'required' => FALSE,
242
      'widget' => array(
243
        'type' => 'text_textfield',
244
      ),
245
      'display' => array(
246
        'default' => array(
247
          'type' => 'text_default',
248
        ),
249
      ),
250
      'settings' => array('user_register_form' => FALSE)
251
    );
252

    
253

    
254
    $fields['field_fname']['field'] = array(
255
      'field_name' => 'field_fname',
256
      'type' => 'text',
257
      'settings' => array(
258
        'max_length' => 64,
259
      )
260
    );
261

    
262
    $fields['field_fname']['instance'] = array(
263
      'field_name' => 'field_fname',
264
      'entity_type' => 'user',
265
      'label' => 'Last Name',
266
      'bundle' => 'user',
267
      'required' => FALSE,
268
      'widget' => array(
269
        'type' => 'text_textfield',
270
      ),
271
      'display' => array(
272
        'default' => array(
273
          'type' => 'text_default',
274
        ),
275
      ),
276
      'settings' => array('user_register_form' => FALSE)
277
    );
278

    
279
    // display name for testing compound tokens
280
    $fields['field_display_name']['field'] = array(
281
      'field_name' => 'field_display_name',
282
      'type' => 'text',
283
      'settings' => array(
284
        'max_length' => 64,
285
      )
286
    );
287

    
288
    $fields['field_display_name']['instance'] = array(
289
      'field_name' => 'field_display_name',
290
      'entity_type' => 'user',
291
      'label' => 'Display Name',
292
      'bundle' => 'user',
293
      'required' => FALSE,
294
      'widget' => array(
295
        'type' => 'text_textfield',
296
      ),
297
      'display' => array(
298
        'default' => array(
299
          'type' => 'text_default',
300
        ),
301
      ),
302
      'settings' => array('user_register_form' => FALSE)
303
    );
304

    
305
    // display name for testing compound tokens
306
    $fields['field_binary_test']['field'] = array(
307
      'field_name' => 'field_binary_test',
308
      'type' => 'text',
309
      'size' => 'big',
310
    );
311

    
312
    $fields['field_binary_test']['instance'] = array(
313
      'field_name' => 'field_binary_test',
314
      'entity_type' => 'user',
315
      'label' => 'Binary Field',
316
      'bundle' => 'user',
317
      'required' => FALSE,
318
      'widget' => array(
319
        'type' => 'text_textfield',
320
      ),
321
      'display' => array(
322
        'default' => array(
323
          'type' => 'text_default',
324
        ),
325
      ),
326
      'settings' => array('user_register_form' => FALSE)
327
    );
328

    
329
    return $fields;
330

    
331
  }
332

    
333
  public function checkConsumerConfSetup($conf_id) {
334

    
335
    $authorization_data = ldap_test_ldap_authorization_data();
336
    $props_set_correctly = TRUE;
337

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

    
357
    return array($props_set_display, $props_set_correctly);
358
  }
359

    
360

    
361
  public function compareFormToProperties($object, $data, $item_id, $map, $lcase_transformed) {
362

    
363
    $mismatches = array();
364
    foreach ($data as $field_id => $values) {
365
      $field_id = drupal_strtolower($field_id);
366
      if (!isset($map[$field_id])) {
367
        continue;
368
      }
369
      $property = $map[$field_id];
370
      if (!is_object($object) || !property_exists($object, $property) && !property_exists($object, drupal_strtolower($property))) {
371
        continue;
372
      }
373
      $property_value = $object->{$property};
374

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

    
377
      if (in_array($field_id, $lcase_transformed) && is_scalar($field_value)) {
378
        $field_value = drupal_strtolower($field_value);
379
      }
380
      $property_value_show = (is_scalar($property_value)) ? $property_value : serialize($property_value);
381
      $field_value_show = (is_scalar($field_value)) ? $field_value : serialize($field_value);
382

    
383
      if (is_array($property_value) && is_array($field_value)) {
384
        $pass = count(array_diff($property_value, $field_value)) == 0;
385
      }
386
      elseif (is_scalar($property_value) && is_scalar($field_value)) {
387
        $pass = ($property_value == $field_value);
388
      }
389
      else {
390
        $pass = FALSE;
391
      }
392

    
393
      if (!$pass) {
394
        $mismatches[] = "property $property ($property_value_show) does not match field $field_id value ($field_value_show)";
395
      }
396
    }
397

    
398
    return $mismatches;
399
  }
400

    
401
}