--- /dev/null
+<?php
+/**
+ * Run database functional 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');
+
+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();
+
+?>
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;
+
+ if (isset($EXT_TEST_DB)) {
+ $this->db = $EXT_TEST_DB;
+ } else {
+ $this->db = $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();
+ $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;
+ if ($dbmanager->table_exists($table)) {
+ $dbmanager->drop_table($table, true, false);
+ }
- // insert records
- $datafile = $CFG->libdir . '/dml/simpletest/fixtures/testdata.xml';
- $xml = simplexml_load_file($datafile);
+ $dbmanager->create_table($table);
+ $tables[$table->getName()] = $table;
- foreach ($xml->record as $record) {
- $this->db->insert_record('testtable', $record);
- }
}
function tearDown() {
$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());
}
$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;
}
$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;
}
$sqlarray = null;
try {
- $sqlarray = $this->db->fix_sql_params($sql, $params);
+ $sqlarray = $DB->fix_sql_params($sql, $params);
} catch (Exception $e) {
$exception_caught = true;
}
$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;
}
$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;
}
$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;
}
// 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]));
}