drupal/core/modules/simpletest/simpletest.module

907 lines
30 KiB
Plaintext

<?php
use Drupal\Core\Database\Database;
use Drupal\Core\Page\HtmlPage;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Render\Element;
use Drupal\simpletest\TestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Process\PhpExecutableFinder;
/**
* @file
* Provides testing functionality.
*/
/**
* Implements hook_help().
*/
function simpletest_help($route_name, Request $request) {
switch ($route_name) {
case 'help.page.simpletest':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$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 <a href="!simpletest">the online documentation for the Testing module</a>.', array('!simpletest' => 'https://drupal.org/documentation/modules/simpletest')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Running tests') . '</dt>';
$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.', array('!admin-simpletest' => \Drupal::url('simpletest.test_form'))) . '</p>';
$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>';
$output .= '</dl>';
return $output;
case 'simpletest.test_form':
$output = t('Select the test(s) or test group(s) you would like to run, and click <em>Run tests</em>.');
return $output;
}
}
/**
* Implements hook_permission().
*/
function simpletest_permission() {
return array(
'administer unit tests' => array(
'title' => t('Administer tests'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_theme().
*/
function simpletest_theme() {
return array(
'simpletest_result_summary' => array(
'render element' => 'form',
'file' => 'simpletest.theme.inc',
),
);
}
/**
* Implements hook_js_alter().
*/
function simpletest_js_alter(&$javascript) {
// Since SimpleTest is a special use case for the table select, stick the
// SimpleTest JavaScript above the table select.
$simpletest = drupal_get_path('module', 'simpletest') . '/simpletest.js';
if (array_key_exists($simpletest, $javascript) && array_key_exists('core/misc/tableselect.js', $javascript)) {
$javascript[$simpletest]['weight'] = $javascript['core/misc/tableselect.js']['weight'] - 1;
}
}
function _simpletest_format_summary_line($summary) {
$parts = array();
$parts[] = $summary['#pass'] == 1 ? '1 pass' : $summary['#pass'] . ' passes';
$parts[] = $summary['#fail'] == 1 ? '1 fail' : $summary['#fail'] . ' fails';
$parts[] = $summary['#exception'] == 1 ? '1 exception' : $summary['#exception'] . ' exceptions';
if ($summary['#debug']) {
$parts[] = $summary['#debug'] == 1 ? '1 debug message' : $summary['#debug'] . ' debug messages';
}
return implode(', ', $parts);
}
/**
* Runs tests.
*
* @param $test_list
* List of tests to run.
* @param $reporter
* Which reporter to use. Allowed values are: text, xml, html and drupal,
* drupal being the default.
*
* @return string
* The test ID.
*/
function simpletest_run_tests($test_list, $reporter = 'drupal') {
$test_id = db_insert('simpletest_test_id')
->useDefaults(array('test_id'))
->execute();
$phpunit_tests = isset($test_list['UnitTest']) ? $test_list['UnitTest'] : array();
if ($phpunit_tests) {
$phpunit_results = simpletest_run_phpunit_tests($test_id, $phpunit_tests);
simpletest_process_phpunit_results($phpunit_results);
}
if (!array_key_exists('WebTest', $test_list) || empty($test_list['WebTest'])) {
// Early return if there are no WebTests to run.
return $test_id;
}
// Contine with SimpleTests only.
$test_list = $test_list['WebTest'];
// Clear out the previous verbose files.
file_unmanaged_delete_recursive('public://simpletest/verbose');
// Get the info for the first test being run.
$first_test = array_shift($test_list);
$first_instance = new $first_test();
array_unshift($test_list, $first_test);
$info = $first_instance->getInfo();
$batch = array(
'title' => t('Running tests'),
'operations' => array(
array('_simpletest_batch_operation', array($test_list, $test_id)),
),
'finished' => '_simpletest_batch_finished',
'progress_message' => '',
'css' => array(drupal_get_path('module', 'simpletest') . '/css/simpletest.module.css'),
'init_message' => t('Processing test @num of @max - %test.', array('%test' => $info['name'], '@num' => '1', '@max' => count($test_list))),
);
batch_set($batch);
\Drupal::moduleHandler()->invokeAll('test_group_started');
return $test_id;
}
/**
* Executes PHPUnit tests and returns the results of the run.
*
* @param $test_id
* The current test ID.
* @param $unescaped_test_classnames
* An array of test class names, including full namespaces, to be passed as
* a regular expression to PHPUnit's --filter option.
*
* @return array
* The parsed results of PHPUnit's JUnit XML output, in the format of
* {simpletest}'s schema.
*/
function simpletest_run_phpunit_tests($test_id, array $unescaped_test_classnames) {
$phpunit_file = simpletest_phpunit_xml_filepath($test_id);
simpletest_phpunit_run_command($unescaped_test_classnames, $phpunit_file);
return simpletest_phpunit_xml_to_rows($test_id, $phpunit_file);
}
/**
* Inserts the parsed PHPUnit results into {simpletest}.
*
* @param array[] $phpunit_results
* An array of test results returned from simpletest_phpunit_xml_to_rows().
*/
function simpletest_process_phpunit_results($phpunit_results) {
// Insert the results of the PHPUnit test run into the database so the results
// are displayed along with Simpletest's results.
if (!empty($phpunit_results)) {
$query = db_insert('simpletest')->fields(array_keys($phpunit_results[0]));
foreach ($phpunit_results as $result) {
$query->values($result);
}
$query->execute();
}
}
/**
* Returns the path to use for PHPUnit's --log-junit option.
*
* @param $test_id
* The current test ID.
*
* @return string
* Path to the PHPUnit XML file to use for the current $test_id.
*/
function simpletest_phpunit_xml_filepath($test_id) {
return drupal_realpath('public://simpletest') . '/phpunit-' . $test_id . '.xml';
}
/**
* Returns the path to core's phpunit.xml.dist configuration file.
*
* @return string
* The path to core's phpunit.xml.dist configuration file.
*/
function simpletest_phpunit_configuration_filepath() {
return DRUPAL_ROOT . '/core/phpunit.xml.dist';
}
/**
* Executes the PHPUnit command.
*
* @param array $unescaped_test_classnames
* An array of test class names, including full namespaces, to be passed as
* a regular expression to PHPUnit's --filter option.
* @param string $phpunit_file
* A filepath to use for PHPUnit's --log-junit option.
*
* @return string
* The results as returned by exec().
*/
function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file) {
$phpunit_bin = simpletest_phpunit_command();
$command = array(
$phpunit_bin,
'--log-junit',
escapeshellarg($phpunit_file),
);
// Optimized for running a single test.
if (count($unescaped_test_classnames) == 1) {
$class = new \ReflectionClass($unescaped_test_classnames[0]);
$command[] = escapeshellarg($class->getFileName());
}
else {
// Double escape namespaces so they'll work in a regexp.
$escaped_test_classnames = array_map(function($class) {
return addslashes($class);
}, $unescaped_test_classnames);
$filter_string = implode("|", $escaped_test_classnames);
$command = array_merge($command, array(
'--filter',
escapeshellarg($filter_string),
));
}
// Need to change directories before running the command so that we can use
// relative paths in the configuration file's exclusions.
$old_cwd = getcwd();
chdir(DRUPAL_ROOT . "/core");
// exec in a subshell so that the environment is isolated when running tests
// via the simpletest UI.
$ret = exec(join($command, " "));
chdir($old_cwd);
return $ret;
}
/**
* Returns the command to run PHPUnit.
*
* @return string
* The command that can be run through exec().
*/
function simpletest_phpunit_command() {
// Don't use the committed version in composer's bin dir if running on
// windows.
if (substr(PHP_OS, 0, 3) == 'WIN') {
$php_executable_finder = new PhpExecutableFinder();
$php = $php_executable_finder->find();
$phpunit_bin = escapeshellarg($php) . " -f " . escapeshellarg(DRUPAL_ROOT . "/core/vendor/phpunit/phpunit/composer/bin/phpunit") . " --";
}
else {
$phpunit_bin = DRUPAL_ROOT . "/core/vendor/bin/phpunit";
}
return $phpunit_bin;
}
/**
* Batch operation callback.
*/
function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
simpletest_classloader_register();
// Get working values.
if (!isset($context['sandbox']['max'])) {
// First iteration: initialize working values.
$test_list = $test_list_init;
$context['sandbox']['max'] = count($test_list);
$test_results = array('#pass' => 0, '#fail' => 0, '#exception' => 0, '#debug' => 0);
}
else {
// Nth iteration: get the current values where we last stored them.
$test_list = $context['sandbox']['tests'];
$test_results = $context['sandbox']['test_results'];
}
$max = $context['sandbox']['max'];
// Perform the next test.
$test_class = array_shift($test_list);
$test = new $test_class($test_id);
$test->run();
$size = count($test_list);
$info = $test->getInfo();
\Drupal::moduleHandler()->invokeAll('test_finished', array($test->results));
// Gather results and compose the report.
$test_results[$test_class] = $test->results;
foreach ($test_results[$test_class] as $key => $value) {
$test_results[$key] += $value;
}
$test_results[$test_class]['#name'] = $info['name'];
$items = array();
foreach (Element::children($test_results) as $class) {
array_unshift($items, '<div class="simpletest-' . ($test_results[$class]['#fail'] + $test_results[$class]['#exception'] ? 'fail' : 'pass') . '">' . t('@name: @summary', array('@name' => $test_results[$class]['#name'], '@summary' => _simpletest_format_summary_line($test_results[$class]))) . '</div>');
}
$context['message'] = t('Processed test @num of @max - %test.', array('%test' => $info['name'], '@num' => $max - $size, '@max' => $max));
$context['message'] .= '<div class="simpletest-' . ($test_results['#fail'] + $test_results['#exception'] ? 'fail' : 'pass') . '">Overall results: ' . _simpletest_format_summary_line($test_results) . '</div>';
$item_list = array(
'#theme' => 'item_list',
'#items' => $items,
);
$context['message'] .= drupal_render($item_list);
// Save working values for the next iteration.
$context['sandbox']['tests'] = $test_list;
$context['sandbox']['test_results'] = $test_results;
// The test_id is the only thing we need to save for the report page.
$context['results']['test_id'] = $test_id;
// Multistep processing: report progress.
$context['finished'] = 1 - $size / $max;
}
function _simpletest_batch_finished($success, $results, $operations, $elapsed) {
if ($success) {
drupal_set_message(t('The test run finished in @elapsed.', array('@elapsed' => $elapsed)));
}
else {
// Use the test_id passed as a parameter to _simpletest_batch_operation().
$test_id = $operations[0][1][1];
// Retrieve the last database prefix used for testing and the last test
// class that was run from. Use the information to read the lgo file
// in case any fatal errors caused the test to crash.
list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
simpletest_log_read($test_id, $last_prefix, $last_test_class);
drupal_set_message(t('The test run did not successfully finish.'), 'error');
drupal_set_message(t('Use the <em>Clean environment</em> button to clean-up temporary files and tables.'), 'warning');
}
\Drupal::moduleHandler()->invokeAll('test_group_finished');
}
/**
* Get information about the last test that ran given a test ID.
*
* @param $test_id
* The test ID to get the last test from.
* @return array
* Array containing the last database prefix used and the last test class
* that ran.
*/
function simpletest_last_test_get($test_id) {
$last_prefix = TestBase::getDatabaseConnection()
->queryRange('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, array(
':test_id' => $test_id,
))
->fetchField();
$last_test_class = TestBase::getDatabaseConnection()
->queryRange('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, array(
':test_id' => $test_id,
))
->fetchField();
return array($last_prefix, $last_test_class);
}
/**
* Reads the error log and reports any errors as assertion failures.
*
* The errors in the log should only be fatal errors since any other errors
* will have been recorded by the error handler.
*
* @param $test_id
* The test ID to which the log relates.
* @param $database_prefix
* The database prefix to which the log relates.
* @param $test_class
* The test class to which the log relates.
*
* @return bool
* Whether any fatal errors were found.
*/
function simpletest_log_read($test_id, $database_prefix, $test_class) {
$log = DRUPAL_ROOT . '/sites/simpletest/' . substr($database_prefix, 10) . '/error.log';
$found = FALSE;
if (file_exists($log)) {
foreach (file($log) as $line) {
if (preg_match('/\[.*?\] (.*?): (.*?) in (.*) on line (\d+)/', $line, $match)) {
// Parse PHP fatal errors for example: PHP Fatal error: Call to
// undefined function break_me() in /path/to/file.php on line 17
$caller = array(
'line' => $match[4],
'file' => $match[3],
);
TestBase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
}
else {
// Unknown format, place the entire message in the log.
TestBase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
}
$found = TRUE;
}
}
return $found;
}
/**
* Gets a list of all of the tests provided by the system.
*
* The list of test classes is loaded by searching the designated directory for
* each module for files matching the PSR-0 standard. Once loaded the test list
* is cached and stored in a static variable.
*
* @param string $module
* Name of a module. If set then only tests belonging to this module are
* returned.
*
* @return array[]
* An array of tests keyed with the groups specified in each of the tests'
* getInfo() methods and then keyed by the test classes. For example:
* @code
* $groups['Block'] => array(
* 'BlockTestCase' => array(
* 'name' => 'Block functionality',
* 'description' => 'Add, edit and delete custom block...',
* 'group' => 'Block',
* ),
* );
* @endcode
*/
function simpletest_test_get_all($module = NULL) {
static $all_groups = array();
$cid = "simpletest:$module";
if (!isset($all_groups[$cid])) {
$all_groups[$cid] = array();
$groups = &$all_groups[$cid];
// Register namespaces (extensions are not necessarily enabled).
simpletest_classloader_register();
// Load test information from cache if available, otherwise retrieve the
// information from each tests getInfo() method.
if ($cache = \Drupal::cache()->get($cid)) {
$groups = $cache->data;
}
else {
// Select all PSR-0 classes in the Tests namespace of all modules.
$listing = new ExtensionDiscovery();
// Ensure that tests in all profiles are discovered.
$listing->setProfileDirectories(array());
$all_data = $listing->scan('module', TRUE);
// If module is set then we keep only that one module.
if (isset($module)) {
$all_data = array(
$module => $all_data[$module],
);
}
else {
$all_data += $listing->scan('profile', TRUE);
$all_data += $listing->scan('theme', TRUE);
}
// Scan all extension folders for class files.
$classes = array();
foreach ($all_data as $name => $data) {
// Build the directory in which simpletest test classes would reside.
$tests_dir = DRUPAL_ROOT . '/' . $data->getPath() . '/src/Tests';
// Check if the directory exists.
if (!is_dir($tests_dir)) {
// This extension has no directory for simpletest cases.
continue;
}
// Scan the directory for class files.
$files = file_scan_directory($tests_dir, '/\.php$/');
if (empty($files)) {
// No class files found.
continue;
}
// Convert the file names into the namespaced class names.
$strlen = strlen($tests_dir) + 1;
$namespace = 'Drupal\\' . $name . '\Tests\\';
foreach ($files as $file) {
$classes[] = $namespace . str_replace('/', '\\', substr($file->uri, $strlen, -4));
}
}
// Check that each class has a getInfo() method and store the information
// in an array keyed with the group specified in the test information.
$groups = array();
foreach ($classes as $class) {
// Test classes need to implement getInfo() to be valid.
if (class_exists($class) && method_exists($class, 'getInfo')) {
$reflectionClass = new ReflectionClass($class);
// Skip abstract classes and interfaces.
if ($reflectionClass->isInstantiable()) {
$reflectionMethod = new ReflectionMethod($class, 'getInfo');
$declaringClass = $reflectionMethod->getDeclaringClass()->getName();
// Avoid testing intermediate classes which do not implement the
// method.
if ($class == $declaringClass) {
$info = call_user_func(array($class, 'getInfo'));
}
else {
continue;
}
}
else {
continue;
}
// If this test class requires a non-existing module, skip it.
if (!empty($info['dependencies'])) {
foreach ($info['dependencies'] as $dependency) {
if (!isset($dependency_data[$dependency])) {
continue 2;
}
}
}
$groups[$info['group']][$class] = $info;
}
}
// Sort the groups and tests within the groups by name.
uksort($groups, 'strnatcasecmp');
foreach ($groups as &$tests) {
uksort($tests, 'strnatcasecmp');
}
// Allow modules extending core tests to disable originals.
\Drupal::moduleHandler()->alter('simpletest', $groups);
\Drupal::cache()->set($cid, $groups);
}
}
return $all_groups[$cid];
}
/**
* Registers namespaces for disabled modules.
*/
function simpletest_classloader_register() {
// Use the same cache prefix as simpletest_test_get_all().
$cid = "simpletest::all";
$types = array(
'theme_engine',
'module',
'theme',
'profile',
);
if ($cache = \Drupal::cache()->get($cid)) {
$extensions = $cache->data;
}
else {
$listing = new ExtensionDiscovery();
// Ensure that tests in all profiles are discovered.
$listing->setProfileDirectories(array());
$extensions = array();
foreach ($types as $type) {
foreach ($listing->scan($type, TRUE) as $name => $file) {
$extensions[$type][$name] = $file->getPathname();
}
}
\Drupal::cache()->set($cid, $extensions);
}
$classloader = drupal_classloader();
foreach ($types as $type) {
foreach ($extensions[$type] as $name => $uri) {
drupal_classloader_register($name, dirname($uri));
$classloader->addPsr4('Drupal\\' . $name . '\\Tests\\', array(
DRUPAL_ROOT . '/' . dirname($uri) . '/tests/Drupal/' . $name . '/Tests',
DRUPAL_ROOT . '/' . dirname($uri) . '/tests/src',
));
// While being there, prime drupal_get_filename().
drupal_get_filename($type, $name, $uri);
}
}
// Register the core test directory so we can find \Drupal\UnitTestCase.
$classloader->add('Drupal\\Tests', DRUPAL_ROOT . '/core/tests');
}
/**
* Generates test file.
*
* @param string $filename
* The name of the file, including the path.
* @param int $width
* The number of characters on one line.
* @param int $lines
* The number of lines in the file.
* @param string $type
* (optional) The type, for example: "text", "binary", or "binary-text".
*
* @return string
* The name of the file, including the path.
*/
function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
$size = $width * $lines - $lines;
// Generate random text
$text = '';
for ($i = 0; $i < $size; $i++) {
switch ($type) {
case 'text':
$text .= chr(rand(32, 126));
break;
case 'binary':
$text .= chr(rand(0, 31));
break;
case 'binary-text':
default:
$text .= rand(0, 1);
break;
}
}
// Add \n for symmetrical file.
$text = wordwrap($text, $width - 1, "\n", TRUE) . "\n";
// Create filename.
file_put_contents('public://' . $filename . '.txt', $text);
return $filename;
}
/**
* Removes all temporary database tables and directories.
*/
function simpletest_clean_environment() {
simpletest_clean_database();
simpletest_clean_temporary_directories();
if (\Drupal::config('simpletest.settings')->get('clear_results')) {
$count = simpletest_clean_results_table();
drupal_set_message(format_plural($count, 'Removed 1 test result.', 'Removed @count test results.'));
}
else {
drupal_set_message(t('Clear results is disabled and the test results table will not be cleared.'), 'warning');
}
// Detect test classes that have been added, renamed or deleted.
\Drupal::cache()->delete('simpletest');
\Drupal::cache()->delete('simpletest_phpunit');
}
/**
* Removes prefixed tables from the database from crashed tests.
*/
function simpletest_clean_database() {
$tables = db_find_tables(Database::getConnection()->prefixTables('{simpletest}') . '%');
$schema = drupal_get_schema_unprocessed('simpletest');
$count = 0;
foreach (array_diff_key($tables, $schema) as $table) {
// Strip the prefix and skip tables without digits following "simpletest",
// e.g. {simpletest_test_id}.
if (preg_match('/simpletest\d+.*/', $table, $matches)) {
db_drop_table($matches[0]);
$count++;
}
}
if ($count > 0) {
drupal_set_message(format_plural($count, 'Removed 1 leftover table.', 'Removed @count leftover tables.'));
}
else {
drupal_set_message(t('No leftover tables to remove.'));
}
}
/**
* Finds all leftover temporary directories and removes them.
*/
function simpletest_clean_temporary_directories() {
$count = 0;
if (is_dir(DRUPAL_ROOT . '/sites/simpletest')) {
$files = scandir(DRUPAL_ROOT . '/sites/simpletest');
foreach ($files as $file) {
if ($file[0] != '.') {
$path = DRUPAL_ROOT . '/sites/simpletest/' . $file;
file_unmanaged_delete_recursive($path, array('Drupal\simpletest\TestBase', 'filePreDeleteCallback'));
$count++;
}
}
}
if ($count > 0) {
drupal_set_message(format_plural($count, 'Removed 1 temporary directory.', 'Removed @count temporary directories.'));
}
else {
drupal_set_message(t('No temporary directories to remove.'));
}
}
/**
* Clears the test result tables.
*
* @param $test_id
* Test ID to remove results for, or NULL to remove all results.
*
* @return int
* The number of results that were removed.
*/
function simpletest_clean_results_table($test_id = NULL) {
if (\Drupal::config('simpletest.settings')->get('clear_results')) {
$connection = TestBase::getDatabaseConnection();
if ($test_id) {
$count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField();
$connection->delete('simpletest')
->condition('test_id', $test_id)
->execute();
$connection->delete('simpletest_test_id')
->condition('test_id', $test_id)
->execute();
}
else {
$count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id}')->fetchField();
// Clear test results.
$connection->delete('simpletest')->execute();
$connection->delete('simpletest_test_id')->execute();
}
return $count;
}
return 0;
}
/**
* Implements hook_mail_alter().
*
* Aborts sending of messages with ID 'simpletest_cancel_test'.
*
* @see MailTestCase::testCancelMessage()
*/
function simpletest_mail_alter(&$message) {
if ($message['id'] == 'simpletest_cancel_test') {
$message['send'] = FALSE;
}
}
/**
* Gets PHPUnit classes.
*
* @param string $module
* Name of a module. If set then only tests belonging to this module is
* returned.
*
* @return array
* Returns an array of test classes.
*
* @throws \RuntimeException
* This is thrown when anything is wrong with a test.
*/
function simpletest_phpunit_get_available_tests($module = NULL) {
// Try to load the class names array from cache.
$cid = 'simpletest_phpunit:' . $module;
if ($cache = \Drupal::cache()->get($cid)) {
$test_classes = $cache->data;
}
else {
if ($module) {
$prefix = 'Drupal\\' . $module . '\\';
$n = strlen($prefix);
}
// If there was no cached data available we have to find the tests.
// Load the PHPUnit configuration file, which tells us where to find the
// tests.
$phpunit_config = simpletest_phpunit_configuration_filepath();
$configuration = PHPUnit_Util_Configuration::getInstance($phpunit_config);
// Find all the tests and get a list of unique class names.
$test_suite = $configuration->getTestSuiteConfiguration(NULL);
$test_classes = array();
foreach ($test_suite as $test) {
// PHPUnit returns a warning message if something is wrong with a test,
// throw an exception to avoid an error when trying to call getInfo() on
// this.
if ($test instanceof PHPUnit_Framework_Warning) {
throw new RuntimeException($test->getMessage());
}
$name = get_class($test);
if (!array_key_exists($name, $test_classes) && (!$module || substr($name, 0, $n) == $prefix)) {
$test_classes[$name] = $test->getInfo();
}
}
// Since we have recalculated, we now need to store the new data into cache.
\Drupal::cache()->set($cid, $test_classes);
}
return $test_classes;
}
/**
* Converts PHPUnit's JUnit XML output to an array.
*
* @param $test_id
* The current test ID.
* @param $phpunit_xml_file
* Path to the PHPUnit XML file.
*
* @return array[]
* The results as array of rows in a format that can be inserted into
* {simpletest}.
*/
function simpletest_phpunit_xml_to_rows($test_id, $phpunit_xml_file) {
$contents = @file_get_contents($phpunit_xml_file);
if (!$contents) {
return;
}
$records = array();
$testcases = simpletest_phpunit_find_testcases(new SimpleXMLElement($contents));
foreach ($testcases as $testcase) {
$records[] = simpletest_phpunit_testcase_to_row($test_id, $testcase);
}
return $records;
}
/**
* Finds all test cases recursively from a test suite list.
*
* @param \SimpleXMLElement $element
* The PHPUnit xml to search for test cases.
* @param \SimpleXMLElement $suite
* (Optional) The parent of the current element. Defaults to NULL.
*
* @return array
* A list of all test cases.
*/
function simpletest_phpunit_find_testcases(\SimpleXMLElement $element, \SimpleXMLElement $parent = NULL) {
$testcases = array();
if (!isset($parent)) {
$parent = $element;
}
if ($element->getName() === 'testcase' && (int) $parent->attributes()->tests > 0) {
// Add the class attribute if the testcase does not have one. This is the
// case for tests using a data provider. The name of the parent testsuite
// will be in the format class::method.
if (!$element->attributes()->class) {
$name = explode('::', $parent->attributes()->name, 2);
$element->addAttribute('class', $name[0]);
}
$testcases[] = $element;
}
else {
foreach ($element as $child) {
$file = (string) $parent->attributes()->file;
if ($file && !$child->attributes()->file) {
$child->addAttribute('file', $file);
}
$testcases = array_merge($testcases, simpletest_phpunit_find_testcases($child, $element));
}
}
return $testcases;
}
/**
* Converts a PHPUnit test case result to a {simpletest} result row.
*
* @param int $test_id
* The current test ID.
* @param \SimpleXMLElement $testcase
* The PHPUnit test case represented as XML element.
*
* @return array
* An array containing the {simpletest} result row.
*/
function simpletest_phpunit_testcase_to_row($test_id, \SimpleXMLElement $testcase) {
$message = '';
$pass = TRUE;
if ($testcase->failure) {
$lines = explode("\n", $testcase->failure);
$message = $lines[2];
$pass = FALSE;
}
if ($testcase->error) {
$message = $testcase->error;
$pass = FALSE;
}
$attributes = $testcase->attributes();
$record = array(
'test_id' => $test_id,
'test_class' => (string) $attributes->class,
'status' => $pass ? 'pass' : 'fail',
'message' => $message,
// @todo: Check on the proper values for this.
'message_group' => 'Other',
'function' => $attributes->class . '->' . $attributes->name . '()',
'line' => $attributes->line ?: 0,
'file' => $attributes->file,
);
return $record;
}