Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Provides testing functionality.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Core\Asset\AttachedAssetsInterface;
|
Chris@0
|
10 use Drupal\Core\Database\Database;
|
Chris@18
|
11 use Drupal\Core\File\Exception\FileException;
|
Chris@0
|
12 use Drupal\Core\Render\Element;
|
Chris@0
|
13 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
14 use Drupal\simpletest\TestBase;
|
Chris@0
|
15 use Drupal\Core\Test\TestDatabase;
|
Chris@0
|
16 use Drupal\simpletest\TestDiscovery;
|
Chris@0
|
17 use Drupal\Tests\Listeners\SimpletestUiPrinter;
|
Chris@0
|
18 use PHPUnit\Framework\TestCase;
|
Chris@0
|
19 use Symfony\Component\Process\PhpExecutableFinder;
|
Chris@0
|
20 use Drupal\Core\Test\TestStatus;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Implements hook_help().
|
Chris@0
|
24 */
|
Chris@0
|
25 function simpletest_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
26 switch ($route_name) {
|
Chris@0
|
27 case 'help.page.simpletest':
|
Chris@0
|
28 $output = '';
|
Chris@0
|
29 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@0
|
30 $output .= '<p>' . t('The Testing module provides a framework for running automated tests. It can be used to verify a working state of Drupal before and after any code changes, or as a means for developers to write and execute tests for their modules. For more information, see the <a href=":simpletest">online documentation for the Testing module</a>.', [':simpletest' => 'https://www.drupal.org/documentation/modules/simpletest']) . '</p>';
|
Chris@0
|
31 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
32 $output .= '<dl>';
|
Chris@0
|
33 $output .= '<dt>' . t('Running tests') . '</dt>';
|
Chris@18
|
34 $output .= '<dd><p>' . t('Visit the <a href=":admin-simpletest">Testing page</a> to display a list of available tests. For comprehensive testing, select <em>all</em> tests, or individually select tests for more targeted testing. Note that it might take several minutes for all tests to complete.', [':admin-simpletest' => Url::fromRoute('simpletest.test_form')->toString()]) . '</p>';
|
Chris@0
|
35 $output .= '<p>' . t('After the tests run, a message will be displayed next to each test group indicating whether tests within it passed, failed, or had exceptions. A pass means that the test returned the expected results, while fail means that it did not. An exception normally indicates an error outside of the test, such as a PHP warning or notice. If there were failures or exceptions, the results will be expanded to show details, and the tests that had failures or exceptions will be indicated in red or pink rows. You can then use these results to refine your code and tests, until all tests pass.') . '</p></dd>';
|
Chris@0
|
36 $output .= '</dl>';
|
Chris@0
|
37 return $output;
|
Chris@0
|
38
|
Chris@0
|
39 case 'simpletest.test_form':
|
Chris@0
|
40 $output = t('Select the test(s) or test group(s) you would like to run, and click <em>Run tests</em>.');
|
Chris@0
|
41 return $output;
|
Chris@0
|
42 }
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Implements hook_theme().
|
Chris@0
|
47 */
|
Chris@0
|
48 function simpletest_theme() {
|
Chris@0
|
49 return [
|
Chris@0
|
50 'simpletest_result_summary' => [
|
Chris@0
|
51 'variables' => ['label' => NULL, 'items' => [], 'pass' => 0, 'fail' => 0, 'exception' => 0, 'debug' => 0],
|
Chris@0
|
52 ],
|
Chris@0
|
53 ];
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Implements hook_js_alter().
|
Chris@0
|
58 */
|
Chris@0
|
59 function simpletest_js_alter(&$javascript, AttachedAssetsInterface $assets) {
|
Chris@0
|
60 // Since SimpleTest is a special use case for the table select, stick the
|
Chris@0
|
61 // SimpleTest JavaScript above the table select.
|
Chris@0
|
62 $simpletest = drupal_get_path('module', 'simpletest') . '/simpletest.js';
|
Chris@0
|
63 if (array_key_exists($simpletest, $javascript) && array_key_exists('core/misc/tableselect.js', $javascript)) {
|
Chris@0
|
64 $javascript[$simpletest]['weight'] = $javascript['core/misc/tableselect.js']['weight'] - 1;
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * Prepares variables for simpletest result summary templates.
|
Chris@0
|
70 *
|
Chris@0
|
71 * Default template: simpletest-result-summary.html.twig.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @param array $variables
|
Chris@0
|
74 * An associative array containing:
|
Chris@0
|
75 * - label: An optional label to be rendered before the results.
|
Chris@0
|
76 * - ok: The overall group result pass or fail.
|
Chris@0
|
77 * - pass: The number of passes.
|
Chris@0
|
78 * - fail: The number of fails.
|
Chris@0
|
79 * - exception: The number of exceptions.
|
Chris@0
|
80 * - debug: The number of debug messages.
|
Chris@0
|
81 */
|
Chris@0
|
82 function template_preprocess_simpletest_result_summary(&$variables) {
|
Chris@0
|
83 $variables['items'] = _simpletest_build_summary_line($variables);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Formats each test result type pluralized summary.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param array $summary
|
Chris@0
|
90 * A summary of the test results.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return array
|
Chris@0
|
93 * The pluralized test summary items.
|
Chris@0
|
94 */
|
Chris@0
|
95 function _simpletest_build_summary_line($summary) {
|
Chris@0
|
96 $translation = \Drupal::translation();
|
Chris@0
|
97 $items['pass'] = $translation->formatPlural($summary['pass'], '1 pass', '@count passes');
|
Chris@0
|
98 $items['fail'] = $translation->formatPlural($summary['fail'], '1 fail', '@count fails');
|
Chris@0
|
99 $items['exception'] = $translation->formatPlural($summary['exception'], '1 exception', '@count exceptions');
|
Chris@0
|
100 if ($summary['debug']) {
|
Chris@0
|
101 $items['debug'] = $translation->formatPlural($summary['debug'], '1 debug message', '@count debug messages');
|
Chris@0
|
102 }
|
Chris@0
|
103 return $items;
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Formats test result summaries into a comma separated string for run-tests.sh.
|
Chris@0
|
108 *
|
Chris@0
|
109 * @param array $summary
|
Chris@0
|
110 * A summary of the test results.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @return string
|
Chris@0
|
113 * A concatenated string of the formatted test results.
|
Chris@0
|
114 */
|
Chris@0
|
115 function _simpletest_format_summary_line($summary) {
|
Chris@0
|
116 $parts = _simpletest_build_summary_line($summary);
|
Chris@0
|
117 return implode(', ', $parts);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * Runs tests.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @param $test_list
|
Chris@0
|
124 * List of tests to run.
|
Chris@0
|
125 *
|
Chris@0
|
126 * @return string
|
Chris@0
|
127 * The test ID.
|
Chris@0
|
128 */
|
Chris@0
|
129 function simpletest_run_tests($test_list) {
|
Chris@0
|
130 // We used to separate PHPUnit and Simpletest tests for a performance
|
Chris@0
|
131 // optimization. In order to support backwards compatibility check if these
|
Chris@0
|
132 // keys are set and create a single test list.
|
Chris@0
|
133 // @todo https://www.drupal.org/node/2748967 Remove BC support in Drupal 9.
|
Chris@0
|
134 if (isset($test_list['simpletest'])) {
|
Chris@0
|
135 $test_list = array_merge($test_list, $test_list['simpletest']);
|
Chris@0
|
136 unset($test_list['simpletest']);
|
Chris@0
|
137 }
|
Chris@0
|
138 if (isset($test_list['phpunit'])) {
|
Chris@0
|
139 $test_list = array_merge($test_list, $test_list['phpunit']);
|
Chris@0
|
140 unset($test_list['phpunit']);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@18
|
143 $test_id = \Drupal::database()->insert('simpletest_test_id')
|
Chris@0
|
144 ->useDefaults(['test_id'])
|
Chris@0
|
145 ->execute();
|
Chris@0
|
146
|
Chris@0
|
147 // Clear out the previous verbose files.
|
Chris@18
|
148 try {
|
Chris@18
|
149 \Drupal::service('file_system')->deleteRecursive('public://simpletest/verbose');
|
Chris@18
|
150 }
|
Chris@18
|
151 catch (FileException $e) {
|
Chris@18
|
152 // Ignore failed deletes.
|
Chris@18
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 // Get the info for the first test being run.
|
Chris@0
|
156 $first_test = reset($test_list);
|
Chris@0
|
157 $info = TestDiscovery::getTestInfo($first_test);
|
Chris@0
|
158
|
Chris@0
|
159 $batch = [
|
Chris@0
|
160 'title' => t('Running tests'),
|
Chris@0
|
161 'operations' => [
|
Chris@0
|
162 ['_simpletest_batch_operation', [$test_list, $test_id]],
|
Chris@0
|
163 ],
|
Chris@0
|
164 'finished' => '_simpletest_batch_finished',
|
Chris@0
|
165 'progress_message' => '',
|
Chris@0
|
166 'library' => ['simpletest/drupal.simpletest'],
|
Chris@0
|
167 'init_message' => t('Processing test @num of @max - %test.', ['%test' => $info['name'], '@num' => '1', '@max' => count($test_list)]),
|
Chris@0
|
168 ];
|
Chris@0
|
169 batch_set($batch);
|
Chris@0
|
170
|
Chris@0
|
171 \Drupal::moduleHandler()->invokeAll('test_group_started');
|
Chris@0
|
172
|
Chris@0
|
173 return $test_id;
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * Executes PHPUnit tests and returns the results of the run.
|
Chris@0
|
178 *
|
Chris@0
|
179 * @param $test_id
|
Chris@0
|
180 * The current test ID.
|
Chris@0
|
181 * @param $unescaped_test_classnames
|
Chris@0
|
182 * An array of test class names, including full namespaces, to be passed as
|
Chris@0
|
183 * a regular expression to PHPUnit's --filter option.
|
Chris@0
|
184 * @param int $status
|
Chris@0
|
185 * (optional) The exit status code of the PHPUnit process will be assigned to
|
Chris@0
|
186 * this variable.
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return array
|
Chris@0
|
189 * The parsed results of PHPUnit's JUnit XML output, in the format of
|
Chris@0
|
190 * {simpletest}'s schema.
|
Chris@0
|
191 */
|
Chris@0
|
192 function simpletest_run_phpunit_tests($test_id, array $unescaped_test_classnames, &$status = NULL) {
|
Chris@0
|
193 $phpunit_file = simpletest_phpunit_xml_filepath($test_id);
|
Chris@0
|
194 simpletest_phpunit_run_command($unescaped_test_classnames, $phpunit_file, $status, $output);
|
Chris@0
|
195
|
Chris@0
|
196 $rows = [];
|
Chris@0
|
197 if ($status == TestStatus::PASS) {
|
Chris@0
|
198 $rows = simpletest_phpunit_xml_to_rows($test_id, $phpunit_file);
|
Chris@0
|
199 }
|
Chris@0
|
200 else {
|
Chris@0
|
201 $rows[] = [
|
Chris@0
|
202 'test_id' => $test_id,
|
Chris@0
|
203 'test_class' => implode(",", $unescaped_test_classnames),
|
Chris@0
|
204 'status' => TestStatus::label($status),
|
Chris@0
|
205 'message' => 'PHPunit Test failed to complete; Error: ' . implode("\n", $output),
|
Chris@0
|
206 'message_group' => 'Other',
|
Chris@0
|
207 'function' => implode(",", $unescaped_test_classnames),
|
Chris@0
|
208 'line' => '0',
|
Chris@0
|
209 'file' => $phpunit_file,
|
Chris@0
|
210 ];
|
Chris@0
|
211 }
|
Chris@0
|
212 return $rows;
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * Inserts the parsed PHPUnit results into {simpletest}.
|
Chris@0
|
217 *
|
Chris@0
|
218 * @param array[] $phpunit_results
|
Chris@0
|
219 * An array of test results returned from simpletest_phpunit_xml_to_rows().
|
Chris@0
|
220 */
|
Chris@0
|
221 function simpletest_process_phpunit_results($phpunit_results) {
|
Chris@0
|
222 // Insert the results of the PHPUnit test run into the database so the results
|
Chris@0
|
223 // are displayed along with Simpletest's results.
|
Chris@0
|
224 if (!empty($phpunit_results)) {
|
Chris@0
|
225 $query = TestDatabase::getConnection()
|
Chris@0
|
226 ->insert('simpletest')
|
Chris@0
|
227 ->fields(array_keys($phpunit_results[0]));
|
Chris@0
|
228 foreach ($phpunit_results as $result) {
|
Chris@0
|
229 $query->values($result);
|
Chris@0
|
230 }
|
Chris@0
|
231 $query->execute();
|
Chris@0
|
232 }
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * Maps phpunit results to a data structure for batch messages and run-tests.sh.
|
Chris@0
|
237 *
|
Chris@0
|
238 * @param array $results
|
Chris@0
|
239 * The output from simpletest_run_phpunit_tests().
|
Chris@0
|
240 *
|
Chris@0
|
241 * @return array
|
Chris@0
|
242 * The test result summary. A row per test class.
|
Chris@0
|
243 */
|
Chris@0
|
244 function simpletest_summarize_phpunit_result($results) {
|
Chris@0
|
245 $summaries = [];
|
Chris@0
|
246 foreach ($results as $result) {
|
Chris@0
|
247 if (!isset($summaries[$result['test_class']])) {
|
Chris@0
|
248 $summaries[$result['test_class']] = [
|
Chris@0
|
249 '#pass' => 0,
|
Chris@0
|
250 '#fail' => 0,
|
Chris@0
|
251 '#exception' => 0,
|
Chris@0
|
252 '#debug' => 0,
|
Chris@0
|
253 ];
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 switch ($result['status']) {
|
Chris@0
|
257 case 'pass':
|
Chris@0
|
258 $summaries[$result['test_class']]['#pass']++;
|
Chris@0
|
259 break;
|
Chris@0
|
260
|
Chris@0
|
261 case 'fail':
|
Chris@0
|
262 $summaries[$result['test_class']]['#fail']++;
|
Chris@0
|
263 break;
|
Chris@0
|
264
|
Chris@0
|
265 case 'exception':
|
Chris@0
|
266 $summaries[$result['test_class']]['#exception']++;
|
Chris@0
|
267 break;
|
Chris@0
|
268
|
Chris@0
|
269 case 'debug':
|
Chris@0
|
270 $summaries[$result['test_class']]['#debug']++;
|
Chris@0
|
271 break;
|
Chris@0
|
272 }
|
Chris@0
|
273 }
|
Chris@0
|
274 return $summaries;
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 /**
|
Chris@0
|
278 * Returns the path to use for PHPUnit's --log-junit option.
|
Chris@0
|
279 *
|
Chris@0
|
280 * @param $test_id
|
Chris@0
|
281 * The current test ID.
|
Chris@0
|
282 *
|
Chris@0
|
283 * @return string
|
Chris@0
|
284 * Path to the PHPUnit XML file to use for the current $test_id.
|
Chris@0
|
285 */
|
Chris@0
|
286 function simpletest_phpunit_xml_filepath($test_id) {
|
Chris@0
|
287 return \Drupal::service('file_system')->realpath('public://simpletest') . '/phpunit-' . $test_id . '.xml';
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 /**
|
Chris@0
|
291 * Returns the path to core's phpunit.xml.dist configuration file.
|
Chris@0
|
292 *
|
Chris@0
|
293 * @return string
|
Chris@0
|
294 * The path to core's phpunit.xml.dist configuration file.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @deprecated in Drupal 8.4.x for removal before Drupal 9.0.0. PHPUnit test
|
Chris@0
|
297 * runners should change directory into core/ and then run the phpunit tool.
|
Chris@0
|
298 * See simpletest_phpunit_run_command() for an example.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @see simpletest_phpunit_run_command()
|
Chris@0
|
301 */
|
Chris@0
|
302 function simpletest_phpunit_configuration_filepath() {
|
Chris@0
|
303 @trigger_error('The ' . __FUNCTION__ . ' function is deprecated since version 8.4.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
|
Chris@0
|
304 return \Drupal::root() . '/core/phpunit.xml.dist';
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 /**
|
Chris@0
|
308 * Executes the PHPUnit command.
|
Chris@0
|
309 *
|
Chris@0
|
310 * @param array $unescaped_test_classnames
|
Chris@0
|
311 * An array of test class names, including full namespaces, to be passed as
|
Chris@0
|
312 * a regular expression to PHPUnit's --filter option.
|
Chris@0
|
313 * @param string $phpunit_file
|
Chris@0
|
314 * A filepath to use for PHPUnit's --log-junit option.
|
Chris@0
|
315 * @param int $status
|
Chris@0
|
316 * (optional) The exit status code of the PHPUnit process will be assigned to
|
Chris@0
|
317 * this variable.
|
Chris@0
|
318 * @param string $output
|
Chris@0
|
319 * (optional) The output by running the phpunit command.
|
Chris@0
|
320 *
|
Chris@0
|
321 * @return string
|
Chris@0
|
322 * The results as returned by exec().
|
Chris@0
|
323 */
|
Chris@0
|
324 function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file, &$status = NULL, &$output = NULL) {
|
Chris@0
|
325 global $base_url;
|
Chris@0
|
326 // Setup an environment variable containing the database connection so that
|
Chris@0
|
327 // functional tests can connect to the database.
|
Chris@0
|
328 putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl());
|
Chris@0
|
329
|
Chris@0
|
330 // Setup an environment variable containing the base URL, if it is available.
|
Chris@0
|
331 // This allows functional tests to browse the site under test. When running
|
Chris@0
|
332 // tests via CLI, core/phpunit.xml.dist or core/scripts/run-tests.sh can set
|
Chris@0
|
333 // this variable.
|
Chris@0
|
334 if ($base_url) {
|
Chris@0
|
335 putenv('SIMPLETEST_BASE_URL=' . $base_url);
|
Chris@0
|
336 putenv('BROWSERTEST_OUTPUT_DIRECTORY=' . \Drupal::service('file_system')->realpath('public://simpletest'));
|
Chris@0
|
337 }
|
Chris@0
|
338 $phpunit_bin = simpletest_phpunit_command();
|
Chris@0
|
339
|
Chris@0
|
340 $command = [
|
Chris@0
|
341 $phpunit_bin,
|
Chris@0
|
342 '--log-junit',
|
Chris@0
|
343 escapeshellarg($phpunit_file),
|
Chris@0
|
344 '--printer',
|
Chris@0
|
345 escapeshellarg(SimpletestUiPrinter::class),
|
Chris@0
|
346 ];
|
Chris@0
|
347
|
Chris@0
|
348 // Optimized for running a single test.
|
Chris@0
|
349 if (count($unescaped_test_classnames) == 1) {
|
Chris@0
|
350 $class = new \ReflectionClass($unescaped_test_classnames[0]);
|
Chris@0
|
351 $command[] = escapeshellarg($class->getFileName());
|
Chris@0
|
352 }
|
Chris@0
|
353 else {
|
Chris@0
|
354 // Double escape namespaces so they'll work in a regexp.
|
Chris@0
|
355 $escaped_test_classnames = array_map(function ($class) {
|
Chris@0
|
356 return addslashes($class);
|
Chris@0
|
357 }, $unescaped_test_classnames);
|
Chris@0
|
358
|
Chris@0
|
359 $filter_string = implode("|", $escaped_test_classnames);
|
Chris@0
|
360 $command = array_merge($command, [
|
Chris@0
|
361 '--filter',
|
Chris@0
|
362 escapeshellarg($filter_string),
|
Chris@0
|
363 ]);
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 // Need to change directories before running the command so that we can use
|
Chris@0
|
367 // relative paths in the configuration file's exclusions.
|
Chris@0
|
368 $old_cwd = getcwd();
|
Chris@0
|
369 chdir(\Drupal::root() . "/core");
|
Chris@0
|
370
|
Chris@0
|
371 // exec in a subshell so that the environment is isolated when running tests
|
Chris@0
|
372 // via the simpletest UI.
|
Chris@0
|
373 $ret = exec(implode(" ", $command), $output, $status);
|
Chris@0
|
374
|
Chris@0
|
375 chdir($old_cwd);
|
Chris@0
|
376 putenv('SIMPLETEST_DB=');
|
Chris@0
|
377 if ($base_url) {
|
Chris@0
|
378 putenv('SIMPLETEST_BASE_URL=');
|
Chris@0
|
379 putenv('BROWSERTEST_OUTPUT_DIRECTORY=');
|
Chris@0
|
380 }
|
Chris@0
|
381 return $ret;
|
Chris@0
|
382 }
|
Chris@0
|
383
|
Chris@0
|
384 /**
|
Chris@0
|
385 * Returns the command to run PHPUnit.
|
Chris@0
|
386 *
|
Chris@0
|
387 * @return string
|
Chris@0
|
388 * The command that can be run through exec().
|
Chris@0
|
389 */
|
Chris@0
|
390 function simpletest_phpunit_command() {
|
Chris@0
|
391 // Load the actual autoloader being used and determine its filename using
|
Chris@0
|
392 // reflection. We can determine the vendor directory based on that filename.
|
Chris@0
|
393 $autoloader = require \Drupal::root() . '/autoload.php';
|
Chris@0
|
394 $reflector = new ReflectionClass($autoloader);
|
Chris@0
|
395 $vendor_dir = dirname(dirname($reflector->getFileName()));
|
Chris@0
|
396
|
Chris@0
|
397 // The file in Composer's bin dir is a *nix link, which does not work when
|
Chris@0
|
398 // extracted from a tarball and generally not on Windows.
|
Chris@16
|
399 $command = escapeshellarg($vendor_dir . '/phpunit/phpunit/phpunit');
|
Chris@0
|
400 if (substr(PHP_OS, 0, 3) == 'WIN') {
|
Chris@0
|
401 // On Windows it is necessary to run the script using the PHP executable.
|
Chris@0
|
402 $php_executable_finder = new PhpExecutableFinder();
|
Chris@0
|
403 $php = $php_executable_finder->find();
|
Chris@16
|
404 $command = $php . ' -f ' . $command . ' --';
|
Chris@0
|
405 }
|
Chris@0
|
406 return $command;
|
Chris@0
|
407 }
|
Chris@0
|
408
|
Chris@0
|
409 /**
|
Chris@0
|
410 * Implements callback_batch_operation().
|
Chris@0
|
411 */
|
Chris@0
|
412 function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
|
Chris@17
|
413 \Drupal::service('test_discovery')->registerTestNamespaces();
|
Chris@0
|
414 // Get working values.
|
Chris@0
|
415 if (!isset($context['sandbox']['max'])) {
|
Chris@0
|
416 // First iteration: initialize working values.
|
Chris@0
|
417 $test_list = $test_list_init;
|
Chris@0
|
418 $context['sandbox']['max'] = count($test_list);
|
Chris@0
|
419 $test_results = ['#pass' => 0, '#fail' => 0, '#exception' => 0, '#debug' => 0];
|
Chris@0
|
420 }
|
Chris@0
|
421 else {
|
Chris@0
|
422 // Nth iteration: get the current values where we last stored them.
|
Chris@0
|
423 $test_list = $context['sandbox']['tests'];
|
Chris@0
|
424 $test_results = $context['sandbox']['test_results'];
|
Chris@0
|
425 }
|
Chris@0
|
426 $max = $context['sandbox']['max'];
|
Chris@0
|
427
|
Chris@0
|
428 // Perform the next test.
|
Chris@0
|
429 $test_class = array_shift($test_list);
|
Chris@0
|
430 if (is_subclass_of($test_class, TestCase::class)) {
|
Chris@0
|
431 $phpunit_results = simpletest_run_phpunit_tests($test_id, [$test_class]);
|
Chris@0
|
432 simpletest_process_phpunit_results($phpunit_results);
|
Chris@0
|
433 $test_results[$test_class] = simpletest_summarize_phpunit_result($phpunit_results)[$test_class];
|
Chris@0
|
434 }
|
Chris@0
|
435 else {
|
Chris@0
|
436 $test = new $test_class($test_id);
|
Chris@0
|
437 $test->run();
|
Chris@0
|
438 \Drupal::moduleHandler()->invokeAll('test_finished', [$test->results]);
|
Chris@0
|
439 $test_results[$test_class] = $test->results;
|
Chris@0
|
440 }
|
Chris@0
|
441 $size = count($test_list);
|
Chris@0
|
442 $info = TestDiscovery::getTestInfo($test_class);
|
Chris@0
|
443
|
Chris@0
|
444 // Gather results and compose the report.
|
Chris@0
|
445 foreach ($test_results[$test_class] as $key => $value) {
|
Chris@0
|
446 $test_results[$key] += $value;
|
Chris@0
|
447 }
|
Chris@0
|
448 $test_results[$test_class]['#name'] = $info['name'];
|
Chris@0
|
449 $items = [];
|
Chris@0
|
450 foreach (Element::children($test_results) as $class) {
|
Chris@0
|
451 $class_test_result = $test_results[$class] + [
|
Chris@0
|
452 '#theme' => 'simpletest_result_summary',
|
Chris@0
|
453 '#label' => t($test_results[$class]['#name'] . ':'),
|
Chris@0
|
454 ];
|
Chris@0
|
455 array_unshift($items, \Drupal::service('renderer')->render($class_test_result));
|
Chris@0
|
456 }
|
Chris@0
|
457 $context['message'] = t('Processed test @num of @max - %test.', ['%test' => $info['name'], '@num' => $max - $size, '@max' => $max]);
|
Chris@0
|
458 $overall_results = $test_results + [
|
Chris@0
|
459 '#theme' => 'simpletest_result_summary',
|
Chris@0
|
460 '#label' => t('Overall results:'),
|
Chris@0
|
461 ];
|
Chris@0
|
462 $context['message'] .= \Drupal::service('renderer')->render($overall_results);
|
Chris@0
|
463
|
Chris@0
|
464 $item_list = [
|
Chris@0
|
465 '#theme' => 'item_list',
|
Chris@0
|
466 '#items' => $items,
|
Chris@0
|
467 ];
|
Chris@0
|
468 $context['message'] .= \Drupal::service('renderer')->render($item_list);
|
Chris@0
|
469
|
Chris@0
|
470 // Save working values for the next iteration.
|
Chris@0
|
471 $context['sandbox']['tests'] = $test_list;
|
Chris@0
|
472 $context['sandbox']['test_results'] = $test_results;
|
Chris@0
|
473 // The test_id is the only thing we need to save for the report page.
|
Chris@0
|
474 $context['results']['test_id'] = $test_id;
|
Chris@0
|
475
|
Chris@0
|
476 // Multistep processing: report progress.
|
Chris@0
|
477 $context['finished'] = 1 - $size / $max;
|
Chris@0
|
478 }
|
Chris@0
|
479
|
Chris@0
|
480 /**
|
Chris@0
|
481 * Implements callback_batch_finished().
|
Chris@0
|
482 */
|
Chris@0
|
483 function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
|
Chris@0
|
484 if ($success) {
|
Chris@17
|
485 \Drupal::messenger()->addStatus(t('The test run finished in @elapsed.', ['@elapsed' => $elapsed]));
|
Chris@0
|
486 }
|
Chris@0
|
487 else {
|
Chris@0
|
488 // Use the test_id passed as a parameter to _simpletest_batch_operation().
|
Chris@0
|
489 $test_id = $operations[0][1][1];
|
Chris@0
|
490
|
Chris@0
|
491 // Retrieve the last database prefix used for testing and the last test
|
Chris@0
|
492 // class that was run from. Use the information to read the lgo file
|
Chris@0
|
493 // in case any fatal errors caused the test to crash.
|
Chris@0
|
494 list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
|
Chris@0
|
495 simpletest_log_read($test_id, $last_prefix, $last_test_class);
|
Chris@0
|
496
|
Chris@17
|
497 \Drupal::messenger()->addError(t('The test run did not successfully finish.'));
|
Chris@17
|
498 \Drupal::messenger()->addWarning(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'));
|
Chris@0
|
499 }
|
Chris@0
|
500 \Drupal::moduleHandler()->invokeAll('test_group_finished');
|
Chris@0
|
501 }
|
Chris@0
|
502
|
Chris@0
|
503 /**
|
Chris@0
|
504 * Get information about the last test that ran given a test ID.
|
Chris@0
|
505 *
|
Chris@0
|
506 * @param $test_id
|
Chris@0
|
507 * The test ID to get the last test from.
|
Chris@0
|
508 * @return array
|
Chris@0
|
509 * Array containing the last database prefix used and the last test class
|
Chris@0
|
510 * that ran.
|
Chris@0
|
511 */
|
Chris@0
|
512 function simpletest_last_test_get($test_id) {
|
Chris@0
|
513 $last_prefix = TestDatabase::getConnection()
|
Chris@0
|
514 ->queryRange('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, [
|
Chris@0
|
515 ':test_id' => $test_id,
|
Chris@0
|
516 ])
|
Chris@0
|
517 ->fetchField();
|
Chris@0
|
518 $last_test_class = TestDatabase::getConnection()
|
Chris@0
|
519 ->queryRange('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, [
|
Chris@0
|
520 ':test_id' => $test_id,
|
Chris@0
|
521 ])
|
Chris@0
|
522 ->fetchField();
|
Chris@0
|
523 return [$last_prefix, $last_test_class];
|
Chris@0
|
524 }
|
Chris@0
|
525
|
Chris@0
|
526 /**
|
Chris@0
|
527 * Reads the error log and reports any errors as assertion failures.
|
Chris@0
|
528 *
|
Chris@0
|
529 * The errors in the log should only be fatal errors since any other errors
|
Chris@0
|
530 * will have been recorded by the error handler.
|
Chris@0
|
531 *
|
Chris@0
|
532 * @param $test_id
|
Chris@0
|
533 * The test ID to which the log relates.
|
Chris@0
|
534 * @param $database_prefix
|
Chris@0
|
535 * The database prefix to which the log relates.
|
Chris@0
|
536 * @param $test_class
|
Chris@0
|
537 * The test class to which the log relates.
|
Chris@0
|
538 *
|
Chris@0
|
539 * @return bool
|
Chris@0
|
540 * Whether any fatal errors were found.
|
Chris@0
|
541 */
|
Chris@0
|
542 function simpletest_log_read($test_id, $database_prefix, $test_class) {
|
Chris@0
|
543 $test_db = new TestDatabase($database_prefix);
|
Chris@0
|
544 $log = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/error.log';
|
Chris@0
|
545 $found = FALSE;
|
Chris@0
|
546 if (file_exists($log)) {
|
Chris@0
|
547 foreach (file($log) as $line) {
|
Chris@0
|
548 if (preg_match('/\[.*?\] (.*?): (.*?) in (.*) on line (\d+)/', $line, $match)) {
|
Chris@0
|
549 // Parse PHP fatal errors for example: PHP Fatal error: Call to
|
Chris@0
|
550 // undefined function break_me() in /path/to/file.php on line 17
|
Chris@0
|
551 $caller = [
|
Chris@0
|
552 'line' => $match[4],
|
Chris@0
|
553 'file' => $match[3],
|
Chris@0
|
554 ];
|
Chris@0
|
555 TestBase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
|
Chris@0
|
556 }
|
Chris@0
|
557 else {
|
Chris@0
|
558 // Unknown format, place the entire message in the log.
|
Chris@0
|
559 TestBase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
|
Chris@0
|
560 }
|
Chris@0
|
561 $found = TRUE;
|
Chris@0
|
562 }
|
Chris@0
|
563 }
|
Chris@0
|
564 return $found;
|
Chris@0
|
565 }
|
Chris@0
|
566
|
Chris@0
|
567 /**
|
Chris@0
|
568 * Gets a list of all of the tests provided by the system.
|
Chris@0
|
569 *
|
Chris@0
|
570 * The list of test classes is loaded by searching the designated directory for
|
Chris@0
|
571 * each module for files matching the PSR-0 standard. Once loaded the test list
|
Chris@0
|
572 * is cached and stored in a static variable.
|
Chris@0
|
573 *
|
Chris@0
|
574 * @param string $extension
|
Chris@0
|
575 * (optional) The name of an extension to limit discovery to; e.g., 'node'.
|
Chris@0
|
576 * @param string[] $types
|
Chris@0
|
577 * An array of included test types.
|
Chris@0
|
578 *
|
Chris@0
|
579 * @return array[]
|
Chris@0
|
580 * An array of tests keyed with the groups, and then keyed by test classes.
|
Chris@0
|
581 * For example:
|
Chris@0
|
582 * @code
|
Chris@0
|
583 * $groups['Block'] => array(
|
Chris@0
|
584 * 'BlockTestCase' => array(
|
Chris@0
|
585 * 'name' => 'Block functionality',
|
Chris@0
|
586 * 'description' => 'Add, edit and delete custom block.',
|
Chris@0
|
587 * 'group' => 'Block',
|
Chris@0
|
588 * ),
|
Chris@0
|
589 * );
|
Chris@0
|
590 * @endcode
|
Chris@0
|
591 *
|
Chris@0
|
592 * @deprecated in Drupal 8.3.x, for removal before 9.0.0 release. Use
|
Chris@0
|
593 * \Drupal::service('test_discovery')->getTestClasses($extension, $types)
|
Chris@0
|
594 * instead.
|
Chris@0
|
595 */
|
Chris@0
|
596 function simpletest_test_get_all($extension = NULL, array $types = []) {
|
Chris@17
|
597 @trigger_error('The ' . __FUNCTION__ . ' function is deprecated in version 8.3.x and will be removed in 9.0.0. Use \Drupal::service(\'test_discovery\')->getTestClasses($extension, $types) instead.', E_USER_DEPRECATED);
|
Chris@0
|
598 return \Drupal::service('test_discovery')->getTestClasses($extension, $types);
|
Chris@0
|
599 }
|
Chris@0
|
600
|
Chris@0
|
601 /**
|
Chris@0
|
602 * Registers test namespaces of all extensions and core test classes.
|
Chris@0
|
603 *
|
Chris@0
|
604 * @deprecated in Drupal 8.3.x for removal before 9.0.0 release. Use
|
Chris@0
|
605 * \Drupal::service('test_discovery')->registerTestNamespaces() instead.
|
Chris@0
|
606 */
|
Chris@0
|
607 function simpletest_classloader_register() {
|
Chris@17
|
608 @trigger_error('The ' . __FUNCTION__ . ' function is deprecated in version 8.3.x and will be removed in 9.0.0. Use \Drupal::service(\'test_discovery\')->registerTestNamespaces() instead.', E_USER_DEPRECATED);
|
Chris@0
|
609 \Drupal::service('test_discovery')->registerTestNamespaces();
|
Chris@0
|
610 }
|
Chris@0
|
611
|
Chris@0
|
612 /**
|
Chris@0
|
613 * Generates a test file.
|
Chris@0
|
614 *
|
Chris@0
|
615 * @param string $filename
|
Chris@0
|
616 * The name of the file, including the path. The suffix '.txt' is appended to
|
Chris@0
|
617 * the supplied file name and the file is put into the public:// files
|
Chris@0
|
618 * directory.
|
Chris@0
|
619 * @param int $width
|
Chris@0
|
620 * The number of characters on one line.
|
Chris@0
|
621 * @param int $lines
|
Chris@0
|
622 * The number of lines in the file.
|
Chris@0
|
623 * @param string $type
|
Chris@0
|
624 * (optional) The type, one of:
|
Chris@0
|
625 * - text: The generated file contains random ASCII characters.
|
Chris@0
|
626 * - binary: The generated file contains random characters whose codes are in
|
Chris@0
|
627 * the range of 0 to 31.
|
Chris@0
|
628 * - binary-text: The generated file contains random sequence of '0' and '1'
|
Chris@0
|
629 * values.
|
Chris@0
|
630 *
|
Chris@0
|
631 * @return string
|
Chris@0
|
632 * The name of the file, including the path.
|
Chris@0
|
633 */
|
Chris@0
|
634 function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
|
Chris@0
|
635 $text = '';
|
Chris@0
|
636 for ($i = 0; $i < $lines; $i++) {
|
Chris@0
|
637 // Generate $width - 1 characters to leave space for the "\n" character.
|
Chris@0
|
638 for ($j = 0; $j < $width - 1; $j++) {
|
Chris@0
|
639 switch ($type) {
|
Chris@0
|
640 case 'text':
|
Chris@0
|
641 $text .= chr(rand(32, 126));
|
Chris@0
|
642 break;
|
Chris@0
|
643 case 'binary':
|
Chris@0
|
644 $text .= chr(rand(0, 31));
|
Chris@0
|
645 break;
|
Chris@0
|
646 case 'binary-text':
|
Chris@0
|
647 default:
|
Chris@0
|
648 $text .= rand(0, 1);
|
Chris@0
|
649 break;
|
Chris@0
|
650 }
|
Chris@0
|
651 }
|
Chris@0
|
652 $text .= "\n";
|
Chris@0
|
653 }
|
Chris@0
|
654
|
Chris@0
|
655 // Create filename.
|
Chris@0
|
656 file_put_contents('public://' . $filename . '.txt', $text);
|
Chris@0
|
657 return $filename;
|
Chris@0
|
658 }
|
Chris@0
|
659
|
Chris@0
|
660 /**
|
Chris@0
|
661 * Removes all temporary database tables and directories.
|
Chris@0
|
662 */
|
Chris@0
|
663 function simpletest_clean_environment() {
|
Chris@0
|
664 simpletest_clean_database();
|
Chris@0
|
665 simpletest_clean_temporary_directories();
|
Chris@0
|
666 if (\Drupal::config('simpletest.settings')->get('clear_results')) {
|
Chris@0
|
667 $count = simpletest_clean_results_table();
|
Chris@17
|
668 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 test result.', 'Removed @count test results.'));
|
Chris@0
|
669 }
|
Chris@0
|
670 else {
|
Chris@17
|
671 \Drupal::messenger()->addWarning(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
|
Chris@0
|
672 }
|
Chris@0
|
673 }
|
Chris@0
|
674
|
Chris@0
|
675 /**
|
Chris@0
|
676 * Removes prefixed tables from the database from crashed tests.
|
Chris@0
|
677 */
|
Chris@0
|
678 function simpletest_clean_database() {
|
Chris@17
|
679 $schema = Database::getConnection()->schema();
|
Chris@18
|
680 $tables = $schema->findTables('test%');
|
Chris@0
|
681 $count = 0;
|
Chris@0
|
682 foreach ($tables as $table) {
|
Chris@0
|
683 // Only drop tables which begin wih 'test' followed by digits, for example,
|
Chris@0
|
684 // {test12345678node__body}.
|
Chris@0
|
685 if (preg_match('/^test\d+.*/', $table, $matches)) {
|
Chris@17
|
686 $schema->dropTable($matches[0]);
|
Chris@0
|
687 $count++;
|
Chris@0
|
688 }
|
Chris@0
|
689 }
|
Chris@0
|
690
|
Chris@0
|
691 if ($count > 0) {
|
Chris@17
|
692 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
|
Chris@0
|
693 }
|
Chris@0
|
694 else {
|
Chris@17
|
695 \Drupal::messenger()->addStatus(t('No leftover tables to remove.'));
|
Chris@0
|
696 }
|
Chris@0
|
697 }
|
Chris@0
|
698
|
Chris@0
|
699 /**
|
Chris@0
|
700 * Finds all leftover temporary directories and removes them.
|
Chris@0
|
701 */
|
Chris@0
|
702 function simpletest_clean_temporary_directories() {
|
Chris@0
|
703 $count = 0;
|
Chris@0
|
704 if (is_dir(DRUPAL_ROOT . '/sites/simpletest')) {
|
Chris@0
|
705 $files = scandir(DRUPAL_ROOT . '/sites/simpletest');
|
Chris@0
|
706 foreach ($files as $file) {
|
Chris@0
|
707 if ($file[0] != '.') {
|
Chris@0
|
708 $path = DRUPAL_ROOT . '/sites/simpletest/' . $file;
|
Chris@18
|
709 try {
|
Chris@18
|
710 \Drupal::service('file_system')->deleteRecursive($path, function ($any_path) {
|
Chris@18
|
711 @chmod($any_path, 0700);
|
Chris@18
|
712 });
|
Chris@18
|
713 }
|
Chris@18
|
714 catch (FileException $e) {
|
Chris@18
|
715 // Ignore failed deletes.
|
Chris@18
|
716 }
|
Chris@0
|
717 $count++;
|
Chris@0
|
718 }
|
Chris@0
|
719 }
|
Chris@0
|
720 }
|
Chris@0
|
721
|
Chris@0
|
722 if ($count > 0) {
|
Chris@17
|
723 \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
|
Chris@0
|
724 }
|
Chris@0
|
725 else {
|
Chris@17
|
726 \Drupal::messenger()->addStatus(t('No temporary directories to remove.'));
|
Chris@0
|
727 }
|
Chris@0
|
728 }
|
Chris@0
|
729
|
Chris@0
|
730 /**
|
Chris@0
|
731 * Clears the test result tables.
|
Chris@0
|
732 *
|
Chris@0
|
733 * @param $test_id
|
Chris@0
|
734 * Test ID to remove results for, or NULL to remove all results.
|
Chris@0
|
735 *
|
Chris@0
|
736 * @return int
|
Chris@0
|
737 * The number of results that were removed.
|
Chris@0
|
738 */
|
Chris@0
|
739 function simpletest_clean_results_table($test_id = NULL) {
|
Chris@0
|
740 if (\Drupal::config('simpletest.settings')->get('clear_results')) {
|
Chris@0
|
741 $connection = TestDatabase::getConnection();
|
Chris@0
|
742 if ($test_id) {
|
Chris@0
|
743 $count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', [':test_id' => $test_id])->fetchField();
|
Chris@0
|
744
|
Chris@0
|
745 $connection->delete('simpletest')
|
Chris@0
|
746 ->condition('test_id', $test_id)
|
Chris@0
|
747 ->execute();
|
Chris@0
|
748 $connection->delete('simpletest_test_id')
|
Chris@0
|
749 ->condition('test_id', $test_id)
|
Chris@0
|
750 ->execute();
|
Chris@0
|
751 }
|
Chris@0
|
752 else {
|
Chris@0
|
753 $count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
|
Chris@0
|
754
|
Chris@0
|
755 // Clear test results.
|
Chris@0
|
756 $connection->delete('simpletest')->execute();
|
Chris@0
|
757 $connection->delete('simpletest_test_id')->execute();
|
Chris@0
|
758 }
|
Chris@0
|
759
|
Chris@0
|
760 return $count;
|
Chris@0
|
761 }
|
Chris@0
|
762 return 0;
|
Chris@0
|
763 }
|
Chris@0
|
764
|
Chris@0
|
765 /**
|
Chris@0
|
766 * Implements hook_mail_alter().
|
Chris@0
|
767 *
|
Chris@0
|
768 * Aborts sending of messages with ID 'simpletest_cancel_test'.
|
Chris@0
|
769 *
|
Chris@0
|
770 * @see MailTestCase::testCancelMessage()
|
Chris@0
|
771 */
|
Chris@0
|
772 function simpletest_mail_alter(&$message) {
|
Chris@0
|
773 if ($message['id'] == 'simpletest_cancel_test') {
|
Chris@0
|
774 $message['send'] = FALSE;
|
Chris@0
|
775 }
|
Chris@0
|
776 }
|
Chris@0
|
777
|
Chris@0
|
778 /**
|
Chris@0
|
779 * Converts PHPUnit's JUnit XML output to an array.
|
Chris@0
|
780 *
|
Chris@0
|
781 * @param $test_id
|
Chris@0
|
782 * The current test ID.
|
Chris@0
|
783 * @param $phpunit_xml_file
|
Chris@0
|
784 * Path to the PHPUnit XML file.
|
Chris@0
|
785 *
|
Chris@0
|
786 * @return array[]|null
|
Chris@0
|
787 * The results as array of rows in a format that can be inserted into
|
Chris@0
|
788 * {simpletest}. If the phpunit_xml_file does not have any contents then the
|
Chris@0
|
789 * function will return NULL.
|
Chris@0
|
790 */
|
Chris@0
|
791 function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
|
Chris@0
|
792 $contents = @file_get_contents($phpunit_xml_file);
|
Chris@0
|
793 if (!$contents) {
|
Chris@0
|
794 return;
|
Chris@0
|
795 }
|
Chris@0
|
796 $records = [];
|
Chris@0
|
797 $testcases = simpletest_phpunit_find_testcases(new SimpleXMLElement($contents));
|
Chris@0
|
798 foreach ($testcases as $testcase) {
|
Chris@0
|
799 $records[] = simpletest_phpunit_testcase_to_row($test_id, $testcase);
|
Chris@0
|
800 }
|
Chris@0
|
801 return $records;
|
Chris@0
|
802 }
|
Chris@0
|
803
|
Chris@0
|
804 /**
|
Chris@0
|
805 * Finds all test cases recursively from a test suite list.
|
Chris@0
|
806 *
|
Chris@0
|
807 * @param \SimpleXMLElement $element
|
Chris@0
|
808 * The PHPUnit xml to search for test cases.
|
Chris@0
|
809 * @param \SimpleXMLElement $parent
|
Chris@0
|
810 * (Optional) The parent of the current element. Defaults to NULL.
|
Chris@0
|
811 *
|
Chris@0
|
812 * @return array
|
Chris@0
|
813 * A list of all test cases.
|
Chris@0
|
814 */
|
Chris@0
|
815 function simpletest_phpunit_find_testcases(\SimpleXMLElement $element, \SimpleXMLElement $parent = NULL) {
|
Chris@0
|
816 $testcases = [];
|
Chris@0
|
817
|
Chris@0
|
818 if (!isset($parent)) {
|
Chris@0
|
819 $parent = $element;
|
Chris@0
|
820 }
|
Chris@0
|
821
|
Chris@0
|
822 if ($element->getName() === 'testcase' && (int) $parent->attributes()->tests > 0) {
|
Chris@0
|
823 // Add the class attribute if the testcase does not have one. This is the
|
Chris@0
|
824 // case for tests using a data provider. The name of the parent testsuite
|
Chris@0
|
825 // will be in the format class::method.
|
Chris@0
|
826 if (!$element->attributes()->class) {
|
Chris@0
|
827 $name = explode('::', $parent->attributes()->name, 2);
|
Chris@0
|
828 $element->addAttribute('class', $name[0]);
|
Chris@0
|
829 }
|
Chris@0
|
830 $testcases[] = $element;
|
Chris@0
|
831 }
|
Chris@0
|
832 else {
|
Chris@0
|
833 foreach ($element as $child) {
|
Chris@0
|
834 $file = (string) $parent->attributes()->file;
|
Chris@0
|
835 if ($file && !$child->attributes()->file) {
|
Chris@0
|
836 $child->addAttribute('file', $file);
|
Chris@0
|
837 }
|
Chris@0
|
838 $testcases = array_merge($testcases, simpletest_phpunit_find_testcases($child, $element));
|
Chris@0
|
839 }
|
Chris@0
|
840 }
|
Chris@0
|
841 return $testcases;
|
Chris@0
|
842 }
|
Chris@0
|
843
|
Chris@0
|
844 /**
|
Chris@0
|
845 * Converts a PHPUnit test case result to a {simpletest} result row.
|
Chris@0
|
846 *
|
Chris@0
|
847 * @param int $test_id
|
Chris@0
|
848 * The current test ID.
|
Chris@0
|
849 * @param \SimpleXMLElement $testcase
|
Chris@0
|
850 * The PHPUnit test case represented as XML element.
|
Chris@0
|
851 *
|
Chris@0
|
852 * @return array
|
Chris@0
|
853 * An array containing the {simpletest} result row.
|
Chris@0
|
854 */
|
Chris@0
|
855 function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcase) {
|
Chris@0
|
856 $message = '';
|
Chris@0
|
857 $pass = TRUE;
|
Chris@0
|
858 if ($testcase->failure) {
|
Chris@0
|
859 $lines = explode("\n", $testcase->failure);
|
Chris@0
|
860 $message = $lines[2];
|
Chris@0
|
861 $pass = FALSE;
|
Chris@0
|
862 }
|
Chris@0
|
863 if ($testcase->error) {
|
Chris@0
|
864 $message = $testcase->error;
|
Chris@0
|
865 $pass = FALSE;
|
Chris@0
|
866 }
|
Chris@0
|
867
|
Chris@0
|
868 $attributes = $testcase->attributes();
|
Chris@0
|
869
|
Chris@17
|
870 $function = $attributes->class . '->' . $attributes->name . '()';
|
Chris@0
|
871 $record = [
|
Chris@0
|
872 'test_id' => $test_id,
|
Chris@0
|
873 'test_class' => (string) $attributes->class,
|
Chris@0
|
874 'status' => $pass ? 'pass' : 'fail',
|
Chris@0
|
875 'message' => $message,
|
Chris@0
|
876 // @todo: Check on the proper values for this.
|
Chris@0
|
877 'message_group' => 'Other',
|
Chris@17
|
878 'function' => $function,
|
Chris@0
|
879 'line' => $attributes->line ?: 0,
|
Chris@17
|
880 // There are situations when the file will not be present because a PHPUnit
|
Chris@17
|
881 // @requires has caused a test to be skipped.
|
Chris@17
|
882 'file' => $attributes->file ?: $function,
|
Chris@0
|
883 ];
|
Chris@0
|
884 return $record;
|
Chris@0
|
885 }
|