simpletest.module 18.5 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
/**
 * @file
 * Provides testing functionality.
 */

9
/**
10
 * Implements hook_help().
11
12
13
14
 */
function simpletest_help($path, $arg) {
  switch ($path) {
    case 'admin/help#simpletest':
15
16
17
18
19
20
21
22
23
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Testing module provides a framework for running automated unit tests. It can be used to verify a working state of Drupal before and after any code changes, or as a means for developers to write and execute tests for their modules. For more information, see the online handbook entry for <a href="@simpletest">Testing module</a>.', array('@simpletest' => 'http://drupal.org/handbook/modules/simpletest', '@blocks' => url('admin/structure/block'))) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Running tests') . '</dt>';
      $output .= '<dd>' . t('Visit the <a href="@admin-simpletest">Testing page</a> to display a list of available tests. For comprehensive testing, select <em>all</em> tests, or individually select tests for more targeted testing. Note that it might take several minutes for all tests to complete. For more information on creating and modifying your own tests, see the <a href="@simpletest-api">Testing API Documentation</a> in the Drupal handbook.', array('@simpletest-api' => 'http://drupal.org/simpletest', '@admin-simpletest' => url('admin/config/development/testing'))) . '</dd>';
      $output .= '<dd>' . t('After the tests run, a message will be displayed next to each test group indicating whether tests within it passed, failed, or had exceptions. A pass means that the test returned the expected results, while fail means that it did not. An exception normally indicates an error outside of the test, such as a PHP warning or notice. If there were failures or exceptions, the results will be expanded to show details, and the tests that had failures or exceptions will be indicated in red or pink rows. You can then use these results to refine your code and tests, until all tests pass.') . '</dd>';
      $output .= '</dl>';
24
25
26
27
28
      return $output;
  }
}

/**
29
 * Implements hook_menu().
30
31
 */
