Simpletest Coverage - modules/simpletest/simpletest.module

1 <?php
2 // $Id: simpletest.module,v 1.66 2009/08/15 17:52:53 webchick Exp $
3
4 /**
5 * @file
6 * Provides testing functionality.
7 */
8
9 /**
10 * Implement hook_help().
11 */
12 function simpletest_help($path, $arg) {
13 switch ($path) {
14 case 'admin/help#simpletest':
15 $output = '<p>' . t('The SimpleTest module is a framework for running automated unit tests in Drupal. 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.') . '</p>';
16 $output .= '<p>' . t('Visit <a href="@admin-simpletest">Administer >> Structure >> SimpleTest</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.)', array('@admin-simpletest' => url('admin/config/development/testing'))) . '</p>';
17 $output .= '<p>' . t('After the tests have 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 a 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 fails or exceptions, the results are expanded, and the tests that had issues will be indicated in red or pink rows. Use these results to refine your code and tests until all tests return a pass.') . '</p>';
18 $output .= '<p>' . t('For more information on creating and modifying your own tests, see the <a href="@simpletest-api">SimpleTest API Documentation</a> in the Drupal handbook.', array('@simpletest-api' => 'http://drupal.org/simpletest')) . '</p>';
19 $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@simpletest">SimpleTest module</a>.', array('@simpletest' => 'http://drupal.org/handbook/modules/simpletest')) . '</p>';
20 return $output;
21 }
22 }
23
24 /**
25 * Implement hook_menu().
26 */
27 function simpletest_menu() {
28 $items['admin/config/development/testing'] = array(
29 'title' => 'Testing',
30 'page callback' => 'drupal_get_form',
31 'page arguments' => array('simpletest_test_form'),
32 'description' => 'Run tests against Drupal core and your active modules. These tests help assure that your site code is working as designed.',
33 'access arguments' => array('administer unit tests'),
34 );
35 $items['admin/config/development/testing/list'] = array(
36 'title' => 'List',
37 'type' => MENU_DEFAULT_LOCAL_TASK,
38 );
39 $items['admin/config/development/testing/settings'] = array(
40 'title' => 'Settings',
41 'page callback' => 'drupal_get_form',
42 'page arguments' => array('simpletest_settings_form'),
43 'access arguments' => array('administer unit tests'),
44 'type' => MENU_LOCAL_TASK,
45 );
46 $items['admin/config/development/testing/results/%'] = array(
47 'title' => 'Test result',
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('simpletest_result_form', 5),
50 'description' => 'View result of tests.',
51 'access arguments' => array('administer unit tests'),
52 'type' => MENU_CALLBACK,
53 );
54 return $items;
55 }
56
57 /**
58 * Implement hook_permission().
59 */
60 function simpletest_permission() {
61 return array(
62 'administer unit tests' => array(
63 'title' => t('Administer unit tests'),
64 'description' => t('Manage and run automated testing. %warning', array('%warning' => t('Warning: Give to trusted roles only; this permission has security implications.'))),
65 ),
66 );
67 }
68
69 /**
70 * Implement hook_theme().
71 */
72 function simpletest_theme() {
73 return array(
74 'simpletest_test_table' => array(
75 'arguments' => array('table' => NULL),
76 'file' => 'simpletest.pages.inc',
77 ),
78 'simpletest_result_summary' => array(
79 'arguments' => array('form' => NULL),
80 'file' => 'simpletest.pages.inc',
81 ),
82 );
83 }
84
85 /**
86 * Implement hook_js_alter().
87 */
88 function simpletest_js_alter(&$javascript) {
89 // Since SimpleTest is a special use case for the table select, stick the
90 // SimpleTest JavaScript above the table select.
91 $simpletest = drupal_get_path('module', 'simpletest') . '/simpletest.js';
92 if (array_key_exists($simpletest, $javascript) && array_key_exists('misc/tableselect.js', $javascript)) {
93 $javascript[$simpletest]['weight'] = $javascript['misc/tableselect.js']['weight'] - 1;
94 }
95 }
96
97 function _simpletest_format_summary_line($summary) {
98 $args = array(
99 '@pass' => format_plural(isset($summary['#pass']) ? $summary['#pass'] : 0, '1 pass', '@count passes'),
100 '@fail' => format_plural(isset($summary['#fail']) ? $summary['#fail'] : 0, '1 fail', '@count fails'),
101 '@exception' => format_plural(isset($summary['#exception']) ? $summary['#exception'] : 0, '1 exception', '@count exceptions'),
102 );
103 if (!$summary['#debug']) {
104 return t('@pass, @fail, and @exception', $args);
105 }
106 $args['@debug'] = format_plural(isset($summary['#debug']) ? $summary['#debug'] : 0, '1 debug message', '@count debug messages');
107 return t('@pass, @fail, @exception, and @debug', $args);
108 }
109
110 /**
111 * Actually runs tests.
112 *
113 * @param $test_list
114 * List of tests to run.
115 * @param $reporter
116 * Which reporter to use. Allowed values are: text, xml, html and drupal,
117 * drupal being the default.
118 */
119 function simpletest_run_tests($test_list, $reporter = 'drupal') {
120 cache_clear_all();
121 $test_id = db_insert('simpletest_test_id')
122 ->useDefaults(array('test_id'))
123 ->execute();
124
125 // Clear out the previous verbose files.
126 file_unmanaged_delete_recursive(file_directory_path() . '/simpletest/verbose');
127
128 // Get the info for the first test being run.
129 $first_test = array_shift($test_list);
130 $first_instance = new $first_test();
131 array_unshift($test_list, $first_test);
132 $info = $first_instance->getInfo();
133
134 $batch = array(
135 'title' => t('Running SimpleTests'),
136 'operations' => array(
137 array('_simpletest_batch_operation', array($test_list, $test_id)),
138 ),
139 'finished' => '_simpletest_batch_finished',
140 'progress_message' => '',
141 'css' => array(drupal_get_path('module', 'simpletest') . '/simpletest.css'),
142 'init_message' => t('Processing test @num of @max - %test.', array('%test' => $info['name'], '@num' => '1', '@max' => count($test_list))),
143 );
144 batch_set($batch);
145
146 module_invoke_all('test_group_started');
147
148 // Normally, the forms portion of the batch API takes care of calling
149 // batch_process(), but in the process it saves the whole $form into the
150 // database (which is huge for the test selection form).
151 // By calling batch_process() directly, we skip that behavior and ensure
152 // that we don't exceed the size of data that can be sent to the database
153 // (max_allowed_packet on MySQL).
154 batch_process('admin/config/development/testing/results/' . $test_id);
155 }
156
157 /**
158 * Batch operation callback.
159 */
160 function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
161 // Get working values.
162 if (!isset($context['sandbox']['max'])) {
163 // First iteration: initialize working values.
164 $test_list = $test_list_init;
165 $context['sandbox']['max'] = count($test_list);
166 $test_results = array('#pass' => 0, '#fail' => 0, '#exception' => 0, '#debug' => 0);
167 }
168 else {
169 // Nth iteration: get the current values where we last stored them.
170 $test_list = $context['sandbox']['tests'];
171 $test_results = $context['sandbox']['test_results'];
172 }
173 $max = $context['sandbox']['max'];
174
175 // Perform the next test.
176 $test_class = array_shift($test_list);
177 $test = new $test_class($test_id);
178 $test->run();
179 $size = count($test_list);
180 $info = $test->getInfo();
181
182 module_invoke_all('test_finished', $test->results);
183
184 // Gather results and compose the report.
185 $test_results[$test_class] = $test->results;
186 foreach ($test_results[$test_class] as $key => $value) {
187 $test_results[$key] += $value;
188 }
189 $test_results[$test_class]['#name'] = $info['name'];
190 $items = array();
191 foreach (element_children($test_results) as $class) {
192 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>');
193 }
194 $context['message'] = t('Processed test @num of @max - %test.', array('%test' => $info['name'], '@num' => $max - $size, '@max' => $max));
195 $context['message'] .= '<div class="simpletest-' . ($test_results['#fail'] + $test_results['#exception'] ? 'fail' : 'pass') . '">Overall results: ' . _simpletest_format_summary_line($test_results) . '</div>';
196 $context['message'] .= theme('item_list', $items);
197
198 // Save working values for the next iteration.
199 $context['sandbox']['tests'] = $test_list;
200 $context['sandbox']['test_results'] = $test_results;
201 // The test_id is the only thing we need to save for the report page.
202 $context['results']['test_id'] = $test_id;
203
204 // Multistep processing: report progress.
205 $context['finished'] = 1 - $size / $max;
206 }
207
208 function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
209 if ($success) {
210 drupal_set_message(t('The test run finished in @elapsed.', array('@elapsed' => $elapsed)));
211 }
212 else {
213 // Use the test_id passed as a parameter to _simpletest_batch_operation().
214 $test_id = $operations[0][1][1];
215
216 // Retrieve the last database prefix used for testing and the last test
217 // class that was run from. Use the information to read the lgo file
218 // in case any fatal errors caused the test to crash.
219 list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
220 simpletest_log_read($test_id, $last_prefix, $last_test_class);
221
222
223 drupal_set_message(t('The test run did not successfully finish.'), 'error');
224 drupal_set_message(t('Please use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning');
225 }
226 module_invoke_all('test_group_finished');
227 }
228
229 /*
230 * Get information about the last test that ran given a test ID.
231 *
232 * @param $test_id
233 * The test ID to get the last test from.
234 * @return
235 * Array containing the last database prefix used and the last test class
236 * that ran.
237 */
238 function simpletest_last_test_get($test_id) {
239 $last_prefix = db_result(db_query_range('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id), 0, 1));
240 $last_test_class = db_result(db_query_range('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', array(':test_id' => $test_id), 0, 1));
241 return array($last_prefix, $last_test_class);
242 }
243
244 /**
245 * Read the error log and report any errors as assertion failures.
246 *
247 * The errors in the log should only be fatal errors since any other errors
248 * will have been recorded by the error handler.
249 *
250 * @param $test_id
251 * The test ID to which the log relates.
252 * @param $prefix
253 * The database prefix to which the log relates.
254 * @param $test_class
255 * The test class to which the log relates.
256 * @param $during_test
257 * Indicates that the current file directory path is a temporary file
258 * file directory used during testing.
259 * @return
260 * Found any entries in log.
261 */
262 function simpletest_log_read($test_id, $prefix, $test_class, $during_test = FALSE) {
263 $log = file_directory_path() . ($during_test ? '' : '/simpletest/' . substr($prefix, 10)) . '/error.log';
264 $found = FALSE;
265 if (file_exists($log)) {
266 foreach (file($log) as $line) {
267 if (preg_match('/\[.*?\] (.*?): (.*?) in (.*) on line (\d+)/', $line, $match)) {
268 // Parse PHP fatal errors for example: PHP Fatal error: Call to
269 // undefined function break_me() in /path/to/file.php on line 17
270 $caller = array(
271 'line' => $match[4],
272 'file' => $match[3],
273 );
274 DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
275 }
276 else {
277 // Unkown format, place the entire message in the log.
278 DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
279 }
280 $found = TRUE;
281 }
282 }
283 return $found;
284 }
285
286 /**
287 * Get a list of all of the tests provided by the system.
288 *
289 * The list of test classes is loaded from the registry where it looks for
290 * files ending in ".test". Once loaded the test list is cached and stored in
291 * a static variable. In order to list tests provided by disabled modules
292 * hook_registry_files_alter() is used to forcefully add them to the registry.
293 *
294 * @return
295 * An array of tests keyed with the groups specified in each of the tests
296 * getInfo() method and then keyed by the test class. An example of the array
297 * structure is provided below.
298 *
299 * @code
300 * $groups['Blog'] => array(
301 * 'BlogTestCase' => array(
302 * 'name' => 'Blog functionality',
303 * 'description' => 'Create, view, edit, delete, ...',
304 * 'group' => 'Blog',
305 * ),
306 * );
307 * @endcode
308 * @see simpletest_registry_files_alter()
309 */
310 function simpletest_test_get_all() {
311 $groups = &drupal_static(__FUNCTION__);
312
313 if (!$groups) {
314 // Load test information from cache if available, otherwise retrieve the
315 // information from each tests getInfo() method.
316 if ($cache = cache_get('simpletest', 'cache')) {
317 $groups = $cache->data;
318 }
319 else {
320 // Select all clases in files ending with .test.
321 $classes = db_select('registry')
322 ->fields('registry', array('name'))
323 ->condition('type', 'class')
324 ->condition('filename', '%.test', 'LIKE')
325 ->execute();
326
327 $groups = array();
328
329 // Check that each class has a getInfo() method and store the information
330 // in an array keyed with the group specified in the test information.
331 foreach ($classes as $class) {
332 $class = $class->name;
333 if (class_exists($class) && method_exists($class, 'getInfo')) {
334 // Valid test class, retrieve test information.
335 $info = call_user_func(array($class, 'getInfo'));
336
337 // Initialize test groups.
338 if (!isset($groups[$info['group']])) {
339 $groups[$info['group']] = array();
340 }
341 $groups[$info['group']][$class] = $info;
342 }
343 }
344 // Sort the groups and tests within the groups by name.
345 uksort($groups, 'strnatcasecmp');
346 foreach ($groups as $group => &$tests) {
347 uksort($tests, 'strnatcasecmp');
348 }
349
350 cache_set('simpletest', $groups);
351 }
352 }
353 return $groups;
354 }
355
356 /**
357 * Implementation of hook_registry_files_alter().
358 *
359 * Add the test files for disabled modules so that we get a list containing
360 * all the avialable tests.
361 */
362 function simpletest_registry_files_alter(&$files, $modules) {
363 foreach ($modules as $module) {
364 // Only add test files for disabled modules, as enabled modules should
365 // already include any test files they provide.
366 if (!$module->status) {
367 $dir = $module->dir;
368 if (!empty($module->info['files'])) {
369 foreach ($module->info['files'] as $file) {
370 if (substr($file, -5) == '.test') {
371 $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
372 }
373 }
374 }
375 }
376 }
377 }
378
379 /**
380 * Remove all temporary database tables and directories.
381 */
382 function simpletest_clean_environment() {
383 simpletest_clean_database();
384 simpletest_clean_temporary_directories();
385 $count = simpletest_clean_results_table();
386 drupal_set_message(format_plural($count, 'Removed 1 test result.', 'Removed @count test results.'));
387 }
388
389 /**
390 * Removed prefixed tables from the database that are left over from crashed tests.
391 */
392 function simpletest_clean_database() {
393 $tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
394 $schema = drupal_get_schema_unprocessed('simpletest');
395 $ret = array();
396 foreach (array_diff_key($tables, $schema) as $table) {
397 // Strip the prefix and skip tables without digits following "simpletest",
398 // e.g. {simpletest_test_id}.
399 if (preg_match('/simpletest\d+.*/', $table, $matches)) {
400 db_drop_table($ret, $matches[0]);
401 }
402 }
403
404 if (count($ret) > 0) {
405 drupal_set_message(format_plural(count($ret), 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
406 }
407 else {
408 drupal_set_message(t('No leftover tables to remove.'));
409 }
410 }
411
412 /**
413 * Find all leftover temporary directories and remove them.
414 */
415 function simpletest_clean_temporary_directories() {
416 $directory = file_directory_path() . '/simpletest';
417 $files = scandir($directory);
418 $count = 0;
419 foreach ($files as $file) {
420 $path = "$directory/$file";
421 if (is_dir($path) && is_numeric($file)) {
422 file_unmanaged_delete_recursive($path);
423 $count++;
424 }
425 }
426
427 if ($count > 0) {
428 drupal_set_message(format_plural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
429 }
430 else {
431 drupal_set_message(t('No temporary directories to remove.'));
432 }
433 }
434
435 /**
436 * Clear the test result tables.
437 *
438 * @param $test_id
439 * Test ID to remove results for, or NULL to remove all results.
440 * @return
441 * The number of results removed or FALSE.
442 */
443 function simpletest_clean_results_table($test_id = NULL) {
444 if (variable_get('simpletest_clear_results', TRUE)) {
445 if ($test_id) {
446 $count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
447
448 db_delete("simpletest")
449 ->condition('test_id', $test_id)
450 ->execute();
451 db_delete("simpletest_test_id")
452 ->condition('test_id', $test_id)
453 ->execute();
454 }
455 else {
456 $count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
457
458 // Clear test results.
459 db_delete("simpletest")->execute();
460 db_delete("simpletest_test_id")->execute();
461 }
462
463 return $count;
464 }
465 return FALSE;
466 }
467

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.