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'));

View File

@ -34,16 +34,16 @@ $ADMIN->add('root', new admin_category('mnet', get_string('net','mnet')));
$ADMIN->add('root', new admin_category('reports', get_string('reports')));
foreach (get_list_of_plugins($CFG->admin.'/report') as $plugin) {
/// This snippet is temporary until simpletest can be fixed to use xmldb. See MDL-7377 XXX TODO
if ($plugin == 'simpletest' && $CFG->dbfamily != 'mysql' && $CFG->dbfamily != 'postgres') {
continue;
$settingsfile = "$CFG->dirroot/$CFG->admin/report/$plugin/settings.php";
if (file_exists($settingsfile)) {
include($settingsfile);
} else {
$reportname = get_string($plugin, 'report_' . $plugin);
if ($reportname[1] == '[') {
$reportname = get_string($plugin, 'admin');
}
$ADMIN->add('reports', new admin_externalpage('report'.$plugin, $reportname, "$CFG->wwwroot/$CFG->admin/report/$plugin/index.php",'moodle/site:viewreports'));
}
/// End of removable snippet
$reportname = get_string($plugin, 'report_' . $plugin);
if ($reportname[1] == '[') {
$reportname = get_string($plugin, 'admin');
}
$ADMIN->add('reports', new admin_externalpage('report'.$plugin, $reportname, "$CFG->wwwroot/$CFG->admin/report/$plugin/index.php",'moodle/site:viewreports'));
}
$ADMIN->add('root', new admin_category('misc', get_string('miscellaneous')));

View File

@ -365,12 +365,5 @@ function rename_index($table, $index, $newname, $continue=true, $feedback=true)
return $DB->get_manager()->rename_index($table, $index, $newname, $continue, $feedback);
}
/// DELETED !!
function table_column($table, $oldfield, $field, $type='integer', $size='10',
$signed='unsigned', $default='0', $null='not null', $after='') {
error('table_column() was removed, please use new ddl functions');
}
?>

View File

