' . t('The SimpleTest module is a framework for running automated unit tests in Drupal. 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.') .'
';
$output .= '
' . t('Visit Administer >> Site building >> SimpleTest to display a list of available tests. For comprehensive testing, select all tests, or individually select tests for more targeted testing. Note that it might take several minutes for all tests to complete.)', array('@admin-simpletest' => url('admin/build/testing'))) .'
';
$output .= '
' . t('After the tests have 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 a 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 fails or exceptions, the results are expanded, and the tests that had issues will be indicated in red or pink rows. Use these results to refine your code and tests until all tests return a pass.') .'
';
$output .= '
' . t('For more information on creating and modifying your own tests, see the SimpleTest API Documentation in the Drupal handbook.', array('@simpletest-api' => 'http://drupal.org/simpletest')) .'
';
$output .= '
' . t('For more information, see the online handbook entry for SimpleTest module.', array('@simpletest' => 'http://drupal.org/handbook/modules/simpletest')) .'
';
return $output;
}
}
/**
* Implementation of hook_menu().
*/
function simpletest_menu() {
$items['admin/build/testing'] = array(
'title' => 'Testing',
'page callback' => 'drupal_get_form',
'page arguments' => array('simpletest_test_form'),
'description' => 'Run tests against Drupal core and your active modules. These tests help assure that your site code is working as designed.',
'access arguments' => array('administer unit tests'),
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function simpletest_perm() {
return array('administer unit tests');
}
/**
* Implementation of hook_theme().
*/
function simpletest_theme() {
return array(
'simpletest_test_table' => array(
'arguments' => array('table' => NULL)
),
'simpletest_result_summary' => array(
'arguments' => array('form' => NULL)
),
);
}
/**
* Menu callback for both running tests and listing possible tests
*/
function simpletest_test_form() {
$form = array();
// List out all tests in groups for selection.
$uncategorized_tests = simpletest_get_all_tests();
$tests = simpletest_categorize_tests($uncategorized_tests);
$selected_tests = array();
if (isset($_SESSION['test_id'])) {
// Select all results using the active test ID used to group them.
$results = db_query("SELECT * FROM {simpletest} WHERE test_id = %d ORDER BY test_class, message_id", $_SESSION['test_id']);
$summary = array(
'#theme' => 'simpletest_result_summary',
'#pass' => 0,
'#fail' => 0,
'#exception' => 0,
'#weight' => -10,
);
$form['summary'] = $summary;
$form['results'] = array();
$group_summary = array();
$map = array(
'pass' => theme('image', 'misc/watchdog-ok.png'),
'fail' => theme('image', 'misc/watchdog-error.png'),
'exception' => theme('image', 'misc/watchdog-warning.png'),
);
$header = array(t('Message'), t('Group'), t('Filename'), t('Line'), t('Function'), array('colspan' => 2, 'data' => t('Status')));
while ($result = db_fetch_object($results)) {
$class = $result->test_class;
$info = $uncategorized_tests[$class]->getInfo();
$group = $info['group'];
$selected_tests[$group][$class] = TRUE;
if (!isset($group_summary[$group])) {
$group_summary[$group] = $summary;
}
$element = &$form['results'][$group][$class];
if (!isset($element)) {
$element['summary'] = $summary;
}
$status = $result->status;
// This reporter can only handle pass, fail and exception.
if (isset($map[$status])) {
$element['#title'] = $info['name'];
$status_index = '#'. $status;
$form['summary'][$status_index]++;
$group_summary[$group][$status_index]++;
$element['summary'][$status_index]++;
$element['result_table']['#rows'][] = array(
'data' => array(
$result->message,
$result->message_group,
basename($result->file),
$result->line,
$result->function,
$map[$status],
),
'class' => "simpletest-$status",
);
}
unset($element);
}
// Clear test results.
if (variable_get('simpletest_clear_results', TRUE)) {
db_query('DELETE FROM {simpletest} WHERE test_id = %d', $_SESSION['test_id']);
db_query('DELETE FROM {simpletest_test_id} WHERE test_id = %d', $_SESSION['test_id']);
}
unset($_SESSION['test_id']);
$all_ok = TRUE;
foreach ($form['results'] as $group => &$elements) {
$group_ok = TRUE;
foreach ($elements as $class => &$element) {
$info = $uncategorized_tests[$class]->getInfo();
$ok = $element['summary']['#fail'] + $element['summary']['#exception'] == 0;
$element += array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => $ok,
'#description' => $info['description'],
);
// $element['result_table']['#markup'] = theme('table', $header, $element['result_table']['#rows']);
$element['result_table']['#value'] = theme('table', $header, $element['result_table']['#rows']);
$element['summary']['#ok'] = $ok;
$group_ok = $group_ok && $ok;
}
$elements += array(
'#type' => 'fieldset',
'#title' => $group,
'#collapsible' => TRUE,
'#collapsed' => $group_ok,
'summary' => $group_summary[$group],
);
$elements['summary']['#ok'] = $group_ok;
$all_ok = $group_ok && $all_ok;
}
$form['summary']['#ok'] = $all_ok;
}
$form['tests'] = array(
'#type' => 'fieldset',
'#title' => t('Tests'),
'#description' => t('Select the tests you would like to run, and click Run tests.'),
);
$form['tests']['table'] = array(
'#theme' => 'simpletest_test_table'
);
foreach ($tests as $group_name => $test_group) {
$form['tests']['table'][$group_name] = array(
'#collapsed' => TRUE,
);
foreach ($test_group as $test) {
$test_info = $test->getInfo();
$test_class = get_class($test);
$is_selected = isset($selected_tests[$group_name][$test_class]);
$form['tests']['table'][$group_name][$test_class] = array(
'#type' => 'checkbox',
'#title' => $test_info['name'],
'#default_value' => $is_selected,
'#description' => $test_info['description'],
);
if ($is_selected) {
$form['tests']['table'][$group_name]['#collapsed'] = FALSE;
}
}
}
// Action buttons.
$form['tests']['op'] = array(
'#type' => 'submit',
'#value' => t('Run tests'),
);
$form['reset'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#title' => t('Clean test environment'),
'#description' => t('Remove tables with the prefix "simpletest" and temporary directories that are left over from tests that crashed. This is intended for developers when creating tests.'),
);
$form['reset']['op'] = array(
'#type' => 'submit',
'#value' => t('Clean environment'),
'#submit' => array('simpletest_clean_environment'),
);
return $form;
}
function theme_simpletest_test_table($table) {
drupal_add_css(drupal_get_path('module', 'simpletest') . '/simpletest.css');
drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js');
// Create header for test selection table.
$header = array(
theme('table_select_header_cell'),
array('data' => t('Test'), 'class' => 'simpletest_test'),
array('data' => t('Description'), 'class' => 'simpletest_description'),
);
// Define the images used to expand/collapse the test groups.
$js = array(
'images' => array(
theme('image', 'misc/menu-collapsed.png', 'Expand', 'Expand'),
theme('image', 'misc/menu-expanded.png', 'Collapsed', 'Collapsed'),
),
);
// Go through each test group and create a row.
$rows = array();
foreach (element_children($table) as $key) {
$element = &$table[$key];
$row = array();
// Make the class name safe for output on the page by replacing all
// non-word/decimal characters with a dash (-).
$test_class = strtolower(trim(preg_replace("/[^\w\d]/", "-", $key)));
// Select the right "expand"/"collapse" image, depending on whether the
// category is expanded (at least one test selected) or not.
$collapsed = !empty($element['#collapsed']);
$image_index = $collapsed ? 0 : 1;
// Place-holder for checkboxes to select group of tests.
$row[] = array('id' => $test_class, 'class' => 'simpletest-select-all');
// Expand/collapse image and group title.
$row[] = array(
'data' => ' ' .
'',
'style' => 'font-weight: bold;'
);
$row[] = isset($element['#description']) ? $element['#description'] : ' ';
$rows[] = array('data' => $row, 'class' => 'simpletest-group');
// Add individual tests to group.
$current_js = array(
'testClass' => $test_class . '-test',
'testNames' => array(),
'imageDirection' => $image_index,
'clickActive' => FALSE,
);
foreach (element_children($element) as $test_name) {
$test = $element[$test_name];
$row = array();
$current_js['testNames'][] = 'edit-' . $test_name;
// Store test title and description so that checkbox won't render them.
$title = $test['#title'];
$description = $test['#description'];
unset($test['#title']);
unset($test['#description']);
// Test name is used to determine what tests to run.
$test['#name'] = $test_name;
$row[] = drupal_render($test);
$row[] = theme('indentation', 1) . '';
$row[] = '
' . $description . '
';
$rows[] = array('data' => $row, 'class' => $test_class . '-test' . ($collapsed ? ' js-hide' : ''));
}
$js['simpletest-test-group-'. $test_class] = $current_js;
unset($table[$key]);
}
// Add js array of settings.
drupal_add_js(array('simpleTest' => $js), 'setting');
if (empty($rows)) {
return '' . t('No tests to display.') . '';
}
else {
return theme('table', $header, $rows, array('id' => 'simpletest-form-table'));
}
}
/**
* Implementation of 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('misc/tableselect.js', $javascript)) {
$javascript[$simpletest]['weight'] = $javascript['misc/tableselect.js']['weight'] - 1;
}
}
function theme_simpletest_result_summary($form, $text = NULL) {
return '
' . _simpletest_format_summary_line($form) . '
';
}
function _simpletest_format_summary_line($summary) {
return t('@pass, @fail, and @exception', 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'),
));
}
/**
* Run selected tests.
*/
function simpletest_test_form_submit($form, &$form_state) {
// Ensure that all classes are loaded before we create instances to get test information and run.
simpletest_get_all_tests();
// Get list of tests.
$tests_list = array();
foreach ($form_state['values'] as $class_name => $value) {
if (class_exists($class_name) && $value === 1) {
$tests_list[] = $class_name;
}
}
if (count($tests_list) > 0 ) {
simpletest_run_tests($tests_list, 'drupal');
}
else {
drupal_set_message(t('No test(s) selected.'), 'error');
}
}
/**
* 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') {
cache_clear_all();
// $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute();
db_query('INSERT INTO {simpletest_test_id} VALUES (default)');
$test_id = db_last_insert_id('simpletest_test_id', 'test_id');
// 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 SimpleTests'),
'operations' => array(
array('_simpletest_batch_operation', array($test_list, $test_id)),
),
'finished' => '_simpletest_batch_finished',
'redirect' => 'admin/build/testing',
'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);
// Normally, the forms portion of the batch API takes care of calling
// batch_process(), but in the process it saves the whole $form into the
// database (which is huge for the test selection form).
// By calling batch_process() directly, we skip that behavior and ensure
// that we don't exceed the size of data that can be sent to the database
// (max_allowed_packet on MySQL).
batch_process();
}
/**
* Batch operation callback.
*/
function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
// Ensure that all classes are loaded before we unserialize some instances.
simpletest_get_all_tests();
// 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);
}
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();
// 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, '