simpletest.module

You are here

  1. 7 modules/simpletest/simpletest.module
  2. 8 core/modules/simpletest/simpletest.module

Provides testing functionality.

Functions

Namesort descending Description
simpletest_classloader_register
simpletest_clean_database Removed prefixed tables from the database that are left over from crashed tests.
simpletest_clean_environment Remove all temporary database tables and directories.
simpletest_clean_results_table Clear the test result tables.
simpletest_clean_temporary_directories Find all leftover temporary directories and remove them.
simpletest_generate_file Generate test file.
simpletest_help Implements hook_help().
simpletest_js_alter Implements hook_js_alter().
simpletest_last_test_get Get information about the last test that ran given a test ID.
simpletest_log_read Read the error log and report any errors as assertion failures.
simpletest_mail_alter Implements hook_mail_alter().
simpletest_menu Implements hook_menu().
simpletest_permission Implements hook_permission().
simpletest_registry_files_alter Implements hook_registry_files_alter().
simpletest_run_tests Actually runs tests.
simpletest_test_get_all Get a list of all of the tests provided by the system.
simpletest_theme Implements hook_theme().
_simpletest_autoload_psr0 Autoload callback to find PSR-0 test classes.
_simpletest_batch_finished
_simpletest_batch_operation Batch operation callback.
_simpletest_format_summary_line

File

