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