Projet

Général

Profil

Paste
Télécharger (17,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / simpletest.pages.inc @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Page callbacks for simpletest module.
6
 */
7

    
8
/**
9
 * List tests arranged in groups that can be selected and run.
10
 */
11
function simpletest_test_form($form) {
12
  $form['tests'] = array(
13
    '#type' => 'fieldset',
14
    '#title' => t('Tests'),
15
    '#description' => t('Select the test(s) or test group(s) you would like to run, and click <em>Run tests</em>.'),
16
  );
17

    
18
  $form['tests']['table'] = array(
19
    '#theme' => 'simpletest_test_table',
20
  );
21

    
22
  // Generate the list of tests arranged by group.
23
  $groups = simpletest_test_get_all();
24
  foreach ($groups as $group => $tests) {
25
    $form['tests']['table'][$group] = array(
26
      '#collapsed' => TRUE,
27
    );
28

    
29
    foreach ($tests as $class => $info) {
30
      $form['tests']['table'][$group][$class] = array(
31
        '#type' => 'checkbox',
32
        '#title' => $info['name'],
33
        '#description' => $info['description'],
34
      );
35
    }
36
  }
37

    
38
  // Operation buttons.
39
  $form['tests']['op'] = array(
40
    '#type' => 'submit',
41
    '#value' => t('Run tests'),
42
  );
43
  $form['clean'] = array(
44
    '#type' => 'fieldset',
45
    '#collapsible' => FALSE,
46
    '#collapsed' => FALSE,
47
    '#title' => t('Clean test environment'),
48
    '#description' => t('Remove tables with the prefix "simpletest" and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
49
  );
50
  $form['clean']['op'] = array(
51
    '#type' => 'submit',
52
    '#value' => t('Clean environment'),
53
    '#submit' => array('simpletest_clean_environment'),
54
  );
55

    
56
  return $form;
57
}
58

    
59
/**
60
 * Returns HTML for a test list generated by simpletest_test_form() into a table.
61
 *
62
 * @param $variables
63
 *   An associative array containing:
64
 *   - table: A render element representing the table.
65
 *
66
 * @ingroup themeable
67
 */
68
function theme_simpletest_test_table($variables) {
69
  $table = $variables['table'];
70

    
71
  drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
72
  drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js');
73
  drupal_add_js('misc/tableselect.js');
74

    
75
  // Create header for test selection table.
76
  $header = array(
77
    array('class' => array('select-all')),
78
    array('data' => t('Test'), 'class' => array('simpletest_test')),
79
    array('data' => t('Description'), 'class' => array('simpletest_description')),
80
  );
81

    
82
  // Define the images used to expand/collapse the test groups.
83
  $js = array(
84
    'images' => array(
85
      theme('image', array('path' => 'misc/menu-collapsed.png', 'width' => 7, 'height' => 7, 'alt' => t('Expand'), 'title' => t('Expand'))) . ' <a href="#" class="simpletest-collapse">(' . t('Expand') . ')</a>',
86
      theme('image', array('path' => 'misc/menu-expanded.png', 'width' => 7, 'height' => 7, 'alt' => t('Collapse'), 'title' => t('Collapse'))) . ' <a href="#" class="simpletest-collapse">(' . t('Collapse') . ')</a>',
87
    ),
88
  );
89

    
90
  // Cycle through each test group and create a row.
91
  $rows = array();
92
  foreach (element_children($table) as $key) {
93
    $element = &$table[$key];
94
    $row = array();
95

    
96
    // Make the class name safe for output on the page by replacing all
97
    // non-word/decimal characters with a dash (-).
98
    $test_class = strtolower(trim(preg_replace("/[^\w\d]/", "-", $key)));
99

    
100
    // Select the right "expand"/"collapse" image, depending on whether the
101
    // category is expanded (at least one test selected) or not.
102
    $collapsed = !empty($element['#collapsed']);
103
    $image_index = $collapsed ? 0 : 1;
104

    
105
    // Place-holder for checkboxes to select group of tests.
106
    $row[] = array('id' => $test_class, 'class' => array('simpletest-select-all'));
107

    
108
    // Expand/collapse image and group title.
109
    $row[] = array(
110
      'data' => '<div class="simpletest-image" id="simpletest-test-group-' . $test_class . '"></div>' .
111
        '<label for="' . $test_class . '-select-all" class="simpletest-group-label">' . $key . '</label>',
112
      'class' => array('simpletest-group-label'),
113
    );
114

    
115
    $row[] = array(
116
      'data' => '&nbsp;',
117
      'class' => array('simpletest-group-description'),
118
    );
119

    
120
    $rows[] = array('data' => $row, 'class' => array('simpletest-group'));
121

    
122
    // Add individual tests to group.
123
    $current_js = array(
124
      'testClass' => $test_class . '-test',
125
      'testNames' => array(),
126
      'imageDirection' => $image_index,
127
      'clickActive' => FALSE,
128
    );
129

    
130
    // Sorting $element by children's #title attribute instead of by class name.
131
    uasort($element, 'element_sort_by_title');
132

    
133
    // Cycle through each test within the current group.
134
    foreach (element_children($element) as $test_name) {
135
      $test = $element[$test_name];
136
      $row = array();
137

    
138
      $current_js['testNames'][] = $test['#id'];
139

    
140
      // Store test title and description so that checkbox won't render them.
141
      $title = $test['#title'];
142
      $description = $test['#description'];
143

    
144
      $test['#title_display'] = 'invisible';
145
      unset($test['#description']);
146

    
147
      // Test name is used to determine what tests to run.
148
      $test['#name'] = $test_name;
149

    
150
      $row[] = array(
151
        'data' => drupal_render($test),
152
        'class' => array('simpletest-test-select'),
153
      );
154
      $row[] = array(
155
        'data' => '<label for="' . $test['#id'] . '">' . $title . '</label>',
156
        'class' => array('simpletest-test-label'),
157
      );
158
      $row[] = array(
159
        'data' => '<div class="description">' . $description . '</div>',
160
        'class' => array('simpletest-test-description'),
161
      );
162

    
163
      $rows[] = array('data' => $row, 'class' => array($test_class . '-test', ($collapsed ? 'js-hide' : '')));
164
    }
165
    $js['simpletest-test-group-' . $test_class] = $current_js;
166
    unset($table[$key]);
167
  }
168

    
169
  // Add js array of settings.
170
  drupal_add_js(array('simpleTest' => $js), 'setting');
171

    
172
  if (empty($rows)) {
173
    return '<strong>' . t('No tests to display.') . '</strong>';
174
  }
175
  else {
176
    return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'simpletest-form-table')));
177
  }