function simpletest_menu() {
32
  $items['admin/config/development/testing'] = array(
33
    'title' => 'Testing',
34
35
    'page callback' => 'drupal_get_form',
    'page arguments' => array('simpletest_test_form'),
36
37
    'description' => 'Run tests against Drupal core and your active modules. These tests help assure that your site code is working as designed.',
    'access arguments' => array('administer unit tests'),
38
    'file' => 'simpletest.pages.inc',
39
  );
40
  $items['admin/config/development/testing/list'] = array(
41
42
43
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
44
  $items['admin/config/development/testing/settings'] = array(
45
46
47
48
49
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('simpletest_settings_form'),
    'access arguments' => array('administer unit tests'),
    'type' => MENU_LOCAL_TASK,
50
    'file' => 'simpletest.pages.inc',
51
  );
52
  $items['admin/config/development/testing/results/%'] = array(
53
54
    'title' => 'Test result',
    'page callback' => 'drupal_get_form',
55
    'page arguments' => array('simpletest_result_form', 5),
56
57
58
    'description' => 'View result of tests.',
    'access arguments' => array('administer unit tests'),
    'type' => MENU_CALLBACK,
59
    'file' => 'simpletest.pages.inc',
60
  );
61
62
63
64
  return $items;
}

/**
65
 * Implements hook_permission().
66
 */
67
function simpletest_permission() {
68
  return array(
69
    'administer unit tests' => array(
70
71
      'title' => t('Administer tests'),
      'description' => theme('placeholder', array('text' => t('Warning: Give to trusted roles only; this permission has security implications.'))),
72
    ),
73
74
75
76
  );
}

/**
77
 * Implements hook_theme().
78
79
80
 */
function simpletest_theme() {
  return array(
81
    'simpletest_test_table' => array(
82
      'render element' => 'table',
83
      'file' => 'simpletest.pages.inc',
84
85
    ),
    'simpletest_result_summary' => array(
86
      'render element' => 'form',
87
      'file' => 'simpletest.pages.inc',
88
89
90
91
    ),
  );
}

92
/**
93
 * Implements hook_stream_wrappers().
94
95
96
97
98
99
100
101
102
103
104
 */
function simpletest_test_stream_wrappers() {
  return array(
    'simpletest' => array(
      'name' => t('Simpletest files'),
      'class' => 'DrupalSimpleTestStreamWrapper',
      'description' => t('Stream Wrapper for Simpletest files.'),
    ),
  );
}

105
/**
106
 * Implements hook_js_alter().
107
108
109
110
111
112
113
114
115
116
 */
function simpletest_js_alter(&$javascript) {
  // Since SimpleTest is a special use case for the table select, stick the
  // SimpleTest JavaScript above the table select.
  $simpletest = drupal_get_path('module', 'simpletest') . '/simpletest.js';
  if (array_key_exists($simpletest, $javascript) && array_key_exists('misc/tableselect.js', $javascript)) {
    $javascript[$simpletest]['weight'] = $javascript['misc/tableselect.js']['weight'] - 1;
  }
}

117
function _simpletest_format_summary_line($summary) {
118
  $args = array(
119
120
121
    '@pass' => format_plural(isset($summary['#pass']) ? $summary['#pass'] : 0, '1 pass', '@count passes'),
    '@fail' => format_plural(isset($summary['#fail']) ? $summary['#fail'] : 0, '1 fail', '@count fails'),
    '@exception' => format_plural(isset($summary['#exception']) ? $summary['#exception'] : 0, '1 exception', '@count exceptions'),
122
123
124
125
126
127
  );
  if (!$summary['#debug']) {
    return t('@pass, @fail, and @exception', $args);
  }
  $args['@debug'] = format_plural(isset($summary['#debug']) ? $summary['#debug'] : 0, '1 debug message', '@count debug messages');
  return t('@pass, @fail, @exception, and @debug', $args);
128
129
}

130
/**
131
132
 * Actually runs tests.
 *
133
134
135
136
137
 * @param $test_list
 *   List of tests to run.
 * @param $reporter
 *   Which reporter to use. Allowed values are: text, xml, html and drupal,
 *   drupal being the default.
138
 */
139
function simpletest_run_tests($test_list, $reporter = 'drupal') {
140
  cache_clear_all();
141
142
143
  $test_id = db_insert('simpletest_test_id')
    ->useDefaults(array('test_id'))
    ->execute();
144

145
146
  // Clear out the previous verbose files.
  file_unmanaged_delete_recursive(file_directory_path() . '/simpletest/verbose');
147

148
149
150
151
152
153
  // Get the info for the first test being run.
  $first_test = array_shift($test_list);
  $first_instance = new $first_test();
  array_unshift($test_list, $first_test);
  $info = $first_instance->getInfo();

154
  $batch = array(
155
    'title' => t('Running tests'),
156
157
    'operations' => array(
      array('_simpletest_batch_operation', array($test_list, $test_id)),
158
    ),
159
    'finished' => '_simpletest_batch_finished',
160
    'progress_message' => '',
161
    'css' => array(drupal_get_path('module', 'simpletest') . '/simpletest.css'),
162
    'init_message' => t('Processing test @num of @max - %test.', array('%test' => $info['name'], '@num' => '1', '@max' => count($test_list))),
163
164
  );
  batch_set($batch);
165

166
  module_invoke_all('test_group_started');
167

168
169
170
171
172
173
  // Normally, the forms portion of the batch API takes care of calling
  // batch_process(), but in the process it saves the whole $form into the
  // database (which is huge for the test selection form).
  // By calling batch_process() directly, we skip that behavior and ensure
  // that we don't exceed the size of data that can be sent to the database
  // (max_allowed_packet on MySQL).
174
  batch_process('admin/config/development/testing/results/' . $test_id);
175
176
177
178
179
180
181
182
183
184
185
}

/**
 * Batch operation callback.
 */
function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
  // Get working values.
  if (!isset($context['sandbox']['max'])) {
    // First iteration: initialize working values.
    $test_list = $test_list_init;
    $context['sandbox']['max'] = count($test_list);
186
    $test_results = array('#pass' => 0, '#fail' => 0, '#exception' => 0, '#debug' => 0);
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  }
  else {
    // Nth iteration: get the current values where we last stored them.
    $test_list = $context['sandbox']['tests'];
    $test_results = $context['sandbox']['test_results'];
  }
  $max = $context['sandbox']['max'];

  // Perform the next test.
  $test_class = array_shift($test_list);
  $test = new $test_class($test_id);
  $test->run();
  $size = count($test_list);
  $info = $test->getInfo();

202
203
  module_invoke_all('test_finished', $test->results);

204
  // Gather results and compose the report.
205
  $test_results[$test_class] = $test->results;
206
207
208
209
210
211
  foreach ($test_results[$test_class] as $key => $value) {
    $test_results[$key] += $value;
  }
  $test_results[$test_class]['#name'] = $info['name'];
  $items = array();
  foreach (element_children($test_results) as $class) {
212
    array_unshift($items, '<div class="simpletest-' . ($test_results[$class]['#fail'] + $test_results[$class]['#exception'] ? 'fail' : 'pass') . '">' . t('@name: @summary', array('@name' => $test_results[$class]['#name'], '@summary' => _simpletest_format_summary_line($test_results[$class]))) . '</div>');
213
  }
214
215
  $context['message'] = t('Processed test @num of @max - %test.', array('%test' => $info['name'], '@num' => $max - $size, '@max' => $max));
  $context['message'] .= '<div class="simpletest-' . ($test_results['#fail'] + $test_results['#exception'] ? 'fail' : 'pass') . '">Overall results: ' . _simpletest_format_summary_line($test_results) . '</div>';
216
  $context['message'] .= theme('item_list', array('items' => $items));
217
218
219
220
221
222
223
224
225
226
227

  // Save working values for the next iteration.
  $context['sandbox']['tests'] = $test_list;
  $context['sandbox']['test_results'] = $test_results;
  // The test_id is the only thing we need to save for the report page.
  $context['results']['test_id'] = $test_id;

  // Multistep processing: report progress.
  $context['finished'] = 1 - $size / $max;
}

