MDL-14905 towards functional DB tests

This commit is contained in:
skodak 2008-06-10 19:54:27 +00:00
parent dfba1c3da1
commit e6b4f00ed7
7 changed files with 200 additions and 86 deletions

View file

@ -0,0 +1,102 @@
<?php
/**
* Run database functional tests.
*
* @copyright &copy; 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
*/
/** */
require_once(dirname(__FILE__).'/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/simpletestlib.php');
require_once('ex_simple_test.php');
require_once('ex_reporter.php');
require_login();
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM));
// CGI arguments
$showpasses = optional_param('showpasses', 0, PARAM_BOOL);
$dbinstance = optional_param('dbinstance', -1, PARAM_INT);
$langfile = 'simpletest';
// Print the header.
admin_externalpage_setup('reportdbtest');
$strtitle = get_string('unittests', $langfile);
admin_externalpage_print_header();
$dbinstances = array();
$dbinstances[0] = $DB;
for ($i=1; $i<=10; $i++) {
$name = 'ext_test_db_'.$i;
if (!isset($CFG->$name)) {
continue;
}
list($library, $driver, $dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions) = $CFG->$name;
$classname = "{$driver}_{$library}_moodle_database";
require_once("$CFG->libdir/dml/$classname.php");
$d = new $classname();
if (!$d->driver_installed()) {
continue;
}
if ($d->connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions)) {
$dbinstances[$i] = $d;
}
}
if (!isset($dbinstances[$dbinstance])) {
$dbinstance = -1;
} else {
global $EXT_TEST_DB;
$EXT_TEST_DB = $dbinstances[$dbinstance];
}
if ($dbinstance >= 0) {
// Create the group of tests.
$test =& new AutoGroupTest(false, true);
// Make the reporter, which is what displays the results.
$reporter = new ExHtmlReporter($showpasses);
// $test->addTestFile($CFG->libdir . '/ddl/simpletest/testddllib.php');
$test->addTestFile($CFG->libdir . '/dml/simpletest/testdmllib.php');
// If we have something to test, do it.
print_heading(get_string('moodleunittests', $langfile, get_string('all', $langfile)));
/* The UNITTEST constant can be checked elsewhere if you need to know
* when your code is being run as part of a unit test. */
define('UNITTEST', true);
$test->run($reporter);
$formheader = get_string('retest', $langfile);
} else {
$formheader = get_string('rununittests', $langfile);
}
// Print the form for adjusting options.
print_simple_box_start('center', '70%');
echo '<form method="get" action="dbtest.php">';
echo '<fieldset class="invisiblefieldset">';
print_heading($formheader);
echo '<p>'; print_checkbox('showpasses', 1, $showpasses, get_string('showpasses', $langfile)); echo '</p>';
echo '<input type="hidden" value="0" name="dbinstance" />';
echo '<input type="submit" value="' . get_string('runtests', $langfile) . '" />';
echo '</fieldset>';
echo '</form>';
print_simple_box_end();
// Footer.
admin_externalpage_print_footer();
?>

View file

@ -28,6 +28,7 @@ $langfile = 'simpletest';
$path = optional_param('path', null, PARAM_PATH);
$showpasses = optional_param('showpasses', false, PARAM_BOOL);
$showsearch = optional_param('showsearch', false, PARAM_BOOL);
$rundbtests = optional_param('rundbtests', false, PARAM_BOOL);
$thorough = optional_param('thorough', false, PARAM_BOOL);
// Print the header.
@ -43,6 +44,8 @@ if (!is_null($path)) {
// keep in CVS, but which is not really relevant. It does no harm
// to leave this here.
$test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
$test->addIgnoreFolder($CFG->libdir . '/ddl');
$test->addIgnoreFolder($CFG->libdir . '/dml');
// Make the reporter, which is what displays the results.
$reporter = new ExHtmlReporter($showpasses);
@ -71,6 +74,16 @@ if (!is_null($path)) {
$ok = false;
}
// Add ddl and dml tests if requested
if ($rundbtests) {
if (!strstr($path, $CFG->libdir . '/ddl')) {
$test->addTestFile($CFG->libdir . '/ddl/simpletest/testddllib.php');
}
if (!strstr($path, $CFG->libdir . '/dml')) {
$test->addTestFile($CFG->libdir . '/dml/simpletest/testdmllib.php');
}
}
// If we have something to test, do it.
if ($ok) {
if ($path == $CFG->dirroot) {
@ -79,6 +92,9 @@ if (!is_null($path)) {
$title = get_string('moodleunittests', $langfile, $displaypath);
}
print_heading($title);
/* The UNITTEST constant can be checked elsewhere if you need to know
* when your code is being run as part of a unit test. */
define('UNITTEST', true);
$test->run($reporter);
}
@ -99,6 +115,7 @@ echo '<p>';
echo '<label for="path">', get_string('onlytest', $langfile), '</label> ';
echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
echo '</p>';
echo '<p>'; print_checkbox('rundbtests', 1, $rundbtests, get_string('rundbtests', $langfile)); echo '</p>';
echo '<input type="submit" value="' . get_string('runtests', $langfile) . '" />';
echo '</fieldset>';
echo '</form>';

View file

@ -0,0 +1,10 @@
<?php //$Id$
$reportname = get_string('simpletest', 'report_simpletest');
if ($reportname[1] == '[') {
$reportname = get_string('simpletest', 'admin');
}
$ADMIN->add('reports', new admin_externalpage('reportsimpletest', $reportname, "$CFG->wwwroot/$CFG->admin/report/simpletest/index.php",'moodle/site:config'));
//TODO: localise
$ADMIN->add('reports', new admin_externalpage('reportdbtest', 'Functional DB tests', "$CFG->wwwroot/$CFG->admin/report/simpletest/dbtest.php",'moodle/site:config'));