178
}
179

    
180
/**
181
 * Run selected tests.
182
 */
183
function simpletest_test_form_submit($form, &$form_state) {
184
  simpletest_classloader_register();
185
  // Get list of tests.
186
  $tests_list = array();
187
  foreach ($form_state['values'] as $class_name => $value) {
188
    // Since class_exists() will likely trigger an autoload lookup,
189
    // we do the fast check first.
190
    if ($value === 1 && class_exists($class_name)) {
191
      $tests_list[] = $class_name;
192
    }
193
  }
194
  if (count($tests_list) > 0 ) {
195
    $test_id = simpletest_run_tests($tests_list, 'drupal');
196
    $form_state['redirect'] = 'admin/config/development/testing/results/' . $test_id;
197
  }
198
  else {
199
    drupal_set_message(t('No test(s) selected.'), 'error');
200
  }
201
}
202

    
203
/**
204
 * Test results form for $test_id.
205
 */
206
function simpletest_result_form($form, &$form_state, $test_id) {
207
  // Make sure there are test results to display and a re-run is not being performed.
208
  $results = array();
209
  if (is_numeric($test_id) && !$results = simpletest_result_get($test_id)) {
210
    drupal_set_message(t('No test results to display.'), 'error');
211
    drupal_goto('admin/config/development/testing');
212
    return $form;
213
  }
214

    
215
  // Load all classes and include CSS.
216
  drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
217

    
218
  // Keep track of which test cases passed or failed.
219
  $filter = array(
220
    'pass' => array(),
221
    'fail' => array(),
222
  );
223

    
224
  // Summary result fieldset.
225
  $form['result'] = array(
226
    '#type' => 'fieldset',
227
    '#title' => t('Results'),
228
  );
229
  $form['result']['summary'] = $summary = array(
230
    '#theme' => 'simpletest_result_summary',
231
    '#pass' => 0,
232
    '#fail' => 0,
233
    '#exception' => 0,
234
    '#debug' => 0,
235
  );
236

    
237
  simpletest_classloader_register();
238

    
239
  // Cycle through each test group.
240
  $header = array(t('Message'), t('Group'), t('Filename'), t('Line'), t('Function'), array('colspan' => 2, 'data' => t('Status')));
241
  $form['result']['results'] = array();
242
  foreach ($results as $group => $assertions) {
243
    // Create group fieldset with summary information.
244
    $info = call_user_func(array($group, 'getInfo'));
245
    $form['result']['results'][$group] = array(
246
      '#type' => 'fieldset',
247
      '#title' => $info['name'],
248
      '#description' => $info['description'],
249
      '#collapsible' => TRUE,
250
    );
251
    $form['result']['results'][$group]['summary'] = $summary;
252
    $group_summary = &$form['result']['results'][$group]['summary'];
253

    
254
    // Create table of assertions for the group.
255
    $rows = array();
256
    foreach ($assertions as $assertion) {
257
      $row = array();
258
      $row[] = $assertion->message;
259
      $row[] = $assertion->message_group;
260
      $row[] = drupal_basename($assertion->file);
261
      $row[] = $assertion->line;
262
      $row[] = $assertion->function;
263
      $row[] = simpletest_result_status_image($assertion->status);
264

    
265
      $class = 'simpletest-' . $assertion->status;
266
      if ($assertion->message_group == 'Debug') {
267
        $class = 'simpletest-debug';
268
      }
269
      $rows[] = array('data' => $row, 'class' => array($class));
270

    
271
      $group_summary['#' . $assertion->status]++;
272
      $form['result']['summary']['#' . $assertion->status]++;
273
    }
274
    $form['result']['results'][$group]['table'] = array(
275
      '#theme' => 'table',
276
      '#header' => $header,
277
      '#rows' => $rows,
278
    );
279

    
280
    // Set summary information.
281
    $group_summary['#ok'] = $group_summary['#fail'] + $group_summary['#exception'] == 0;
282
    $form['result']['results'][$group]['#collapsed'] = $group_summary['#ok'];
283

    
284
    // Store test group (class) as for use in filter.
285
    $filter[$group_summary['#ok'] ? 'pass' : 'fail'][] = $group;
286
  }
287

    
288
  // Overal summary status.
289
  $form['result']['summary']['#ok'] = $form['result']['summary']['#fail'] + $form['result']['summary']['#exception'] == 0;
290

    
291
  // Actions.
292
  $form['#action'] = url('admin/config/development/testing/results/re-run');
293
  $form['action'] = array(
294
    '#type' => 'fieldset',
295
    '#title' => t('Actions'),
296
    '#attributes' => array('class' => array('container-inline')),
297
    '#weight' => -11,
298
  );
299

    
300
  $form['action']['filter'] = array(
301
    '#type' => 'select',
302
    '#title' => 'Filter',
303
    '#options' => array(
304
      'all' => t('All (@count)', array('@count' => count($filter['pass']) + count($filter['fail']))),
305
      'pass' => t('Pass (@count)', array('@count' => count($filter['pass']))),
306
      'fail' => t('Fail (@count)', array('@count' => count($filter['fail']))),
307
    ),
308
  );
309
  $form['action']['filter']['#default_value'] = ($filter['fail'] ? 'fail' : 'all');
310

    
311
  // Categorized test classes for to be used with selected filter value.
312
  $form['action']['filter_pass'] = array(
313
    '#type' => 'hidden',
314
    '#default_value' => implode(',', $filter['pass']),
315
  );
316
  $form['action']['filter_fail'] = array(
317
    '#type' => 'hidden',
318
    '#default_value' => implode(',', $filter['fail']),
319
  );
320

    
321
  $form['action']['op'] = array(
322
    '#type' => 'submit',
323
    '#value' => t('Run tests'),
324
  );
325

    
326
  $form['action']['return'] = array(
327
    '#type' => 'link',
328
    '#title' => t('Return to list'),
329
    '#href' => 'admin/config/development/testing',
330
  );
331

    
332
  if (is_numeric($test_id)) {
333
    simpletest_clean_results_table($test_id);
334
  }
335

    
336
  return $form;
337
}
338

    
339
/**
340
 * Re-run the tests that match the filter.
341
 */
