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 @ 32700c57

1
<?php
2

    
3
/**
4
 * @file
5
 * Simpletest class for LDAP simpletests.
6
 */
7

    
8
module_load_include('php', 'ldap_test', 'LdapTestFunctions.class');
9
/**
10
 *
11
 */
12
class LdapTestCase extends DrupalWebTestCase {
13

    
14
  public $testFunctions;
15
  public $module_name;
16

    
17
  /**
18
   * Storage for test data.
19
   */
20
  public $useFeatureData;
21
  public $featurePath;
22
  public $featureName;
23
  public $consumerAdminConf;
24

    
25
  public $ldapTestId;
26
  public $authorizationData;
27
  public $authenticationData;
28
  public $testData = [];
29

    
30
  /**
31
   * Current, or only, sid.
32
   */
33
  public $sid;
34

    
35
  /**
36
   *
37
   */
38
  public function __construct($test_id = NULL) {
39
    parent::__construct($test_id);
40
    $this->testFunctions = new LdapTestFunctions();
41
  }
42

    
43
  /**
44
   *
45
   */
46
  public function setUp() {
47
    $modules = func_get_args();
48
    if (isset($modules[0]) && is_array($modules[0])) {
49
      $modules = $modules[0];
50
    }
51
    parent::setUp($modules);
52
    variable_set('ldap_simpletest', 2);
53
    variable_set('ldap_help_watchdog_detail', 0);
54
    $this->createTestUserFields();
55

    
56
  }
57

    
58
  /**
59
   *
60
   */
61
  public function tearDown() {
62
    parent::tearDown();
63
    variable_del('ldap_help_watchdog_detail');
64
    variable_del('ldap_simpletest');
65
  }
66

    
67
  /**
68
   * Setup configuration and fake test data for all ldap modules.
69
   *
70
   * @param string $test_ldap_id
71
   *   name of directory in ldap_test where data is (e.g. hogwarts)
72
   *
73
   *   the following params are ids that indicate what config data in /ldap_test/<module_name>.conf.inc to use
74
   *   for example if $ldap_user_conf_id = 'ad_authentication', the array /ldap_test/ldap_user.conf.inc with the key
75
   *   'ad_authentication' will be used for the user module cofiguration.
76
   *
77
   * @param array $sids
78
   *   to setup.
79
   * @param string $ldap_user_conf_id
80
   * @param string $ldap_authentication_conf_id
81
   *   = NULL,.
82
   * @param string $ldap_authorization_conf_id
83
   *   = NULL,.
84
   * @param string $ldap_feeds_conf_id
85
   *   = NULL,.
86
   * @param string $ldap_query_conf_id
87
   *   = NULL.
88
   */
89
  public function prepTestData(
90
      $test_ldap_id,
91
      $sids,
92
      $ldap_user_conf_id = NULL,
93
      $ldap_authentication_conf_id = NULL,
94
      $ldap_authorization_conf_id = NULL,
95
      $ldap_feeds_conf_id = NULL,
96
      $ldap_query_conf_id = NULL
97
    ) {
98

    
99
    $this->testFunctions->configureLdapServers($sids);
100

    
101
    foreach ($sids as $sid) {
102
      $this->testFunctions->populateFakeLdapServerData($test_ldap_id, $sid);
103
    }
104

    
105
    if ($ldap_user_conf_id) {
106
      $this->testFunctions->configureLdapUser($ldap_user_conf_id);
107
    }
108
    if ($ldap_authentication_conf_id) {
109
      $this->testFunctions->configureLdapAuthentication($ldap_authentication_conf_id, $sids);
110
    }
111

    
112
    if ($ldap_authorization_conf_id) {
113
      $authorization_data = ldap_test_ldap_authorization_data();
114
      if (!empty($authorization_data[$ldap_authorization_conf_id])) {
115
        $this->testFunctions->prepConsumerConf($authorization_data[$ldap_authorization_conf_id]);
116
        foreach ($authorization_data[$ldap_authorization_conf_id] as $consumer_type => $discard) {
117
          $this->consumerAdminConf[$consumer_type] = ldap_authorization_get_consumer_admin_object($consumer_type);
118
        }
119
      }
120
    }
121
  }
122

    
123
  /**
124
   * Attempt to derive a testid from backtrace.
125
   */
126
  public function testId($description = NULL, $method = NULL) {
127

    
128
    static $test_id;
129
    static $i;
130

    
131
    if ($description || $method) {
132
      $test_id = NULL;
133
      $i = 0;
134
    }
135
    // Default test id.
136
    elseif ($test_id) {
137
      $i++;
138
      return $test_id . '.' . $i;
139
    }
140
    if (!$method) {
141
      $trace = debug_backtrace();
142

    
143
      $caller = array_shift($trace);
144
      $caller = array_shift($trace);
145
      $method = $caller['function'];
146
      $count = 1;
147
      $method = str_replace('test', '', $method, $count);
148
    }
149

    
150
    $test_id = join(".", [$this->module_name, $method, $description]);
151
    return $test_id;
152

    
153
  }
154

    
155
  /**
156
   *
157
   */
158
  public function removeUserFromGroup(&$test_data, $user_dn, $group_dn, $domain = "dc=hogwarts,dc=edu") {
159

    
160
    $filter = "(&(objectClass=group)(member=$user_dn))";
161
    if (!empty($test_data['search_results'][$filter][$domain]) &&
162
            in_array($group_dn, $test_data['search_results'][$filter][$domain])) {
163
      $test_data['search_results'][$filter][$domain] = array_diff($test_data['search_results'][$filter][$domain], [$group_dn]);
164
      $test_data['search_results'][$filter][$domain]['count'] = count($test_data['search_results'][$filter][$domain] - 1);
165
    }
166

    
167
    if (!empty($test_data['users'][$user_dn]['attr']['memberof']) && in_array($group_dn, $test_data['users'][$user_dn]['attr']['memberof'])) {
168
      $test_data['users'][$user_dn]['attr']['memberof'] = array_diff($test_data['users'][$user_dn]['attr']['memberof'], [$group_dn]);
169
      $test_data['users'][$user_dn]['attr']['memberof']['count'] = count($test_data['users'][$user_dn]['attr']['memberof'] - 1);
170
    }
171

    
172
    if (!empty($test_data['ldap'][$user_dn]['memberof']) && in_array($group_dn, $test_data['ldap'][$user_dn]['memberof'])) {
173
      $test_data['ldap'][$user_dn]['memberof'] = array_diff($test_data['ldap'][$user_dn]['memberof'], [$group_dn]);
174
      $test_data['ldap'][$user_dn]['memberof']['count'] = count($test_data['ldap'][$user_dn]['memberof']) - 1;
175
    }
176

    
177
    if (!empty($test_data['groups'][$group_dn]['attr']['member']) && in_array($group_dn, $test_data['groups'][$group_dn]['attr']['member'])) {
178
      $members = array_diff($test_data['groups'][$group_dn]['attr']['member'], [$group_dn]);
179
      $test_data['groups'][$group_dn]['attr']['member'] = $members;
180
      $test_data['groups'][$group_dn]['attr']['member'][$i]['count'] = count($members - 1);
181
    }
182
  }
183

    
184
  /**
185
   *
186
   */
187
  public function AttemptLogonNewUser($name, $goodpwd = TRUE) {
188

    
189
    $this->drupalLogout();
190

    
191
    $edit = [
192
      'name' => $name,
193
      'pass' => ($goodpwd) ? "goodpwd" : "badpwd",
194
    ];
195
    $user = user_load_by_name($name);
196
    if ($user) {
197
      user_delete($user->uid);
198
    }
199
    $this->drupalPost('user', $edit, t('Log in'));
200
  }
201

    
202
  /**
203
   * Keep user entity fields function for ldap_user
204
   * in base class instead of user test class in case
205
   * module integration testing is needed.
206
   */
207
  public function createTestUserFields() {
208
    foreach ($this->ldap_user_test_entity_fields() as $field_id => $field_conf) {
209
      $field_info = field_info_field($field_id);
210
      if (!$field_info) {
211
        field_create_field($field_conf['field']);
212
        field_create_instance($field_conf['instance']);
213
      }
214
      $field_info = field_info_field($field_id);
215
    }
216
  }
217

    
218
  /**
219
   *
220
   */
221
  public function ldap_user_test_entity_fields() {
222

    
223
    $fields = [];
224

    
225
    $fields['field_lname']['field'] = [
226
      'field_name' => 'field_lname',
227
      'type' => 'text',
228
      'settings' => [
229
        'max_length' => 64,
230
      ],
231
    ];
232

    
233
    $fields['field_lname']['instance'] = [
234
      'field_name' => 'field_lname',
235
      'entity_type' => 'user',
236
      'label' => 'Last Name',
237
      'bundle' => 'user',
238
      'required' => FALSE,
239
      'widget' => [
240
        'type' => 'text_textfield',
241
      ],
242
      'display' => [
243
        'default' => [
244
          'type' => 'text_default',
245
        ],
246
      ],
247
      'settings' => ['user_register_form' => FALSE],
248
    ];
249

    
250
    $fields['field_department']['field'] = [
251
      'field_name' => 'field_department',
252
      'type' => 'text',
253
      'settings' => [
254
        'max_length' => 64,
255
      ],
256
    ];
257

    
258
    $fields['field_department']['instance'] = [
259
      'field_name' => 'field_department',
260
      'entity_type' => 'user',
261
      'label' => 'Department',
262
      'bundle' => 'user',
263
      'required' => FALSE,
264
      'widget' => [
265
        'type' => 'text_textfield',
266
      ],
267
      'display' => [
268
        'default' => [
269
          'type' => 'text_default',
270
        ],
271
      ],
272
      'settings' => ['user_register_form' => FALSE],
273
    ];
274

    
275
    $fields['field_fname']['field'] = [
276
      'field_name' => 'field_fname',
277
      'type' => 'text',
278
      'settings' => [
279
        'max_length' => 64,
280
      ],
281
    ];
282

    
283
    $fields['field_fname']['instance'] = [
284
      'field_name' => 'field_fname',
285
      'entity_type' => 'user',
286
      'label' => 'Last Name',
287
      'bundle' => 'user',
288
      'required' => FALSE,
289
      'widget' => [
290
        'type' => 'text_textfield',
291
      ],
292
      'display' => [
293
        'default' => [
294
          'type' => 'text_default',
295
        ],
296
      ],
297
      'settings' => ['user_register_form' => FALSE],
298
    ];
299

    
300
    // Display name for testing compound tokens.
301
    $fields['field_display_name']['field'] = [
302
      'field_name' => 'field_display_name',
303
      'type' => 'text',
304
      'settings' => [
305
        'max_length' => 64,
306
      ],
307
    ];
308

    
309
    $fields['field_display_name']['instance'] = [
310
      'field_name' => 'field_display_name',
311
      'entity_type' => 'user',
312
      'label' => 'Display Name',
313
      'bundle' => 'user',
314
      'required' => FALSE,
315
      'widget' => [
316
        'type' => 'text_textfield',
317
      ],
318
      'display' => [
319
        'default' => [
320
          'type' => 'text_default',
321
        ],
322
      ],
323
      'settings' => ['user_register_form' => FALSE],
324
    ];
325

    
326
    // Display name for testing compound tokens.
327
    $fields['field_binary_test']['field'] = [
328
      'field_name' => 'field_binary_test',
329
      'type' => 'text',
330
      'size' => 'big',
331
    ];
332

    
333
    $fields['field_binary_test']['instance'] = [
334
      'field_name' => 'field_binary_test',
335
      'entity_type' => 'user',
336
      'label' => 'Binary Field',
337
      'bundle' => 'user',
338
      'required' => FALSE,
339
      'widget' => [
340
        'type' => 'text_textfield',
341
      ],
342
      'display' => [
343
        'default' => [
344
          'type' => 'text_default',
345
        ],
346
      ],
347
      'settings' => ['user_register_form' => FALSE],
348
    ];
349

    
350
    return $fields;
351

    
352
  }
353

    
354
  /**
355
   *
356
   */
357
  public function checkConsumerConfSetup($conf_id) {
358

    
359
    $authorization_data = ldap_test_ldap_authorization_data();
360
    $props_set_correctly = TRUE;
361

    
362
    foreach ($authorization_data[$conf_id] as $consumer_type => $conf) {
363
      $props_set_display = [];
364
      foreach ($conf as $prop => $values) {
365
        if (!property_exists($this->consumerAdminConf[$consumer_type], $prop)) {
366
          $props_set_correctly = FALSE;
367
          $props_set_display[] = $prop . " $consumer_type property does not exist in consumer conf object";
368
        }
369
        elseif (is_scalar($this->consumerAdminConf[$consumer_type]->{$prop})) {
370
          if ($this->consumerAdminConf[$consumer_type]->{$prop} == $values) {
371
            $props_set_display[] = $prop . " $consumer_type set to $values correctly";
372
          }
373
          else {
374
            $props_set_correctly = FALSE;
375
            $props_set_display[] = $prop . " $consumer_type not set to $values correctly";
376
          }
377
        }
378
      }
379
    }
380

    
381
    return [$props_set_display, $props_set_correctly];
382
  }
383

    
384
  /**
385
   *
386
   */
387
  public function compareFormToProperties($object, $data, $item_id, $map, $lcase_transformed) {
388

    
389
    $mismatches = [];
390
    foreach ($data as $field_id => $values) {
391
      $field_id = drupal_strtolower($field_id);
392
      if (!isset($map[$field_id])) {
393
        continue;
394
      }
395
      $property = $map[$field_id];
396
      if (!is_object($object) || !property_exists($object, $property) && !property_exists($object, drupal_strtolower($property))) {
397
        continue;
398
      }
399
      $property_value = $object->{$property};
400

    
401
      // For cases where string input is not same as array.
402
      $field_value = isset($values[$item_id + 2]) ? $values[$item_id + 2] : $values[$item_id];
403

    
404
      if (in_array($field_id, $lcase_transformed) && is_scalar($field_value)) {
405
        $field_value = drupal_strtolower($field_value);
406
      }
407
      $property_value_show = (is_scalar($property_value)) ? $property_value : serialize($property_value);
408
      $field_value_show = (is_scalar($field_value)) ? $field_value : serialize($field_value);
409

    
410
      if (is_array($property_value) && is_array($field_value)) {
411
        $pass = count(array_diff($property_value, $field_value)) == 0;
412
      }
413
      elseif (is_scalar($property_value) && is_scalar($field_value)) {
414
        $pass = ($property_value == $field_value);
415
      }
416
      else {
417
        $pass = FALSE;
418
      }
419

    
420
      if (!$pass) {
421
        $mismatches[] = "property $property ($property_value_show) does not match field $field_id value ($field_value_show)";
422
      }
423
    }
424

    
425
    return $mismatches;
426
  }
427

    
428
}