2006-06-30 14:59:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Run the unit tests.
|
|
|
|
*
|
|
|
|
* @copyright © 2006 The Open University
|
|
|
|
* @author N.D.Freear@open.ac.uk, T.J.Hunt@open.ac.uk
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @version $Id$
|
|
|
|
* @package SimpleTestEx
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** */
|
2007-06-09 16:17:33 +00:00
|
|
|
require_once(dirname(__FILE__).'/../../../config.php');
|
2006-09-03 14:45:57 +00:00
|
|
|
require_once($CFG->libdir.'/adminlib.php');
|
2007-06-09 16:17:33 +00:00
|
|
|
require_once($CFG->libdir.'/simpletestlib.php');
|
2006-06-30 14:59:05 +00:00
|
|
|
require_once('ex_simple_test.php');
|
|
|
|
require_once('ex_reporter.php');
|
|
|
|
|
2006-09-03 14:45:57 +00:00
|
|
|
require_login();
|
2008-05-02 04:37:02 +00:00
|
|
|
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
|
2006-09-03 14:45:57 +00:00
|
|
|
|
2006-07-03 10:04:04 +00:00
|
|
|
$langfile = 'simpletest';
|
2008-09-17 14:31:30 +00:00
|
|
|
$unittest = true;
|
2006-06-30 14:59:05 +00:00
|
|
|
|
|
|
|
// CGI arguments
|
2006-09-13 16:19:56 +00:00
|
|
|
$path = optional_param('path', null, PARAM_PATH);
|
2006-06-30 14:59:05 +00:00
|
|
|
$showpasses = optional_param('showpasses', false, PARAM_BOOL);
|
|
|
|
$showsearch = optional_param('showsearch', false, PARAM_BOOL);
|
2008-06-10 19:54:27 +00:00
|
|
|
$rundbtests = optional_param('rundbtests', false, PARAM_BOOL);
|
2006-06-30 14:59:05 +00:00
|
|
|
$thorough = optional_param('thorough', false, PARAM_BOOL);
|
2008-09-16 12:19:43 +00:00
|
|
|
$addconfigprefix = optional_param('addconfigprefix', false, PARAM_RAW);
|
|
|
|
$setuptesttables = optional_param('setuptesttables', false, PARAM_BOOL);
|
2008-09-17 14:31:30 +00:00
|
|
|
$continuesetuptesttables = optional_param('continuesetuptesttables', false, PARAM_BOOL);
|
2008-09-16 16:40:15 +00:00
|
|
|
$droptesttables = optional_param('droptesttables', false, PARAM_BOOL);
|
2008-09-18 13:43:27 +00:00
|
|
|
$testtablesok = optional_param('testtablesok', false, PARAM_BOOL);
|
2006-06-30 14:59:05 +00:00
|
|
|
|
2008-06-13 08:35:29 +00:00
|
|
|
global $UNITTEST;
|
|
|
|
$UNITTEST = new object();
|
|
|
|
|
2006-06-30 14:59:05 +00:00
|
|
|
// Print the header.
|
2007-04-30 17:08:34 +00:00
|
|
|
admin_externalpage_setup('reportsimpletest');
|
2006-07-03 10:04:04 +00:00
|
|
|
$strtitle = get_string('unittests', $langfile);
|
2007-04-30 17:08:34 +00:00
|
|
|
admin_externalpage_print_header();
|
2006-09-03 14:45:57 +00:00
|
|
|
|
2008-09-18 13:43:27 +00:00
|
|
|
if ($testtablesok) {
|
|
|
|
print_heading(get_string('testtablesok', 'simpletest'));
|
|
|
|
}
|
|
|
|
|
2008-09-16 12:19:43 +00:00
|
|
|
$baseurl = $CFG->wwwroot . '/admin/report/simpletest/index.php';
|
|
|
|
|
|
|
|
// Add unittest prefix to config.php if needed
|
2008-09-18 10:05:20 +00:00
|
|
|
if ($addconfigprefix && !isset($CFG->unittestprefix)) {
|
2008-09-16 12:19:43 +00:00
|
|
|
// Open config file, search for $CFG->prefix and append a new line under it
|
|
|
|
$handle = fopen($CFG->dirroot.'/config.php', 'r+');
|
|
|
|
|
|
|
|
$new_file = '';
|
|
|
|
|
|
|
|
while (!feof($handle)) {
|
|
|
|
$line = fgets($handle, 4096);
|
|
|
|
$prefix_line = null;
|
|
|
|
|
|
|
|
if (preg_match('/CFG\-\>prefix/', $line, $matches)) {
|
2008-09-18 10:05:20 +00:00
|
|
|
$prefix_line = "\$CFG->unittestprefix = '$addconfigprefix';\n";
|
2008-09-16 12:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$new_file .= $line;
|
|
|
|
$new_file .= $prefix_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
$handle = fopen($CFG->dirroot.'/config.php', 'w');
|
|
|
|
fwrite($handle, $new_file);
|
|
|
|
fclose($handle);
|
2008-09-18 10:05:20 +00:00
|
|
|
$CFG->unittestprefix = $addconfigprefix;
|
2008-09-16 12:19:43 +00:00
|
|
|
}
|
|
|
|
|
2008-09-18 10:05:20 +00:00
|
|
|
if (empty($CFG->unittestprefix)) {
|
2008-09-16 12:19:43 +00:00
|
|
|
// TODO replace error with proper admin dialog
|
|
|
|
print_box_start('generalbox', 'notice');
|
|
|
|
echo '<p>'.get_string("prefixnotset", 'simpletest').'</p>';
|
|
|
|
echo '<form method="post" action="'.$baseurl.'">
|
|
|
|
<table class="generaltable">
|
|
|
|
<tr>
|
|
|
|
<th class="header"><label for="prefix">'.get_string('prefix', 'simpletest').'</label></th>
|
|
|
|
<td class="cell"><input type="text" size="5" name="addconfigprefix" id="prefix" value="tst_" /></td>
|
|
|
|
<td class="cell"><input type="submit" value="'.get_string('addconfigprefix', 'simpletest').'" /></td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
</form>';
|
|
|
|
print_box_end();
|
|
|
|
admin_externalpage_print_footer();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2008-09-17 14:31:30 +00:00
|
|
|
// Temporarily override $DB and $CFG for a fresh install on the unit test prefix
|
2008-09-16 16:40:15 +00:00
|
|
|
$real_db = clone($DB);
|
2008-09-17 14:31:30 +00:00
|
|
|
$real_cfg = clone($CFG);
|
|
|
|
$CFG = new stdClass();
|
2008-09-18 09:42:53 +00:00
|
|
|
$CFG->dbhost = $real_cfg->dbhost;
|
|
|
|
$CFG->dbtype = $real_cfg->dbtype;
|
|
|
|
$CFG->dblibrary = $real_cfg->dblibrary;
|
|
|
|
$CFG->dbuser = $real_cfg->dbuser;
|
|
|
|
$CFG->dbpass = $real_cfg->dbpass;
|
|
|
|
$CFG->dbname = $real_cfg->dbname;
|
|
|
|
$CFG->dbpersist = $real_cfg->dbpersist;
|
2008-09-18 13:43:27 +00:00
|
|
|
$CFG->unittestprefix = $real_cfg->unittestprefix;
|
2008-09-18 09:42:53 +00:00
|
|
|
$CFG->wwwroot = $real_cfg->wwwroot;
|
|
|
|
$CFG->dirroot = $real_cfg->dirroot;
|
|
|
|
$CFG->libdir = $real_cfg->libdir;
|
|
|
|
$CFG->dataroot = $real_cfg->dataroot;
|
|
|
|
$CFG->admin = $real_cfg->admin;
|
|
|
|
$CFG->release = $real_cfg->release;
|
2008-09-18 13:43:27 +00:00
|
|
|
$CFG->version = $real_cfg->version;
|
2008-09-18 09:42:53 +00:00
|
|
|
$CFG->config_php_settings = $real_cfg->config_php_settings;
|
|
|
|
$CFG->frametarget = $real_cfg->frametarget;
|
|
|
|
$CFG->framename = $real_cfg->framename;
|
|
|
|
$CFG->footer = $real_cfg->footer;
|
|
|
|
$CFG->debug = $real_cfg->debug;
|
2008-09-17 14:31:30 +00:00
|
|
|
|
2008-09-16 16:40:15 +00:00
|
|
|
$DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
|
2008-09-18 10:05:20 +00:00
|
|
|
$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->unittestprefix);
|
2008-09-17 14:31:30 +00:00
|
|
|
|
2008-09-18 13:43:27 +00:00
|
|
|
if ($DB->get_manager()->table_exists(new xmldb_table('config')) && $config = $DB->get_records('config')) {
|
2008-09-17 14:31:30 +00:00
|
|
|
foreach ($config as $conf) {
|
|
|
|
$CFG->{$conf->name} = $conf->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-16 16:40:15 +00:00
|
|
|
$test_tables = $DB->get_tables();
|
2008-09-16 12:19:43 +00:00
|
|
|
|
|
|
|
// Build test tables if requested and needed
|
2008-09-17 14:31:30 +00:00
|
|
|
if ($setuptesttables || $continuesetuptesttables) {
|
2008-09-16 12:19:43 +00:00
|
|
|
$version = null;
|
|
|
|
$release = null;
|
|
|
|
include("$CFG->dirroot/version.php"); // defines $version and $release
|
|
|
|
|
2008-09-17 14:31:30 +00:00
|
|
|
if (!$continuesetuptesttables) {
|
|
|
|
// Drop all tables first if they exist
|
|
|
|
$manager = $DB->get_manager();
|
|
|
|
foreach ($test_tables as $table) {
|
|
|
|
$xmldbtable = new xmldb_table($table);
|
|
|
|
$manager->drop_table($xmldbtable);
|
|
|
|
}
|
2008-09-16 12:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
upgrade_db($version, $release, true);
|
2008-09-16 16:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($droptesttables) {
|
|
|
|
$manager = $DB->get_manager();
|
|
|
|
foreach ($test_tables as $table) {
|
2008-09-17 14:31:30 +00:00
|
|
|
$xmldbtable = new xmldb_table($table);
|
|
|
|
$manager->drop_table($xmldbtable);
|
2008-09-16 16:40:15 +00:00
|
|
|
}
|
|
|
|
$test_tables = $DB->get_tables();
|
2008-09-16 12:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($test_tables['config'])) {
|
|
|
|
// TODO replace error with proper admin dialog
|
|
|
|
notice_yesno(get_string('tablesnotsetup', 'simpletest'), $baseurl . '?setuptesttables=1', $baseurl);
|
2008-09-16 16:40:15 +00:00
|
|
|
$DB = $real_db;
|
2008-09-16 12:19:43 +00:00
|
|
|
admin_externalpage_print_footer();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2008-09-16 16:40:15 +00:00
|
|
|
$DB = $real_db;
|
2008-09-17 14:31:30 +00:00
|
|
|
$CFG = $real_cfg;
|
2008-09-16 16:40:15 +00:00
|
|
|
|
2006-09-13 16:19:56 +00:00
|
|
|
if (!is_null($path)) {
|
|
|
|
// Create the group of tests.
|
2008-06-12 09:38:49 +00:00
|
|
|
$test = new AutoGroupTest($showsearch, $thorough);
|
2006-09-20 21:00:45 +00:00
|
|
|
|
|
|
|
// OU specific. We use the _nonproject folder for stuff we want to
|
2006-09-13 16:19:56 +00:00
|
|
|
// keep in CVS, but which is not really relevant. It does no harm
|
|
|
|
// to leave this here.
|
|
|
|
$test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
|
2008-06-10 19:54:27 +00:00
|
|
|
$test->addIgnoreFolder($CFG->libdir . '/ddl');
|
|
|
|
$test->addIgnoreFolder($CFG->libdir . '/dml');
|
2006-09-20 21:00:45 +00:00
|
|
|
|
2006-09-13 16:19:56 +00:00
|
|
|
// Make the reporter, which is what displays the results.
|
|
|
|
$reporter = new ExHtmlReporter($showpasses);
|
2006-09-20 21:00:45 +00:00
|
|
|
|
2006-09-13 16:19:56 +00:00
|
|
|
if ($showsearch) {
|
|
|
|
print_heading('Searching for test cases');
|
|
|
|
}
|
|
|
|
flush();
|
2006-09-20 21:00:45 +00:00
|
|
|
|
2006-09-13 16:19:56 +00:00
|
|
|
// Work out what to test.
|
|
|
|
if (substr($path, 0, 1) == '/') {
|
|
|
|
$path = substr($path, 1);
|
|
|
|
}
|
|
|
|
$path = $CFG->dirroot . '/' . $path;
|
|
|
|
if (substr($path, -1) == '/') {
|
|
|
|
$path = substr($path, 0, -1);
|
|
|
|
}
|
|
|
|
$displaypath = substr($path, strlen($CFG->dirroot) + 1);
|
|
|
|
$ok = true;
|
|
|
|
if (is_file($path)) {
|
|
|
|
$test->addTestFile($path);
|
|
|
|
} else if (is_dir($path)){
|
|
|
|
$test->findTestFiles($path);
|
2006-06-30 14:59:05 +00:00
|
|
|
} else {
|
2006-09-13 16:19:56 +00:00
|
|
|
print_simple_box(get_string('pathdoesnotexist', $langfile, $path), '', '', '', '', 'errorbox');
|
|
|
|
$ok = false;
|
|
|
|
}
|
2006-09-20 21:00:45 +00:00
|
|
|
|
2008-06-10 19:54:27 +00:00
|
|
|
// Add ddl and dml tests if requested
|
|
|
|
if ($rundbtests) {
|
|
|
|
if (!strstr($path, $CFG->libdir . '/ddl')) {
|
2008-06-15 11:35:25 +00:00
|
|
|
$test->addTestFile($CFG->libdir . '/ddl/simpletest/testddl.php');
|
2008-06-10 19:54:27 +00:00
|
|
|
}
|
|
|
|
if (!strstr($path, $CFG->libdir . '/dml')) {
|
2008-06-15 11:35:25 +00:00
|
|
|
$test->addTestFile($CFG->libdir . '/dml/simpletest/testdml.php');
|
2008-06-10 19:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 16:19:56 +00:00
|
|
|
// If we have something to test, do it.
|
|
|
|
if ($ok) {
|
|
|
|
if ($path == $CFG->dirroot) {
|
|
|
|
$title = get_string('moodleunittests', $langfile, get_string('all', $langfile));
|
|
|
|
} else {
|
|
|
|
$title = get_string('moodleunittests', $langfile, $displaypath);
|
|
|
|
}
|
|
|
|
print_heading($title);
|
|
|
|
$test->run($reporter);
|
2006-06-30 14:59:05 +00:00
|
|
|
}
|
|
|
|
|
2006-09-13 16:19:56 +00:00
|
|
|
$formheader = get_string('retest', $langfile);
|
|
|
|
} else {
|
|
|
|
$displaypath = '';
|
|
|
|
$formheader = get_string('rununittests', $langfile);
|
|
|
|
}
|
2006-06-30 14:59:05 +00:00
|
|
|
// Print the form for adjusting options.
|
2008-09-05 07:44:07 +00:00
|
|
|
print_box_start('generalbox boxwidthwide boxaligncenter');
|
2006-11-30 09:32:46 +00:00
|
|
|
echo '<form method="get" action="index.php">';
|
2007-01-09 12:32:51 +00:00
|
|
|
echo '<fieldset class="invisiblefieldset">';
|
2006-09-13 16:19:56 +00:00
|
|
|
print_heading($formheader);
|
2006-07-03 10:04:04 +00:00
|
|
|
echo '<p>'; print_checkbox('showpasses', 1, $showpasses, get_string('showpasses', $langfile)); echo '</p>';
|
|
|
|
echo '<p>'; print_checkbox('showsearch', 1, $showsearch, get_string('showsearch', $langfile)); echo '</p>';
|
|
|
|
echo '<p>'; print_checkbox('thorough', 1, $thorough, get_string('thorough', $langfile)); echo '</p>';
|
2006-06-30 14:59:05 +00:00
|
|
|
echo '<p>';
|
2006-07-03 10:04:04 +00:00
|
|
|
echo '<label for="path">', get_string('onlytest', $langfile), '</label> ';
|
2007-01-15 08:02:10 +00:00
|
|
|
echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
|
2006-06-30 14:59:05 +00:00
|
|
|
echo '</p>';
|
2008-06-10 22:05:29 +00:00
|
|
|
echo '<p>'; print_checkbox('rundbtests', 1, $rundbtests, get_string('rundbtests', $langfile)); echo '</p>'; // TODO: localise
|
2006-07-03 10:04:04 +00:00
|
|
|
echo '<input type="submit" value="' . get_string('runtests', $langfile) . '" />';
|
2007-01-09 12:32:51 +00:00
|
|
|
echo '</fieldset>';
|
2006-06-30 14:59:05 +00:00
|
|
|
echo '</form>';
|
2008-09-05 07:26:01 +00:00
|
|
|
print_box_end();
|
2006-06-30 14:59:05 +00:00
|
|
|
|
|
|
|
// Footer.
|
2007-04-30 17:08:34 +00:00
|
|
|
admin_externalpage_print_footer();
|
2006-06-30 14:59:05 +00:00
|
|
|
|
2006-09-03 14:45:57 +00:00
|
|
|
?>
|