modules/simpletest/simpletest.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides testing functionality.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function simpletest_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#simpletest':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $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/documentation/modules/simpletest', '@blocks' => url('admin/structure/block'))) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Running tests') . '</dt>';
  18. $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>';
  19. $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>';
  20. $output .= '</dl>';
  21. return $output;
  22. }
  23. }
  24. /**
  25. * Implements 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. 'file' => 'simpletest.pages.inc',
  35. 'weight' => -5,
  36. );
  37. $items['admin/config/development/testing/list'] = array(
  38. 'title' => 'List',
  39. 'type' => MENU_DEFAULT_LOCAL_TASK,
  40. );
  41. $items['admin/config/development/testing/settings'] = array(
  42. 'title' => 'Settings',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('simpletest_settings_form'),
  45. 'access arguments' => array('administer unit tests'),
  46. 'type' => MENU_LOCAL_TASK,
  47. 'file' => 'simpletest.pages.inc',
  48. );
  49. $items['admin/config/development/testing/results/%'] = array(
  50. 'title' => 'Test result',
  51. 'page callback' => 'drupal_get_form',
  52. 'page arguments' => array('simpletest_result_form', 5),
  53. 'description' => 'View result of tests.',
  54. 'access arguments' => array('administer unit tests'),
  55. 'file' => 'simpletest.pages.inc',
  56. );
  57. return $items;
  58. }
  59. /**
  60. * Implements hook_permission().
  61. */
  62. function simpletest_permission() {
  63. return array(
  64. 'administer unit tests' => array(
  65. 'title' => t('Administer tests'),
  66. 'restrict access' => TRUE,
  67. ),
  68. );
  69. }
  70. /**
  71. * Implements hook_theme().
  72. */
  73. function simpletest_theme() {
  74. return array(
  75. 'simpletest_test_table' => array(
  76. 'render element' => 'table',
  77. 'file' => 'simpletest.pages.inc',
  78. ),
  79. 'simpletest_result_summary' => array(
  80. 'render element' => 'form',
  81. 'file' => 'simpletest.pages.inc',
  82. ),
  83. );
  84. }
  85. /**
  86. * Implements 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. function _simpletest_format_summary_line($summary) {
  97. $args = array(
  98. '@pass' => format_plural(isset($summary['#pass']) ? $summary['#pass'] : 0, '1 pass', '@count passes'),
  99. '@fail' => format_plural(isset($summary['#fail']) ? $summary['#fail'] : 0, '1 fail', '@count fails'),
  100. '@exception' => format_plural(isset($summary['#exception']) ? $summary['#exception'] : 0, '1 exception', '@count exceptions'),
  101. );
  102. if (!$summary['#debug']) {
  103. return t('@pass, @fail, and @exception', $args);
  104. }
  105. $args['@debug'] = format_plural(isset($summary['#debug']) ? $summary['#debug'] : 0, '1 debug message', '@count debug messages');
  106. return t('@pass, @fail, @exception, and @debug', $args);
  107. }
  108. /**
  109. * Actually runs tests.
  110. *
  111. * @param $test_list
  112. * List of tests to run.
  113. * @param $reporter
  114. * Which reporter to use. Allowed values are: text, xml, html and drupal,
  115. * drupal being the default.
  116. */
  117. function simpletest_run_tests($test_list, $reporter = 'drupal') {
  118. $test_id = db_insert('simpletest_test_id')
  119. ->useDefaults(array('test_id'))
  120. ->execute();
  121. // Clear out the previous verbose files.
  122. file_unmanaged_delete_recursive('public://simpletest/verbose');
  123. // Get the info for the first test being run.
  124. $first_test = array_shift($test_list);
  125. $first_instance = new $first_test();
  126. array_unshift($test_list, $first_test);
  127. $info = $first_instance->getInfo();
  128. $batch = array(
  129. 'title' => t('Running tests'),
  130. 'operations' => array(
  131. array('_simpletest_batch_operation', array($test_list, $test_id)),
  132. ),
  133. 'finished' => '_simpletest_batch_finished',
  134. 'progress_message' => '',
  135. 'css' => array(drupal_get_path('module', 'simpletest') . '/simpletest.css'),
  136. 'init_message' => t('Processing test @num of @max - %test.', array('%test' => $info['name'], '@num' => '1', '@max' => count($test_list))),
  137. );
  138. batch_set($batch);
  139. module_invoke_all('test_group_started');
  140. return $test_id;
  141. }
  142. /**
  143. * Batch operation callback.
  144. */
  145. function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
  146. simpletest_classloader_register();
  147. // Get working values.
  148. if (!isset($context['sandbox']['max'])) {
  149. // First iteration: initialize working values.
  150. $test_list = $test_list_init;
  151. $context['sandbox']['max'] = count($test_list);
  152. $test_results = array('#pass' => 0, '#fail' => 0, '#exception' => 0, '#debug' => 0);
  153. }
  154. else {
  155. // Nth iteration: get the current values where we last stored them.
  156. $test_list = $context['sandbox']['tests'];
  157. $test_results = $context['sandbox']['test_results'];
  158. }
  159. $max = $context['sandbox']['max'];
  160. // Perform the next test.
  161. $test_class = array_shift($test_list);
  162. $test = new $test_class($test_id);
  163. $test->run();
  164. $size = count($test_list);
  165. $info = $test->getInfo();
  166. module_invoke_all('test_finished', $test->results);
  167. // Gather results and compose the report.
  168. $test_results[$test_class] = $test->results;
  169. foreach ($test_results[$test_class] as $key => $value) {
  170. $test_results[$key] += $value;
  171. }
  172. $test_results[$test_class]['#name'] = $info['name'];
  173. $items = array();
  174. foreach (element_children($test_results) as $class) {
  175. 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>');
  176. }
  177. $context['message'] = t('Processed test @num of @max - %test.', array('%test' => $info['name'], '@num' => $max - $size, '@max' => $max));
  178. $context['message'] .= '<div class="simpletest-' . ($test_results['#fail'] + $test_results['#exception'] ? 'fail' : 'pass') . '">Overall results: ' . _simpletest_format_summary_line($test_results) . '</div>';
  179. $context['message'] .= theme('item_list', array('items' => $items));
  180. // Save working values for the next iteration.
  181. $context['sandbox']['tests'] = $test_list;
  182. $context['sandbox']['test_results'] = $test_results;
  183. // The test_id is the only thing we need to save for the report page.
  184. $context['results']['test_id'] = $test_id;
  185. // Multistep processing: report progress.
  186. $context['finished'] = 1 - $size / $max;
  187. }
  188. function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
  189. if ($success) {
  190. drupal_set_message(t('The test run finished in @elapsed.', array('@elapsed' => $elapsed)));
  191. }
  192. else {
  193. // Use the test_id passed as a parameter to _simpletest_batch_operation().
  194. $test_id = $operations[0][1][1];
  195. // Retrieve the last database prefix used for testing and the last test
  196. // class that was run from. Use the information to read the lgo file
  197. // in case any fatal errors caused the test to crash.
  198. list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
  199. simpletest_log_read($test_id, $last_prefix, $last_test_class);
  200. drupal_set_message(t('The test run did not successfully finish.'), 'error');
  201. drupal_set_message(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning');
  202. }
  203. module_invoke_all('test_group_finished');
  204. }
  205. /**
  206. * Get information about the last test that ran given a test ID.
  207. *
  208. * @param $test_id
  209. * The test ID to get the last test from.
  210. * @return
  211. * Array containing the last database prefix used and the last test class
  212. * that ran.
  213. */
  214. function simpletest_last_test_get($test_id) {
  215. $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();
  216. $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();
  217. return array($last_prefix, $last_test_class);
  218. }
  219. /**
  220. * Read the error log and report any errors as assertion failures.
  221. *
  222. * The errors in the log should only be fatal errors since any other errors
  223. * will have been recorded by the error handler.
  224. *
  225. * @param $test_id
  226. * The test ID to which the log relates.
  227. * @param $prefix
  228. * The database prefix to which the log relates.
  229. * @param $test_class
  230. * The test class to which the log relates.
  231. * @param $during_test
  232. * Indicates that the current file directory path is a temporary file
  233. * file directory used during testing.
  234. * @return
  235. * Found any entries in log.
  236. */
  237. function simpletest_log_read($test_id, $prefix, $test_class, $during_test = FALSE) {
  238. $log = 'public://' . ($during_test ? '' : '/simpletest/' . substr($prefix, 10)) . '/error.log';
  239. $found = FALSE;
  240. if (file_exists($log)) {
  241. foreach (file($log) as $line) {
  242. if (preg_match('/\[.*?\] (.*?): (.*?) in (.*) on line (\d+)/', $line, $match)) {
  243. // Parse PHP fatal errors for example: PHP Fatal error: Call to
  244. // undefined function break_me() in /path/to/file.php on line 17
  245. $caller = array(
  246. 'line' => $match[4],
  247. 'file' => $match[3],
  248. );
  249. DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
  250. }
  251. else {
  252. // Unknown format, place the entire message in the log.
  253. DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
  254. }
  255. $found = TRUE;
  256. }
  257. }
  258. return $found;
  259. }
  260. /**
  261. * Get a list of all of the tests provided by the system.
  262. *
  263. * The list of test classes is loaded from the registry where it looks for
  264. * files ending in ".test". Once loaded the test list is cached and stored in
  265. * a static variable. In order to list tests provided by disabled modules
  266. * hook_registry_files_alter() is used to forcefully add them to the registry.
  267. *
  268. * PSR-0 classes are found by searching the designated directory for each module
  269. * for files matching the PSR-0 standard.
  270. *
  271. * @return
  272. * An array of tests keyed with the groups specified in each of the tests
  273. * getInfo() method and then keyed by the test class. An example of the array
  274. * structure is provided below.
  275. *
  276. * @code
  277. * $groups['Blog'] => array(
  278. * 'BlogTestCase' => array(
  279. * 'name' => 'Blog functionality',
  280. * 'description' => 'Create, view, edit, delete, ...',
  281. * 'group' => 'Blog',
  282. * ),
  283. * );
  284. * @endcode
  285. * @see simpletest_registry_files_alter()
  286. */
  287. function simpletest_test_get_all() {
  288. $groups = &drupal_static(__FUNCTION__);
  289. if (!$groups) {
  290. // Register a simple class loader for PSR-0 test classes.
  291. simpletest_classloader_register();
  292. // Load test information from cache if available, otherwise retrieve the
  293. // information from each tests getInfo() method.
  294. if ($cache = cache_get('simpletest', 'cache')) {
  295. $groups = $cache->data;
  296. }
  297. else {
  298. // Select all clases in files ending with .test.
  299. $classes = db_query("SELECT name FROM {registry} WHERE type = :type AND filename LIKE :name", array(':type' => 'class', ':name' => '%.test'))->fetchCol();
  300. // Also discover PSR-0 test classes, if the PHP version allows it.
  301. if (version_compare(PHP_VERSION, '5.3') > 0) {
  302. // Select all PSR-0 classes in the Tests namespace of all modules.
  303. $system_list = db_query("SELECT name, filename FROM {system}")->fetchAllKeyed();
  304. foreach ($system_list as $name => $filename) {
  305. // Build directory in which the test files would reside.
  306. $tests_dir = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/Drupal/' . $name . '/Tests';
  307. // Scan it for test files if it exists.
  308. if (is_dir($tests_dir)) {
  309. $files = file_scan_directory($tests_dir, '/.*\.php/');
  310. if (!empty($files)) {
  311. $basedir = DRUPAL_ROOT . '/' . dirname($filename) . '/lib/';
  312. foreach ($files as $file) {
  313. // Convert the file name into the namespaced class name.
  314. $replacements = array(
  315. '/' => '\\',
  316. $basedir => '',
  317. '.php' => '',
  318. );
  319. $classes[] = strtr($file->uri, $replacements);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. // Check that each class has a getInfo() method and store the information
  326. // in an array keyed with the group specified in the test information.
  327. $groups = array();
  328. foreach ($classes as $class) {
  329. // Test classes need to implement getInfo() to be valid.
  330. if (class_exists($class) && method_exists($class, 'getInfo')) {
  331. $info = call_user_func(array($class, 'getInfo'));
  332. // If this test class requires a non-existing module, skip it.
  333. if (!empty($info['dependencies'])) {
  334. foreach ($info['dependencies'] as $module) {
  335. if (!drupal_get_filename('module', $module)) {
  336. continue 2;
  337. }
  338. }
  339. }
  340. $groups[$info['group']][$class] = $info;
  341. }
  342. }
  343. // Sort the groups and tests within the groups by name.
  344. uksort($groups, 'strnatcasecmp');
  345. foreach ($groups as $group => &$tests) {
  346. uksort($tests, 'strnatcasecmp');
  347. }
  348. // Allow modules extending core tests to disable originals.
  349. drupal_alter('simpletest', $groups);
  350. cache_set('simpletest', $groups);
  351. }
  352. }
  353. return $groups;
  354. }
  355. /*
  356. * Register a simple class loader that can find D8-style PSR-0 test classes.
  357. *
  358. * Other PSR-0 class loading can happen in contrib, but those contrib class
  359. * loader modules will not be enabled when testbot runs. So we need to do this
  360. * one in core.
  361. */
  362. function simpletest_classloader_register() {
  363. // Prevent duplicate classloader registration.
  364. static $first_run = TRUE;
  365. if (!$first_run) {
  366. return;
  367. }
  368. $first_run = FALSE;
  369. // Only register PSR-0 class loading if we are on PHP 5.3 or higher.
  370. if (version_compare(PHP_VERSION, '5.3') > 0) {
  371. spl_autoload_register('_simpletest_autoload_psr0');
  372. }
  373. }
  374. /**
  375. * Autoload callback to find PSR-0 test classes.
  376. *
  377. * This will only work on classes where the namespace is of the pattern
  378. * "Drupal\$extension\Tests\.."
  379. */
  380. function _simpletest_autoload_psr0($class) {
  381. // Static cache for extension paths.
  382. // This cache is lazily filled as soon as it is needed.
  383. static $extensions;
  384. // Check that the first namespace fragment is "Drupal\"
  385. if (substr($class, 0, 7) === 'Drupal\\') {
  386. // Find the position of the second namespace separator.
  387. $pos = strpos($class, '\\', 7);
  388. // Check that the third namespace fragment is "\Tests\".
  389. if (substr($class, $pos, 7) === '\\Tests\\') {
  390. // Extract the second namespace fragment, which we expect to be the
  391. // extension name.
  392. $extension = substr($class, 7, $pos - 7);
  393. // Lazy-load the extension paths, both enabled and disabled.
  394. if (!isset($extensions)) {
  395. $extensions = db_query("SELECT name, filename FROM {system}")->fetchAllKeyed();
  396. }
  397. // Check if the second namespace fragment is a known extension name.
  398. if (isset($extensions[$extension])) {
  399. // Split the class into namespace and classname.
  400. $nspos = strrpos($class, '\\');
  401. $namespace = substr($class, 0, $nspos);
  402. $classname = substr($class, $nspos + 1);
  403. // Build the filepath where we expect the class to be defined.
  404. $path = dirname($extensions[$extension]) . '/lib/' .
  405. str_replace('\\', '/', $namespace) . '/' .
  406. str_replace('_', '/', $classname) . '.php';
  407. // Include the file, if it does exist.
  408. if (file_exists($path)) {
  409. include $path;
  410. }
  411. }
  412. }
  413. }
  414. }
  415. /**
  416. * Implements hook_registry_files_alter().
  417. *
  418. * Add the test files for disabled modules so that we get a list containing
  419. * all the avialable tests.
  420. */
  421. function simpletest_registry_files_alter(&$files, $modules) {
  422. foreach ($modules as $module) {
  423. // Only add test files for disabled modules, as enabled modules should
  424. // already include any test files they provide.
  425. if (!$module->status) {
  426. $dir = $module->dir;
  427. if (!empty($module->info['files'])) {
  428. foreach ($module->info['files'] as $file) {
  429. if (substr($file, -5) == '.test') {
  430. $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. /**
  438. * Generate test file.
  439. */
  440. function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
  441. $size = $width * $lines - $lines;
  442. // Generate random text
  443. $text = '';
  444. for ($i = 0; $i < $size; $i++) {
  445. switch ($type) {
  446. case 'text':
  447. $text .= chr(rand(32, 126));
  448. break;
  449. case 'binary':
  450. $text .= chr(rand(0, 31));
  451. break;
  452. case 'binary-text':
  453. default:
  454. $text .= rand(0, 1);
  455. break;
  456. }
  457. }
  458. $text = wordwrap($text, $width - 1, "\n", TRUE) . "\n"; // Add \n for symmetrical file.
  459. // Create filename.
  460. file_put_contents('public://' . $filename . '.txt', $text);
  461. return $filename;
  462. }
  463. /**
  464. * Remove all temporary database tables and directories.
  465. */
  466. function simpletest_clean_environment() {
  467. simpletest_clean_database();
  468. simpletest_clean_temporary_directories();
  469. if (variable_get('simpletest_clear_results', TRUE)) {
  470. $count = simpletest_clean_results_table();
  471. drupal_set_message(format_plural($count, 'Removed 1 test result.', 'Removed @count test results.'));
  472. }
  473. else {
  474. drupal_set_message(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
  475. }
  476. // Detect test classes that have been added, renamed or deleted.
  477. registry_rebuild();
  478. cache_clear_all('simpletest', 'cache');
  479. }
  480. /**
  481. * Removed prefixed tables from the database that are left over from crashed tests.
  482. */
  483. function simpletest_clean_database() {
  484. $tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
  485. $schema = drupal_get_schema_unprocessed('simpletest');
  486. $count = 0;
  487. foreach (array_diff_key($tables, $schema) as $table) {
  488. // Strip the prefix and skip tables without digits following "simpletest",
  489. // e.g. {simpletest_test_id}.
  490. if (preg_match('/simpletest\d+.*/', $table, $matches)) {
  491. db_drop_table($matches[0]);
  492. $count++;
  493. }
  494. }
  495. if ($count > 0) {
  496. drupal_set_message(format_plural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
  497. }
  498. else {
  499. drupal_set_message(t('No leftover tables to remove.'));
  500. }
  501. }
  502. /**
  503. * Find all leftover temporary directories and remove them.
  504. */
  505. function simpletest_clean_temporary_directories() {
  506. $count = 0;
  507. if (is_dir('public://simpletest')) {
  508. $files = scandir('public://simpletest');
  509. foreach ($files as $file) {
  510. $path = 'public://simpletest/' . $file;
  511. if (is_dir($path) && is_numeric($file)) {
  512. file_unmanaged_delete_recursive($path);
  513. $count++;
  514. }
  515. }
  516. }
  517. if ($count > 0) {
  518. drupal_set_message(format_plural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
  519. }
  520. else {
  521. drupal_set_message(t('No temporary directories to remove.'));
  522. }
  523. }
  524. /**
  525. * Clear the test result tables.
  526. *
  527. * @param $test_id
  528. * Test ID to remove results for, or NULL to remove all results.
  529. * @return
  530. * The number of results removed.
  531. */
  532. function simpletest_clean_results_table($test_id = NULL) {
  533. if (variable_get('simpletest_clear_results', TRUE)) {
  534. if ($test_id) {
  535. $count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
  536. db_delete('simpletest')
  537. ->condition('test_id', $test_id)
  538. ->execute();
  539. db_delete('simpletest_test_id')
  540. ->condition('test_id', $test_id)
  541. ->execute();
  542. }
  543. else {
  544. $count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
  545. // Clear test results.
  546. db_delete('simpletest')->execute();
  547. db_delete('simpletest_test_id')->execute();
  548. }
  549. return $count;
  550. }
  551. return 0;
  552. }
  553. /**
  554. * Implements hook_mail_alter().
  555. *
  556. * Aborts sending of messages with ID 'simpletest_cancel_test'.
  557. *
  558. * @see MailTestCase::testCancelMessage()
  559. */
  560. function simpletest_mail_alter(&$message) {
  561. if ($message['id'] == 'simpletest_cancel_test') {
  562. $message['send'] = FALSE;
  563. }
  564. }