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