mirror of
https://github.com/moodle/moodle.git
synced 2025-02-23 19:44:19 +01:00
This is not as ambitious as the abortive FakeDBUnitTests scheme, but this one works for simple cases. There is a new test case class UnitTestCaseUsingDatabase to inherit from. I hope it is sufficiently well documented in its PHPdocs. * It users $CFG->unittestprefix. * You can access that database using $this->testdb. * That database is empty by default, you have to call create_test_table to create the ones you want, and drop_test_table to clean them up in the end. The table definitions are read from the XMLDB file. * When you are ready to call real Moodle code that users $DB, call switch_to_test_db and then revert_to_real_db when you are done. * If you forget to call drop_test_table or switch_to_test_db, the class will attempt to clean up after you, but will also print rude developer debug messages telling you not to be stupid. * There is also a load_test_data method for populating a table from an array. The is an example of its use in lib/simpletest/testunittestusingdb.php.
142 lines
4.7 KiB
PHP
142 lines
4.7 KiB
PHP
<?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
|
|
*/
|
|
|
|
/** */
|
|
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');
|
|
|
|
// Always run the unit tests in developer debug mode.
|
|
$CFG->debug = DEBUG_DEVELOPER;
|
|
error_reporting($CFG->debug);
|
|
|
|
// page parameters
|
|
$path = optional_param('path', null, PARAM_PATH);
|
|
$showpasses = optional_param('showpasses', false, PARAM_BOOL);
|
|
$showsearch = optional_param('showsearch', false, PARAM_BOOL);
|
|
|
|
admin_externalpage_setup('reportsimpletest', '', array('showpasses'=>$showpasses, 'showsearch'=>$showsearch));
|
|
|
|
$langfile = 'simpletest';
|
|
$unittest = true;
|
|
|
|
global $UNITTEST;
|
|
$UNITTEST = new object();
|
|
|
|
// Print the header.
|
|
$strtitle = get_string('unittests', $langfile);
|
|
|
|
if (!is_null($path)) {
|
|
// Turn off xmlstrictheaders during the unit test run.
|
|
$origxmlstrictheaders = !empty($CFG->xmlstrictheaders);
|
|
$CFG->xmlstrictheaders = false;
|
|
admin_externalpage_print_header();
|
|
$CFG->xmlstrictheaders = $origxmlstrictheaders;
|
|
unset($origxmlstrictheaders);
|
|
|
|
// Create the group of tests.
|
|
$test = new AutoGroupTest($showsearch);
|
|
|
|
// OU specific. We use the _nonproject folder for stuff we want to
|
|
// keep in CVS, but which is not really relevant. It does no harm
|
|
// to leave this here.
|
|
$test->addIgnoreFolder($CFG->dirroot . '/_nonproject');
|
|
|
|
// Make the reporter, which is what displays the results.
|
|
$reporter = new ExHtmlReporter($showpasses);
|
|
|
|
if ($showsearch) {
|
|
print_heading('Searching for test cases');
|
|
}
|
|
flush();
|
|
|
|
// 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);
|
|
} else {
|
|
print_simple_box(get_string('pathdoesnotexist', $langfile, $path), '', '', '', '', 'errorbox');
|
|
$ok = false;
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
$formheader = get_string('retest', $langfile);
|
|
} else {
|
|
$displaypath = '';
|
|
admin_externalpage_print_header();
|
|
$formheader = get_string('rununittests', $langfile);
|
|
}
|
|
// Print the form for adjusting options.
|
|
print_box_start('generalbox boxwidthwide boxaligncenter');
|
|
print_heading($formheader);
|
|
echo '<form method="get" action="index.php">';
|
|
echo '<fieldset class="invisiblefieldset">';
|
|
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>';
|
|
echo '<label for="path">', get_string('onlytest', $langfile), '</label> ';
|
|
echo '<input type="text" id="path" name="path" value="', $displaypath, '" size="40" />';
|
|
echo '</p>';
|
|
echo '<input type="submit" value="' . get_string('runtests', $langfile) . '" />';
|
|
echo '</fieldset>';
|
|
echo '</form>';
|
|
print_box_end();
|
|
|
|
print_box_start('generalbox boxwidthwide boxaligncenter');
|
|
if (true) {
|
|
echo "<p>Fake test tables are disabled for now, sorry</p>"; // DO NOT LOCALISE!!! to be removed soon
|
|
|
|
} else if (empty($CFG->unittestprefix)) {
|
|
print_heading(get_string('testdboperations', 'simpletest'));
|
|
// TODO: localise
|
|
echo '<p>Please add $CFG->unittestprefix="tst_"; or some other unique test table prefix if you want to execute all tests';
|
|
|
|
} else {
|
|
print_heading(get_string('testdboperations', 'simpletest'));
|
|
echo '<p>'.get_string('unittestprefixsetting', 'simpletest', $CFG).'</p>';
|
|
|
|
echo '<form style="display:inline" method="get" action="index.php">';
|
|
echo '<fieldset class="invisiblefieldset">';
|
|
echo '<input type="hidden" name="setuptesttables" value="1" />';
|
|
echo '<input type="submit" value="' . get_string('reinstalltesttables', 'simpletest') . '" />';
|
|
echo '</fieldset>';
|
|
echo '</form>';
|
|
}
|
|
print_box_end();
|
|
|
|
|
|
// Footer.
|
|
admin_externalpage_print_footer();
|
|
|
|
?>
|