@ -439,7 +439,6 @@ function get_current_group($courseid, $full = false) {
function error($message, $link='') {
global $CFG, $SESSION, $THEME;
debugging('error() is a deprecated function, please call print_error() instead of error()', DEBUG_DEVELOPER);
$message = clean_text($message); // In case nasties are in here
/**
@ -451,6 +450,8 @@ function error($message, $link='') {
throw new Exception('error() call: '. $message.($link!=='' ? ' ['.$link.']' : ''));
}
debugging('error() is a deprecated function, please call print_error() instead of error()', DEBUG_DEVELOPER);
if (defined('FULLME') && FULLME == 'cron') {
// Errors in cron should be mtrace'd.
mtrace($message);
@ -767,4 +768,8 @@ function get_field($table, $return, $field1, $value1, $field2='', $value2='', $f
error('get_field() not available anymore');
}
function table_column($table, $oldfield, $field, $type='integer', $size='10',
$signed='unsigned', $default='0', $null='not null', $after='') {
error('table_column() was removed, please use new ddl functions');
}

View File

@ -8,60 +8,55 @@ if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir . '/simpletestlib/web_tester.php');
require_once($CFG->libdir . '/dmllib.php');
require_once($CFG->libdir . '/dml/mysql_adodb_moodle_database.php');
class dmllib_test extends UnitTestCase {
private $tables = array();
private $dbmanager;
private $db;
function setUp() {
global $CFG;
global $CFG, $DB, $EXT_TEST_DB;
$this->db = new mysqli_adodb_moodle_database();
$this->db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->dbpersist, $CFG->prefix);
$this->dbmanager = $this->db->get_manager();
if (isset($EXT_TEST_DB)) {
$this->db = $EXT_TEST_DB;
} else {
$this->db = $DB;
}
$dbmanager = $this->db->get_manager();
$table = new xmldb_table("testtable");
$table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->addFieldInfo('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
$table->addFieldInfo('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
$table->addFieldInfo('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
$table->addFieldInfo('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
$table->addFieldInfo('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
$table->addFieldInfo('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
$table->addFieldInfo('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
$table->addFieldInfo('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addFieldInfo('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->addIndexInfo('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null);
$table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, null, null);
$table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, XMLDB_NOTNULL, null, null, null);
$table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1');
$table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null, null, null);
$table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, null, null, null);
$table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course'));
$table->setComment("This is a test'n drop table. You can drop it safely");
$this->dbmanager->create_table($table);
$this->tables[] = $table;
// insert records
$datafile = $CFG->libdir . '/dml/simpletest/fixtures/testdata.xml';
$xml = simplexml_load_file($datafile);
foreach ($xml->record as $record) {
$this->db->insert_record('testtable', $record);
if ($dbmanager->table_exists($table)) {
$dbmanager->drop_table($table, true, false);
}
$dbmanager->create_table($table);
$tables[$table->getName()] = $table;
}
function tearDown() {
@ -70,41 +65,33 @@ class dmllib_test extends UnitTestCase {
$this->dbmanager->drop_table($table, true, false);
}
}
unset($this->tables);
setup_DB();
}
function test_insert_record() {
}
function test_get_record_select() {
$record = $this->db->get_record_select('testtable', 'id = 1');
$this->tables = array();
}
function test_fix_sql_params() {
$DB = $this->db; // do not use global $DB!
// Malformed table placeholder
$sql = "SELECT * FROM [testtable]";
$sqlarray = $this->db->fix_sql_params($sql);
$sqlarray = $DB->fix_sql_params($sql);
$this->assertEqual($sql, $sqlarray[0]);
// Correct table placeholder substitution
$sql = "SELECT * FROM {testtable}";
$sqlarray = $this->db->fix_sql_params($sql);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable", $sqlarray[0]);
// Malformed param placeholders
$sql = "SELECT * FROM {testtable} WHERE name = ?param1";
$params = array('param1' => 'first record');
$sqlarray = $this->db->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?param1", $sqlarray[0]);
// Mixed param types (colon and dollar)
$sql = "SELECT * FROM {testtable} WHERE name = :param1, rsstype = \$1";
$params = array('param1' => 'first record', 'param2' => 1);
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$this->assertEqual('error() call: ERROR: Mixed types of sql query parameters!!', $e->getMessage());
}
@ -114,7 +101,7 @@ class dmllib_test extends UnitTestCase {
$params = array('param1' => 'first record', 'param2' => 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -125,7 +112,7 @@ class dmllib_test extends UnitTestCase {
$params = array('first record', 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -138,7 +125,7 @@ class dmllib_test extends UnitTestCase {
$sqlarray = null;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -150,7 +137,7 @@ class dmllib_test extends UnitTestCase {
$params = array('wrongname' => 'first record', 'rsstype' => 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -161,7 +148,7 @@ class dmllib_test extends UnitTestCase {
$params = array('name' => 'first record', 'rsstype' => 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -172,7 +159,7 @@ class dmllib_test extends UnitTestCase {
$params = array('first record', 1);
$exception_caught = false;
try {
$sqlarray = $this->db->fix_sql_params($sql, $params);
$sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
@ -181,15 +168,15 @@ class dmllib_test extends UnitTestCase {
// Correct named param placeholders
$sql = "SELECT * FROM {testtable} WHERE name = :name, rsstype = :rsstype";
$params = array('name' => 'first record', 'rsstype' => 1);
$sqlarray = $this->db->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$this->assertEqual(2, count($sqlarray[1]));
// Correct ? params
$sql = "SELECT * FROM {testtable} WHERE name = ?, rsstype = ?";
$params = array('first record', 1);
$sqlarray = $this->db->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$this->db->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$sqlarray = $DB->fix_sql_params($sql, $params);
$this->assertEqual("SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, rsstype = ?", $sqlarray[0]);
$this->assertEqual(2, count($sqlarray[1]));
}