Newer
Older

Dries Buytaert
committed
$results = simpletest_script_load_messages_by_test_id($test_ids);

Dries Buytaert
committed
$test_class = '';
$xml_files = array();
foreach ($results as $result) {
if (isset($results_map[$result->status])) {
if ($result->test_class != $test_class) {
// We've moved onto a new class, so write the last classes results to a file:
if (isset($xml_files[$test_class])) {
file_put_contents($args['xml'] . '/' . str_replace('\\', '_', $test_class) . '.xml', $xml_files[$test_class]['doc']->saveXML());

Dries Buytaert
committed
unset($xml_files[$test_class]);
}
$test_class = $result->test_class;
if (!isset($xml_files[$test_class])) {
$doc = new DomDocument('1.0');
$root = $doc->createElement('testsuite');
$root = $doc->appendChild($root);
$xml_files[$test_class] = array('doc' => $doc, 'suite' => $root);
}
}
// For convenience:
$dom_document = &$xml_files[$test_class]['doc'];
// Create the XML element for this test case:
$case = $dom_document->createElement('testcase');
$case->setAttribute('classname', $test_class);

Alex Pott
committed
if (strpos($result->function, '->') !== FALSE) {
list($class, $name) = explode('->', $result->function, 2);
}
else {
$name = $result->function;
}

Dries Buytaert
committed
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
$case->setAttribute('name', $name);
// Passes get no further attention, but failures and exceptions get to add more detail:
if ($result->status == 'fail') {
$fail = $dom_document->createElement('failure');
$fail->setAttribute('type', 'failure');
$fail->setAttribute('message', $result->message_group);
$text = $dom_document->createTextNode($result->message);
$fail->appendChild($text);
$case->appendChild($fail);
}
elseif ($result->status == 'exception') {
// In the case of an exception the $result->function may not be a class
// method so we record the full function name:
$case->setAttribute('name', $result->function);
$fail = $dom_document->createElement('error');
$fail->setAttribute('type', 'exception');
$fail->setAttribute('message', $result->message_group);
$full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
$text = $dom_document->createTextNode($full_message);
$fail->appendChild($text);
$case->appendChild($fail);
}
// Append the test case XML to the test suite:
$xml_files[$test_class]['suite']->appendChild($case);
}
}
// The last test case hasn't been saved to a file yet, so do that now:
if (isset($xml_files[$test_class])) {
file_put_contents($args['xml'] . '/' . str_replace('\\', '_', $test_class) . '.xml', $xml_files[$test_class]['doc']->saveXML());

Dries Buytaert
committed
unset($xml_files[$test_class]);
}
}
/**
* Stop the test timer.
*/
function simpletest_script_reporter_timer_stop() {

Dries Buytaert
committed
echo "\n";
$end = Timer::stop('run-tests');

Alex Pott
committed
echo "Test run duration: " . \Drupal::service('date.formatter')->formatInterval($end['time'] / 1000);

Dries Buytaert
committed
echo "\n\n";

Dries Buytaert
committed
}
/**
* Display test results.
*/
function simpletest_script_reporter_display_results() {

Dries Buytaert
committed
global $args, $test_ids, $results_map;

Dries Buytaert
committed

Dries Buytaert
committed
if ($args['verbose']) {
// Report results.

Dries Buytaert
committed
echo "Detailed test results\n";
echo "---------------------\n";

Dries Buytaert
committed
$results = simpletest_script_load_messages_by_test_id($test_ids);

Dries Buytaert
committed
$test_class = '';

Dries Buytaert
committed
foreach ($results as $result) {

Dries Buytaert
committed
if (isset($results_map[$result->status])) {
if ($result->test_class != $test_class) {
// Display test class every time results are for new test class.
echo "\n\n---- $result->test_class ----\n\n\n";
$test_class = $result->test_class;

Dries Buytaert
committed

catch
committed
// Print table header.
echo "Status Group Filename Line Function \n";
echo "--------------------------------------------------------------------------------\n";

Dries Buytaert
committed
}
simpletest_script_format_result($result);
}
}

Dries Buytaert
committed
/**
* Format the result so that it fits within the default 80 character
* terminal size.
*
* @param $result The result object to format.
*/
function simpletest_script_format_result($result) {
global $args, $results_map, $color;

Dries Buytaert
committed

Dries Buytaert
committed
$summary = sprintf("%-9.9s %-10.10s %-17.17s %4.4s %-35.35s\n",
$results_map[$result->status], $result->message_group, basename($result->file), $result->line, $result->function);

Dries Buytaert
committed
simpletest_script_print($summary, simpletest_script_color_code($result->status));

Dries Buytaert
committed
$message = trim(strip_tags($result->message));
if ($args['non-html']) {
$message = Html::decodeEntities($message, ENT_QUOTES, 'UTF-8');
}
$lines = explode("\n", wordwrap($message), 76);

Dries Buytaert
committed
foreach ($lines as $line) {
echo " $line\n";
}
}

Dries Buytaert
committed

Dries Buytaert
committed
/**

Dries Buytaert
committed
* Print error message prefixed with " ERROR: " and displayed in fail color
* if color output is enabled.
*
* @param $message The message to print.

Dries Buytaert
committed
*/

Dries Buytaert
committed
function simpletest_script_print_error($message) {
simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);

Dries Buytaert
committed
}

Dries Buytaert
committed

Dries Buytaert
committed
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
/**
* Print a message to the console, if color is enabled then the specified
* color code will be used.
*
* @param $message The message to print.
* @param $color_code The color code to use for coloring.
*/
function simpletest_script_print($message, $color_code) {
global $args;
if ($args['color']) {
echo "\033[" . $color_code . "m" . $message . "\033[0m";
}
else {
echo $message;
}
}
/**
* Get the color code associated with the specified status.
*
* @param $status The status string to get code for.
* @return Color code.
*/
function simpletest_script_color_code($status) {
switch ($status) {
case 'pass':
return SIMPLETEST_SCRIPT_COLOR_PASS;
case 'fail':
return SIMPLETEST_SCRIPT_COLOR_FAIL;
case 'exception':
return SIMPLETEST_SCRIPT_COLOR_EXCEPTION;
}
return 0; // Default formatting.
}

Angie Byron
committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
/**
* Prints alternative test names.
*
* Searches the provided array of string values for close matches based on the
* Levenshtein algorithm.
*
* @see http://php.net/manual/en/function.levenshtein.php
*
* @param string $string
* A string to test.
* @param array $array
* A list of strings to search.
* @param int $degree
* The matching strictness. Higher values return fewer matches. A value of
* 4 means that the function will return strings from $array if the candidate
* string in $array would be identical to $string by changing 1/4 or fewer of
* its characters.
*/
function simpletest_script_print_alternatives($string, $array, $degree = 4) {
$alternatives = array();
foreach ($array as $item) {
$lev = levenshtein($string, $item);
if ($lev <= strlen($item) / $degree || FALSE !== strpos($string, $item)) {
$alternatives[] = $item;
}
}
if (!empty($alternatives)) {
simpletest_script_print(" Did you mean?\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
foreach ($alternatives as $alternative) {
simpletest_script_print(" - $alternative\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
}
}
}

Angie Byron
committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
/**
* Loads the simpletest messages from the database.
*
* Messages are ordered by test class and message id.
*
* @param array $test_ids
* Array of test IDs of the messages to be loaded.
*
* @return array
* Array of simpletest messages from the database.
*/
function simpletest_script_load_messages_by_test_id($test_ids) {
global $args;
$results = array();
// Sqlite has a maximum number of variables per query. If required, the
// database query is split into chunks.
if (count($test_ids) > SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT && !empty($args['sqlite'])) {
$test_id_chunks = array_chunk($test_ids, SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT);
}
else {
$test_id_chunks = array($test_ids);
}
foreach ($test_id_chunks as $test_id_chunk) {
$result_chunk = Database::getConnection('default', 'test-runner')
->query("SELECT * FROM {simpletest} WHERE test_id IN ( :test_ids[] ) ORDER BY test_class, message_id", array(
':test_ids[]' => $test_id_chunk,
))->fetchAll();

Angie Byron
committed
if ($result_chunk) {
$results = array_merge($results, $result_chunk);
}
}
return $results;
}
/**
* Display test results.
*/
function simpletest_script_open_browser() {
global $test_ids;
$connection = Database::getConnection('default', 'test-runner');
$results = $connection->select('simpletest')
->fields('simpletest')
->condition('test_id', $test_ids, 'IN')
->orderBy('test_class')
->orderBy('message_id')
->execute()
->fetchAll();
// Get the results form.
$form = array();
SimpletestResultsForm::addResultForm($form, $results);
// Get the assets to make the details element collapsible and theme the result
// form.
$assets = new \Drupal\Core\Asset\AttachedAssets();
$assets->setLibraries(['core/drupal.collapse', 'system/admin', 'simpletest/drupal.simpletest']);
$resolver = \Drupal::service('asset.resolver');
list($js_assets_header, $js_assets_footer) = $resolver->getJsAssets($assets, FALSE);
$js_collection_renderer = \Drupal::service('asset.js.collection_renderer');
$js_assets_header = $js_collection_renderer->render($js_assets_header);
$js_assets_footer = $js_collection_renderer->render($js_assets_footer);
$css_assets = \Drupal::service('asset.css.collection_renderer')->render($resolver->getCssAssets($assets, FALSE));
// Make the html page to write to disk.
$render_service = \Drupal::service('renderer');
$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>';
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
// Ensure we have assets verbose directory - tests with no verbose output will not
// have created one.
$directory = PublicStream::basePath() . '/simpletest/verbose';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$uuid = new Php();
$filename = $directory .'/results-'. $uuid->generate() .'.html';
file_put_contents($filename, $html);
// See if we can find an OS helper to open URLs in default browser.
$browser = FALSE;
if (shell_exec('which xdg-open')) {
$browser = 'xdg-open';
}
elseif (shell_exec('which open')) {
$browser = 'open';
}
elseif (substr(PHP_OS, 0, 3) == 'WIN') {
$browser = 'start';
}
if ($browser) {
shell_exec($browser . ' ' . escapeshellarg($filename));
}
else {
// Can't find assets valid browser.
print 'Open file://' . realpath($filename) . ' in your browser to see the verbose output.';
}
}