342
function simpletest_result_form_submit($form, &$form_state) {
343
  $pass = $form_state['values']['filter_pass'] ? explode(',', $form_state['values']['filter_pass']) : array();
344
  $fail = $form_state['values']['filter_fail'] ? explode(',', $form_state['values']['filter_fail']) : array();
345

    
346
  if ($form_state['values']['filter'] == 'all') {
347
    $classes = array_merge($pass, $fail);
348
  }
349
  elseif ($form_state['values']['filter'] == 'pass') {
350
    $classes = $pass;
351
  }
352
  else {
353
    $classes = $fail;
354
  }
355

    
356
  if (!$classes) {
357
    $form_state['redirect'] = 'admin/config/development/testing';
358
    return;
359
  }
360

    
361
  $form_state_execute = array('values' => array());
362
  foreach ($classes as $class) {
363
    $form_state_execute['values'][$class] = 1;
364
  }
365

    
366
  simpletest_test_form_submit(array(), $form_state_execute);
367
  $form_state['redirect'] = $form_state_execute['redirect'];
368
}
369

    
370
/**
371
 * Returns HTML for the summary status of a simpletest result.
372
 *
373
 * @param $variables
374
 *   An associative array containing:
375
 *   - form: A render element representing the form.
376
 *
377
 * @ingroup themeable
378
 */
