123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- <?php
- 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;
- }
- }
- 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 active 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;
- }
- function simpletest_permission() {
- return array(
- 'administer unit tests' => array(
- 'title' => t('Administer tests'),
- 'restrict access' => TRUE,
- ),
- );
- }
- 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',
- ),
- );
- }
- function simpletest_js_alter(&$javascript) {
-
-
- $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 _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);
- }
- function simpletest_run_tests($test_list, $reporter = 'drupal') {
- $test_id = db_insert('simpletest_test_id')
- ->useDefaults(array('test_id'))
- ->execute();
-
- file_unmanaged_delete_recursive('public://simpletest/verbose');
-
- $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;
- }
- function _simpletest_batch_operation($test_list_init, $test_id, &$context) {
- simpletest_classloader_register();
-
- if (!isset($context['sandbox']['max'])) {
-
- $test_list = $test_list_init;
- $context['sandbox']['max'] = count($test_list);
- $test_results = array('#pass' => 0, '#fail' => 0, '#exception' => 0, '#debug' => 0);
- }
- else {
-
- $test_list = $context['sandbox']['tests'];
- $test_results = $context['sandbox']['test_results'];
- }
- $max = $context['sandbox']['max'];
-
- $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);
-
- $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));
-
- $context['sandbox']['tests'] = $test_list;
- $context['sandbox']['test_results'] = $test_results;
-
- $context['results']['test_id'] = $test_id;
-
- $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 {
-
- $test_id = $operations[0][1][1];
-
-
-
- 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');
- }
- 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);
- }
- 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)) {
-
-
- $caller = array(
- 'line' => $match[4],
- 'file' => $match[3],
- );
- DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $match[2], $match[1], $caller);
- }
- else {
-
- DrupalTestCase::insertAssert($test_id, $test_class, FALSE, $line, 'Fatal error');
- }
- $found = TRUE;
- }
- }
- return $found;
- }
- function simpletest_test_get_all() {
- $groups = &drupal_static(__FUNCTION__);
- if (!$groups) {
-
- simpletest_classloader_register();
-
-
- if ($cache = cache_get('simpletest', 'cache')) {
- $groups = $cache->data;
- }
- else {
-
- $classes = db_query("SELECT name FROM {registry} WHERE type = :type AND filename LIKE :name", array(':type' => 'class', ':name' => '%.test'))->fetchCol();
-
- if (version_compare(PHP_VERSION, '5.3') > 0) {
-
-
- $system_list = db_query("SELECT name, filename FROM {system}")->fetchAllKeyed();
- foreach ($system_list as $name => $filename) {
- $module_dir = DRUPAL_ROOT . '/' . dirname($filename);
-
-
- foreach(array('lib/Drupal/' . $name, 'src') as $subdir) {
-
- $tests_dir = $module_dir . '/' . $subdir . '/Tests';
-
- if (is_dir($tests_dir)) {
- $files = file_scan_directory($tests_dir, '/.*\.php/');
- if (!empty($files)) {
- foreach ($files as $file) {
-
- $replacements = array(
- '/' => '\\',
- $module_dir . '/' => '',
- 'lib/' => '',
- 'src/' => 'Drupal\\' . $name . '\\',
- '.php' => '',
- );
- $classes[] = strtr($file->uri, $replacements);
- }
- }
- }
- }
- }
- }
-
-
- $groups = array();
- foreach ($classes as $class) {
-
- if (class_exists($class) && method_exists($class, 'getInfo')) {
- $info = call_user_func(array($class, 'getInfo'));
-
- if (!empty($info['dependencies'])) {
- foreach ($info['dependencies'] as $module) {
- if (!drupal_get_filename('module', $module)) {
- continue 2;
- }
- }
- }
- $groups[$info['group']][$class] = $info;
- }
- }
-
- uksort($groups, 'strnatcasecmp');
- foreach ($groups as $group => &$tests) {
- uksort($tests, 'strnatcasecmp');
- }
-
- drupal_alter('simpletest', $groups);
- cache_set('simpletest', $groups);
- }
- }
- return $groups;
- }
- function simpletest_classloader_register() {
-
- static $first_run = TRUE;
- if (!$first_run) {
- return;
- }
- $first_run = FALSE;
-
- if (version_compare(PHP_VERSION, '5.3') > 0) {
- spl_autoload_register('_simpletest_autoload_psr4_psr0');
- }
- }
- function _simpletest_autoload_psr4_psr0($class) {
-
-
- static $extensions;
-
- if (substr($class, 0, 7) === 'Drupal\\') {
-
- $pos = strpos($class, '\\', 7);
-
- if (substr($class, $pos, 7) === '\\Tests\\') {
-
-
- $extension = substr($class, 7, $pos - 7);
-
- if (!isset($extensions)) {
- $extensions = db_query("SELECT name, filename FROM {system}")->fetchAllKeyed();
- }
-
- if (isset($extensions[$extension])) {
-
- $nspos = strrpos($class, '\\');
- $namespace = substr($class, 0, $nspos);
- $classname = substr($class, $nspos + 1);
-
-
- $psr4_path = dirname($extensions[$extension]) . '/src/' .
- str_replace('\\', '/', substr($namespace, strlen('Drupal\\' . $extension . '\\'))) . '/' .
- str_replace('_', '/', $classname) . '.php';
-
- if (file_exists($psr4_path)) {
- include $psr4_path;
- }
- else {
-
- $psr0_path = dirname($extensions[$extension]) . '/lib/' .
- str_replace('\\', '/', $namespace) . '/' .
- str_replace('_', '/', $classname) . '.php';
-
- if (file_exists($psr0_path)) {
- include $psr0_path;
- }
- }
- }
- }
- }
- }
- function simpletest_registry_files_alter(&$files, $modules) {
- foreach ($modules as $module) {
-
-
- if (!$module->status) {
- $dir = $module->dir;
- if (!empty($module->info['files'])) {
- foreach ($module->info['files'] as $file) {
- if (substr($file, -5) == '.test') {
- $files["$dir/$file"] = array('module' => $module->name, 'weight' => $module->weight);
- }
- }
- }
- }
- }
- }
- function simpletest_generate_file($filename, $width, $lines, $type = 'binary-text') {
- $size = $width * $lines - $lines;
-
- $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";
-
- file_put_contents('public://' . $filename . '.txt', $text);
- return $filename;
- }
- function simpletest_clean_environment() {
- simpletest_clean_database();
- simpletest_clean_temporary_directories();
- if (variable_get('simpletest_clear_results', TRUE)) {
- $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');
- }
-
- registry_rebuild();
- cache_clear_all('simpletest', 'cache');
- }
- 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) {
-
-
- 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.'));
- }
- }
- 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)) {
- file_unmanaged_delete_recursive($path);
- $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.'));
- }
- }
- function simpletest_clean_results_table($test_id = NULL) {
- if (variable_get('simpletest_clear_results', TRUE)) {
- 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();
-
- db_delete('simpletest')->execute();
- db_delete('simpletest_test_id')->execute();
- }
- return $count;
- }
- return 0;
- }
- function simpletest_mail_alter(&$message) {
- if ($message['id'] == 'simpletest_cancel_test') {
- $message['send'] = FALSE;
- }
- }
|