Provides testing functionality.
<?php
use Drupal\Core\Database\Database;
use Drupal\simpletest\TestBase;
/**
* @file
* Provides testing functionality.
*/
/**
* Implements hook_help().
*/
function simpletest_help($path, $arg) {
switch ($path) {
case 'admin/help#simpletest':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Testing module provides a framework for running automated unit 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 online handbook entry for <a href="@simpletest">Testing module</a>.', array(
'@simpletest' => 'http://drupal.org/documentation/modules/simpletest',
'@blocks' => url('admin/structure/block'),
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Running tests') . '</dt>';
$output .= '<dd>' . 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. For more information on creating and modifying your own tests, see the <a href="@simpletest-api">Testing API Documentation</a> in the Drupal handbook.', array(
'@simpletest-api' => 'http://drupal.org/simpletest',
'@admin-simpletest' => url('admin/config/development/testing'),
)) . '</dd>';
$output .= '<dd>' . 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.') . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_menu().
*/
function simpletest_menu() {
$items['admin/config/development/testing'] = array(
'title' => 'Testing',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'simpletest_test_form',
),
'description' => 'Run tests against Drupal core and your modules. These tests help assure that your site code is working as designed.',
'access arguments' => array(
'administer unit tests',
),
'file' => 'simpletest.pages.inc',
'weight' => -5,
);
$items['admin/config/development/testing/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/development/testing/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'simpletest_settings_form',
),
'access arguments' => array(
'administer unit tests',
),
'type' => MENU_LOCAL_TASK,
'file' => 'simpletest.pages.inc',
);
$items['admin/config/development/testing/results/%'] = array(
'title' => 'Test result',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'simpletest_result_form',
5,
),
'description' => 'View result of tests.',
'access arguments' => array(
'administer unit tests',
),
'file' => 'simpletest.pages.inc',
);
return $items;
}
/**
* 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_test_table' => array(
'render element' => 'table',
'file' => 'simpletest.pages.inc',
),
'simpletest_result_summary' => array(
'render element' => 'form',
'file' => 'simpletest.pages.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) {
$args = array(
'@pass' => format_plural(isset($summary['#pass']) ? $summary['#pass'] : 0, '1 pass', '@count passes'),
'@fail' => format_plural(isset($summary['#fail']) ? $summary['#fail'] : 0, '1 fail', '@count fails'),
'@exception' => format_plural(isset($summary['#exception']) ? $summary['#exception'] : 0, '1 exception', '@count exceptions'),
);
if (!$summary['#debug']) {
return t('@pass, @fail, and @exception', $args);
}
$args['@debug'] = format_plural(isset($summary['#debug']) ? $summary['#debug'] : 0, '1 debug message', '@count debug messages');
return t('@pass, @fail, @exception, and @debug', $args);
}
/**
* Actually 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.
*/
function simpletest_run_tests($test_list, $reporter = 'drupal') {
$test_id = db_insert('simpletest_test_id')
->useDefaults(array(
'test_id',
))
->execute();
// 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') . '/simpletest.css',
),
'init_message' => t('Processing test @num of @max - %test.', array(
'%test' => $info['name'],
'@num' => '1',
'@max' => count($test_list),
)),
);
batch_set($batch);
module_invoke_all('test_group_started');
return $test_id;
}
/**
* 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();
module_invoke_all('test_finished', $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>';
$context['message'] .= theme('item_list', array(
'items' => $items,
));
// 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');
}
module_invoke_all('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 containing the last database prefix used and the last test class
* that ran.
*/
function simpletest_last_test_get($test_id) {
$last_prefix = db_query_range('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, array(
':test_id' => $test_id,
))
->fetchField();
$last_test_class = db_query_range('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,
);
}
/**
* Read the error log and report 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 $prefix
* The database prefix to which the log relates.
* @param $test_class
* The test class to which the log relates.
* @param $during_test
* Indicates that the current file directory path is a temporary file
* file directory used during testing.
* @return
* Found any entries in log.
*/
function simpletest_log_read($test_id, $prefix, $test_class, $during_test = FALSE) {
$log = 'public://' . ($during_test ? '' : '/simpletest/' . substr($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;
}
/**
* Get 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.
*
* @return
* An array of tests keyed with the groups specified in each of the tests
* getInfo() method and then keyed by the test class. An example of the array
* structure is provided below.
*
* @code
* $groups['Block'] => array(
* 'BlockTestCase' => array(
* 'name' => 'Block functionality',
* 'description' => 'Add, edit and delete custom block...',
* 'group' => 'Block',
* ),
* );
* @endcode
*/
function simpletest_test_get_all() {
$groups =& drupal_static(__FUNCTION__);
if (!$groups) {
// Make sure that namespaces for disabled modules are registered so that the
// checks below will find them.
simpletest_classloader_register();
// Load test information from cache if available, otherwise retrieve the
// information from each tests getInfo() method.
if ($cache = cache()
->get('simpletest')) {
$groups = $cache->data;
}
else {
// Select all PSR-0 classes in the Tests namespace of all modules.
$classes = array();
$module_data = system_rebuild_module_data();
$all_data = $module_data + system_rebuild_theme_data();
$all_data += drupal_system_listing('/\\.profile$/', 'profiles', 'name');
foreach ($all_data as $name => $data) {
// Build directory in which the test files would reside.
$tests_dir = DRUPAL_ROOT . '/' . dirname($data->uri) . '/lib/Drupal/' . $name . '/Tests';
// Scan it for test files if it exists.
if (is_dir($tests_dir)) {
$files = file_scan_directory($tests_dir, '/.*\\.php/');
if (!empty($files)) {
$basedir = DRUPAL_ROOT . '/' . dirname($data->uri) . '/lib/';
foreach ($files as $file) {
// Convert the file name into the namespaced class name.
$replacements = array(
'/' => '\\',
$basedir => '',
'.php' => '',
);
$classes[] = strtr($file->uri, $replacements);
}
}
}
}
// 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')) {
$info = call_user_func(array(
$class,
'getInfo',
));
// If this test class requires a non-existing module, skip it.
if (!empty($info['dependencies'])) {
foreach ($info['dependencies'] as $module) {
if (!isset($module_data[$module])) {
continue 2;
}
}
}
$groups[$info['group']][$class] = $info;
}
}
// Sort the groups and tests within the groups by name.
uksort($groups, 'strnatcasecmp');
foreach ($groups as $group => &$tests) {
uksort($tests, 'strnatcasecmp');
}
// Allow modules extending core tests to disable originals.
drupal_alter('simpletest', $groups);
cache()
->set('simpletest', $groups);
}
}
return $groups;
}
/**
* Registers namespaces for disabled modules.
*/
function simpletest_classloader_register() {
// @see drupal_get_filename()
$types = array(
'theme_engine' => array(
'dir' => 'themes/engines',
'extension' => 'engine',
),
'module' => array(
'dir' => 'modules',
'extension' => 'module',
),
'theme' => array(
'dir' => 'themes',
'extension' => 'info',
),
'profile' => array(
'dir' => 'profiles',
'extension' => 'profile',
),
);
foreach ($types as $type => $info) {
$matches = drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\\.' . $info['extension'] . '$/', $info['dir']);
foreach ($matches as $name => $file) {
drupal_classloader_register($name, dirname($file->uri));
// While being there, prime drupal_get_filename().
drupal_get_filename($type, $name, $file->uri);
}
}
}
/**
* Generate test file.
*/
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;
}
}
$text = wordwrap($text, $width - 1, "\n", TRUE) . "\n";
// Add \n for symmetrical file.
// Create filename.
file_put_contents('public://' . $filename . '.txt', $text);
return $filename;
}
/**
* Remove all temporary database tables and directories.
*/
function simpletest_clean_environment() {
simpletest_clean_database();
simpletest_clean_temporary_directories();
if (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.
cache()
->delete('simpletest');
}
/**
* Removed prefixed tables from the database that are left over 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.'));
}
}
/**
* Find all leftover temporary directories and remove them.
*/
function simpletest_clean_temporary_directories() {
$count = 0;
if (is_dir('public://simpletest')) {
$files = scandir('public://simpletest');
foreach ($files as $file) {
$path = 'public://simpletest/' . $file;
if (is_dir($path) && (is_numeric($file) || strpos($file, 'config_simpletest') !== FALSE)) {
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.'));
}
}
/**
* Clear the test result tables.
*
* @param $test_id
* Test ID to remove results for, or NULL to remove all results.
* @return
* The number of results removed.
*/
function simpletest_clean_results_table($test_id = NULL) {
if (config('simpletest.settings')
->get('clear_results')) {
if ($test_id) {
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(
':test_id' => $test_id,
))
->fetchField();
db_delete('simpletest')
->condition('test_id', $test_id)
->execute();
db_delete('simpletest_test_id')
->condition('test_id', $test_id)
->execute();
}
else {
$count = db_query('SELECT COUNT(test_id) FROM {simpletest_test_id}')
->fetchField();
// Clear test results.
db_delete('simpletest')
->execute();
db_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;
}
}
/**
* Implements hook_library_info().
*/
function simpletest_library_info() {
$libraries['drupal.simpletest'] = array(
'title' => 'Simpletest',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'simpletest') . '/simpletest.js' => array(),
),
'css' => array(
drupal_get_path('module', 'simpletest') . '/simpletest.css' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
array(
'system',
'drupalSettings',
),
array(
'system',
'jquery.once',
),
array(
'system',
'drupal.tableselect',
),
),
);
return $libraries;
}
Name | Description |
---|---|
simpletest_classloader_register | Registers namespaces for disabled modules. |
simpletest_clean_database | Removed prefixed tables from the database that are left over from crashed tests. |
simpletest_clean_environment | Remove all temporary database tables and directories. |
simpletest_clean_results_table | Clear the test result tables. |
simpletest_clean_temporary_directories | Find all leftover temporary directories and remove them. |
simpletest_generate_file | Generate test file. |
simpletest_help | Implements hook_help(). |
simpletest_js_alter | Implements hook_js_alter(). |
simpletest_last_test_get | Get information about the last test that ran given a test ID. |
simpletest_library_info | Implements hook_library_info(). |
simpletest_log_read | Read the error log and report any errors as assertion failures. |
simpletest_mail_alter | Implements hook_mail_alter(). |
simpletest_menu | Implements hook_menu(). |
simpletest_permission | Implements hook_permission(). |
simpletest_run_tests | Actually runs tests. |
simpletest_test_get_all | Get a list of all of the tests provided by the system. |
simpletest_theme | Implements hook_theme(). |
_simpletest_batch_finished | |
_simpletest_batch_operation | Batch operation callback. |
_simpletest_format_summary_line |