379
function theme_simpletest_result_summary($variables) {
380
  $form = $variables['form'];
381
  return '<div class="simpletest-' . ($form['#ok'] ? 'pass' : 'fail') . '">' . _simpletest_format_summary_line($form) . '</div>';
382
}
383

    
384
/**
385
 * Get test results for $test_id.
386
 *
387
 * @param $test_id The test_id to retrieve results of.
388
 * @return Array of results grouped by test_class.
389
 */
390
function simpletest_result_get($test_id) {
391
  $results = db_select('simpletest')
392
    ->fields('simpletest')
393
    ->condition('test_id', $test_id)
394
    ->orderBy('test_class')
395
    ->orderBy('message_id')
396
    ->execute();
397

    
398
  $test_results = array();
399
  foreach ($results as $result) {
400
    if (!isset($test_results[$result->test_class])) {
401
      $test_results[$result->test_class] = array();
402
    }
403
    $test_results[$result->test_class][] = $result;
404
  }
405
  return $test_results;
406
}
407

    
408
/**
409
 * Get the appropriate image for the status.
410
 *
411
 * @param $status Status string, either: pass, fail, exception.
412
 * @return HTML image or false.
413
 */
414
function simpletest_result_status_image($status) {
415
  // $map does not use drupal_static() as its value never changes.
416
  static $map;
417

    
418
  if (!isset($map)) {
419
    $map = array(
420
      'pass' => theme('image', array('path' => 'misc/watchdog-ok.png', 'width' => 18, 'height' => 18, 'alt' => t('Pass'))),
421
      'fail' => theme('image', array('path' => 'misc/watchdog-error.png', 'width' => 18, 'height' => 18, 'alt' => t('Fail'))),
422
      'exception' => theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('Exception'))),
423
      'debug' => theme('image', array('path' => 'misc/watchdog-warning.png', 'width' => 18, 'height' => 18, 'alt' => t('Debug'))),
424
    );
425
  }
426
  if (isset($map[$status])) {
427
    return $map[$status];
428
  }
429
  return FALSE;
430
}
431

    
432
/**
433
 * Provides settings form for SimpleTest variables.
434
 *
435
 * @ingroup forms
436
 * @see simpletest_settings_form_validate()
437
 */
