-<?php
+<?php // $Id$
/**
* 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_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);
+$selected = optional_param('selected', array(), PARAM_INT);
-$langfile = 'simpletest';
+if (!data_submitted()) {
+ $selected = array();
+ for ($i=0; $i<=10; $i++) {
+ $selected[$i] = 1;
+ }
+}
// Print the header.
admin_externalpage_setup('reportdbtest');
-$strtitle = get_string('unittests', $langfile);
admin_externalpage_print_header();
-$dbinstances = array();
-$dbinstances[0] = $DB;
+$dbinfos = array();
+$tests = array();
+
+$dbinfos[0] = array('name'=>"Current database", 'installed'=>true, 'configured'=>true); // TODO: localise
+if (data_submitted() and !empty($selected[0])) {
+ $tests[0] = $DB;
+}
for ($i=1; $i<=10; $i++) {
- $name = 'ext_test_db_'.$i;
+ $name = 'func_test_db_'.$i;
if (!isset($CFG->$name)) {
continue;
}
list($library, $driver, $dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions) = $CFG->$name;
+ $dbinfos[$i] = array('name'=>"External database $library/$driver/$dbhost/$dbname", 'installed'=>false, 'configured'=>false);
$classname = "{$driver}_{$library}_moodle_database";
require_once("$CFG->libdir/dml/$classname.php");
if (!$d->driver_installed()) {
continue;
}
+ $dbinfos[$i]['installed'] = true;
if ($d->connect($dbhost, $dbuser, $dbpass, $dbname, $dbpersist, $prefix, $dboptions)) {
- $dbinstances[$i] = $d;
+ $dbinfos[$i]['configured'] = true;
+ if (data_submitted() and !empty($selected[$i])) {
+ $tests[$i] = $d;
+ } else {
+ $d->dispose();
+ }
}
}
-if (!isset($dbinstances[$dbinstance])) {
- $dbinstance = -1;
-} else {
- global $EXT_TEST_DB;
- $EXT_TEST_DB = $dbinstances[$dbinstance];
-}
+if (!empty($tests)) {
+ /* 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);
-if ($dbinstance >= 0) {
+ @ob_implicit_flush(true);
+ while(@ob_end_flush());
- // Create the group of tests.
- $test =& new AutoGroupTest(false, true);
+ global $FUNCT_TEST_DB; // hack - we pass the connected db to functional database tests through this global
- // Make the reporter, which is what displays the results.
- $reporter = new ExHtmlReporter($showpasses);
+ foreach ($tests as $i=>$database) {
+ $dbinfo = $dbinfos[$i];
+ $FUNCT_TEST_DB = $database;
- $test->addTestFile($CFG->libdir . '/dml/simpletest/testdmllib.php');
- $test->addTestFile($CFG->libdir . '/ddl/simpletest/testddllib.php');
+ print_heading('Running tests on: '.$dbinfo['name'], '', 3); // TODO: localise
- // If we have something to test, do it.
- print_heading(get_string('moodleunittests', $langfile, get_string('all', $langfile)));
+ // Create the group of tests.
+ $test = new AutoGroupTest(false, true);
- /* 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);
+ $test->addTestFile($CFG->libdir . '/dml/simpletest/testdmllib.php');
+ $test->addTestFile($CFG->libdir . '/ddl/simpletest/testddllib.php');
+
+ // Make the reporter, which is what displays the results.
+ $reporter = new ExHtmlReporter($showpasses);
- $formheader = get_string('retest', $langfile);
+ $test->run($reporter);
+
+ echo '<hr />';
+ }
-} 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 method="post" action="dbtest.php">';
+echo '<div>';
+print_heading("Run functional database tests"); // TODO: localise
+echo '<p>'; print_checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'simpletest')); echo '</p>';
+echo '<p><strong>'."Databases:".'</strong>';
+echo '<ul>';
+foreach ($dbinfos as $i=>$dbinfo) {
+ $name = $dbinfo['name'];
+ if ($dbinfo['installed']) {
+ if (!$dbinfo['configured']) {
+ $name = "$name (misconfigured)"; // TODO: localise
+ }
+ echo '<li>'; print_checkbox('selected['.$i.']', 1, intval(!empty($selected[$i])), $name); echo '</li>';
+ } else {
+ echo '<li>'."$name: driver not installed".'</li'; // TODO: localise
+ }
+}
+echo '</ul></p>';
+echo '<input type="submit" value="' . get_string('runtests', 'simpletest') . '" />';
+echo '</div>';
echo '</form>';
print_simple_box_end();
private $db;
public function setUp() {
- global $CFG, $DB, $EXT_TEST_DB;
+ global $CFG, $DB, $FUNCT_TEST_DB;
- if (isset($EXT_TEST_DB)) {
- $this->db = $EXT_TEST_DB;
+ if (isset($FUNCT_TEST_DB)) {
+ $this->db = $FUNCT_TEST_DB;
} else {
$this->db = $DB;
}
$dbmanager = $this->db->get_manager();
- $table = new xmldb_table('deftable0');
+ $table = new xmldb_table('test_table0');
$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');
+ array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general');
$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);
$this->tables[$table->getName()] = $table;
// Second, smaller table
- $table = new xmldb_table ('deftable1');
+ $table = new xmldb_table ('test_table1');
$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('name', XMLDB_TYPE_CHAR, '30', null, null, null, null, null, 'Moodle');
// drop custom test tables
for ($i=0; $i<3; $i++) {
- $table = new xmldb_table('testtable'.$i);
+ $table = new xmldb_table('test_table_cust'.$i);
if ($dbmanager->table_exists($table)) {
$dbmanager->drop_table($table, true, false);
}
$dbmanager = $this->db->get_manager();
// TODO: this should be done with $this->db object
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
// Test giving a string
$this->assertFalse($dbmanager->table_exists('nonexistenttable'));
- $this->assertTrue($dbmanager->table_exists('deftable0'));
+ $this->assertTrue($dbmanager->table_exists('test_table0'));
// Test giving a table object
$nonexistenttable = new xmldb_table('nonexistenttable');
$dbmanager = $this->db->get_manager();
// TODO: add all data types, comments, keys and indexes
- $table = $this->tables['deftable0'];
+ $table = $this->tables['test_table0'];
$this->assertTrue($dbmanager->create_table($table));
- $this->assertTrue($dbmanager->table_exists('deftable0'));
+ $this->assertTrue($dbmanager->table_exists('test_table0'));
$dbmanager->drop_table($table);
- $this->assertFalse($dbmanager->table_exists('deftable0'));
+ $this->assertFalse($dbmanager->table_exists('test_table0'));
// Give a wrong table param (expect a debugging message)
$table = 'string';
public function testDropTable() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$this->assertTrue($dbmanager->drop_table($table, true, false));
- $this->assertFalse($dbmanager->table_exists('deftable0'));
+ $this->assertFalse($dbmanager->table_exists('test_table0'));
// Try dropping non-existent table
$table = new xmldb_table('nonexistenttable');
$dbmanager = $this->db->get_manager();
// TODO: verify the type in DB
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
/// Create a new field with complex specs (enums are good candidates)
$field = new xmldb_field('type2');
$dbmanager = $this->db->get_manager();
// TODO: verify the type in DB
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
/// Create a new field with complex specs (enums are good candidates)
$field = new xmldb_field('onenumber');
$field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, 'type');
public function testDropField() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$field = $table->getField('type');
$this->assertTrue($dbmanager->field_exists($table, $field));
$dbmanager = $this->db->get_manager();
// TODO: verify the type is changed in db
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$field = new xmldb_field('course');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, '0');
$this->assertTrue($dbmanager->change_field_type($table, $field));
$dbmanager = $this->db->get_manager();
// TODO: verify the precision is changed in db
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$field = new xmldb_field('intro');
$field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null, null, null);
$this->assertTrue($dbmanager->change_field_precision($table, $field));
$dbmanager = $this->db->get_manager();
// TODO: verify the signed is changed in db
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$field = new xmldb_field('grade');
$field->set_attributes(XMLDB_TYPE_NUMBER, '10,2', XMLDB_UNSIGNED, null, null, null, null, null);
$this->assertTrue($dbmanager->change_field_unsigned($table, $field));
$dbmanager = $this->db->get_manager();
// TODO: verify the type is nullability in db
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$field = new xmldb_field('name');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, null, 'Moodle');
$this->assertTrue($dbmanager->change_field_notnull($table, $field));
public function testChangeFieldDefault() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$field = new xmldb_field('name');
$field->set_attributes(XMLDB_TYPE_CHAR, '30', null, null, null, null, null, null);
$this->assertTrue($dbmanager->change_field_default($table, $field));
public function testAddUniqueIndex() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$index = new xmldb_index('secondname');
$index->set_attributes(XMLDB_INDEX_UNIQUE, array('name', 'secondname', 'grade'));
$this->assertTrue($dbmanager->add_index($table, $index));
public function testAddNonUniqueIndex() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$index = new xmldb_index('secondname');
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
$this->assertTrue($dbmanager->add_index($table, $index));
public function testFindIndexName() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$index = new xmldb_index('secondname');
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
$dbmanager->add_index($table, $index);
public function testDropIndex() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$index = new xmldb_index('secondname');
$index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name'));
$dbmanager->add_index($table, $index);
public function testAddUniqueKey() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
+ $table = $this->create_deftable('test_table1');
$key = new xmldb_key('id-course-grade');
$key->set_attributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade'));
$this->assertTrue($dbmanager->add_key($table, $key));
public function testAddForeignUniqueKey() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
- $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table1');
+ $this->create_deftable('test_table0');
$key = new xmldb_key('course');
- $key->set_attributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'deftable0', array('id'));
+ $key->set_attributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'test_table0', array('id'));
$this->assertTrue($dbmanager->add_key($table, $key));
}
public function testDropKey() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
- $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table1');
+ $this->create_deftable('test_table0');
$key = new xmldb_key('course');
- $key->set_attributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'deftable0', array('id'));
+ $key->set_attributes(XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'test_table0', array('id'));
$dbmanager->add_key($table, $key);
$this->assertTrue($dbmanager->drop_key($table, $key));
public function testAddForeignKey() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
- $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table1');
+ $this->create_deftable('test_table0');
$key = new xmldb_key('course');
- $key->set_attributes(XMLDB_KEY_FOREIGN, array('course'), 'deftable0', array('id'));
+ $key->set_attributes(XMLDB_KEY_FOREIGN, array('course'), 'test_table0', array('id'));
$this->assertTrue($dbmanager->add_key($table, $key));
}
public function testDropForeignKey() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable1');
- $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table1');
+ $this->create_deftable('test_table0');
$key = new xmldb_key('course');
- $key->set_attributes(XMLDB_KEY_FOREIGN, array('course'), 'deftable0', array('id'));
+ $key->set_attributes(XMLDB_KEY_FOREIGN, array('course'), 'test_table0', array('id'));
$dbmanager->add_key($table, $key);
$this->assertTrue($dbmanager->drop_key($table, $key));
public function testChangeFieldEnum() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
// Removing an enum value
$field = new xmldb_field('type');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null);
// unsupported!
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$index = new xmldb_index('course');
$index->set_attributes(XMLDB_INDEX_UNIQUE, array('course'));
//unsupported
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$key = new xmldb_key('course');
$key->set_attributes(XMLDB_KEY_UNIQUE, array('course'));
public function testRenameField() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$field = new xmldb_field('type');
$field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, XMLDB_ENUM,
array('single', 'news', 'general', 'social', 'eachuser', 'teacher', 'qanda'), 'general', 'course');
public function testRenameTable() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
- $this->assertFalse($dbmanager->table_exists('testtable0'));
- $this->assertTrue($dbmanager->rename_table($table, 'testtable0'));
+ $this->assertFalse($dbmanager->table_exists('test_table_cust0'));
+ $this->assertTrue($dbmanager->rename_table($table, 'test_table_cust0'));
- $table->setName('testtable0');
+ $table->setName('test_table_cust0');
$dbmanager->drop_table($table);
}
public function testFieldExists() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
// String params
// Give a nonexistent table as first param
$this->assertFalse($dbmanager->field_exists('nonexistenttable', 'id'));
// Give a nonexistent field as second param
- $this->assertFalse($dbmanager->field_exists('deftable0', 'nonexistentfield'));
+ $this->assertFalse($dbmanager->field_exists('test_table0', 'nonexistentfield'));
// Correct string params
- $this->assertTrue($dbmanager->field_exists('deftable0', 'id'));
+ $this->assertTrue($dbmanager->field_exists('test_table0', 'id'));
// Object params
$realfield = $table->getField('id');
public function testFindCheckConstraintName() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$field = $table->getField('type');
- $this->assertEqual($dbmanager->find_check_constraint_name($table, $field), 'type');
+ $result = $dbmanager->find_check_constraint_name($table, $field);
+ $this->assertTrue(!empty($result));
}
public function testCheckConstraintExists() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$field = $table->getField('type');
$this->assertTrue($dbmanager->check_constraint_exists($table, $field), 'type');
}
public function testFindKeyName() {
$dbmanager = $this->db->get_manager();
- $table = $this->create_deftable('deftable0');
+ $table = $this->create_deftable('test_table0');
$key = $table->getKey('primary');
$invalid_key = 'invalid_key';
ob_end_clean();
// Give existing and valid table param
- $table = $this->create_deftable('deftable0');
- $this->assertEqual(false, $dbmanager->find_sequence_name($table));
+ $table = $this->create_deftable('test_table0');
+//TODO: this returns stuff depending on db internals
+ // $this->assertEqual(false, $dbmanager->find_sequence_name($table));
}
unset($CFG->xmldbreconstructprevnext); // remove this unhack ;-)
- $this->create_deftable('deftable1');
+ $this->create_deftable('test_table1');
- $this->assertTrue($dbmanager->table_exists('deftable1'));
+ $this->assertTrue($dbmanager->table_exists('test_table1'));
// feed nonexistent file
ob_start(); // hide debug warning
ob_end_clean();
// Check that the table has not been deleted from DB
- $this->assertTrue($dbmanager->table_exists('deftable1'));
+ $this->assertTrue($dbmanager->table_exists('test_table1'));
// Real and valid xml file
$this->assertTrue($dbmanager->delete_tables_from_xmldb_file($CFG->libdir . '/ddl/simpletest/fixtures/xmldb_table.xml', false));
// Check that the table has been deleted from DB
- $this->assertFalse($dbmanager->table_exists('deftable1'));
+ $this->assertFalse($dbmanager->table_exists('test_table1'));
}
public function testInstallFromXmldbFile() {
ob_end_clean();
// Check that the table has not yet been created in DB
- $this->assertFalse($dbmanager->table_exists('deftable1'));
+ $this->assertFalse($dbmanager->table_exists('test_table1'));
// Real and valid xml file
$this->assertTrue($dbmanager->install_from_xmldb_file($CFG->libdir . '/ddl/simpletest/fixtures/xmldb_table.xml', false));
- $this->assertTrue($dbmanager->table_exists('deftable1'));
+ $this->assertTrue($dbmanager->table_exists('test_table1'));
}
public function testCreateTempTable() {
// Feed incorrect table param
ob_start(); // hide debug warning
- $this->assertFalse($dbmanager->create_temp_table('deftable1'));
+ $this->assertFalse($dbmanager->create_temp_table('test_table1'));
ob_end_clean();
- $table = $this->tables['deftable1'];
+ $table = $this->tables['test_table1'];
// New table
$this->assertTrue($dbmanager->create_temp_table($table));
- $this->assertTrue($dbmanager->table_exists('deftable1', true));
+ $this->assertTrue($dbmanager->table_exists('test_table1', true));
// Delete
$this->assertTrue($dbmanager->drop_temp_table($table));
- $this->assertFalse($dbmanager->table_exists('deftable1', true));
+ $this->assertFalse($dbmanager->table_exists('test_table1', true));
}
}