run-tests.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. /**
  3. * @file
  4. * This script runs Drupal tests from command line.
  5. */
  6. define('SIMPLETEST_SCRIPT_COLOR_PASS', 32); // Green.
  7. define('SIMPLETEST_SCRIPT_COLOR_FAIL', 31); // Red.
  8. define('SIMPLETEST_SCRIPT_COLOR_EXCEPTION', 33); // Brown.
  9. // Set defaults and get overrides.
  10. list($args, $count) = simpletest_script_parse_args();
  11. if ($args['help'] || $count == 0) {
  12. simpletest_script_help();
  13. exit;
  14. }
  15. if ($args['execute-test']) {
  16. // Masquerade as Apache for running tests.
  17. simpletest_script_init("Apache");
  18. simpletest_script_run_one_test($args['test-id'], $args['execute-test']);
  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_FULL);
  26. if (!module_exists('simpletest')) {
  27. simpletest_script_print_error("The simpletest module must be enabled before this script can run.");
  28. exit;
  29. }
  30. if ($args['clean']) {
  31. // Clean up left-over times and directories.
  32. simpletest_clean_environment();
  33. echo "\nEnvironment cleaned.\n";
  34. // Get the status messages and print them.
  35. $messages = array_pop(drupal_get_messages('status'));
  36. foreach ($messages as $text) {
  37. echo " - " . $text . "\n";
  38. }
  39. exit;
  40. }
  41. // Load SimpleTest files.
  42. $groups = simpletest_test_get_all();
  43. $all_tests = array();
  44. foreach ($groups as $group => $tests) {
  45. $all_tests = array_merge($all_tests, array_keys($tests));
  46. }
  47. $test_list = array();
  48. if ($args['list']) {
  49. // Display all available tests.
  50. echo "\nAvailable test groups & classes\n";
  51. echo "-------------------------------\n\n";
  52. foreach ($groups as $group => $tests) {
  53. echo $group . "\n";
  54. foreach ($tests as $class => $info) {
  55. echo " - " . $info['name'] . ' (' . $class . ')' . "\n";
  56. }
  57. }
  58. exit;
  59. }
  60. $test_list = simpletest_script_get_test_list();
  61. // Try to allocate unlimited time to run the tests.
  62. drupal_set_time_limit(0);
  63. simpletest_script_reporter_init();
  64. // Setup database for test results.
  65. $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute();
  66. // Execute tests.
  67. simpletest_script_execute_batch($test_id, simpletest_script_get_test_list());
  68. // Retrieve the last database prefix used for testing and the last test class
  69. // that was run from. Use the information to read the lgo file in case any
  70. // fatal errors caused the test to crash.
  71. list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
  72. simpletest_log_read($test_id, $last_prefix, $last_test_class);
  73. // Stop the timer.
  74. simpletest_script_reporter_timer_stop();
  75. // Display results before database is cleared.
  76. simpletest_script_reporter_display_results();
  77. if ($args['xml']) {
  78. simpletest_script_reporter_write_xml_results();
  79. }
  80. // Cleanup our test results.
  81. simpletest_clean_results_table($test_id);
  82. // Test complete, exit.
  83. exit;
  84. /**
  85. * Print help text.
  86. */
  87. function simpletest_script_help() {
  88. global $args;
  89. echo <<<EOF
  90. Run Drupal tests from the shell.
  91. Usage: {$args['script']} [OPTIONS] <tests>
  92. Example: {$args['script']} Profile
  93. All arguments are long options.
  94. --help Print this page.
  95. --list Display all available test groups.
  96. --clean Cleans up database tables or directories from previous, failed,
  97. tests and then exits (no tests are run).
  98. --url Immediately precedes a URL to set the host and path. You will
  99. need this parameter if Drupal is in a subdirectory on your
  100. localhost and you have not set \$base_url in settings.php. Tests
  101. can be run under SSL by including https:// in the URL.
  102. --php The absolute path to the PHP executable. Usually not needed.
  103. --concurrency [num]
  104. Run tests in parallel, up to [num] tests at a time.
  105. --all Run all available tests.
  106. --class Run tests identified by specific class names, instead of group names.
  107. --file Run tests identified by specific file names, instead of group names.
  108. Specify the path and the extension (i.e. 'modules/user/user.test').
  109. --xml <path>
  110. If provided, test results will be written as xml files to this path.
  111. --color Output text format results with color highlighting.
  112. --verbose Output detailed assertion messages in addition to summary.
  113. <test1>[,<test2>[,<test3> ...]]
  114. One or more tests to be run. By default, these are interpreted
  115. as the names of test groups as shown at
  116. ?q=admin/config/development/testing.
  117. These group names typically correspond to module names like "User"
  118. or "Profile" or "System", but there is also a group "XML-RPC".
  119. If --class is specified then these are interpreted as the names of
  120. specific test classes whose test methods will be run. Tests must
  121. be separated by commas. Ignored if --all is specified.
  122. To run this script you will normally invoke it from the root directory of your
  123. Drupal installation as the webserver user (differs per configuration), or root:
  124. sudo -u [wwwrun|www-data|etc] php ./scripts/{$args['script']}
  125. --url http://example.com/ --all
  126. sudo -u [wwwrun|www-data|etc] php ./scripts/{$args['script']}
  127. --url http://example.com/ --class BlockTestCase
  128. \n
  129. EOF;
  130. }
  131. /**
  132. * Parse execution argument and ensure that all are valid.
  133. *
  134. * @return The list of arguments.
  135. */
  136. function simpletest_script_parse_args() {
  137. // Set default values.
  138. $args = array(
  139. 'script' => '',
  140. 'help' => FALSE,
  141. 'list' => FALSE,
  142. 'clean' => FALSE,
  143. 'url' => '',
  144. 'php' => '',
  145. 'concurrency' => 1,
  146. 'all' => FALSE,
  147. 'class' => FALSE,
  148. 'file' => FALSE,
  149. 'color' => FALSE,
  150. 'verbose' => FALSE,
  151. 'test_names' => array(),
  152. // Used internally.
  153. 'test-id' => 0,
  154. 'execute-test' => '',
  155. 'xml' => '',
  156. );
  157. // Override with set values.
  158. $args['script'] = basename(array_shift($_SERVER['argv']));
  159. $count = 0;
  160. while ($arg = array_shift($_SERVER['argv'])) {
  161. if (preg_match('/--(\S+)/', $arg, $matches)) {
  162. // Argument found.
  163. if (array_key_exists($matches[1], $args)) {
  164. // Argument found in list.
  165. $previous_arg = $matches[1];
  166. if (is_bool($args[$previous_arg])) {
  167. $args[$matches[1]] = TRUE;
  168. }
  169. else {
  170. $args[$matches[1]] = array_shift($_SERVER['argv']);
  171. }
  172. // Clear extraneous values.
  173. $args['test_names'] = array();
  174. $count++;
  175. }
  176. else {
  177. // Argument not found in list.
  178. simpletest_script_print_error("Unknown argument '$arg'.");
  179. exit;
  180. }
  181. }
  182. else {
  183. // Values found without an argument should be test names.
  184. $args['test_names'] += explode(',', $arg);
  185. $count++;
  186. }
  187. }
  188. // Validate the concurrency argument
  189. if (!is_numeric($args['concurrency']) || $args['concurrency'] <= 0) {
  190. simpletest_script_print_error("--concurrency must be a strictly positive integer.");
  191. exit;
  192. }
  193. return array($args, $count);
  194. }
  195. /**
  196. * Initialize script variables and perform general setup requirements.
  197. */
  198. function simpletest_script_init($server_software) {
  199. global $args, $php;
  200. $host = 'localhost';
  201. $path = '';
  202. // Determine location of php command automatically, unless a command line argument is supplied.
  203. if (!empty($args['php'])) {
  204. $php = $args['php'];
  205. }
  206. elseif ($php_env = getenv('_')) {
  207. // '_' is an environment variable set by the shell. It contains the command that was executed.
  208. $php = $php_env;
  209. }
  210. elseif ($sudo = getenv('SUDO_COMMAND')) {
  211. // 'SUDO_COMMAND' is an environment variable set by the sudo program.
  212. // Extract only the PHP interpreter, not the rest of the command.
  213. list($php, ) = explode(' ', $sudo, 2);
  214. }
  215. else {
  216. simpletest_script_print_error('Unable to automatically determine the path to the PHP interpreter. Supply the --php command line argument.');
  217. simpletest_script_help();
  218. exit();
  219. }
  220. // Get URL from arguments.
  221. if (!empty($args['url'])) {
  222. $parsed_url = parse_url($args['url']);
  223. $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  224. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  225. // If the passed URL schema is 'https' then setup the $_SERVER variables
  226. // properly so that testing will run under HTTPS.
  227. if ($parsed_url['scheme'] == 'https') {
  228. $_SERVER['HTTPS'] = 'on';
  229. }
  230. }
  231. $_SERVER['HTTP_HOST'] = $host;
  232. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  233. $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  234. $_SERVER['SERVER_SOFTWARE'] = $server_software;
  235. $_SERVER['SERVER_NAME'] = 'localhost';
  236. $_SERVER['REQUEST_URI'] = $path .'/';
  237. $_SERVER['REQUEST_METHOD'] = 'GET';
  238. $_SERVER['SCRIPT_NAME'] = $path .'/index.php';
  239. $_SERVER['PHP_SELF'] = $path .'/index.php';
  240. $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
  241. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  242. // Ensure that any and all environment variables are changed to https://.
  243. foreach ($_SERVER as $key => $value) {
  244. $_SERVER[$key] = str_replace('http://', 'https://', $_SERVER[$key]);
  245. }
  246. }
  247. chdir(realpath(dirname(__FILE__) . '/..'));
  248. define('DRUPAL_ROOT', getcwd());
  249. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  250. }
  251. /**
  252. * Execute a batch of tests.
  253. */
  254. function simpletest_script_execute_batch($test_id, $test_classes) {
  255. global $args;
  256. // Multi-process execution.
  257. $children = array();
  258. while (!empty($test_classes) || !empty($children)) {
  259. while (count($children) < $args['concurrency']) {
  260. if (empty($test_classes)) {
  261. break;
  262. }
  263. // Fork a child process.
  264. $test_class = array_shift($test_classes);
  265. $command = simpletest_script_command($test_id, $test_class);
  266. $process = proc_open($command, array(), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
  267. if (!is_resource($process)) {
  268. echo "Unable to fork test process. Aborting.\n";
  269. exit;
  270. }
  271. // Register our new child.
  272. $children[] = array(
  273. 'process' => $process,
  274. 'class' => $test_class,
  275. 'pipes' => $pipes,
  276. );
  277. }
  278. // Wait for children every 200ms.
  279. usleep(200000);
  280. // Check if some children finished.
  281. foreach ($children as $cid => $child) {
  282. $status = proc_get_status($child['process']);
  283. if (empty($status['running'])) {
  284. // The child exited, unregister it.
  285. proc_close($child['process']);
  286. if ($status['exitcode']) {
  287. echo 'FATAL ' . $test_class . ': test runner returned a non-zero error code (' . $status['exitcode'] . ').' . "\n";
  288. }
  289. unset($children[$cid]);
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Bootstrap Drupal and run a single test.
  296. */
  297. function simpletest_script_run_one_test($test_id, $test_class) {
  298. try {
  299. // Bootstrap Drupal.
  300. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  301. simpletest_classloader_register();
  302. $test = new $test_class($test_id);
  303. $test->run();
  304. $info = $test->getInfo();
  305. $had_fails = (isset($test->results['#fail']) && $test->results['#fail'] > 0);
  306. $had_exceptions = (isset($test->results['#exception']) && $test->results['#exception'] > 0);
  307. $status = ($had_fails || $had_exceptions ? 'fail' : 'pass');
  308. simpletest_script_print($info['name'] . ' ' . _simpletest_format_summary_line($test->results) . "\n", simpletest_script_color_code($status));
  309. // Finished, kill this runner.
  310. exit(0);
  311. }
  312. catch (Exception $e) {
  313. echo (string) $e;
  314. exit(1);
  315. }
  316. }
  317. /**
  318. * Return a command used to run a test in a separate process.
  319. *
  320. * @param $test_id
  321. * The current test ID.
  322. * @param $test_class
  323. * The name of the test class to run.
  324. */
  325. function simpletest_script_command($test_id, $test_class) {
  326. global $args, $php;
  327. $command = escapeshellarg($php) . ' ' . escapeshellarg('./scripts/' . $args['script']) . ' --url ' . escapeshellarg($args['url']);
  328. if ($args['color']) {
  329. $command .= ' --color';
  330. }
  331. $command .= " --php " . escapeshellarg($php) . " --test-id $test_id --execute-test " . escapeshellarg($test_class);
  332. return $command;
  333. }
  334. /**
  335. * Get list of tests based on arguments. If --all specified then
  336. * returns all available tests, otherwise reads list of tests.
  337. *
  338. * Will print error and exit if no valid tests were found.
  339. *
  340. * @return List of tests.
  341. */
  342. function simpletest_script_get_test_list() {
  343. global $args, $all_tests, $groups;
  344. $test_list = array();
  345. if ($args['all']) {
  346. $test_list = $all_tests;
  347. }
  348. else {
  349. if ($args['class']) {
  350. // Check for valid class names.
  351. $test_list = array();
  352. foreach ($args['test_names'] as $test_class) {
  353. if (class_exists($test_class)) {
  354. $test_list[] = $test_class;
  355. }
  356. else {
  357. $groups = simpletest_test_get_all();
  358. $all_classes = array();
  359. foreach ($groups as $group) {
  360. $all_classes = array_merge($all_classes, array_keys($group));
  361. }
  362. simpletest_script_print_error('Test class not found: ' . $test_class);
  363. simpletest_script_print_alternatives($test_class, $all_classes, 6);
  364. exit(1);
  365. }
  366. }
  367. }
  368. elseif ($args['file']) {
  369. $files = array();
  370. foreach ($args['test_names'] as $file) {
  371. $files[drupal_realpath($file)] = 1;
  372. }
  373. // Check for valid class names.
  374. foreach ($all_tests as $class_name) {
  375. $refclass = new ReflectionClass($class_name);
  376. $file = $refclass->getFileName();
  377. if (isset($files[$file])) {
  378. $test_list[] = $class_name;
  379. }
  380. }
  381. }
  382. else {
  383. // Check for valid group names and get all valid classes in group.
  384. foreach ($args['test_names'] as $group_name) {
  385. if (isset($groups[$group_name])) {
  386. $test_list = array_merge($test_list, array_keys($groups[$group_name]));
  387. }
  388. else {
  389. simpletest_script_print_error('Test group not found: ' . $group_name);
  390. simpletest_script_print_alternatives($group_name, array_keys($groups));
  391. exit(1);
  392. }
  393. }
  394. }
  395. }
  396. if (empty($test_list)) {
  397. simpletest_script_print_error('No valid tests were specified.');
  398. exit;
  399. }
  400. return $test_list;
  401. }
  402. /**
  403. * Initialize the reporter.
  404. */
  405. function simpletest_script_reporter_init() {
  406. global $args, $all_tests, $test_list, $results_map;
  407. $results_map = array(
  408. 'pass' => 'Pass',
  409. 'fail' => 'Fail',
  410. 'exception' => 'Exception'
  411. );
  412. echo "\n";
  413. echo "Drupal test run\n";
  414. echo "---------------\n";
  415. echo "\n";
  416. // Tell the user about what tests are to be run.
  417. if ($args['all']) {
  418. echo "All tests will run.\n\n";
  419. }
  420. else {
  421. echo "Tests to be run:\n";
  422. foreach ($test_list as $class_name) {
  423. $info = call_user_func(array($class_name, 'getInfo'));
  424. echo " - " . $info['name'] . ' (' . $class_name . ')' . "\n";
  425. }
  426. echo "\n";
  427. }
  428. echo "Test run started:\n";
  429. echo " " . format_date($_SERVER['REQUEST_TIME'], 'long') . "\n";
  430. timer_start('run-tests');
  431. echo "\n";
  432. echo "Test summary\n";
  433. echo "------------\n";
  434. echo "\n";
  435. }
  436. /**
  437. * Display jUnit XML test results.
  438. */
  439. function simpletest_script_reporter_write_xml_results() {
  440. global $args, $test_id, $results_map;
  441. $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  442. $test_class = '';
  443. $xml_files = array();
  444. foreach ($results as $result) {
  445. if (isset($results_map[$result->status])) {
  446. if ($result->test_class != $test_class) {
  447. // We've moved onto a new class, so write the last classes results to a file:
  448. if (isset($xml_files[$test_class])) {
  449. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  450. unset($xml_files[$test_class]);
  451. }
  452. $test_class = $result->test_class;
  453. if (!isset($xml_files[$test_class])) {
  454. $doc = new DomDocument('1.0');
  455. $root = $doc->createElement('testsuite');
  456. $root = $doc->appendChild($root);
  457. $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root);
  458. }
  459. }
  460. // For convenience:
  461. $dom_document = &$xml_files[$test_class]['doc'];
  462. // Create the XML element for this test case:
  463. $case = $dom_document->createElement('testcase');
  464. $case->setAttribute('classname', $test_class);
  465. list($class, $name) = explode('->', $result->function, 2);
  466. $case->setAttribute('name', $name);
  467. // Passes get no further attention, but failures and exceptions get to add more detail:
  468. if ($result->status == 'fail') {
  469. $fail = $dom_document->createElement('failure');
  470. $fail->setAttribute('type', 'failure');
  471. $fail->setAttribute('message', $result->message_group);
  472. $text = $dom_document->createTextNode($result->message);
  473. $fail->appendChild($text);
  474. $case->appendChild($fail);
  475. }
  476. elseif ($result->status == 'exception') {
  477. // In the case of an exception the $result->function may not be a class
  478. // method so we record the full function name:
  479. $case->setAttribute('name', $result->function);
  480. $fail = $dom_document->createElement('error');
  481. $fail->setAttribute('type', 'exception');
  482. $fail->setAttribute('message', $result->message_group);
  483. $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
  484. $text = $dom_document->createTextNode($full_message);
  485. $fail->appendChild($text);
  486. $case->appendChild($fail);
  487. }
  488. // Append the test case XML to the test suite:
  489. $xml_files[$test_class]['suite']->appendChild($case);
  490. }
  491. }
  492. // The last test case hasn't been saved to a file yet, so do that now:
  493. if (isset($xml_files[$test_class])) {
  494. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  495. unset($xml_files[$test_class]);
  496. }
  497. }
  498. /**
  499. * Stop the test timer.
  500. */
  501. function simpletest_script_reporter_timer_stop() {
  502. echo "\n";
  503. $end = timer_stop('run-tests');
  504. echo "Test run duration: " . format_interval($end['time'] / 1000);
  505. echo "\n\n";
  506. }
  507. /**
  508. * Display test results.
  509. */
  510. function simpletest_script_reporter_display_results() {
  511. global $args, $test_id, $results_map;
  512. if ($args['verbose']) {
  513. // Report results.
  514. echo "Detailed test results\n";
  515. echo "---------------------\n";
  516. $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  517. $test_class = '';
  518. foreach ($results as $result) {
  519. if (isset($results_map[$result->status])) {
  520. if ($result->test_class != $test_class) {
  521. // Display test class every time results are for new test class.
  522. echo "\n\n---- $result->test_class ----\n\n\n";
  523. $test_class = $result->test_class;
  524. // Print table header.
  525. echo "Status Group Filename Line Function \n";
  526. echo "--------------------------------------------------------------------------------\n";
  527. }
  528. simpletest_script_format_result($result);
  529. }
  530. }
  531. }
  532. }
  533. /**
  534. * Format the result so that it fits within the default 80 character
  535. * terminal size.
  536. *
  537. * @param $result The result object to format.
  538. */
  539. function simpletest_script_format_result($result) {
  540. global $results_map, $color;
  541. $summary = sprintf("%-9.9s %-10.10s %-17.17s %4.4s %-35.35s\n",
  542. $results_map[$result->status], $result->message_group, basename($result->file), $result->line, $result->function);
  543. simpletest_script_print($summary, simpletest_script_color_code($result->status));
  544. $lines = explode("\n", wordwrap(trim(strip_tags($result->message)), 76));
  545. foreach ($lines as $line) {
  546. echo " $line\n";
  547. }
  548. }
  549. /**
  550. * Print error message prefixed with " ERROR: " and displayed in fail color
  551. * if color output is enabled.
  552. *
  553. * @param $message The message to print.
  554. */
  555. function simpletest_script_print_error($message) {
  556. simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  557. }
  558. /**
  559. * Print a message to the console, if color is enabled then the specified
  560. * color code will be used.
  561. *
  562. * @param $message The message to print.
  563. * @param $color_code The color code to use for coloring.
  564. */
  565. function simpletest_script_print($message, $color_code) {
  566. global $args;
  567. if ($args['color']) {
  568. echo "\033[" . $color_code . "m" . $message . "\033[0m";
  569. }
  570. else {
  571. echo $message;
  572. }
  573. }
  574. /**
  575. * Get the color code associated with the specified status.
  576. *
  577. * @param $status The status string to get code for.
  578. * @return Color code.
  579. */
  580. function simpletest_script_color_code($status) {
  581. switch ($status) {
  582. case 'pass':
  583. return SIMPLETEST_SCRIPT_COLOR_PASS;
  584. case 'fail':
  585. return SIMPLETEST_SCRIPT_COLOR_FAIL;
  586. case 'exception':
  587. return SIMPLETEST_SCRIPT_COLOR_EXCEPTION;
  588. }
  589. return 0; // Default formatting.
  590. }
  591. /**
  592. * Prints alternative test names.
  593. *
  594. * Searches the provided array of string values for close matches based on the
  595. * Levenshtein algorithm.
  596. *
  597. * @see http://php.net/manual/en/function.levenshtein.php
  598. *
  599. * @param string $string
  600. * A string to test.
  601. * @param array $array
  602. * A list of strings to search.
  603. * @param int $degree
  604. * The matching strictness. Higher values return fewer matches. A value of
  605. * 4 means that the function will return strings from $array if the candidate
  606. * string in $array would be identical to $string by changing 1/4 or fewer of
  607. * its characters.
  608. */
  609. function simpletest_script_print_alternatives($string, $array, $degree = 4) {
  610. $alternatives = array();
  611. foreach ($array as $item) {
  612. $lev = levenshtein($string, $item);
  613. if ($lev <= strlen($item) / $degree || FALSE !== strpos($string, $item)) {
  614. $alternatives[] = $item;
  615. }
  616. }
  617. if (!empty($alternatives)) {
  618. simpletest_script_print(" Did you mean?\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  619. foreach ($alternatives as $alternative) {
  620. simpletest_script_print(" - $alternative\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  621. }
  622. }
  623. }