run-tests.sh

This script runs Drupal tests from command line.

File

drupal/core/scripts/run-tests.sh
View source
  1. /**
  2. * @file
  3. * This script runs Drupal tests from command line.
  4. */
  5. const SIMPLETEST_SCRIPT_COLOR_PASS = 32; // Green.
  6. const SIMPLETEST_SCRIPT_COLOR_FAIL = 31; // Red.
  7. const SIMPLETEST_SCRIPT_COLOR_EXCEPTION = 33; // Brown.
  8. // Set defaults and get overrides.
  9. list($args, $count) = simpletest_script_parse_args();
  10. if ($args['help'] || $count == 0) {
  11. simpletest_script_help();
  12. exit;
  13. }
  14. if ($args['execute-test']) {
  15. // Masquerade as Apache for running tests.
  16. simpletest_script_init("Apache");
  17. simpletest_script_run_one_test($args['test-id'], $args['execute-test']);
  18. // Sub-process script execution ends here.
  19. }
  20. else {
  21. // Run administrative functions as CLI.
  22. simpletest_script_init(NULL);
  23. }
  24. // Bootstrap to perform initial validation or other operations.
  25. drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
  26. simpletest_classloader_register();
  27. if (!module_exists('simpletest')) {
  28. simpletest_script_print_error("The simpletest module must be enabled before this script can run.");
  29. exit;
  30. }
  31. if ($args['clean']) {
  32. // Clean up left-over times and directories.
  33. simpletest_clean_environment();
  34. echo "\nEnvironment cleaned.\n";
  35. // Get the status messages and print them.
  36. $messages = array_pop(drupal_get_messages('status'));
  37. foreach ($messages as $text) {
  38. echo " - " . $text . "\n";
  39. }
  40. exit;
  41. }
  42. if ($args['list']) {
  43. // Display all available tests.
  44. echo "\nAvailable test groups & classes\n";
  45. echo "-------------------------------\n\n";
  46. $groups = simpletest_script_get_all_tests();
  47. foreach ($groups as $group => $tests) {
  48. echo $group . "\n";
  49. foreach ($tests as $class => $info) {
  50. echo " - " . $info['name'] . ' (' . $class . ')' . "\n";
  51. }
  52. }
  53. exit;
  54. }
  55. $test_list = simpletest_script_get_test_list();
  56. // Try to allocate unlimited time to run the tests.
  57. drupal_set_time_limit(0);
  58. simpletest_script_reporter_init();
  59. // Execute tests.
  60. for ($i = 0; $i < $args['repeat']; $i++) {
  61. simpletest_script_execute_batch($test_list);
  62. }
  63. // Stop the timer.
  64. simpletest_script_reporter_timer_stop();
  65. // Display results before database is cleared.
  66. simpletest_script_reporter_display_results();
  67. if ($args['xml']) {
  68. simpletest_script_reporter_write_xml_results();
  69. }
  70. // Clean up all test results.
  71. if (!$args['keep-results']) {
  72. simpletest_clean_results_table();
  73. }
  74. // Test complete, exit.
  75. exit;
  76. /**
  77. * Print help text.
  78. */
  79. function simpletest_script_help() {
  80. global $args;
  81. echo <<
  82. Run Drupal tests from the shell.
  83. Usage: {$args['script']} [OPTIONS]
  84. Example: {$args['script']} Profile
  85. All arguments are long options.
  86. --help Print this page.
  87. --list Display all available test groups.
  88. --clean Cleans up database tables or directories from previous, failed,
  89. tests and then exits (no tests are run).
  90. --url Immediately precedes a URL to set the host and path. You will
  91. need this parameter if Drupal is in a subdirectory on your
  92. localhost and you have not set \$base_url in settings.php. Tests
  93. can be run under SSL by including https:// in the URL.
  94. --php The absolute path to the PHP executable. Usually not needed.
  95. --concurrency [num]
  96. Run tests in parallel, up to [num] tests at a time.
  97. --all Run all available tests.
  98. --module Run all tests belonging to the specified module name.
  99. (e.g., 'node')
  100. --class Run tests identified by specific class names, instead of group names.
  101. --file Run tests identified by specific file names, instead of group names.
  102. Specify the path and the extension
  103. (i.e. 'core/modules/user/user.test').
  104. --xml
  105. If provided, test results will be written as xml files to this path.
  106. --color Output text format results with color highlighting.
  107. --verbose Output detailed assertion messages in addition to summary.
  108. --keep-results
  109. Keeps detailed assertion results (in the database) after tests
  110. have completed. By default, assertion results are cleared.
  111. --repeat Number of times to repeat the test.
  112. --die-on-fail
  113. Exit test execution immediately upon any failed assertion. This
  114. allows to access the test site by changing settings.php to use the
  115. test database and configuration directories. Use in combination
  116. with --repeat for debugging random test failures.
  117. [,[, ...]]
  118. One or more tests to be run. By default, these are interpreted
  119. as the names of test groups as shown at
  120. admin/config/development/testing.
  121. These group names typically correspond to module names like "User"
  122. or "Profile" or "System", but there is also a group "XML-RPC".
  123. If --class is specified then these are interpreted as the names of
  124. specific test classes whose test methods will be run. Tests must
  125. be separated by commas. Ignored if --all is specified.
  126. To run this script you will normally invoke it from the root directory of your
  127. Drupal installation as the webserver user (differs per configuration), or root:
  128. sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$args['script']}
  129. --url http://example.com/ --all
  130. sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$args['script']}
  131. --url http://example.com/ --class "Drupal\block\Tests\BlockTest"
  132. \n
  133. EOF;
  134. }
  135. /**
  136. * Parse execution argument and ensure that all are valid.
  137. *
  138. * @return The list of arguments.
  139. */
  140. function simpletest_script_parse_args() {
  141. // Set default values.
  142. $args = array(
  143. 'script' => '',
  144. 'help' => FALSE,
  145. 'list' => FALSE,
  146. 'clean' => FALSE,
  147. 'url' => '',
  148. 'php' => '',
  149. 'concurrency' => 1,
  150. 'all' => FALSE,
  151. 'module' => FALSE,
  152. 'class' => FALSE,
  153. 'file' => FALSE,
  154. 'color' => FALSE,
  155. 'verbose' => FALSE,
  156. 'keep-results' => FALSE,
  157. 'test_names' => array(),
  158. 'repeat' => 1,
  159. 'die-on-fail' => FALSE,
  160. // Used internally.
  161. 'test-id' => 0,
  162. 'execute-test' => '',
  163. 'xml' => '',
  164. );
  165. // Override with set values.
  166. $args['script'] = basename(array_shift($_SERVER['argv']));
  167. $count = 0;
  168. while ($arg = array_shift($_SERVER['argv'])) {
  169. if (preg_match('/--(\S+)/', $arg, $matches)) {
  170. // Argument found.
  171. if (array_key_exists($matches[1], $args)) {
  172. // Argument found in list.
  173. $previous_arg = $matches[1];
  174. if (is_bool($args[$previous_arg])) {
  175. $args[$matches[1]] = TRUE;
  176. }
  177. else {
  178. $args[$matches[1]] = array_shift($_SERVER['argv']);
  179. }
  180. // Clear extraneous values.
  181. $args['test_names'] = array();
  182. $count++;
  183. }
  184. else {
  185. // Argument not found in list.
  186. simpletest_script_print_error("Unknown argument '$arg'.");
  187. exit;
  188. }
  189. }
  190. else {
  191. // Values found without an argument should be test names.
  192. $args['test_names'] += explode(',', $arg);
  193. $count++;
  194. }
  195. }
  196. // Validate the concurrency argument
  197. if (!is_numeric($args['concurrency']) || $args['concurrency'] <= 0) {
  198. simpletest_script_print_error("--concurrency must be a strictly positive integer.");
  199. exit;
  200. }
  201. return array($args, $count);
  202. }
  203. /**
  204. * Initialize script variables and perform general setup requirements.
  205. */
  206. function simpletest_script_init($server_software) {
  207. global $args, $php;
  208. $host = 'localhost';
  209. $path = '';
  210. // Determine location of php command automatically, unless a command line argument is supplied.
  211. if (!empty($args['php'])) {
  212. $php = $args['php'];
  213. }
  214. elseif ($php_env = getenv('_')) {
  215. // '_' is an environment variable set by the shell. It contains the command that was executed.
  216. $php = $php_env;
  217. }
  218. elseif ($sudo = getenv('SUDO_COMMAND')) {
  219. // 'SUDO_COMMAND' is an environment variable set by the sudo program.
  220. // Extract only the PHP interpreter, not the rest of the command.
  221. list($php, ) = explode(' ', $sudo, 2);
  222. }
  223. else {
  224. simpletest_script_print_error('Unable to automatically determine the path to the PHP interpreter. Supply the --php command line argument.');
  225. simpletest_script_help();
  226. exit();
  227. }
  228. // Get URL from arguments.
  229. if (!empty($args['url'])) {
  230. $parsed_url = parse_url($args['url']);
  231. $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  232. $path = isset($parsed_url['path']) ? rtrim($parsed_url['path']) : '';
  233. if ($path == '/') {
  234. $path = '';
  235. }
  236. // If the passed URL schema is 'https' then setup the $_SERVER variables
  237. // properly so that testing will run under HTTPS.
  238. if ($parsed_url['scheme'] == 'https') {
  239. $_SERVER['HTTPS'] = 'on';
  240. }
  241. }
  242. $_SERVER['HTTP_HOST'] = $host;
  243. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  244. $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  245. $_SERVER['SERVER_SOFTWARE'] = $server_software;
  246. $_SERVER['SERVER_NAME'] = 'localhost';
  247. $_SERVER['REQUEST_URI'] = $path .'/';
  248. $_SERVER['REQUEST_METHOD'] = 'GET';
  249. $_SERVER['SCRIPT_NAME'] = $path .'/index.php';
  250. $_SERVER['PHP_SELF'] = $path .'/index.php';
  251. $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
  252. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  253. // Ensure that any and all environment variables are changed to https://.
  254. foreach ($_SERVER as $key => $value) {
  255. $_SERVER[$key] = str_replace('http://', 'https://', $_SERVER[$key]);
  256. }
  257. }
  258. chdir(realpath(__DIR__ . '/../..'));
  259. require_once dirname(__DIR__) . '/includes/bootstrap.inc';
  260. }
  261. /**
  262. * Get all available tests from simpletest and PHPUnit.
  263. *
  264. * @return
  265. * An array of tests keyed with the groups specified in each of the tests
  266. * getInfo() method and then keyed by the test class. An example of the array
  267. * structure is provided below.
  268. *
  269. * @code
  270. * $groups['Block'] => array(
  271. * 'BlockTestCase' => array(
  272. * 'name' => 'Block functionality',
  273. * 'description' => 'Add, edit and delete custom block...',
  274. * 'group' => 'Block',
  275. * ),
  276. * );
  277. * @endcode
  278. */
  279. function simpletest_script_get_all_tests() {
  280. $tests = simpletest_test_get_all();
  281. $tests['PHPUnit'] = simpletest_phpunit_get_available_tests();
  282. return $tests;
  283. }
  284. /**
  285. * Execute a batch of tests.
  286. */
  287. function simpletest_script_execute_batch($test_classes) {
  288. global $args, $test_ids;
  289. // Multi-process execution.
  290. $children = array();
  291. while (!empty($test_classes) || !empty($children)) {
  292. while (count($children) < $args['concurrency']) {
  293. if (empty($test_classes)) {
  294. break;
  295. }
  296. $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute();
  297. $test_ids[] = $test_id;
  298. $test_class = array_shift($test_classes);
  299. // Process phpunit tests immediately since they are fast and we don't need
  300. // to fork for them.
  301. if (is_subclass_of($test_class, 'Drupal\Tests\UnitTestCase')) {
  302. simpletest_script_run_phpunit($test_id, $test_class);
  303. continue;
  304. }
  305. // Fork a child process.
  306. $command = simpletest_script_command($test_id, $test_class);
  307. $process = proc_open($command, array(), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
  308. if (!is_resource($process)) {
  309. echo "Unable to fork test process. Aborting.\n";
  310. exit;
  311. }
  312. // Register our new child.
  313. $children[] = array(
  314. 'process' => $process,
  315. 'test_id' => $test_id,
  316. 'class' => $test_class,
  317. 'pipes' => $pipes,
  318. );
  319. }
  320. // Wait for children every 200ms.
  321. usleep(200000);
  322. // Check if some children finished.
  323. foreach ($children as $cid => $child) {
  324. $status = proc_get_status($child['process']);
  325. if (empty($status['running'])) {
  326. // The child exited, unregister it.
  327. proc_close($child['process']);
  328. if ($status['exitcode']) {
  329. echo 'FATAL ' . $child['class'] . ': test runner returned a non-zero error code (' . $status['exitcode'] . ').' . "\n";
  330. if ($args['die-on-fail']) {
  331. list($db_prefix, ) = simpletest_last_test_get($child['test_id']);
  332. $public_files = variable_get('file_public_path', conf_path() . '/files');
  333. $test_directory = $public_files . '/simpletest/' . substr($db_prefix, 10);
  334. echo 'Simpletest database and files kept and test exited immediately on fail so should be reproducible if you change settings.php to use the database prefix '. $db_prefix . ' and config directories in '. $test_directory . "\n";
  335. $args['keep-results'] = TRUE;
  336. // Exit repeat loop immediately.
  337. $args['repeat'] = -1;
  338. }
  339. }
  340. // Free-up space by removing any potentially created resources.
  341. if (!$args['keep-results']) {
  342. simpletest_script_cleanup($child['test_id'], $child['class'], $status['exitcode']);
  343. }
  344. // Remove this child.
  345. unset($children[$cid]);
  346. }
  347. }
  348. }
  349. }
  350. /**
  351. * Run a group of phpunit tests.
  352. */
  353. function simpletest_script_run_phpunit($test_id, $class) {
  354. $results = simpletest_run_phpunit_tests($test_id, array($class));
  355. simpletest_process_phpunit_results($results);
  356. // Map phpunit results to a data structure we can pass to
  357. // _simpletest_format_summary_line.
  358. $summaries = array();
  359. foreach ($results as $result) {
  360. if (!isset($summaries[$result['test_class']])) {
  361. $summaries[$result['test_class']] = array(
  362. '#pass' => 0,
  363. '#fail' => 0,
  364. '#exception' => 0,
  365. '#debug' => 0,
  366. );
  367. }
  368. switch ($result['status']) {
  369. case 'pass':
  370. $summaries[$result['test_class']]['#pass']++;
  371. break;
  372. case 'fail':
  373. $summaries[$result['test_class']]['#fail']++;
  374. break;
  375. case 'exception':
  376. $summaries[$result['test_class']]['#exception']++;
  377. break;
  378. case 'debug':
  379. $summaries[$result['test_class']]['#debug']++;
  380. break;
  381. }
  382. }
  383. foreach ($summaries as $class => $summary) {
  384. $had_fails = $summary['#fail'] > 0;
  385. $had_exceptions = $summary['#exception'] > 0;
  386. $status = ($had_fails || $had_exceptions ? 'fail' : 'pass');
  387. $info = call_user_func(array($class, 'getInfo'));
  388. simpletest_script_print($info['name'] . ' ' . _simpletest_format_summary_line($summary) . "\n", simpletest_script_color_code($status));
  389. }
  390. }
  391. /**
  392. * Bootstrap Drupal and run a single test.
  393. */
  394. function simpletest_script_run_one_test($test_id, $test_class) {
  395. global $args, $conf;
  396. try {
  397. // Bootstrap Drupal.
  398. drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
  399. simpletest_classloader_register();
  400. // Override configuration according to command line parameters.
  401. $conf['simpletest.settings']['verbose'] = $args['verbose'];
  402. $conf['simpletest.settings']['clear_results'] = !$args['keep-results'];
  403. $test = new $test_class($test_id);
  404. $test->dieOnFail = (bool) $args['die-on-fail'];
  405. $test->run();
  406. $info = $test->getInfo();
  407. $had_fails = (isset($test->results['#fail']) && $test->results['#fail'] > 0);
  408. $had_exceptions = (isset($test->results['#exception']) && $test->results['#exception'] > 0);
  409. $status = ($had_fails || $had_exceptions ? 'fail' : 'pass');
  410. simpletest_script_print($info['name'] . ' ' . _simpletest_format_summary_line($test->results) . "\n", simpletest_script_color_code($status));
  411. // Finished, kill this runner.
  412. exit(0);
  413. }
  414. // DrupalTestCase::run() catches exceptions already, so this is only reached
  415. // when an exception is thrown in the wrapping test runner environment.
  416. catch (Exception $e) {
  417. echo (string) $e;
  418. exit(1);
  419. }
  420. }
  421. /**
  422. * Return a command used to run a test in a separate process.
  423. *
  424. * @param $test_id
  425. * The current test ID.
  426. * @param $test_class
  427. * The name of the test class to run.
  428. */
  429. function simpletest_script_command($test_id, $test_class) {
  430. global $args, $php;
  431. $command = escapeshellarg($php) . ' ' . escapeshellarg('./core/scripts/' . $args['script']);
  432. $command .= ' --url ' . escapeshellarg($args['url']);
  433. $command .= ' --php ' . escapeshellarg($php);
  434. $command .= " --test-id $test_id";
  435. foreach (array('verbose', 'keep-results', 'color', 'die-on-fail') as $arg) {
  436. if ($args[$arg]) {
  437. $command .= ' --' . $arg;
  438. }
  439. }
  440. // --execute-test and class name needs to come last.
  441. $command .= ' --execute-test ' . escapeshellarg($test_class);
  442. return $command;
  443. }
  444. /**
  445. * Removes all remnants of a test runner.
  446. *
  447. * In case a (e.g., fatal) error occurs after the test site has been fully setup
  448. * and the error happens in many tests, the environment that executes the tests
  449. * can easily run out of memory or disk space. This function ensures that all
  450. * created resources are properly cleaned up after every executed test.
  451. *
  452. * This clean-up only exists in this script, since SimpleTest module itself does
  453. * not use isolated sub-processes for each test being run, so a fatal error
  454. * halts not only the test, but also the test runner (i.e., the parent site).
  455. *
  456. * @param int $test_id
  457. * The test ID of the test run.
  458. * @param string $test_class
  459. * The class name of the test run.
  460. * @param int $exitcode
  461. * The exit code of the test runner.
  462. *
  463. * @see simpletest_script_run_one_test()
  464. */
  465. function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
  466. // Retrieve the last database prefix used for testing.
  467. list($db_prefix, ) = simpletest_last_test_get($test_id);
  468. // If no database prefix was found, then the test was not set up correctly.
  469. if (empty($db_prefix)) {
  470. echo "\nFATAL $test_class: Found no database prefix for test ID $test_id. (Check whether setUp() is invoked correctly.)";
  471. return;
  472. }
  473. // Do not output verbose cleanup messages in case of a positive exitcode.
  474. $output = !empty($exitcode);
  475. $messages = array();
  476. $messages[] = "- Found database prefix '$db_prefix' for test ID $test_id.";
  477. // Read the log file in case any fatal errors caused the test to crash.
  478. simpletest_log_read($test_id, $db_prefix, $test_class);
  479. // Check whether a test file directory was setup already.
  480. // @see prepareEnvironment()
  481. $public_files = variable_get('file_public_path', conf_path() . '/files');
  482. $test_directory = $public_files . '/simpletest/' . substr($db_prefix, 10);
  483. if (is_dir($test_directory)) {
  484. // Output the error_log.
  485. if (is_file($test_directory . '/error.log')) {
  486. if ($errors = file_get_contents($test_directory . '/error.log')) {
  487. $output = TRUE;
  488. $messages[] = $errors;
  489. }
  490. }
  491. // Delete the test files directory.
  492. // simpletest_clean_temporary_directories() cannot be used here, since it
  493. // would also delete file directories of other tests that are potentially
  494. // running concurrently.
  495. file_unmanaged_delete_recursive($test_directory, array('Drupal\simpletest\TestBase', 'filePreDeleteCallback'));
  496. $messages[] = "- Removed test files directory.";
  497. }
  498. // Clear out all database tables from the test.
  499. $count = 0;
  500. foreach (db_find_tables($db_prefix . '%') as $table) {
  501. db_drop_table($table);
  502. $count++;
  503. }
  504. if ($count) {
  505. $messages[] = "- " . format_plural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.');
  506. }
  507. if ($output) {
  508. echo implode("\n", $messages);
  509. echo "\n";
  510. }
  511. }
  512. /**
  513. * Get list of tests based on arguments. If --all specified then
  514. * returns all available tests, otherwise reads list of tests.
  515. *
  516. * Will print error and exit if no valid tests were found.
  517. *
  518. * @return List of tests.
  519. */
  520. function simpletest_script_get_test_list() {
  521. global $args;
  522. $test_list = array();
  523. if ($args['all']) {
  524. $groups = simpletest_script_get_all_tests();
  525. $all_tests = array();
  526. foreach ($groups as $group => $tests) {
  527. $all_tests = array_merge($all_tests, array_keys($tests));
  528. }
  529. $test_list = $all_tests;
  530. }
  531. else {
  532. if ($args['class']) {
  533. foreach ($args['test_names'] as $class_name) {
  534. $test_list[] = $class_name;
  535. }
  536. }
  537. elseif ($args['module']) {
  538. $modules = drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.module$/', 'modules', 'name', 0);
  539. foreach ($args['test_names'] as $module) {
  540. // PSR-0 only.
  541. $dir = dirname($modules[$module]->uri) . "/lib/Drupal/$module/Tests";
  542. $files = file_scan_directory($dir, '@\.php$@', array(
  543. 'key' => 'name',
  544. 'recurse' => TRUE,
  545. ));
  546. foreach ($files as $test => $file) {
  547. $test_list[] = "Drupal\\$module\\Tests\\$test";
  548. }
  549. }
  550. }
  551. elseif ($args['file']) {
  552. // Extract test case class names from specified files.
  553. foreach ($args['test_names'] as $file) {
  554. if (!file_exists($file)) {
  555. simpletest_script_print_error('File not found: ' . $file);
  556. exit;
  557. }
  558. $content = file_get_contents($file);
  559. // Extract a potential namespace.
  560. $namespace = FALSE;
  561. if (preg_match('@^namespace ([^ ;]+)@m', $content, $matches)) {
  562. $namespace = $matches[1];
  563. }
  564. // Extract all class names.
  565. // Abstract classes are excluded on purpose.
  566. preg_match_all('@^class ([^ ]+)@m', $content, $matches);
  567. if (!$namespace) {
  568. $test_list = array_merge($test_list, $matches[1]);
  569. }
  570. else {
  571. foreach ($matches[1] as $class_name) {
  572. $test_list[] = $namespace . '\\' . $class_name;
  573. }
  574. }
  575. }
  576. }
  577. else {
  578. $groups = simpletest_script_get_all_tests();
  579. foreach ($args['test_names'] as $group_name) {
  580. $test_list = array_merge($test_list, array_keys($groups[$group_name]));
  581. }
  582. }
  583. }
  584. if (empty($test_list)) {
  585. simpletest_script_print_error('No valid tests were specified.');
  586. exit;
  587. }
  588. return $test_list;
  589. }
  590. /**
  591. * Initialize the reporter.
  592. */
  593. function simpletest_script_reporter_init() {
  594. global $args, $test_list, $results_map;
  595. $results_map = array(
  596. 'pass' => 'Pass',
  597. 'fail' => 'Fail',
  598. 'exception' => 'Exception'
  599. );
  600. echo "\n";
  601. echo "Drupal test run\n";
  602. echo "---------------\n";
  603. echo "\n";
  604. // Tell the user about what tests are to be run.
  605. if ($args['all']) {
  606. echo "All tests will run.\n\n";
  607. }
  608. else {
  609. echo "Tests to be run:\n";
  610. foreach ($test_list as $class_name) {
  611. $info = call_user_func(array($class_name, 'getInfo'));
  612. echo " - " . $info['name'] . ' (' . $class_name . ')' . "\n";
  613. }
  614. echo "\n";
  615. }
  616. echo "Test run started:\n";
  617. echo " " . format_date($_SERVER['REQUEST_TIME'], 'long') . "\n";
  618. timer_start('run-tests');
  619. echo "\n";
  620. echo "Test summary\n";
  621. echo "------------\n";
  622. echo "\n";
  623. }
  624. /**
  625. * Display jUnit XML test results.
  626. */
  627. function simpletest_script_reporter_write_xml_results() {
  628. global $args, $test_ids, $results_map;
  629. $results = db_query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(':test_ids' => $test_ids));
  630. $test_class = '';
  631. $xml_files = array();
  632. foreach ($results as $result) {
  633. if (isset($results_map[$result->status])) {
  634. if ($result->test_class != $test_class) {
  635. // We've moved onto a new class, so write the last classes results to a file:
  636. if (isset($xml_files[$test_class])) {
  637. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  638. unset($xml_files[$test_class]);
  639. }
  640. $test_class = $result->test_class;
  641. if (!isset($xml_files[$test_class])) {
  642. $doc = new DomDocument('1.0');
  643. $root = $doc->createElement('testsuite');
  644. $root = $doc->appendChild($root);
  645. $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root);
  646. }
  647. }
  648. // For convenience:
  649. $dom_document = &$xml_files[$test_class]['doc'];
  650. // Create the XML element for this test case:
  651. $case = $dom_document->createElement('testcase');
  652. $case->setAttribute('classname', $test_class);
  653. list($class, $name) = explode('->', $result->function, 2);
  654. $case->setAttribute('name', $name);
  655. // Passes get no further attention, but failures and exceptions get to add more detail:
  656. if ($result->status == 'fail') {
  657. $fail = $dom_document->createElement('failure');
  658. $fail->setAttribute('type', 'failure');
  659. $fail->setAttribute('message', $result->message_group);
  660. $text = $dom_document->createTextNode($result->message);
  661. $fail->appendChild($text);
  662. $case->appendChild($fail);
  663. }
  664. elseif ($result->status == 'exception') {
  665. // In the case of an exception the $result->function may not be a class
  666. // method so we record the full function name:
  667. $case->setAttribute('name', $result->function);
  668. $fail = $dom_document->createElement('error');
  669. $fail->setAttribute('type', 'exception');
  670. $fail->setAttribute('message', $result->message_group);
  671. $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
  672. $text = $dom_document->createTextNode($full_message);
  673. $fail->appendChild($text);
  674. $case->appendChild($fail);
  675. }
  676. // Append the test case XML to the test suite:
  677. $xml_files[$test_class]['suite']->appendChild($case);
  678. }
  679. }
  680. // The last test case hasn't been saved to a file yet, so do that now:
  681. if (isset($xml_files[$test_class])) {
  682. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  683. unset($xml_files[$test_class]);
  684. }
  685. }
  686. /**
  687. * Stop the test timer.
  688. */
  689. function simpletest_script_reporter_timer_stop() {
  690. echo "\n";
  691. $end = timer_stop('run-tests');
  692. echo "Test run duration: " . format_interval($end['time'] / 1000);
  693. echo "\n\n";
  694. }
  695. /**
  696. * Display test results.
  697. */
  698. function simpletest_script_reporter_display_results() {
  699. global $args, $test_ids, $results_map;
  700. if ($args['verbose']) {
  701. // Report results.
  702. echo "Detailed test results\n";
  703. echo "---------------------\n";
  704. $results = db_query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(':test_ids' => $test_ids));
  705. $test_class = '';
  706. foreach ($results as $result) {
  707. if (isset($results_map[$result->status])) {
  708. if ($result->test_class != $test_class) {
  709. // Display test class every time results are for new test class.
  710. echo "\n\n---- $result->test_class ----\n\n\n";
  711. $test_class = $result->test_class;
  712. // Print table header.
  713. echo "Status Group Filename Line Function \n";
  714. echo "--------------------------------------------------------------------------------\n";
  715. }
  716. simpletest_script_format_result($result);
  717. }
  718. }
  719. }
  720. }
  721. /**
  722. * Format the result so that it fits within the default 80 character
  723. * terminal size.
  724. *
  725. * @param $result The result object to format.
  726. */
  727. function simpletest_script_format_result($result) {
  728. global $results_map, $color;
  729. $summary = sprintf("%-9.9s %-10.10s %-17.17s %4.4s %-35.35s\n",
  730. $results_map[$result->status], $result->message_group, basename($result->file), $result->line, $result->function);
  731. simpletest_script_print($summary, simpletest_script_color_code($result->status));
  732. $lines = explode("\n", wordwrap(trim(strip_tags($result->message)), 76));
  733. foreach ($lines as $line) {
  734. echo " $line\n";
  735. }
  736. }
  737. /**
  738. * Print error message prefixed with " ERROR: " and displayed in fail color
  739. * if color output is enabled.
  740. *
  741. * @param $message The message to print.
  742. */
  743. function simpletest_script_print_error($message) {
  744. simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  745. }
  746. /**
  747. * Print a message to the console, if color is enabled then the specified
  748. * color code will be used.
  749. *
  750. * @param $message The message to print.
  751. * @param $color_code The color code to use for coloring.
  752. */
  753. function simpletest_script_print($message, $color_code) {
  754. global $args;
  755. if ($args['color']) {
  756. echo "\033[" . $color_code . "m" . $message . "\033[0m";
  757. }
  758. else {
  759. echo $message;
  760. }
  761. }
  762. /**
  763. * Get the color code associated with the specified status.
  764. *
  765. * @param $status The status string to get code for.
  766. * @return Color code.
  767. */
  768. function simpletest_script_color_code($status) {
  769. switch ($status) {
  770. case 'pass':
  771. return SIMPLETEST_SCRIPT_COLOR_PASS;
  772. case 'fail':
  773. return SIMPLETEST_SCRIPT_COLOR_FAIL;
  774. case 'exception':
  775. return SIMPLETEST_SCRIPT_COLOR_EXCEPTION;
  776. }
  777. return 0; // Default formatting.
  778. }