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