comparison core/scripts/run-tests.sh @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /**
4 * @file
5 * This script runs Drupal tests from command line.
6 */
7
8 use Drupal\Component\FileSystem\FileSystem;
9 use Drupal\Component\Utility\Html;
10 use Drupal\Component\Utility\Timer;
11 use Drupal\Component\Uuid\Php;
12 use Drupal\Core\Database\Database;
13 use Drupal\Core\Site\Settings;
14 use Drupal\Core\StreamWrapper\PublicStream;
15 use Drupal\Core\Test\TestDatabase;
16 use Drupal\Core\Test\TestRunnerKernel;
17 use Drupal\simpletest\Form\SimpletestResultsForm;
18 use Drupal\simpletest\TestBase;
19 use Drupal\simpletest\TestDiscovery;
20 use PHPUnit\Framework\TestCase;
21 use Symfony\Component\HttpFoundation\Request;
22
23 $autoloader = require_once __DIR__ . '/../../autoload.php';
24
25 // Define some colors for display.
26 // A nice calming green.
27 const SIMPLETEST_SCRIPT_COLOR_PASS = 32;
28 // An alerting Red.
29 const SIMPLETEST_SCRIPT_COLOR_FAIL = 31;
30 // An annoying brown.
31 const SIMPLETEST_SCRIPT_COLOR_EXCEPTION = 33;
32
33 // Restricting the chunk of queries prevents memory exhaustion.
34 const SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT = 350;
35
36 const SIMPLETEST_SCRIPT_EXIT_SUCCESS = 0;
37 const SIMPLETEST_SCRIPT_EXIT_FAILURE = 1;
38 const SIMPLETEST_SCRIPT_EXIT_EXCEPTION = 2;
39
40 if (!class_exists(TestCase::class)) {
41 echo "\nrun-tests.sh requires the PHPUnit testing framework. Please use 'composer install --dev' to ensure that it is present.\n\n";
42 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
43 }
44
45 // Set defaults and get overrides.
46 list($args, $count) = simpletest_script_parse_args();
47
48 if ($args['help'] || $count == 0) {
49 simpletest_script_help();
50 exit(($count == 0) ? SIMPLETEST_SCRIPT_EXIT_FAILURE : SIMPLETEST_SCRIPT_EXIT_SUCCESS);
51 }
52
53 simpletest_script_init();
54
55 try {
56 $request = Request::createFromGlobals();
57 $kernel = TestRunnerKernel::createFromRequest($request, $autoloader);
58 $kernel->prepareLegacyRequest($request);
59 }
60 catch (Exception $e) {
61 echo (string) $e;
62 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
63 }
64
65 if ($args['execute-test']) {
66 simpletest_script_setup_database();
67 simpletest_script_run_one_test($args['test-id'], $args['execute-test']);
68 // Sub-process exited already; this is just for clarity.
69 exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
70 }
71
72 if ($args['list']) {
73 // Display all available tests.
74 echo "\nAvailable test groups & classes\n";
75 echo "-------------------------------\n\n";
76 try {
77 $groups = simpletest_test_get_all($args['module']);
78 }
79 catch (Exception $e) {
80 error_log((string) $e);
81 echo (string) $e;
82 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
83 }
84 foreach ($groups as $group => $tests) {
85 echo $group . "\n";
86 foreach ($tests as $class => $info) {
87 echo " - $class\n";
88 }
89 }
90 exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
91 }
92
93 // List-files and list-files-json provide a way for external tools such as the
94 // testbot to prioritize running changed tests.
95 // @see https://www.drupal.org/node/2569585
96 if ($args['list-files'] || $args['list-files-json']) {
97 // List all files which could be run as tests.
98 $test_discovery = NULL;
99 try {
100 $test_discovery = \Drupal::service('test_discovery');
101 } catch (Exception $e) {
102 error_log((string) $e);
103 echo (string)$e;
104 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
105 }
106 // TestDiscovery::findAllClassFiles() gives us a classmap similar to a
107 // Composer 'classmap' array.
108 $test_classes = $test_discovery->findAllClassFiles();
109 // JSON output is the easiest.
110 if ($args['list-files-json']) {
111 echo json_encode($test_classes);
112 exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
113 }
114 // Output the list of files.
115 else {
116 foreach(array_values($test_classes) as $test_class) {
117 echo $test_class . "\n";
118 }
119 }
120 exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
121 }
122
123 simpletest_script_setup_database(TRUE);
124
125 if ($args['clean']) {
126 // Clean up left-over tables and directories.
127 try {
128 simpletest_clean_environment();
129 }
130 catch (Exception $e) {
131 echo (string) $e;
132 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
133 }
134 echo "\nEnvironment cleaned.\n";
135
136 // Get the status messages and print them.
137 $messages = drupal_get_messages('status');
138 foreach ($messages['status'] as $text) {
139 echo " - " . $text . "\n";
140 }
141 exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
142 }
143
144 $test_list = simpletest_script_get_test_list();
145
146 // Try to allocate unlimited time to run the tests.
147 drupal_set_time_limit(0);
148 simpletest_script_reporter_init();
149
150 $tests_to_run = array();
151 for ($i = 0; $i < $args['repeat']; $i++) {
152 $tests_to_run = array_merge($tests_to_run, $test_list);
153 }
154
155 // Execute tests.
156 $status = simpletest_script_execute_batch($tests_to_run);
157
158 // Stop the timer.
159 simpletest_script_reporter_timer_stop();
160
161 // Ensure all test locks are released once finished. If tests are run with a
162 // concurrency of 1 the each test will clean up its own lock. Test locks are
163 // not released if using a higher concurrency to ensure each test method has
164 // unique fixtures.
165 TestDatabase::releaseAllTestLocks();
166
167 // Display results before database is cleared.
168 if ($args['browser']) {
169 simpletest_script_open_browser();
170 }
171 else {
172 simpletest_script_reporter_display_results();
173 }
174
175 if ($args['xml']) {
176 simpletest_script_reporter_write_xml_results();
177 }
178
179 // Clean up all test results.
180 if (!$args['keep-results']) {
181 try {
182 simpletest_clean_results_table();
183 }
184 catch (Exception $e) {
185 echo (string) $e;
186 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
187 }
188 }
189
190 // Test complete, exit.
191 exit($status);
192
193 /**
194 * Print help text.
195 */
196 function simpletest_script_help() {
197 global $args;
198
199 echo <<<EOF
200
201 Run Drupal tests from the shell.
202
203 Usage: {$args['script']} [OPTIONS] <tests>
204 Example: {$args['script']} Profile
205
206 All arguments are long options.
207
208 --help Print this page.
209
210 --list Display all available test groups.
211
212 --list-files
213 Display all discoverable test file paths.
214
215 --list-files-json
216 Display all discoverable test files as JSON. The array key will be
217 the test class name, and the value will be the file path of the
218 test.
219
220 --clean Cleans up database tables or directories from previous, failed,
221 tests and then exits (no tests are run).
222
223 --url The base URL of the root directory of this Drupal checkout; e.g.:
224 http://drupal.test/
225 Required unless the Drupal root directory maps exactly to:
226 http://localhost:80/
227 Use a https:// URL to force all tests to be run under SSL.
228
229 --sqlite A pathname to use for the SQLite database of the test runner.
230 Required unless this script is executed with a working Drupal
231 installation that has Simpletest module installed.
232 A relative pathname is interpreted relative to the Drupal root
233 directory.
234 Note that ':memory:' cannot be used, because this script spawns
235 sub-processes. However, you may use e.g. '/tmpfs/test.sqlite'
236
237 --keep-results-table
238
239 Boolean flag to indicate to not cleanup the simpletest result
240 table. For testbots or repeated execution of a single test it can
241 be helpful to not cleanup the simpletest result table.
242
243 --dburl A URI denoting the database driver, credentials, server hostname,
244 and database name to use in tests.
245 Required when running tests without a Drupal installation that
246 contains default database connection info in settings.php.
247 Examples:
248 mysql://username:password@localhost/databasename#table_prefix
249 sqlite://localhost/relative/path/db.sqlite
250 sqlite://localhost//absolute/path/db.sqlite
251
252 --php The absolute path to the PHP executable. Usually not needed.
253
254 --concurrency [num]
255
256 Run tests in parallel, up to [num] tests at a time.
257
258 --all Run all available tests.
259
260 --module Run all tests belonging to the specified module name.
261 (e.g., 'node')
262
263 --class Run tests identified by specific class names, instead of group names.
264 A specific test method can be added, for example,
265 'Drupal\book\Tests\BookTest::testBookExport'.
266
267 --file Run tests identified by specific file names, instead of group names.
268 Specify the path and the extension
269 (i.e. 'core/modules/user/user.test').
270
271 --types
272
273 Runs just tests from the specified test type, for example
274 run-tests.sh
275 (i.e. --types "Simpletest,PHPUnit-Functional")
276
277 --directory Run all tests found within the specified file directory.
278
279 --xml <path>
280
281 If provided, test results will be written as xml files to this path.
282
283 --color Output text format results with color highlighting.
284
285 --verbose Output detailed assertion messages in addition to summary.
286
287 --keep-results
288
289 Keeps detailed assertion results (in the database) after tests
290 have completed. By default, assertion results are cleared.
291
292 --repeat Number of times to repeat the test.
293
294 --die-on-fail
295
296 Exit test execution immediately upon any failed assertion. This
297 allows to access the test site by changing settings.php to use the
298 test database and configuration directories. Use in combination
299 with --repeat for debugging random test failures.
300
301 --browser Opens the results in the browser. This enforces --keep-results and
302 if you want to also view any pages rendered in the simpletest
303 browser you need to add --verbose to the command line.
304
305 --non-html Removes escaping from output. Useful for reading results on the
306 CLI.
307
308 <test1>[,<test2>[,<test3> ...]]
309
310 One or more tests to be run. By default, these are interpreted
311 as the names of test groups as shown at
312 admin/config/development/testing.
313 These group names typically correspond to module names like "User"
314 or "Profile" or "System", but there is also a group "Database".
315 If --class is specified then these are interpreted as the names of
316 specific test classes whose test methods will be run. Tests must
317 be separated by commas. Ignored if --all is specified.
318
319 To run this script you will normally invoke it from the root directory of your
320 Drupal installation as the webserver user (differs per configuration), or root:
321
322 sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$args['script']}
323 --url http://example.com/ --all
324 sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$args['script']}
325 --url http://example.com/ --class "Drupal\block\Tests\BlockTest"
326
327 Without a preinstalled Drupal site and enabled Simpletest module, specify a
328 SQLite database pathname to create and the default database connection info to
329 use in tests:
330
331 sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$args['script']}
332 --sqlite /tmpfs/drupal/test.sqlite
333 --dburl mysql://username:password@localhost/database
334 --url http://example.com/ --all
335
336 EOF;
337 }
338
339 /**
340 * Parse execution argument and ensure that all are valid.
341 *
342 * @return array
343 * The list of arguments.
344 */
345 function simpletest_script_parse_args() {
346 // Set default values.
347 $args = array(
348 'script' => '',
349 'help' => FALSE,
350 'list' => FALSE,
351 'list-files' => FALSE,
352 'list-files-json' => FALSE,
353 'clean' => FALSE,
354 'url' => '',
355 'sqlite' => NULL,
356 'dburl' => NULL,
357 'php' => '',
358 'concurrency' => 1,
359 'all' => FALSE,
360 'module' => NULL,
361 'class' => FALSE,
362 'file' => FALSE,
363 'types' => [],
364 'directory' => NULL,
365 'color' => FALSE,
366 'verbose' => FALSE,
367 'keep-results' => FALSE,
368 'keep-results-table' => FALSE,
369 'test_names' => array(),
370 'repeat' => 1,
371 'die-on-fail' => FALSE,
372 'browser' => FALSE,
373 // Used internally.
374 'test-id' => 0,
375 'execute-test' => '',
376 'xml' => '',
377 'non-html' => FALSE,
378 );
379
380 // Override with set values.
381 $args['script'] = basename(array_shift($_SERVER['argv']));
382
383 $count = 0;
384 while ($arg = array_shift($_SERVER['argv'])) {
385 if (preg_match('/--(\S+)/', $arg, $matches)) {
386 // Argument found.
387 if (array_key_exists($matches[1], $args)) {
388 // Argument found in list.
389 $previous_arg = $matches[1];
390 if (is_bool($args[$previous_arg])) {
391 $args[$matches[1]] = TRUE;
392 }
393 elseif (is_array($args[$previous_arg])) {
394 $value = array_shift($_SERVER['argv']);
395 $args[$matches[1]] = array_map('trim', explode(',', $value));
396 }
397 else {
398 $args[$matches[1]] = array_shift($_SERVER['argv']);
399 }
400 // Clear extraneous values.
401 $args['test_names'] = array();
402 $count++;
403 }
404 else {
405 // Argument not found in list.
406 simpletest_script_print_error("Unknown argument '$arg'.");
407 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
408 }
409 }
410 else {
411 // Values found without an argument should be test names.
412 $args['test_names'] += explode(',', $arg);
413 $count++;
414 }
415 }
416
417 // Validate the concurrency argument.
418 if (!is_numeric($args['concurrency']) || $args['concurrency'] <= 0) {
419 simpletest_script_print_error("--concurrency must be a strictly positive integer.");
420 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
421 }
422
423 if ($args['browser']) {
424 $args['keep-results'] = TRUE;
425 }
426 return array($args, $count);
427 }
428
429 /**
430 * Initialize script variables and perform general setup requirements.
431 */
432 function simpletest_script_init() {
433 global $args, $php;
434
435 $host = 'localhost';
436 $path = '';
437 $port = '80';
438
439 // Determine location of php command automatically, unless a command line
440 // argument is supplied.
441 if (!empty($args['php'])) {
442 $php = $args['php'];
443 }
444 elseif ($php_env = getenv('_')) {
445 // '_' is an environment variable set by the shell. It contains the command
446 // that was executed.
447 $php = $php_env;
448 }
449 elseif ($sudo = getenv('SUDO_COMMAND')) {
450 // 'SUDO_COMMAND' is an environment variable set by the sudo program.
451 // Extract only the PHP interpreter, not the rest of the command.
452 list($php) = explode(' ', $sudo, 2);
453 }
454 else {
455 simpletest_script_print_error('Unable to automatically determine the path to the PHP interpreter. Supply the --php command line argument.');
456 simpletest_script_help();
457 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
458 }
459
460 // Get URL from arguments.
461 if (!empty($args['url'])) {
462 $parsed_url = parse_url($args['url']);
463 $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
464 $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
465 $port = (isset($parsed_url['port']) ? $parsed_url['port'] : $port);
466 if ($path == '/') {
467 $path = '';
468 }
469 // If the passed URL schema is 'https' then setup the $_SERVER variables
470 // properly so that testing will run under HTTPS.
471 if ($parsed_url['scheme'] == 'https') {
472 $_SERVER['HTTPS'] = 'on';
473 }
474 }
475
476 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
477 $base_url = 'https://';
478 }
479 else {
480 $base_url = 'http://';
481 }
482 $base_url .= $host;
483 if ($path !== '') {
484 $base_url .= $path;
485 }
486 putenv('SIMPLETEST_BASE_URL=' . $base_url);
487 $_SERVER['HTTP_HOST'] = $host;
488 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
489 $_SERVER['SERVER_ADDR'] = '127.0.0.1';
490 $_SERVER['SERVER_PORT'] = $port;
491 $_SERVER['SERVER_SOFTWARE'] = NULL;
492 $_SERVER['SERVER_NAME'] = 'localhost';
493 $_SERVER['REQUEST_URI'] = $path . '/';
494 $_SERVER['REQUEST_METHOD'] = 'GET';
495 $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
496 $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
497 $_SERVER['PHP_SELF'] = $path . '/index.php';
498 $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
499
500 if ($args['concurrency'] > 1) {
501 $directory = FileSystem::getOsTemporaryDirectory();
502 $test_symlink = @symlink(__FILE__, $directory . '/test_symlink');
503 if (!$test_symlink) {
504 throw new \RuntimeException('In order to use a concurrency higher than 1 the test system needs to be able to create symlinks in ' . $directory);
505 }
506 unlink($directory . '/test_symlink');
507 putenv('RUN_TESTS_CONCURRENCY=' . $args['concurrency']);
508 }
509
510 if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
511 // Ensure that any and all environment variables are changed to https://.
512 foreach ($_SERVER as $key => $value) {
513 $_SERVER[$key] = str_replace('http://', 'https://', $_SERVER[$key]);
514 }
515 }
516
517 chdir(realpath(__DIR__ . '/../..'));
518 }
519
520 /**
521 * Sets up database connection info for running tests.
522 *
523 * If this script is executed from within a real Drupal installation, then this
524 * function essentially performs nothing (unless the --sqlite or --dburl
525 * parameters were passed).
526 *
527 * Otherwise, there are three database connections of concern:
528 * - --sqlite: The test runner connection, providing access to Simpletest
529 * database tables for recording test IDs and assertion results.
530 * - --dburl: A database connection that is used as base connection info for all
531 * tests; i.e., every test will spawn from this connection. In case this
532 * connection uses e.g. SQLite, then all tests will run against SQLite. This
533 * is exposed as $databases['default']['default'] to Drupal.
534 * - The actual database connection used within a test. This is the same as
535 * --dburl, but uses an additional database table prefix. This is
536 * $databases['default']['default'] within a test environment. The original
537 * connection is retained in
538 * $databases['simpletest_original_default']['default'] and restored after
539 * each test.
540 *
541 * @param bool $new
542 * Whether this process is a run-tests.sh master process. If TRUE, the SQLite
543 * database file specified by --sqlite (if any) is set up. Otherwise, database
544 * connections are prepared only.
545 */
546 function simpletest_script_setup_database($new = FALSE) {
547 global $args;
548
549 // If there is an existing Drupal installation that contains a database
550 // connection info in settings.php, then $databases['default']['default'] will
551 // hold the default database connection already. This connection is assumed to
552 // be valid, and this connection will be used in tests, so that they run
553 // against e.g. MySQL instead of SQLite.
554 // However, in case no Drupal installation exists, this default database
555 // connection can be set and/or overridden with the --dburl parameter.
556 if (!empty($args['dburl'])) {
557 // Remove a possibly existing default connection (from settings.php).
558 Database::removeConnection('default');
559 try {
560 $databases['default']['default'] = Database::convertDbUrlToConnectionInfo($args['dburl'], DRUPAL_ROOT);
561 }
562 catch (\InvalidArgumentException $e) {
563 simpletest_script_print_error('Invalid --dburl. Reason: ' . $e->getMessage());
564 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
565 }
566 }
567 // Otherwise, use the default database connection from settings.php.
568 else {
569 $databases['default'] = Database::getConnectionInfo('default');
570 }
571
572 // If there is no default database connection for tests, we cannot continue.
573 if (!isset($databases['default']['default'])) {
574 simpletest_script_print_error('Missing default database connection for tests. Use --dburl to specify one.');
575 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
576 }
577 Database::addConnectionInfo('default', 'default', $databases['default']['default']);
578
579 // If no --sqlite parameter has been passed, then Simpletest module is assumed
580 // to be installed, so the test runner database connection is the default
581 // database connection.
582 if (empty($args['sqlite'])) {
583 $sqlite = FALSE;
584 $databases['test-runner']['default'] = $databases['default']['default'];
585 }
586 // Otherwise, set up a SQLite connection for the test runner.
587 else {
588 if ($args['sqlite'][0] === '/') {
589 $sqlite = $args['sqlite'];
590 }
591 else {
592 $sqlite = DRUPAL_ROOT . '/' . $args['sqlite'];
593 }
594 $databases['test-runner']['default'] = array(
595 'driver' => 'sqlite',
596 'database' => $sqlite,
597 'prefix' => array(
598 'default' => '',
599 ),
600 );
601 // Create the test runner SQLite database, unless it exists already.
602 if ($new && !file_exists($sqlite)) {
603 if (!is_dir(dirname($sqlite))) {
604 mkdir(dirname($sqlite));
605 }
606 touch($sqlite);
607 }
608 }
609
610 // Add the test runner database connection.
611 Database::addConnectionInfo('test-runner', 'default', $databases['test-runner']['default']);
612
613 // Create the Simpletest schema.
614 try {
615 $connection = Database::getConnection('default', 'test-runner');
616 $schema = $connection->schema();
617 }
618 catch (\PDOException $e) {
619 simpletest_script_print_error($databases['test-runner']['default']['driver'] . ': ' . $e->getMessage());
620 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
621 }
622 if ($new && $sqlite) {
623 require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'simpletest') . '/simpletest.install';
624 foreach (simpletest_schema() as $name => $table_spec) {
625 try {
626 $table_exists = $schema->tableExists($name);
627 if (empty($args['keep-results-table']) && $table_exists) {
628 $connection->truncate($name)->execute();
629 }
630 if (!$table_exists) {
631 $schema->createTable($name, $table_spec);
632 }
633 }
634 catch (Exception $e) {
635 echo (string) $e;
636 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
637 }
638 }
639 }
640 // Verify that the Simpletest database schema exists by checking one table.
641 try {
642 if (!$schema->tableExists('simpletest')) {
643 simpletest_script_print_error('Missing Simpletest database schema. Either install Simpletest module or use the --sqlite parameter.');
644 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
645 }
646 }
647 catch (Exception $e) {
648 echo (string) $e;
649 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
650 }
651 }
652
653 /**
654 * Execute a batch of tests.
655 */
656 function simpletest_script_execute_batch($test_classes) {
657 global $args, $test_ids;
658
659 $total_status = SIMPLETEST_SCRIPT_EXIT_SUCCESS;
660
661 // Multi-process execution.
662 $children = array();
663 while (!empty($test_classes) || !empty($children)) {
664 while (count($children) < $args['concurrency']) {
665 if (empty($test_classes)) {
666 break;
667 }
668
669 try {
670 $test_id = Database::getConnection('default', 'test-runner')
671 ->insert('simpletest_test_id')
672 ->useDefaults(array('test_id'))
673 ->execute();
674 }
675 catch (Exception $e) {
676 echo (string) $e;
677 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
678 }
679 $test_ids[] = $test_id;
680
681 $test_class = array_shift($test_classes);
682 // Fork a child process.
683 $command = simpletest_script_command($test_id, $test_class);
684 $process = proc_open($command, array(), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
685
686 if (!is_resource($process)) {
687 echo "Unable to fork test process. Aborting.\n";
688 exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
689 }
690
691 // Register our new child.
692 $children[] = array(
693 'process' => $process,
694 'test_id' => $test_id,
695 'class' => $test_class,
696 'pipes' => $pipes,
697 );
698 }
699
700 // Wait for children every 200ms.
701 usleep(200000);
702
703 // Check if some children finished.
704 foreach ($children as $cid => $child) {
705 $status = proc_get_status($child['process']);
706 if (empty($status['running'])) {
707 // The child exited, unregister it.
708 proc_close($child['process']);
709 if ($status['exitcode'] === SIMPLETEST_SCRIPT_EXIT_FAILURE) {
710 $total_status = max($status['exitcode'], $total_status);
711 }
712 elseif ($status['exitcode']) {
713 $message = 'FATAL ' . $child['class'] . ': test runner returned a non-zero error code (' . $status['exitcode'] . ').';
714 echo $message . "\n";
715 // @todo Return SIMPLETEST_SCRIPT_EXIT_EXCEPTION instead, when
716 // DrupalCI supports this.
717 // @see https://www.drupal.org/node/2780087
718 $total_status = max(SIMPLETEST_SCRIPT_EXIT_FAILURE, $total_status);
719 // Insert a fail for xml results.
720 TestBase::insertAssert($child['test_id'], $child['class'], FALSE, $message, 'run-tests.sh check');
721 // Ensure that an error line is displayed for the class.
722 simpletest_script_reporter_display_summary(
723 $child['class'],
724 ['#pass' => 0, '#fail' => 1, '#exception' => 0, '#debug' => 0]
725 );
726 if ($args['die-on-fail']) {
727 list($db_prefix) = simpletest_last_test_get($child['test_id']);
728 $test_db = new TestDatabase($db_prefix);
729 $test_directory = $test_db->getTestSitePath();
730 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";
731 $args['keep-results'] = TRUE;
732 // Exit repeat loop immediately.
733 $args['repeat'] = -1;
734 }
735 }
736 // Free-up space by removing any potentially created resources.
737 if (!$args['keep-results']) {
738 simpletest_script_cleanup($child['test_id'], $child['class'], $status['exitcode']);
739 }
740
741 // Remove this child.
742 unset($children[$cid]);
743 }
744 }
745 }
746 return $total_status;
747 }
748
749 /**
750 * Run a PHPUnit-based test.
751 */
752 function simpletest_script_run_phpunit($test_id, $class) {
753 $reflection = new \ReflectionClass($class);
754 if ($reflection->hasProperty('runLimit')) {
755 set_time_limit($reflection->getStaticPropertyValue('runLimit'));
756 }
757
758 $results = simpletest_run_phpunit_tests($test_id, array($class), $status);
759 simpletest_process_phpunit_results($results);
760
761 // Map phpunit results to a data structure we can pass to
762 // _simpletest_format_summary_line.
763 $summaries = simpletest_summarize_phpunit_result($results);
764 foreach ($summaries as $class => $summary) {
765 simpletest_script_reporter_display_summary($class, $summary);
766 }
767 return $status;
768 }
769
770 /**
771 * Run a single test, bootstrapping Drupal if needed.
772 */
773 function simpletest_script_run_one_test($test_id, $test_class) {
774 global $args;
775
776 try {
777 if (strpos($test_class, '::') > 0) {
778 list($class_name, $method) = explode('::', $test_class, 2);
779 $methods = [$method];
780 }
781 else {
782 $class_name = $test_class;
783 // Use empty array to run all the test methods.
784 $methods = array();
785 }
786 $test = new $class_name($test_id);
787 if (is_subclass_of($test_class, TestCase::class)) {
788 $status = simpletest_script_run_phpunit($test_id, $test_class);
789 }
790 else {
791 $test->dieOnFail = (bool) $args['die-on-fail'];
792 $test->verbose = (bool) $args['verbose'];
793 $test->run($methods);
794 simpletest_script_reporter_display_summary($test_class, $test->results);
795
796 $status = SIMPLETEST_SCRIPT_EXIT_SUCCESS;
797 // Finished, kill this runner.
798 if ($test->results['#fail'] || $test->results['#exception']) {
799 $status = SIMPLETEST_SCRIPT_EXIT_FAILURE;
800 }
801 }
802
803 exit($status);
804 }
805 // DrupalTestCase::run() catches exceptions already, so this is only reached
806 // when an exception is thrown in the wrapping test runner environment.
807 catch (Exception $e) {
808 echo (string) $e;
809 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
810 }
811 }
812
813 /**
814 * Return a command used to run a test in a separate process.
815 *
816 * @param int $test_id
817 * The current test ID.
818 * @param string $test_class
819 * The name of the test class to run.
820 *
821 * @return string
822 * The assembled command string.
823 */
824 function simpletest_script_command($test_id, $test_class) {
825 global $args, $php;
826
827 $command = escapeshellarg($php) . ' ' . escapeshellarg('./core/scripts/' . $args['script']);
828 $command .= ' --url ' . escapeshellarg($args['url']);
829 if (!empty($args['sqlite'])) {
830 $command .= ' --sqlite ' . escapeshellarg($args['sqlite']);
831 }
832 if (!empty($args['dburl'])) {
833 $command .= ' --dburl ' . escapeshellarg($args['dburl']);
834 }
835 $command .= ' --php ' . escapeshellarg($php);
836 $command .= " --test-id $test_id";
837 foreach (array('verbose', 'keep-results', 'color', 'die-on-fail') as $arg) {
838 if ($args[$arg]) {
839 $command .= ' --' . $arg;
840 }
841 }
842 // --execute-test and class name needs to come last.
843 $command .= ' --execute-test ' . escapeshellarg($test_class);
844 return $command;
845 }
846
847 /**
848 * Removes all remnants of a test runner.
849 *
850 * In case a (e.g., fatal) error occurs after the test site has been fully setup
851 * and the error happens in many tests, the environment that executes the tests
852 * can easily run out of memory or disk space. This function ensures that all
853 * created resources are properly cleaned up after every executed test.
854 *
855 * This clean-up only exists in this script, since SimpleTest module itself does
856 * not use isolated sub-processes for each test being run, so a fatal error
857 * halts not only the test, but also the test runner (i.e., the parent site).
858 *
859 * @param int $test_id
860 * The test ID of the test run.
861 * @param string $test_class
862 * The class name of the test run.
863 * @param int $exitcode
864 * The exit code of the test runner.
865 *
866 * @see simpletest_script_run_one_test()
867 */
868 function simpletest_script_cleanup($test_id, $test_class, $exitcode) {
869 if (is_subclass_of($test_class, TestCase::class)) {
870 // PHPUnit test, move on.
871 return;
872 }
873 // Retrieve the last database prefix used for testing.
874 try {
875 list($db_prefix) = simpletest_last_test_get($test_id);
876 }
877 catch (Exception $e) {
878 echo (string) $e;
879 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
880 }
881
882 // If no database prefix was found, then the test was not set up correctly.
883 if (empty($db_prefix)) {
884 echo "\nFATAL $test_class: Found no database prefix for test ID $test_id. (Check whether setUp() is invoked correctly.)";
885 return;
886 }
887
888 // Do not output verbose cleanup messages in case of a positive exitcode.
889 $output = !empty($exitcode);
890 $messages = array();
891
892 $messages[] = "- Found database prefix '$db_prefix' for test ID $test_id.";
893
894 // Read the log file in case any fatal errors caused the test to crash.
895 try {
896 simpletest_log_read($test_id, $db_prefix, $test_class);
897 }
898 catch (Exception $e) {
899 echo (string) $e;
900 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
901 }
902
903 // Check whether a test site directory was setup already.
904 // @see \Drupal\simpletest\TestBase::prepareEnvironment()
905 $test_db = new TestDatabase($db_prefix);
906 $test_directory = DRUPAL_ROOT . '/' . $test_db->getTestSitePath();
907 if (is_dir($test_directory)) {
908 // Output the error_log.
909 if (is_file($test_directory . '/error.log')) {
910 if ($errors = file_get_contents($test_directory . '/error.log')) {
911 $output = TRUE;
912 $messages[] = $errors;
913 }
914 }
915 // Delete the test site directory.
916 // simpletest_clean_temporary_directories() cannot be used here, since it
917 // would also delete file directories of other tests that are potentially
918 // running concurrently.
919 file_unmanaged_delete_recursive($test_directory, array('Drupal\simpletest\TestBase', 'filePreDeleteCallback'));
920 $messages[] = "- Removed test site directory.";
921 }
922
923 // Clear out all database tables from the test.
924 try {
925 $schema = Database::getConnection('default', 'default')->schema();
926 $count = 0;
927 foreach ($schema->findTables($db_prefix . '%') as $table) {
928 $schema->dropTable($table);
929 $count++;
930 }
931 }
932 catch (Exception $e) {
933 echo (string) $e;
934 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
935 }
936
937 if ($count) {
938 $messages[] = "- Removed $count leftover tables.";
939 }
940
941 if ($output) {
942 echo implode("\n", $messages);
943 echo "\n";
944 }
945 }
946
947 /**
948 * Get list of tests based on arguments.
949 *
950 * If --all specified then return all available tests, otherwise reads list of
951 * tests.
952 *
953 * @return array
954 * List of tests.
955 */
956 function simpletest_script_get_test_list() {
957 global $args;
958
959 $types_processed = empty($args['types']);
960 $test_list = array();
961 if ($args['all'] || $args['module']) {
962 try {
963 $groups = simpletest_test_get_all($args['module'], $args['types']);
964 $types_processed = TRUE;
965 }
966 catch (Exception $e) {
967 echo (string) $e;
968 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
969 }
970 $all_tests = array();
971 foreach ($groups as $group => $tests) {
972 $all_tests = array_merge($all_tests, array_keys($tests));
973 }
974 $test_list = $all_tests;
975 }
976 else {
977 if ($args['class']) {
978 $test_list = array();
979 foreach ($args['test_names'] as $test_class) {
980 list($class_name) = explode('::', $test_class, 2);
981 if (class_exists($class_name)) {
982 $test_list[] = $test_class;
983 }
984 else {
985 try {
986 $groups = simpletest_test_get_all(NULL, $args['types']);
987 }
988 catch (Exception $e) {
989 echo (string) $e;
990 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
991 }
992 $all_classes = array();
993 foreach ($groups as $group) {
994 $all_classes = array_merge($all_classes, array_keys($group));
995 }
996 simpletest_script_print_error('Test class not found: ' . $class_name);
997 simpletest_script_print_alternatives($class_name, $all_classes, 6);
998 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
999 }
1000 }
1001 }
1002 elseif ($args['file']) {
1003 // Extract test case class names from specified files.
1004 foreach ($args['test_names'] as $file) {
1005 if (!file_exists($file)) {
1006 simpletest_script_print_error('File not found: ' . $file);
1007 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
1008 }
1009 $content = file_get_contents($file);
1010 // Extract a potential namespace.
1011 $namespace = FALSE;
1012 if (preg_match('@^namespace ([^ ;]+)@m', $content, $matches)) {
1013 $namespace = $matches[1];
1014 }
1015 // Extract all class names.
1016 // Abstract classes are excluded on purpose.
1017 preg_match_all('@^class ([^ ]+)@m', $content, $matches);
1018 if (!$namespace) {
1019 $test_list = array_merge($test_list, $matches[1]);
1020 }
1021 else {
1022 foreach ($matches[1] as $class_name) {
1023 $namespace_class = $namespace . '\\' . $class_name;
1024 if (is_subclass_of($namespace_class, '\Drupal\simpletest\TestBase') || is_subclass_of($namespace_class, TestCase::class)) {
1025 $test_list[] = $namespace_class;
1026 }
1027 }
1028 }
1029 }
1030 }
1031 elseif ($args['directory']) {
1032 // Extract test case class names from specified directory.
1033 // Find all tests in the PSR-X structure; Drupal\$extension\Tests\*.php
1034 // Since we do not want to hard-code too many structural file/directory
1035 // assumptions about PSR-0/4 files and directories, we check for the
1036 // minimal conditions only; i.e., a '*.php' file that has '/Tests/' in
1037 // its path.
1038 // Ignore anything from third party vendors.
1039 $ignore = array('.', '..', 'vendor');
1040 $files = [];
1041 if ($args['directory'][0] === '/') {
1042 $directory = $args['directory'];
1043 }
1044 else {
1045 $directory = DRUPAL_ROOT . "/" . $args['directory'];
1046 }
1047 foreach (file_scan_directory($directory, '/\.php$/', $ignore) as $file) {
1048 // '/Tests/' can be contained anywhere in the file's path (there can be
1049 // sub-directories below /Tests), but must be contained literally.
1050 // Case-insensitive to match all Simpletest and PHPUnit tests:
1051 // ./lib/Drupal/foo/Tests/Bar/Baz.php
1052 // ./foo/src/Tests/Bar/Baz.php
1053 // ./foo/tests/Drupal/foo/Tests/FooTest.php
1054 // ./foo/tests/src/FooTest.php
1055 // $file->filename doesn't give us a directory, so we use $file->uri
1056 // Strip the drupal root directory and trailing slash off the URI.
1057 $filename = substr($file->uri, strlen(DRUPAL_ROOT) + 1);
1058 if (stripos($filename, '/Tests/')) {
1059 $files[$filename] = $filename;
1060 }
1061 }
1062 foreach ($files as $file) {
1063 $content = file_get_contents($file);
1064 // Extract a potential namespace.
1065 $namespace = FALSE;
1066 if (preg_match('@^\s*namespace ([^ ;]+)@m', $content, $matches)) {
1067 $namespace = $matches[1];
1068 }
1069 // Extract all class names.
1070 // Abstract classes are excluded on purpose.
1071 preg_match_all('@^\s*class ([^ ]+)@m', $content, $matches);
1072 if (!$namespace) {
1073 $test_list = array_merge($test_list, $matches[1]);
1074 }
1075 else {
1076 foreach ($matches[1] as $class_name) {
1077 $namespace_class = $namespace . '\\' . $class_name;
1078 if (is_subclass_of($namespace_class, '\Drupal\simpletest\TestBase') || is_subclass_of($namespace_class, TestCase::class)) {
1079 $test_list[] = $namespace_class;
1080 }
1081 }
1082 }
1083 }
1084 }
1085 else {
1086 try {
1087 $groups = simpletest_test_get_all(NULL, $args['types']);
1088 $types_processed = TRUE;
1089 }
1090 catch (Exception $e) {
1091 echo (string) $e;
1092 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
1093 }
1094 foreach ($args['test_names'] as $group_name) {
1095 if (isset($groups[$group_name])) {
1096 $test_list = array_merge($test_list, array_keys($groups[$group_name]));
1097 }
1098 else {
1099 simpletest_script_print_error('Test group not found: ' . $group_name);
1100 simpletest_script_print_alternatives($group_name, array_keys($groups));
1101 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
1102 }
1103 }
1104 }
1105 }
1106
1107 // If the test list creation does not automatically limit by test type then
1108 // we need to do so here.
1109 if (!$types_processed) {
1110 $test_list = array_filter($test_list, function ($test_class) use ($args) {
1111 $test_info = TestDiscovery::getTestInfo($test_class);
1112 return in_array($test_info['type'], $args['types'], TRUE);
1113 });
1114 }
1115
1116 if (empty($test_list)) {
1117 simpletest_script_print_error('No valid tests were specified.');
1118 exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
1119 }
1120 return $test_list;
1121 }
1122
1123 /**
1124 * Initialize the reporter.
1125 */
1126 function simpletest_script_reporter_init() {
1127 global $args, $test_list, $results_map;
1128
1129 $results_map = array(
1130 'pass' => 'Pass',
1131 'fail' => 'Fail',
1132 'exception' => 'Exception',
1133 );
1134
1135 echo "\n";
1136 echo "Drupal test run\n";
1137 echo "---------------\n";
1138 echo "\n";
1139
1140 // Tell the user about what tests are to be run.
1141 if ($args['all']) {
1142 echo "All tests will run.\n\n";
1143 }
1144 else {
1145 echo "Tests to be run:\n";
1146 foreach ($test_list as $class_name) {
1147 echo " - $class_name\n";
1148 }
1149 echo "\n";
1150 }
1151
1152 echo "Test run started:\n";
1153 echo " " . date('l, F j, Y - H:i', $_SERVER['REQUEST_TIME']) . "\n";
1154 Timer::start('run-tests');
1155 echo "\n";
1156
1157 echo "Test summary\n";
1158 echo "------------\n";
1159 echo "\n";
1160 }
1161
1162 /**
1163 * Displays the assertion result summary for a single test class.
1164 *
1165 * @param string $class
1166 * The test class name that was run.
1167 * @param array $results
1168 * The assertion results using #pass, #fail, #exception, #debug array keys.
1169 */
1170 function simpletest_script_reporter_display_summary($class, $results) {
1171 // Output all test results vertically aligned.
1172 // Cut off the class name after 60 chars, and pad each group with 3 digits
1173 // by default (more than 999 assertions are rare).
1174 $output = vsprintf('%-60.60s %10s %9s %14s %12s', array(
1175 $class,
1176 $results['#pass'] . ' passes',
1177 !$results['#fail'] ? '' : $results['#fail'] . ' fails',
1178 !$results['#exception'] ? '' : $results['#exception'] . ' exceptions',
1179 !$results['#debug'] ? '' : $results['#debug'] . ' messages',
1180 ));
1181
1182 $status = ($results['#fail'] || $results['#exception'] ? 'fail' : 'pass');
1183 simpletest_script_print($output . "\n", simpletest_script_color_code($status));
1184 }
1185
1186 /**
1187 * Display jUnit XML test results.
1188 */
1189 function simpletest_script_reporter_write_xml_results() {
1190 global $args, $test_ids, $results_map;
1191
1192 try {
1193 $results = simpletest_script_load_messages_by_test_id($test_ids);
1194 }
1195 catch (Exception $e) {
1196 echo (string) $e;
1197 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
1198 }
1199
1200 $test_class = '';
1201 $xml_files = array();
1202
1203 foreach ($results as $result) {
1204 if (isset($results_map[$result->status])) {
1205 if ($result->test_class != $test_class) {
1206 // We've moved onto a new class, so write the last classes results to a
1207 // file:
1208 if (isset($xml_files[$test_class])) {
1209 file_put_contents($args['xml'] . '/' . str_replace('\\', '_', $test_class) . '.xml', $xml_files[$test_class]['doc']->saveXML());
1210 unset($xml_files[$test_class]);
1211 }
1212 $test_class = $result->test_class;
1213 if (!isset($xml_files[$test_class])) {
1214 $doc = new DomDocument('1.0');
1215 $root = $doc->createElement('testsuite');
1216 $root = $doc->appendChild($root);
1217 $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root);
1218 }
1219 }
1220
1221 // For convenience:
1222 $dom_document = &$xml_files[$test_class]['doc'];
1223
1224 // Create the XML element for this test case:
1225 $case = $dom_document->createElement('testcase');
1226 $case->setAttribute('classname', $test_class);
1227 if (strpos($result->function, '->') !== FALSE) {
1228 list($class, $name) = explode('->', $result->function, 2);
1229 }
1230 else {
1231 $name = $result->function;
1232 }
1233 $case->setAttribute('name', $name);
1234
1235 // Passes get no further attention, but failures and exceptions get to add
1236 // more detail:
1237 if ($result->status == 'fail') {
1238 $fail = $dom_document->createElement('failure');
1239 $fail->setAttribute('type', 'failure');
1240 $fail->setAttribute('message', $result->message_group);
1241 $text = $dom_document->createTextNode($result->message);
1242 $fail->appendChild($text);
1243 $case->appendChild($fail);
1244 }
1245 elseif ($result->status == 'exception') {
1246 // In the case of an exception the $result->function may not be a class
1247 // method so we record the full function name:
1248 $case->setAttribute('name', $result->function);
1249
1250 $fail = $dom_document->createElement('error');
1251 $fail->setAttribute('type', 'exception');
1252 $fail->setAttribute('message', $result->message_group);
1253 $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
1254 $text = $dom_document->createTextNode($full_message);
1255 $fail->appendChild($text);
1256 $case->appendChild($fail);
1257 }
1258 // Append the test case XML to the test suite:
1259 $xml_files[$test_class]['suite']->appendChild($case);
1260 }
1261 }
1262 // The last test case hasn't been saved to a file yet, so do that now:
1263 if (isset($xml_files[$test_class])) {
1264 file_put_contents($args['xml'] . '/' . str_replace('\\', '_', $test_class) . '.xml', $xml_files[$test_class]['doc']->saveXML());
1265 unset($xml_files[$test_class]);
1266 }
1267 }
1268
1269 /**
1270 * Stop the test timer.
1271 */
1272 function simpletest_script_reporter_timer_stop() {
1273 echo "\n";
1274 $end = Timer::stop('run-tests');
1275 echo "Test run duration: " . \Drupal::service('date.formatter')->formatInterval($end['time'] / 1000);
1276 echo "\n\n";
1277 }
1278
1279 /**
1280 * Display test results.
1281 */
1282 function simpletest_script_reporter_display_results() {
1283 global $args, $test_ids, $results_map;
1284
1285 if ($args['verbose']) {
1286 // Report results.
1287 echo "Detailed test results\n";
1288 echo "---------------------\n";
1289
1290 try {
1291 $results = simpletest_script_load_messages_by_test_id($test_ids);
1292 }
1293 catch (Exception $e) {
1294 echo (string) $e;
1295 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
1296 }
1297 $test_class = '';
1298 foreach ($results as $result) {
1299 if (isset($results_map[$result->status])) {
1300 if ($result->test_class != $test_class) {
1301 // Display test class every time results are for new test class.
1302 echo "\n\n---- $result->test_class ----\n\n\n";
1303 $test_class = $result->test_class;
1304
1305 // Print table header.
1306 echo "Status Group Filename Line Function \n";
1307 echo "--------------------------------------------------------------------------------\n";
1308 }
1309
1310 simpletest_script_format_result($result);
1311 }
1312 }
1313 }
1314 }
1315
1316 /**
1317 * Format the result so that it fits within 80 characters.
1318 *
1319 * @param object $result
1320 * The result object to format.
1321 */
1322 function simpletest_script_format_result($result) {
1323 global $args, $results_map, $color;
1324
1325 $summary = sprintf("%-9.9s %-10.10s %-17.17s %4.4s %-35.35s\n",
1326 $results_map[$result->status], $result->message_group, basename($result->file), $result->line, $result->function);
1327
1328 simpletest_script_print($summary, simpletest_script_color_code($result->status));
1329
1330 $message = trim(strip_tags($result->message));
1331 if ($args['non-html']) {
1332 $message = Html::decodeEntities($message, ENT_QUOTES, 'UTF-8');
1333 }
1334 $lines = explode("\n", wordwrap($message), 76);
1335 foreach ($lines as $line) {
1336 echo " $line\n";
1337 }
1338 }
1339
1340 /**
1341 * Print error messages so the user will notice them.
1342 *
1343 * Print error message prefixed with " ERROR: " and displayed in fail color if
1344 * color output is enabled.
1345 *
1346 * @param string $message
1347 * The message to print.
1348 */
1349 function simpletest_script_print_error($message) {
1350 simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
1351 }
1352
1353 /**
1354 * Print a message to the console, using a color.
1355 *
1356 * @param string $message
1357 * The message to print.
1358 * @param int $color_code
1359 * The color code to use for coloring.
1360 */
1361 function simpletest_script_print($message, $color_code) {
1362 global $args;
1363 if ($args['color']) {
1364 echo "\033[" . $color_code . "m" . $message . "\033[0m";
1365 }
1366 else {
1367 echo $message;
1368 }
1369 }
1370
1371 /**
1372 * Get the color code associated with the specified status.
1373 *
1374 * @param string $status
1375 * The status string to get code for. Special cases are: 'pass', 'fail', or
1376 * 'exception'.
1377 *
1378 * @return int
1379 * Color code. Returns 0 for default case.
1380 */
1381 function simpletest_script_color_code($status) {
1382 switch ($status) {
1383 case 'pass':
1384 return SIMPLETEST_SCRIPT_COLOR_PASS;
1385
1386 case 'fail':
1387 return SIMPLETEST_SCRIPT_COLOR_FAIL;
1388
1389 case 'exception':
1390 return SIMPLETEST_SCRIPT_COLOR_EXCEPTION;
1391 }
1392 // Default formatting.
1393 return 0;
1394 }
1395
1396 /**
1397 * Prints alternative test names.
1398 *
1399 * Searches the provided array of string values for close matches based on the
1400 * Levenshtein algorithm.
1401 *
1402 * @param string $string
1403 * A string to test.
1404 * @param array $array
1405 * A list of strings to search.
1406 * @param int $degree
1407 * The matching strictness. Higher values return fewer matches. A value of
1408 * 4 means that the function will return strings from $array if the candidate
1409 * string in $array would be identical to $string by changing 1/4 or fewer of
1410 * its characters.
1411 *
1412 * @see http://php.net/manual/en/function.levenshtein.php
1413 */
1414 function simpletest_script_print_alternatives($string, $array, $degree = 4) {
1415 $alternatives = array();
1416 foreach ($array as $item) {
1417 $lev = levenshtein($string, $item);
1418 if ($lev <= strlen($item) / $degree || FALSE !== strpos($string, $item)) {
1419 $alternatives[] = $item;
1420 }
1421 }
1422 if (!empty($alternatives)) {
1423 simpletest_script_print(" Did you mean?\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
1424 foreach ($alternatives as $alternative) {
1425 simpletest_script_print(" - $alternative\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
1426 }
1427 }
1428 }
1429
1430 /**
1431 * Loads the simpletest messages from the database.
1432 *
1433 * Messages are ordered by test class and message id.
1434 *
1435 * @param array $test_ids
1436 * Array of test IDs of the messages to be loaded.
1437 *
1438 * @return array
1439 * Array of simpletest messages from the database.
1440 */
1441 function simpletest_script_load_messages_by_test_id($test_ids) {
1442 global $args;
1443 $results = array();
1444
1445 // Sqlite has a maximum number of variables per query. If required, the
1446 // database query is split into chunks.
1447 if (count($test_ids) > SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT && !empty($args['sqlite'])) {
1448 $test_id_chunks = array_chunk($test_ids, SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT);
1449 }
1450 else {
1451 $test_id_chunks = array($test_ids);
1452 }
1453
1454 foreach ($test_id_chunks as $test_id_chunk) {
1455 try {
1456 $result_chunk = Database::getConnection('default', 'test-runner')
1457 ->query("SELECT * FROM {simpletest} WHERE test_id IN ( :test_ids[] ) ORDER BY test_class, message_id", array(
1458 ':test_ids[]' => $test_id_chunk,
1459 ))->fetchAll();
1460 }
1461 catch (Exception $e) {
1462 echo (string) $e;
1463 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
1464 }
1465 if ($result_chunk) {
1466 $results = array_merge($results, $result_chunk);
1467 }
1468 }
1469
1470 return $results;
1471 }
1472
1473 /**
1474 * Display test results.
1475 */
1476 function simpletest_script_open_browser() {
1477 global $test_ids;
1478
1479 try {
1480 $connection = Database::getConnection('default', 'test-runner');
1481 $results = $connection->select('simpletest')
1482 ->fields('simpletest')
1483 ->condition('test_id', $test_ids, 'IN')
1484 ->orderBy('test_class')
1485 ->orderBy('message_id')
1486 ->execute()
1487 ->fetchAll();
1488 }
1489 catch (Exception $e) {
1490 echo (string) $e;
1491 exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
1492 }
1493
1494 // Get the results form.
1495 $form = array();
1496 SimpletestResultsForm::addResultForm($form, $results);
1497
1498 // Get the assets to make the details element collapsible and theme the result
1499 // form.
1500 $assets = new \Drupal\Core\Asset\AttachedAssets();
1501 $assets->setLibraries([
1502 'core/drupal.collapse',
1503 'system/admin',
1504 'simpletest/drupal.simpletest',
1505 ]);
1506 $resolver = \Drupal::service('asset.resolver');
1507 list($js_assets_header, $js_assets_footer) = $resolver->getJsAssets($assets, FALSE);
1508 $js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
1509 $js_assets_header = $js_collection_renderer->render($js_assets_header);
1510 $js_assets_footer = $js_collection_renderer->render($js_assets_footer);
1511 $css_assets = \Drupal::service('asset.css.collection_renderer')->render($resolver->getCssAssets($assets, FALSE));
1512
1513 // Make the html page to write to disk.
1514 $render_service = \Drupal::service('renderer');
1515 $html = '<head>' . $render_service->renderPlain($js_assets_header) . $render_service->renderPlain($css_assets) . '</head><body>' . $render_service->renderPlain($form) . $render_service->renderPlain($js_assets_footer) . '</body>';
1516
1517 // Ensure we have assets verbose directory - tests with no verbose output will
1518 // not have created one.
1519 $directory = PublicStream::basePath() . '/simpletest/verbose';
1520 file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
1521 $php = new Php();
1522 $uuid = $php->generate();
1523 $filename = $directory . '/results-' . $uuid . '.html';
1524 $base_url = getenv('SIMPLETEST_BASE_URL');
1525 if (empty($base_url)) {
1526 simpletest_script_print_error("--browser needs argument --url.");
1527 }
1528 $url = $base_url . '/' . PublicStream::basePath() . '/simpletest/verbose/results-' . $uuid . '.html';
1529 file_put_contents($filename, $html);
1530
1531 // See if we can find an OS helper to open URLs in default browser.
1532 $browser = FALSE;
1533 if (shell_exec('which xdg-open')) {
1534 $browser = 'xdg-open';
1535 }
1536 elseif (shell_exec('which open')) {
1537 $browser = 'open';
1538 }
1539 elseif (substr(PHP_OS, 0, 3) == 'WIN') {
1540 $browser = 'start';
1541 }
1542
1543 if ($browser) {
1544 shell_exec($browser . ' ' . escapeshellarg($url));
1545 }
1546 else {
1547 // Can't find assets valid browser.
1548 print 'Open file://' . realpath($filename) . ' in your browser to see the verbose output.';
1549 }
1550 }