228
function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
229
  if ($success) {
230
    drupal_set_message(t('The test run finished in @elapsed.', array('@elapsed' => $elapsed)));
231
232
  }
  else {
233
    // Use the test_id passed as a parameter to _simpletest_batch_operation().
234
235
236
237
238
239
240
241
    $test_id = $operations[0][1][1];

    // Retrieve the last database prefix used for testing and the last test
    // class that was run from. Use the information to read the lgo file
    // in case any fatal errors caused the test to crash.
    list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
    simpletest_log_read($test_id, $last_prefix, $last_test_class);

242

243
    drupal_set_message(t('The test run did not successfully finish.'), 'error');
244
    drupal_set_message(t('Please use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning');
245
  }
246
  module_invoke_all('test_group_finished');
247
248
}

249
250
251
252
253
254
255
256
257
258
/*
 * Get information about the last test that ran given a test ID.
 *
 * @param $test_id
 *   The test ID to get the last test from.
 * @return
 *   Array containing the last database prefix used and the last test class
 *   that ran.
 */
function simpletest_last_test_get($test_id) {
259
260
  $last_prefix = db_query_range('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, array(':test_id' => $test_id))->fetchField();
  $last_test_class = db_query_range('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, array(':test_id' => $test_id))->fetchField();
261
262
263
  return array($last_prefix, $last_test_class);
}

264
265
266
267
268
269
270
/**
 * Read the error log and report any errors as assertion failures.
 *
 * The errors in the log should only be fatal errors since any other errors
 * will have been recorded by the error handler.
 *
 * @param $test_id
271
272
273
274
275
276
277
278
279
280
 *   The test ID to which the log relates.
 * @param $prefix
 *   The database prefix to which the log relates.
 * @param $test_class
 *   The test class to which the log relates.
 * @param $during_test
 *   Indicates that the current file directory path is a temporary file
 *   file directory used during testing.
 * @return
 *   Found any entries in log.
281
 */
282
function simpletest_log_read($test_id, $prefix, $test_class, $during_test = FALSE) {
283
  $log = 'public://' . ($during_test ? '' : '/simpletest/' . substr($prefix, 10)) . '/error.log';
284
  $found = FALSE;
285
286
  if (file_exists($log)) {
    foreach (file($log) as $line) {
287
288
289
      if (preg_match('/\[.*?\] (.*?): (.*?) in (.*) on line (\d+)/', $line, $match)) {
        // Parse PHP fatal errors for example: PHP Fatal error: Call to
        // undefined function break_me() in /path/to/file.php on line 17
290
        $caller = array(
291
292
          'line' => $match[4],
          'file' => $match[3],
293
        );
294
        DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
295
296
      }
      else {
297
298
        // Unkown format, place the entire message in the log.
        DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
299
      }
300
      $found = TRUE;
301
302
    }
  }
303
  return $found;
304
305
}

306
/**
307
308
309
310
311
312
 * Get a list of all of the tests provided by the system.
 *
 * The list of test classes is loaded from the registry where it looks for
 * files ending in ".test". Once loaded the test list is cached and stored in
 * a static variable. In order to list tests provided by disabled modules
 * hook_registry_files_alter() is used to forcefully add them to the registry.
313
314
 *
 * @return
315
316
317
318
319
320
321
322
323
324
325
326
327
328
 *   An array of tests keyed with the groups specified in each of the tests
 *   getInfo() method and then keyed by the test class. An example of the array
 *   structure is provided below.
 *
 *   @code
 *     $groups['Blog'] => array(
 *       'BlogTestCase' => array(
 *         'name' => 'Blog functionality',
 *         'description' => 'Create, view, edit, delete, ...',
 *         'group' => 'Blog',
 *       ),
 *     );
 *   @endcode
 * @see simpletest_registry_files_alter()
329
 */
330
331
function simpletest_test_get_all() {
  $groups = &drupal_static(__FUNCTION__);
332

333
334
335
336
337
  if (!$groups) {
    // Load test information from cache if available, otherwise retrieve the
    // information from each tests getInfo() method.
    if ($cache = cache_get('simpletest', 'cache')) {
      $groups = $cache->data;
338
    }
339
340
    else {
      // Select all clases in files ending with .test.
341
      $classes = db_query("SELECT name FROM {registry} WHERE type = :type AND filename LIKE :name", array(':type' => 'class', ':name' => '%.test'));
342
343
344

      // Check that each class has a getInfo() method and store the information
      // in an array keyed with the group specified in the test information.
345
      $groups = array();
346
347
      foreach ($classes as $class) {
        $class = $class->name;
348
        // Test classes need to implement getInfo() to be valid.
349
350
351
        if (class_exists($class) && method_exists($class, 'getInfo')) {
          $info = call_user_func(array($class, 'getInfo'));

352
353
354
355
356
357
358
          // If this test class requires a non-existing module, skip it.
          if (!empty($info['dependencies'])) {
            foreach ($info['dependencies'] as $module) {
              if (!drupal_get_filename('module', $module)) {
                continue 2;
              }
            }
359
          }
360

361
362
363
          $groups[$info['group']][$class] = $info;
        }
      }
364

365
366
367
368
      // Sort the groups and tests within the groups by name.
      uksort($groups, 'strnatcasecmp');
      foreach ($groups as $group => &$tests) {
        uksort($tests, 'strnatcasecmp');
369
      }
370
371

      cache_set('simpletest', $groups);
372
    }
373
  }
374
  return $groups;
375
}
376

377
/**
378
 * Implements hook_registry_files_alter().
379
 *
380
381
 * Add the test files for disabled modules so that we get a list containing
 * all the avialable tests.
382
 */
383
384
385
386
387
388
function simpletest_registry_files_alter(&$files, $modules) {
  foreach ($modules as $module) {
    // Only add test files for disabled modules, as enabled modules should
    // already include any test files they provide.
    if (!$module->status) {
      $dir = $module->dir;
389
390
391
392
393
      if (!empty($module->info['files'])) {
        foreach ($module->info['files'] as $file) {
          if (substr($file, -5) == '.test') {
            $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
          }
394
395
396
        }
      }
    }
397
  }
398
399
400
401
402
403
404
405
}

/**
 * Remove all temporary database tables and directories.
 */
function simpletest_clean_environment() {
  simpletest_clean_database();
  simpletest_clean_temporary_directories();
406
407
408
409
410
411
412
  if (variable_get('simpletest_clear_results', TRUE)) {
    $count = simpletest_clean_results_table();
    drupal_set_message(format_plural($count, 'Removed 1 test result.', 'Removed @count test results.'));
  }
  else {
    drupal_set_message(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
  }
413
414
415
416

  // Detect test classes that have been added, renamed or deleted.
  registry_rebuild();
  cache_clear_all('simpletest', 'cache');
417
418
419
}

/**
420
 * Removed prefixed tables from the database that are left over from crashed tests.
421
422
 */
function simpletest_clean_database() {
423
  $tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
424
  $schema = drupal_get_schema_unprocessed('simpletest');
425
  $count = 0;
426
  foreach (array_diff_key($tables, $schema) as $table) {
427
428
    // Strip the prefix and skip tables without digits following "simpletest",
    // e.g. {simpletest_test_id}.
429
    if (preg_match('/simpletest\d+.*/', $table, $matches)) {
430
      db_drop_table($matches[0]);
431
      $count++;
432
    }
433
434
  }

435
436
  if ($count > 0) {
    drupal_set_message(format_plural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
437
438
  }
  else {
439
    drupal_set_message(t('No leftover tables to remove.'));
440
441
442
443
  }
}

/**
444
 * Find all leftover temporary directories and remove them.
445
446
 */
function simpletest_clean_temporary_directories() {
447
  $files = scandir('public://simpletest');
448
449
  $count = 0;
  foreach ($files as $file) {
450
451
    $path = 'public://simpletest/' . $file;
    if (is_dir($path) && is_numeric($file)) {
452
      file_unmanaged_delete_recursive($path);
453
454
455
456
457
      $count++;
    }
  }

  if ($count > 0) {
458
    drupal_set_message(format_plural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
459
460
461
462
463
464
  }
  else {
    drupal_set_message(t('No temporary directories to remove.'));
  }
}

465
/**
466
467
468
469
470
 * Clear the test result tables.
 *
 * @param $test_id
 *   Test ID to remove results for, or NULL to remove all results.
 * @return
471
 *   The number of results removed.
472
 */
473
function simpletest_clean_results_table($test_id = NULL) {
474
  if (variable_get('simpletest_clear_results', TRUE)) {
475
    if ($test_id) {
476
      $count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
477

478
      db_delete('simpletest')
479
480
        ->condition('test_id', $test_id)
        ->execute();
481
      db_delete('simpletest_test_id')
482
483
484
485
        ->condition('test_id', $test_id)
        ->execute();
    }
    else {
486
      $count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
487

488
      // Clear test results.
489
490
      db_delete('simpletest')->execute();
      db_delete('simpletest_test_id')->execute();
491
    }
492

493
    return $count;
494
  }
495
  return 0;
496
}