438
function simpletest_settings_form($form, &$form_state) {
439
  $form['general'] = array(
440
    '#type' => 'fieldset',
441
    '#title' => t('General'),
442
  );
443
  $form['general']['simpletest_clear_results'] = array(
444
    '#type' => 'checkbox',
445
    '#title' => t('Clear results after each complete test suite run'),
446
    '#description' => t('By default SimpleTest will clear the results after they have been viewed on the results page, but in some cases it may be useful to leave the results in the database. The results can then be viewed at <em>admin/config/development/testing/[test_id]</em>. The test ID can be found in the database, simpletest table, or kept track of when viewing the results the first time. Additionally, some modules may provide more analysis or features that require this setting to be disabled.'),
447
    '#default_value' => variable_get('simpletest_clear_results', TRUE),
448
  );
449
  $form['general']['simpletest_verbose'] = array(
450
    '#type' => 'checkbox',
451
    '#title' => t('Provide verbose information when running tests'),
452
    '#description' => t('The verbose data will be printed along with the standard assertions and is useful for debugging. The verbose data will be erased between each test suite run. The verbose data output is very detailed and should only be used when debugging.'),
453
    '#default_value' => variable_get('simpletest_verbose', TRUE),
454
  );
455

    
456
  $form['httpauth'] = array(
457
    '#type' => 'fieldset',
458
    '#title' => t('HTTP authentication'),
459
    '#description' => t('HTTP auth settings to be used by the SimpleTest browser during testing. Useful when the site requires basic HTTP authentication.'),
460
    '#collapsible' => TRUE,
461
    '#collapsed' => TRUE,
462
  );
463
  $form['httpauth']['simpletest_httpauth_method'] = array(
464
    '#type' => 'select',
465
    '#title' => t('Method'),
466
    '#options' => array(
467
      CURLAUTH_BASIC => t('Basic'),
468
      CURLAUTH_DIGEST => t('Digest'),
469
      CURLAUTH_GSSNEGOTIATE => t('GSS negotiate'),
470
      CURLAUTH_NTLM => t('NTLM'),
471
      CURLAUTH_ANY => t('Any'),
472
      CURLAUTH_ANYSAFE => t('Any safe'),
473
    ),
474
    '#default_value' => variable_get('simpletest_httpauth_method', CURLAUTH_BASIC),
475
  );
476
  $username = variable_get('simpletest_httpauth_username');
477
  $password = variable_get('simpletest_httpauth_password');
478
  $form['httpauth']['simpletest_httpauth_username'] = array(
479
    '#type' => 'textfield',
480
    '#title' => t('Username'),
481
    '#default_value' => $username,
482
  );
483
  if ($username && $password) {
484
    $form['httpauth']['simpletest_httpauth_username']['#description'] = t('Leave this blank to delete both the existing username and password.');
485
  }
486
  $form['httpauth']['simpletest_httpauth_password'] = array(
487
    '#type' => 'password',
488
    '#title' => t('Password'),
489
  );
490
  if ($password) {
491
    $form['httpauth']['simpletest_httpauth_password']['#description'] = t('To change the password, enter the new password here.');
492
  }
493

    
494
  return system_settings_form($form);
495
}
496

    
497
/**
498
 * Validation handler for simpletest_settings_form().
499
 */
500
function simpletest_settings_form_validate($form, &$form_state) {
501
  // If a username was provided but a password wasn't, preserve the existing
502
  // password.
503
  if (!empty($form_state['values']['simpletest_httpauth_username']) && empty($form_state['values']['simpletest_httpauth_password'])) {
504
    $form_state['values']['simpletest_httpauth_password'] = variable_get('simpletest_httpauth_password', '');
505
  }
506

    
507
  // If a password was provided but a username wasn't, the credentials are
508
  // incorrect, so throw an error.
509
  if (empty($form_state['values']['simpletest_httpauth_username']) && !empty($form_state['values']['simpletest_httpauth_password'])) {
510
    form_set_error('simpletest_httpauth_username', t('HTTP authentication credentials must include a username in addition to a password.'));
511
  }
512
}
513