| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
function simpletest_help($path, $arg) {
|
| 13 |
|
| 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 |
|
| 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 |
|
| 59 |
|
| 60 |
function simpletest_permission() {
|
| 61 |
|
| 62 |
|
| 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 |
|
| 71 |
|
| 72 |
function simpletest_theme() {
|
| 73 |
|
| 74 |
|
| 75 |
'arguments' => array('table' => NULL),
|
| 76 |
'file' => 'simpletest.pages.inc',
|
| 77 |
),
|
| 78 |
|
| 79 |
'arguments' => array('form' => NULL),
|
| 80 |
'file' => 'simpletest.pages.inc',
|
| 81 |
),
|
| 82 |
);
|
| 83 |
}
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
function simpletest_js_alter(&$javascript) {
|
| 89 |
|
| 90 |
|
| 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 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 126 |
file_unmanaged_delete_recursive(file_directory_path() . '/simpletest/verbose');
|
| 127 |
|
| 128 |
|
| 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 |
|
| 135 |
'title' => t('Running SimpleTests'),
|
| 136 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
batch_process('admin/config/development/testing/results/' . $test_id);
|
| 155 |
}
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
|
| 161 |
|
| 162 |
if (!isset($context['sandbox']['max'])) {
|
| 163 |
|
| 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 |
|
| 169 |
|
| 170 |
$test_list = $context['sandbox']['tests'];
|
| 171 |
$test_results = $context['sandbox']['test_results'];
|
| 172 |
|
| 173 |
$max = $context['sandbox']['max'];
|
| 174 |
|
| 175 |
|
| 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 |
|
| 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 |
|
| 199 |
$context['sandbox']['tests'] = $test_list;
|
| 200 |
$context['sandbox']['test_results'] = $test_results;
|
| 201 |
|
| 202 |
$context['results']['test_id'] = $test_id;
|
| 203 |
|
| 204 |
|
| 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 |
|
| 213 |
|
| 214 |
$test_id = $operations[0][1][1];
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 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 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 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 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 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 |
|
| 269 |
|
| 270 |
|
| 271 |
'line' => $match[4],
|
| 272 |
'file' => $match[3],
|
| 273 |
);
|
| 274 |
DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
|
| 275 |
}
|
| 276 |
|
| 277 |
|
| 278 |
DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
|
| 279 |
|
| 280 |
$found = TRUE;
|
| 281 |
}
|
| 282 |
}
|
| 283 |
return $found;
|
| 284 |
}
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
function simpletest_test_get_all() {
|
| 311 |
$groups = &drupal_static(__FUNCTION__);
|
| 312 |
|
| 313 |
if (!$groups) {
|
| 314 |
|
| 315 |
|
| 316 |
if ($cache = cache_get('simpletest', 'cache')) {
|
| 317 |
$groups = $cache->data;
|
| 318 |
}
|
| 319 |
|
| 320 |
|
| 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 |
|
| 330 |
|
| 331 |
foreach ($classes as $class) {
|
| 332 |
$class = $class->name;
|
| 333 |
if (class_exists($class) && method_exists($class, 'getInfo')) {
|
| 334 |
|
| 335 |
$info = call_user_func(array($class, 'getInfo'));
|
| 336 |
|
| 337 |
|
| 338 |
if (!isset($groups[$info['group']])) {
|
| 339 |
$groups[$info['group']] = array();
|
| 340 |
}
|
| 341 |
$groups[$info['group']][$class] = $info;
|
| 342 |
}
|
| 343 |
}
|
| 344 |
|
| 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 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
function simpletest_registry_files_alter(&$files, $modules) {
|
| 363 |
foreach ($modules as $module) {
|
| 364 |
|
| 365 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 398 |
|
| 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 |
|
| 408 |
drupal_set_message(t('No leftover tables to remove.'));
|
| 409 |
|
| 410 |
}
|
| 411 |
|
| 412 |
|
| 413 |
|
| 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 |
|
| 431 |
drupal_set_message(t('No temporary directories to remove.'));
|
| 432 |
|
| 433 |
}
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 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 |
|
| 456 |
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
|
| 457 |
|
| 458 |
|
| 459 |
db_delete("simpletest")->execute();
|
| 460 |
db_delete("simpletest_test_id")->execute();
|
| 461 |
|
| 462 |
|
| 463 |
return $count;
|
| 464 |
}
|
| 465 |
return FALSE;
|
| 466 |
}
|
| 467 |
|