From 05d531a5fb10bb048ae41dd06e833327f2b1b6a4 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 21 Apr 2012 16:32:14 +0200 Subject: [PATCH] MDL-32569 drop old functional DB tests --- admin/tool/unittest/dbtest.php | 169 - admin/tool/unittest/settings.php | 1 - lib/ddl/simpletest/fixtures/invalid.xml | 19 - lib/ddl/simpletest/fixtures/xmldb_table.xml | 19 - lib/ddl/simpletest/testddl.php | 1864 -------- lib/dml/simpletest/fixtures/clob.txt | 197 - lib/dml/simpletest/fixtures/randombinary | Bin 10000 -> 0 bytes lib/dml/simpletest/testdml.php | 4628 ------------------- 8 files changed, 6897 deletions(-) delete mode 100644 admin/tool/unittest/dbtest.php delete mode 100644 lib/ddl/simpletest/fixtures/invalid.xml delete mode 100644 lib/ddl/simpletest/fixtures/xmldb_table.xml delete mode 100644 lib/ddl/simpletest/testddl.php delete mode 100644 lib/dml/simpletest/fixtures/clob.txt delete mode 100644 lib/dml/simpletest/fixtures/randombinary delete mode 100644 lib/dml/simpletest/testdml.php diff --git a/admin/tool/unittest/dbtest.php b/admin/tool/unittest/dbtest.php deleted file mode 100644 index b3f060cb9f9..00000000000 --- a/admin/tool/unittest/dbtest.php +++ /dev/null @@ -1,169 +0,0 @@ -. - -/** - * Run database functional tests. - * - * @package tool - * @subpackage unittest - * @copyright 2008 Petr Skoda {@link http://skodak.org} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -define('NO_OUTPUT_BUFFERING', true); - -require(dirname(__FILE__) . '/../../../config.php'); -require_once($CFG->libdir.'/adminlib.php'); - -require_once('simpletestlib.php'); -require_once('simpletestcoveragelib.php'); -require_once('ex_simple_test.php'); -require_once('ex_reporter.php'); - -$showpasses = optional_param('showpasses', false, PARAM_BOOL); -$codecoverage = optional_param('codecoverage', false, PARAM_BOOL); -$selected = optional_param_array('selected', array(), PARAM_INT); - -// Print the header and check access. -admin_externalpage_setup('tooldbtest'); -echo $OUTPUT->header(); - -global $UNITTEST; -$UNITTEST = new stdClass(); - -if (!data_submitted()) { - $selected = array(); - for ($i=0; $i<=10; $i++) { - $selected[$i] = 1; - } -} - - -$dbinfos = array(); -$tests = array(); - -$dbinfos[0] = array('name'=>"Current database ($CFG->dblibrary/$CFG->dbtype)", 'installed'=>true, 'configured'=>true); // TODO: localise -if (data_submitted() and !empty($selected[0])) { - $tests[0] = $DB; -} - -for ($i=1; $i<=10; $i++) { - $name = 'func_test_db_'.$i; - if (!isset($CFG->$name)) { - continue; - } - list($library, $driver, $dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions) = $CFG->$name; - $dbinfos[$i] = array('name'=>"External database $i ($library/$driver/$dbhost/$dbname/$prefix)", 'installed'=>false, 'configured'=>false); - - $classname = "{$driver}_{$library}_moodle_database"; - require_once("$CFG->libdir/dml/$classname.php"); - $d = new $classname(); - if (!$d->driver_installed()) { - continue; - } - $dbinfos[$i]['installed'] = true; - - try { - $d->connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, $dboptions); - $dbinfos[$i]['configured'] = true; - if (data_submitted() and !empty($selected[$i])) { - $tests[$i] = $d; - } else { - $d->dispose(); - } - } catch (dml_connection_exception $e) { - $dbinfos[$i]['configured'] = false; - } -} - -if (!empty($tests)) { - $covreporter = new moodle_coverage_reporter('Functional DB Tests Code Coverage Report', 'dbtest'); - $covrecorder = new moodle_coverage_recorder($covreporter); - - foreach ($tests as $i=>$database) { - $dbinfo = $dbinfos[$i]; - - $UNITTEST->func_test_db = $database; // pass the db to the tests through global - - echo $OUTPUT->heading('Running tests on: '.$dbinfo['name'], 3); // TODO: localise - - // Create the group of tests. - $test = new autogroup_test_coverage(false, true, $codecoverage); - - - $test->addTestFile($CFG->libdir.'/dml/simpletest/testdml.php'); - $test->addTestFile($CFG->libdir.'/ddl/simpletest/testddl.php'); - - // Make the reporter, which is what displays the results. - $reporter = new ExHtmlReporter($showpasses); - - set_time_limit(300); // 5 mins per DB should be enough - $test->run_with_external_coverage($reporter, $covrecorder); - - unset($UNITTEST->func_test_db); - - echo '
'; - } - if ($codecoverage) { - $covrecorder->generate_report(); - moodle_coverage_reporter::print_summary_info('dbtest'); - } - -} - -// Print the form for adjusting options. -echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter'); -echo '
'; -echo '
'; -echo $OUTPUT->heading("Run functional database tests"); // TODO: localise -echo '

'.html_writer::checkbox('showpasses', 1, $showpasses, get_string('showpasses', 'tool_unittest')).'

'; -if (moodle_coverage_recorder::can_run_codecoverage()) { - echo '

'.html_writer::checkbox('codecoverage', 1, $codecoverage, get_string('codecoverageanalysis', 'tool_unittest')).'

'; -} else { - echo '

'; print_string('codecoveragedisabled', 'tool_unittest'); echo '

'; -} -echo '

'."Databases:".'

'; -echo '
    '; -foreach ($dbinfos as $i=>$dbinfo) { - $name = $dbinfo['name']; - if ($dbinfo['installed']) { - if (!$dbinfo['configured']) { - $name = "$name (misconfigured)"; // TODO: localise - } - echo '
  • '.html_writer::checkbox('selected['.$i.']', 1, intval(!empty($selected[$i])), $name).'
  • '; - } else { - echo '
  • '."$name: driver not installed".'
  • '; // TODO: localise - } -} -echo '
'; -echo '

External databases are configured in config.php, add lines:

-
-$CFG->func_test_db_1 = array("native", "pgsql", "localhost", "moodleuser", "password", "moodle", "test", null);
-$CFG->func_test_db_2 = array("native", "mssql", "localhost", "moodleuser", "password", "moodle", "test", null);
-
-

where order of parameters is: dblibrary, dbtype, dbhost, dbuser, dbpass, dbname, prefix, dboptions

'; -echo '

'; -echo '
'; -echo '
'; -echo $OUTPUT->box_end(); - -// Print link to latest code coverage for this report type -if (!data_submitted() || !$codecoverage) { - moodle_coverage_reporter::print_link_to_latest('dbtest'); -} - -// Footer. -echo $OUTPUT->footer(); diff --git a/admin/tool/unittest/settings.php b/admin/tool/unittest/settings.php index 9543d86440d..ee3406a7e87 100644 --- a/admin/tool/unittest/settings.php +++ b/admin/tool/unittest/settings.php @@ -27,4 +27,3 @@ defined('MOODLE_INTERNAL') || die; $ADMIN->add('development', new admin_externalpage('toolsimpletest', get_string('pluginname', 'tool_unittest'), "$CFG->wwwroot/$CFG->admin/tool/unittest/index.php", 'tool/unittest:execute', true)); -$ADMIN->add('development', new admin_externalpage('tooldbtest', get_string('dbtest', 'tool_unittest'), "$CFG->wwwroot/$CFG->admin/tool/unittest/dbtest.php", 'tool/unittest:execute', true)); diff --git a/lib/ddl/simpletest/fixtures/invalid.xml b/lib/ddl/simpletest/fixtures/invalid.xml deleted file mode 100644 index 36880563a31..00000000000 --- a/lib/ddl/simpletest/fixtures/invalid.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - -
-
-
diff --git a/lib/ddl/simpletest/fixtures/xmldb_table.xml b/lib/ddl/simpletest/fixtures/xmldb_table.xml deleted file mode 100644 index 90cb13768a2..00000000000 --- a/lib/ddl/simpletest/fixtures/xmldb_table.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - -
-
-
diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php deleted file mode 100644 index 40a0d3d14cb..00000000000 --- a/lib/ddl/simpletest/testddl.php +++ /dev/null @@ -1,1864 +0,0 @@ -libdir . '/adminlib.php'); - -class ddl_test extends UnitTestCase { - private $tables = array(); - private $records= array(); - /** @var moodle_database */ - private $tdb; - public static $includecoverage = array('lib/ddl'); - public static $excludecoverage = array('lib/ddl/simpletest'); - - public function setUp() { - global $CFG, $DB, $UNITTEST; - - if (isset($UNITTEST->func_test_db)) { - $this->tdb = $UNITTEST->func_test_db; - } else { - $this->tdb = $DB; - } - - unset($CFG->xmldbreconstructprevnext); // remove this unhack ;-) - - $dbman = $this->tdb->get_manager(); - - $table = new xmldb_table('test_table0'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('type', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null); - $table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null); - $table->add_field('logo', XMLDB_TYPE_BINARY, 'big', null, null, null); - $table->add_field('assessed', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('assesstimestart', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('scale', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); - $table->add_field('maxbytes', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('forcesubscribe', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('trackingtype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '1'); - $table->add_field('rsstype', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('rssarticles', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('grade', XMLDB_TYPE_NUMBER, '20,0', XMLDB_UNSIGNED, null, null, null); - $table->add_field('percent', XMLDB_TYPE_NUMBER, '5,2', null, null, null, 66.6); - $table->add_field('warnafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('blockafter', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('blockperiod', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_key('course', XMLDB_KEY_UNIQUE, array('course')); - $table->add_index('type-name', XMLDB_INDEX_UNIQUE, array('type', 'name')); - $table->add_index('rsstype', XMLDB_INDEX_NOTUNIQUE, array('rsstype')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - // Define 2 initial records for this table - $this->records[$table->getName()] = array( - (object)array( - 'course' => '1', - 'type' => 'general', - 'name' => 'record', - 'intro' => 'first record'), - (object)array( - 'course' => '2', - 'type' => 'social', - 'name' => 'record', - 'intro' => 'second record')); - - // Second, smaller table - $table = new xmldb_table ('test_table1'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '30', null, null, null, 'Moodle'); - $table->add_field('secondname', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null); - $table->add_field('thirdname', XMLDB_TYPE_CHAR, '30', null, null, null, ''); // nullable column with empty default - $table->add_field('intro', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null); - $table->add_field('avatar', XMLDB_TYPE_BINARY, 'medium', null, null, null, null); - $table->add_field('grade', XMLDB_TYPE_NUMBER, '20,10', null, null, null); - $table->add_field('gradefloat', XMLDB_TYPE_FLOAT, '20,0', XMLDB_UNSIGNED, null, null, null); - $table->add_field('percentfloat', XMLDB_TYPE_FLOAT, '5,2', null, null, null, 99.9); - $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_key('course', XMLDB_KEY_FOREIGN_UNIQUE, array('course'), 'test_table0', array('course')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - // Define 2 initial records for this table - $this->records[$table->getName()] = array( - (object)array( - 'course' => '1', - 'secondname' => 'first record', // > 10 cc, please don't modify. Some tests below depend of this - 'intro' => 'first record'), - (object)array( - 'course' => '2', - 'secondname' => 'second record', // > 10 cc, please don't modify. Some tests below depend of this - 'intro' => 'second record')); - - // make sure no tables are present! - $this->tearDown(); - } - - public function tearDown() { - $dbman = $this->tdb->get_manager(); - - // drop custom test tables - for ($i=0; $i<3; $i++) { - $table = new xmldb_table('test_table_cust'.$i); - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - } - - // drop default tables - foreach ($this->tables as $table) { - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - } - } - - private function create_deftable($tablename) { - $dbman = $this->tdb->get_manager(); - - if (!isset($this->tables[$tablename])) { - return null; - } - - $table = $this->tables[$tablename]; - - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $dbman->create_table($table); - - return $table; - } - - /** - * Fill the given test table with some records, as far as - * DDL behaviour must be tested both with real data and - * with empty tables - */ - private function fill_deftable($tablename) { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - if (!isset($this->records[$tablename])) { - return null; - } - - if ($dbman->table_exists($tablename)) { - foreach ($this->records[$tablename] as $row) { - $DB->insert_record($tablename, $row); - } - } else { - return null; - } - - return count($this->records[$tablename]); - } - - /** - * Test behaviour of table_exists() - */ - public function test_table_exists() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // first make sure it returns false if table does not exist - $table = $this->tables['test_table0']; - - ob_start(); // hide debug warning - try { - $result = $DB->get_records('test_table0'); - } catch (dml_exception $e) { - $result = false; - } - ob_end_clean(); - - $this->assertFalse($result); - - $this->assertFalse($dbman->table_exists('test_table0')); // by name - $this->assertFalse($dbman->table_exists($table)); // by xmldb_table - - // create table and test again - $dbman->create_table($table); - - $this->assertTrue($DB->get_records('test_table0') === array()); - $this->assertTrue($dbman->table_exists('test_table0')); // by name - $this->assertTrue($dbman->table_exists($table)); // by xmldb_table - - // drop table and test again - $dbman->drop_table($table); - - ob_start(); // hide debug warning - try { - $result = $DB->get_records('test_table0'); - } catch (dml_exception $e) { - $result = false; - } - ob_end_clean(); - - $this->assertFalse($result); - - $this->assertFalse($dbman->table_exists('test_table0')); // by name - $this->assertFalse($dbman->table_exists($table)); // by xmldb_table - } - - /** - * Test behaviour of create_table() - */ - public function test_create_table() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // create table - $table = $this->tables['test_table1']; - - $dbman->create_table($table); - $this->assertTrue($dbman->table_exists($table)); - - // basic get_tables() test - $tables = $DB->get_tables(); - $this->assertTrue(array_key_exists('test_table1', $tables)); - - // basic get_columns() tests - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['id']->meta_type, 'R'); - $this->assertEqual($columns['course']->meta_type, 'I'); - $this->assertEqual($columns['name']->meta_type, 'C'); - $this->assertEqual($columns['secondname']->meta_type, 'C'); - $this->assertEqual($columns['thirdname']->meta_type, 'C'); - $this->assertEqual($columns['intro']->meta_type, 'X'); - $this->assertEqual($columns['avatar']->meta_type, 'B'); - $this->assertEqual($columns['grade']->meta_type, 'N'); - $this->assertEqual($columns['percentfloat']->meta_type, 'N'); - $this->assertEqual($columns['userid']->meta_type, 'I'); - // some defaults - $this->assertTrue($columns['course']->has_default); - $this->assertEqual($columns['course']->default_value, 0); - $this->assertTrue($columns['name']->has_default); - $this->assertEqual($columns['name']->default_value, 'Moodle'); - $this->assertTrue($columns['secondname']->has_default); - $this->assertEqual($columns['secondname']->default_value, ''); - $this->assertTrue($columns['thirdname']->has_default); - $this->assertEqual($columns['thirdname']->default_value, ''); - $this->assertTrue($columns['percentfloat']->has_default); - $this->assertEqual($columns['percentfloat']->default_value, 99.9); - $this->assertTrue($columns['userid']->has_default); - $this->assertEqual($columns['userid']->default_value, 0); - - // basic get_indexes() test - $indexes = $DB->get_indexes('test_table1'); - $courseindex = reset($indexes); - $this->assertEqual($courseindex['unique'], 1); - $this->assertEqual($courseindex['columns'][0], 'course'); - - // check sequence returns 1 for first insert - $rec = (object)array( - 'course' => 10, - 'secondname' => 'not important', - 'intro' => 'not important'); - $this->assertIdentical($DB->insert_record('test_table1', $rec), 1); - - // check defined defaults are working ok - $dbrec = $DB->get_record('test_table1', array('id' => 1)); - $this->assertEqual($dbrec->name, 'Moodle'); - $this->assertEqual($dbrec->thirdname, ''); - - // check exceptions if multiple R columns - $table = new xmldb_table ('test_table2'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('rid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_key('primaryx', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_exception); - } - - // check exceptions missing primary key on R column - $table = new xmldb_table ('test_table2'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_exception); - } - - // long table name names - the largest allowed - $table = new xmldb_table('abcdef____0123456789_____xyz'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - $dbman->create_table($table); - $this->assertTrue($dbman->table_exists($table)); - $dbman->drop_table($table); - - // table name is too long - $table = new xmldb_table('abcdef____0123456789_____xyz9'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // invalid table name - $table = new xmldb_table('abCD'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - - // weird column names - the largest allowed - $table = new xmldb_table('test_table3'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('abcdef____0123456789_______xyz', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - $dbman->create_table($table); - $this->assertTrue($dbman->table_exists($table)); - $dbman->drop_table($table); - - // Too long field name - max 30 - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('abcdeabcdeabcdeabcdeabcdeabcdez', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid field name - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('abCD', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid integer length - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '21', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid integer default - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid decimal length - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('num', XMLDB_TYPE_NUMBER, '21,10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid decimal decimals - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('num', XMLDB_TYPE_NUMBER, '10,11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid decimal default - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('num', XMLDB_TYPE_NUMBER, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid float length - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('num', XMLDB_TYPE_FLOAT, '21,10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid float decimals - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('num', XMLDB_TYPE_FLOAT, '10,11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - // Invalid float default - $table = new xmldb_table('test_table4'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('num', XMLDB_TYPE_FLOAT, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->setComment("This is a test'n drop table. You can drop it safely"); - - $this->tables[$table->getName()] = $table; - - try { - $dbman->create_table($table); - $this->fail('Exception expected'); - } catch (Exception $e) { - $this->assertIdentical(get_class($e), 'coding_exception'); - } - - } - - /** - * Test behaviour of drop_table() - */ - public function test_drop_table() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // initially table doesn't exist - $this->assertFalse($dbman->table_exists('test_table0')); - - // create table with contents - $table = $this->create_deftable('test_table0'); - $this->assertTrue($dbman->table_exists('test_table0')); - - // fill the table with some records before dropping it - $this->fill_deftable('test_table0'); - - // drop by xmldb_table object - $dbman->drop_table($table); - $this->assertFalse($dbman->table_exists('test_table0')); - - // basic get_tables() test - $tables = $DB->get_tables(); - $this->assertFalse(array_key_exists('test_table0', $tables)); - - try { // columns cache must be empty, so sentence throw exception - $columns = $DB->get_columns('test_table0'); - } catch (dml_exception $e) { - $columns = false; - } - $this->assertFalse($columns); - - try { /// throw exception - $indexes = $DB->get_indexes('test_table0'); - } catch (dml_exception $e) { - $indexes = false; - } - $this->assertFalse($indexes); - } - - /** - * Test behaviour of rename_table() - */ - public function test_rename_table() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - - // fill the table with some records before renaming it - $insertedrows = $this->fill_deftable('test_table1'); - - $this->assertFalse($dbman->table_exists('test_table_cust1')); - $dbman->rename_table($table, 'test_table_cust1'); - $this->assertTrue($dbman->table_exists('test_table_cust1')); - - // check sequence returns $insertedrows + 1 for this insert (after rename) - $rec = (object)array( - 'course' => 20, - 'secondname' => 'not important', - 'intro' => 'not important'); - $this->assertIdentical($DB->insert_record('test_table_cust1', $rec), $insertedrows + 1); - } - - /** - * Test behaviour of field_exists() - */ - public function test_field_exists() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table0'); - - // String params - // Give a nonexistent table as first param (throw exception) - try { - $dbman->field_exists('nonexistenttable', 'id'); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof moodle_exception); - } - - // Give a nonexistent field as second param (return false) - $this->assertFalse($dbman->field_exists('test_table0', 'nonexistentfield')); - - // Correct string params - $this->assertTrue($dbman->field_exists('test_table0', 'id')); - - // Object params - $realfield = $table->getField('id'); - - // Give a nonexistent table as first param (throw exception) - $nonexistenttable = new xmldb_table('nonexistenttable'); - try { - $dbman->field_exists($nonexistenttable, $realfield); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof moodle_exception); - } - - // Give a nonexistent field as second param (return false) - $nonexistentfield = new xmldb_field('nonexistentfield'); - $this->assertFalse($dbman->field_exists($table, $nonexistentfield)); - - // Correct object params - $this->assertTrue($dbman->field_exists($table, $realfield)); - - // Mix string and object params - // Correct ones - $this->assertTrue($dbman->field_exists($table, 'id')); - $this->assertTrue($dbman->field_exists('test_table0', $realfield)); - // Non existing tables (throw exception) - try { - $this->assertFalse($dbman->field_exists($nonexistenttable, 'id')); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof moodle_exception); - } - try { - $this->assertFalse($dbman->field_exists('nonexistenttable', $realfield)); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof moodle_exception); - } - // Non existing fields (return false) - $this->assertFalse($dbman->field_exists($table, 'nonexistentfield')); - $this->assertFalse($dbman->field_exists('test_table0', $nonexistentfield)); - } - - /** - * Test behaviour of add_field() - */ - public function test_add_field() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - - // fill the table with some records before adding fields - $this->fill_deftable('test_table1'); - - /// add one not null field without specifying default value (throws ddl_exception) - $field = new xmldb_field('onefield'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - try { - $dbman->add_field($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_exception); - } - - /// add one existing field (throws ddl_exception) - $field = new xmldb_field('course'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 2); - try { - $dbman->add_field($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_exception); - } - - // TODO: add one field with invalid type, must throw exception - // TODO: add one text field with default, must throw exception - // TODO: add one binary field with default, must throw exception - - /// add one integer field and check it - $field = new xmldb_field('oneinteger'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 2); - $dbman->add_field($table, $field); - $this->assertTrue($dbman->field_exists($table, 'oneinteger')); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['oneinteger']->name ,'oneinteger'); - $this->assertEqual($columns['oneinteger']->not_null , true); - // max_length and scale cannot be checked under all DBs at all for integer fields - $this->assertEqual($columns['oneinteger']->primary_key , false); - $this->assertEqual($columns['oneinteger']->binary , false); - $this->assertEqual($columns['oneinteger']->has_default , true); - $this->assertEqual($columns['oneinteger']->default_value, 2); - $this->assertEqual($columns['oneinteger']->meta_type ,'I'); - $this->assertEqual($DB->get_field('test_table1', 'oneinteger', array(), IGNORE_MULTIPLE), 2); //check default has been applied - - /// add one numeric field and check it - $field = new xmldb_field('onenumber'); - $field->set_attributes(XMLDB_TYPE_NUMBER, '6,3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 2.55); - $dbman->add_field($table, $field); - $this->assertTrue($dbman->field_exists($table, 'onenumber')); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['onenumber']->name ,'onenumber'); - $this->assertEqual($columns['onenumber']->max_length , 6); - $this->assertEqual($columns['onenumber']->scale , 3); - $this->assertEqual($columns['onenumber']->not_null , true); - $this->assertEqual($columns['onenumber']->primary_key , false); - $this->assertEqual($columns['onenumber']->binary , false); - $this->assertEqual($columns['onenumber']->has_default , true); - $this->assertEqual($columns['onenumber']->default_value, 2.550); - $this->assertEqual($columns['onenumber']->meta_type ,'N'); - $this->assertEqual($DB->get_field('test_table1', 'onenumber', array(), IGNORE_MULTIPLE), 2.550); //check default has been applied - - /// add one float field and check it (not official type - must work as number) - $field = new xmldb_field('onefloat'); - $field->set_attributes(XMLDB_TYPE_FLOAT, '6,3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 3.550); - $dbman->add_field($table, $field); - $this->assertTrue($dbman->field_exists($table, 'onefloat')); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['onefloat']->name ,'onefloat'); - $this->assertEqual($columns['onefloat']->not_null , true); - // max_length and scale cannot be checked under all DBs at all for float fields - $this->assertEqual($columns['onefloat']->primary_key , false); - $this->assertEqual($columns['onefloat']->binary , false); - $this->assertEqual($columns['onefloat']->has_default , true); - $this->assertEqual($columns['onefloat']->default_value, 3.550); - $this->assertEqual($columns['onefloat']->meta_type ,'N'); - // Just rounding DB information to 7 decimal digits. Fair enough to test 3.550 and avoids one nasty bug - // in MSSQL core returning wrong floats (http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/5e08de63-16bb-4f24-b645-0cf8fc669de3) - // In any case, floats aren't officially supported by Moodle, with number/decimal type being the correct ones, so - // this isn't a real problem at all. - $this->assertEqual(round($DB->get_field('test_table1', 'onefloat', array(), IGNORE_MULTIPLE), 7), 3.550); //check default has been applied - - /// add one char field and check it - $field = new xmldb_field('onechar'); - $field->set_attributes(XMLDB_TYPE_CHAR, '25', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'Nice dflt!'); - $dbman->add_field($table, $field); - $this->assertTrue($dbman->field_exists($table, 'onechar')); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['onechar']->name ,'onechar'); - $this->assertEqual($columns['onechar']->max_length , 25); - $this->assertEqual($columns['onechar']->scale , null); - $this->assertEqual($columns['onechar']->not_null , true); - $this->assertEqual($columns['onechar']->primary_key , false); - $this->assertEqual($columns['onechar']->binary , false); - $this->assertEqual($columns['onechar']->has_default , true); - $this->assertEqual($columns['onechar']->default_value,'Nice dflt!'); - $this->assertEqual($columns['onechar']->meta_type ,'C'); - $this->assertEqual($DB->get_field('test_table1', 'onechar', array(), IGNORE_MULTIPLE), 'Nice dflt!'); //check default has been applied - - /// add one big text field and check it - $field = new xmldb_field('onetext'); - $field->set_attributes(XMLDB_TYPE_TEXT, 'big'); - $dbman->add_field($table, $field); - $this->assertTrue($dbman->field_exists($table, 'onetext')); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['onetext']->name ,'onetext'); - $this->assertEqual($columns['onetext']->max_length , -1); // -1 means unknown or big - $this->assertEqual($columns['onetext']->scale , null); - $this->assertEqual($columns['onetext']->not_null , false); - $this->assertEqual($columns['onetext']->primary_key , false); - $this->assertEqual($columns['onetext']->binary , false); - $this->assertEqual($columns['onetext']->has_default , false); - $this->assertEqual($columns['onetext']->default_value, null); - $this->assertEqual($columns['onetext']->meta_type ,'X'); - - /// add one medium text field and check it - $field = new xmldb_field('mediumtext'); - $field->set_attributes(XMLDB_TYPE_TEXT, 'medium'); - $dbman->add_field($table, $field); - $columns = $DB->get_columns('test_table1'); - $this->assertTrue(($columns['mediumtext']->max_length == -1) or ($columns['mediumtext']->max_length >= 16777215)); // -1 means unknown or big - - /// add one small text field and check it - $field = new xmldb_field('smalltext'); - $field->set_attributes(XMLDB_TYPE_TEXT, 'small'); - $dbman->add_field($table, $field); - $columns = $DB->get_columns('test_table1'); - $this->assertTrue(($columns['smalltext']->max_length == -1) or ($columns['smalltext']->max_length >= 65535)); // -1 means unknown or big - - /// add one binary field and check it - $field = new xmldb_field('onebinary'); - $field->set_attributes(XMLDB_TYPE_BINARY); - $dbman->add_field($table, $field); - $this->assertTrue($dbman->field_exists($table, 'onebinary')); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['onebinary']->name ,'onebinary'); - $this->assertEqual($columns['onebinary']->max_length , -1); - $this->assertEqual($columns['onebinary']->scale , null); - $this->assertEqual($columns['onebinary']->not_null , false); - $this->assertEqual($columns['onebinary']->primary_key , false); - $this->assertEqual($columns['onebinary']->binary , true); - $this->assertEqual($columns['onebinary']->has_default , false); - $this->assertEqual($columns['onebinary']->default_value, null); - $this->assertEqual($columns['onebinary']->meta_type ,'B'); - - // TODO: check datetime type. Although unused should be fully supported. - } - - /** - * Test behaviour of drop_field() - */ - public function test_drop_field() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table0'); - - // fill the table with some records before dropping fields - $this->fill_deftable('test_table0'); - - // drop field with simple xmldb_field having indexes, must return exception - $field = new xmldb_field('type'); // Field has indexes and default clause - $this->assertTrue($dbman->field_exists($table, 'type')); - try { - $dbman->drop_field($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_dependency_exception); - } - $this->assertTrue($dbman->field_exists($table, 'type')); // continues existing, drop aborted - - // drop field with complete xmldb_field object and related indexes, must return exception - $field = $table->getField('course'); // Field has indexes and default clause - $this->assertTrue($dbman->field_exists($table, $field)); - try { - $dbman->drop_field($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_dependency_exception); - } - $this->assertTrue($dbman->field_exists($table, $field)); // continues existing, drop aborted - - // drop one non-existing field, must return exception - $field = new xmldb_field('nonexistingfield'); - $this->assertFalse($dbman->field_exists($table, $field)); - try { - $dbman->drop_field($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_field_missing_exception); - } - - // drop field with simple xmldb_field, not having related indexes - $field = new xmldb_field('forcesubscribe'); // Field has default clause - $this->assertTrue($dbman->field_exists($table, 'forcesubscribe')); - $dbman->drop_field($table, $field); - $this->assertFalse($dbman->field_exists($table, 'forcesubscribe')); - - - // drop field with complete xmldb_field object, not having related indexes - $field = new xmldb_field('trackingtype'); // Field has default clause - $this->assertTrue($dbman->field_exists($table, $field)); - $dbman->drop_field($table, $field); - $this->assertFalse($dbman->field_exists($table, $field)); - } - - /** - * Test behaviour of change_field_type() - */ - public function test_change_field_type() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // create table with indexed field and not indexed field to - // perform tests in both fields, both having defaults - $table = new xmldb_table('test_table_cust0'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2'); - $table->add_field('anothernumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '4'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('onenumber', XMLDB_INDEX_NOTUNIQUE, array('onenumber')); - $dbman->create_table($table); - - $record = new stdClass(); - $record->onenumber = 2; - $record->anothernumber = 4; - $recoriginal = $DB->insert_record('test_table_cust0', $record); - - // change column from integer to varchar. Must return exception because of dependent index - $field = new xmldb_field('onenumber'); - $field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'test'); - try { - $dbman->change_field_type($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_dependency_exception); - } - // column continues being integer 10 not null default 2 - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['onenumber']->meta_type, 'I'); - //TODO: check the rest of attributes - - // change column from integer to varchar. Must work because column has no dependencies - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'test'); - $dbman->change_field_type($table, $field); - // column is char 30 not null default 'test' now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'C'); - //TODO: check the rest of attributes - - // change column back from char to integer - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '8', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '5'); - $dbman->change_field_type($table, $field); - // column is integer 8 not null default 5 now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'I'); - //TODO: check the rest of attributes - - // change column once more from integer to char - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, "test'n drop"); - $dbman->change_field_type($table, $field); - // column is char 30 not null default "test'n drop" now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'C'); - //TODO: check the rest of attributes - - // insert one string value and try to convert to integer. Must throw exception - $record = new stdClass(); - $record->onenumber = 7; - $record->anothernumber = 'string value'; - $rectodrop = $DB->insert_record('test_table_cust0', $record); - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '5'); - try { - $dbman->change_field_type($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_change_structure_exception); - } - // column continues being char 30 not null default "test'n drop" now - $this->assertEqual($columns['anothernumber']->meta_type, 'C'); - //TODO: check the rest of attributes - $DB->delete_records('test_table_cust0', array('id' => $rectodrop)); // Delete the string record - - // change the column from varchar to float - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_FLOAT, '20,10', XMLDB_UNSIGNED, null, null, null); - $dbman->change_field_type($table, $field); - // column is float 20,10 null default null - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'N'); // floats are seen as number - //TODO: check the rest of attributes - - // change the column back from float to varchar - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'test'); - $dbman->change_field_type($table, $field); - // column is char 20 not null default "test" now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'C'); - //TODO: check the rest of attributes - - // change the column from varchar to number - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null); - $dbman->change_field_type($table, $field); - // column is number 20,10 null default null now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'N'); - //TODO: check the rest of attributes - - // change the column from number to integer - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, null); - $dbman->change_field_type($table, $field); - // column is integer 2 null default null now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'I'); - //TODO: check the rest of attributes - - // change the column from integer to text - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null); - $dbman->change_field_type($table, $field); - // column is char text not null default null - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'X'); - - // change the column back from text to number - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_NUMBER, '20,10', XMLDB_UNSIGNED, null, null, null); - $dbman->change_field_type($table, $field); - // column is number 20,10 null default null now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'N'); - //TODO: check the rest of attributes - - // change the column from number to text - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null); - $dbman->change_field_type($table, $field); - // column is char text not null default "test" now - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'X'); - //TODO: check the rest of attributes - - // change the column back from text to integer - $field = new xmldb_field('anothernumber'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 10); - $dbman->change_field_type($table, $field); - // column is integer 10 not null default 10 - $columns = $DB->get_columns('test_table_cust0'); - $this->assertEqual($columns['anothernumber']->meta_type, 'I'); - //TODO: check the rest of attributes - - // check original value has survived to all the type changes - $this->assertTrue($rec = $DB->get_record('test_table_cust0', array('id' => $recoriginal))); - $this->assertEqual($rec->anothernumber, 4); - - $dbman->drop_table($table); - $this->assertFalse($dbman->table_exists($table)); - } - - /** - * Test behaviour of test_change_field_precision() - */ - public function test_change_field_precision() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - - // fill the table with some records before dropping fields - $this->fill_deftable('test_table1'); - - // change text field from medium to big - $field = new xmldb_field('intro'); - $field->set_attributes(XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null); - $dbman->change_field_precision($table, $field); - $columns = $DB->get_columns('test_table1'); - // cannot check the text type, only the metatype - $this->assertEqual($columns['intro']->meta_type, 'X'); - //TODO: check the rest of attributes - - // change char field from 30 to 20 - $field = new xmldb_field('secondname'); - $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null); - $dbman->change_field_precision($table, $field); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['secondname']->meta_type, 'C'); - //TODO: check the rest of attributes - - // change char field from 20 to 10, having contents > 10cc. Throw exception - $field = new xmldb_field('secondname'); - $field->set_attributes(XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, null); - try { - $dbman->change_field_precision($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_change_structure_exception); - } - // No changes in field specs at all - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['secondname']->meta_type, 'C'); - //TODO: check the rest of attributes - - // change number field from 20,10 to 10,2 - $field = new xmldb_field('grade'); - $field->set_attributes(XMLDB_TYPE_NUMBER, '10,2', null, null, null, null); - $dbman->change_field_precision($table, $field); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['grade']->meta_type, 'N'); - //TODO: check the rest of attributes - - // change integer field from 10 to 2 - $field = new xmldb_field('userid'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $dbman->change_field_precision($table, $field); - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['userid']->meta_type, 'I'); - //TODO: check the rest of attributes - - // change the column from integer (2) to integer (6) (forces change of type in some DBs) - $field = new xmldb_field('userid'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '6', XMLDB_UNSIGNED, null, null, null); - $dbman->change_field_precision($table, $field); - // column is integer 6 null default null now - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['userid']->meta_type, 'I'); - //TODO: check the rest of attributes - - // insert one record with 6-digit field - $record = new stdClass(); - $record->course = 10; - $record->secondname = 'third record'; - $record->intro = 'third record'; - $record->userid = 123456; - $DB->insert_record('test_table1', $record); - // change integer field from 6 to 2, contents are bigger. must throw exception - $field = new xmldb_field('userid'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - try { - $dbman->change_field_precision($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_change_structure_exception); - } - // No changes in field specs at all - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['userid']->meta_type, 'I'); - //TODO: check the rest of attributes - - // change integer field from 10 to 3, in field used by index. must throw exception. - $field = new xmldb_field('course'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - try { - $dbman->change_field_precision($table, $field); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_dependency_exception); - } - // No changes in field specs at all - $columns = $DB->get_columns('test_table1'); - $this->assertEqual($columns['course']->meta_type, 'I'); - //TODO: check the rest of attributes - } - - public function testChangeFieldNullability() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = new xmldb_table('test_table_cust0'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $record = new stdClass(); - $record->name = NULL; - - ob_start(); // hide debug warning - try { - $result = $DB->insert_record('test_table_cust0', $record, false); - } catch (dml_exception $e) { - $result = false; - } - ob_end_clean(); - $this->assertFalse($result); - - $field = new xmldb_field('name'); - $field->set_attributes(XMLDB_TYPE_CHAR, '30', null, null, null, null); - $dbman->change_field_notnull($table, $field); - - $this->assertTrue($DB->insert_record('test_table_cust0', $record, false)); - - // TODO: add some tests with existing data in table - $DB->delete_records('test_table_cust0'); - - $field = new xmldb_field('name'); - $field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null); - $dbman->change_field_notnull($table, $field); - - ob_start(); // hide debug warning - try { - $result = $DB->insert_record('test_table_cust0', $record, false); - } catch (dml_exception $e) { - $result = false; - } - ob_end_clean(); - $this->assertFalse($result); - - $dbman->drop_table($table); - } - - public function testChangeFieldDefault() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = new xmldb_table('test_table_cust0'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $field = new xmldb_field('name'); - $field->set_attributes(XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle2'); - $dbman->change_field_default($table, $field); - - $record = new stdClass(); - $record->onenumber = 666; - $id = $DB->insert_record('test_table_cust0', $record); - - $record = $DB->get_record('test_table_cust0', array('id'=>$id)); - $this->assertEqual($record->name, 'Moodle2'); - - - $field = new xmldb_field('onenumber'); - $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 666); - $dbman->change_field_default($table, $field); - - $record = new stdClass(); - $record->name = 'something'; - $id = $DB->insert_record('test_table_cust0', $record); - - $record = $DB->get_record('test_table_cust0', array('id'=>$id)); - $this->assertEqual($record->onenumber, '666'); - - $dbman->drop_table($table); - } - - public function testAddUniqueIndex() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = new xmldb_table('test_table_cust0'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('onenumber', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, 'Moodle'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $record = new stdClass(); - $record->onenumber = 666; - $record->name = 'something'; - $DB->insert_record('test_table_cust0', $record, false); - - $index = new xmldb_index('onenumber-name'); - $index->set_attributes(XMLDB_INDEX_UNIQUE, array('onenumber', 'name')); - $dbman->add_index($table, $index); - - ob_start(); // hide debug warning - try { - $result = $DB->insert_record('test_table_cust0', $record, false); - } catch (dml_exception $e) { - $result = false;; - } - ob_end_clean(); - $this->assertFalse($result); - - $dbman->drop_table($table); - } - - public function testAddNonUniqueIndex() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - $index = new xmldb_index('secondname'); - $index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name')); - $dbman->add_index($table, $index); - } - - public function testFindIndexName() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - $index = new xmldb_index('secondname'); - $index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name')); - $dbman->add_index($table, $index); - - //DBM Systems name their indices differently - do not test the actual index name - $result = $dbman->find_index_name($table, $index); - $this->assertTrue(!empty($result)); - - $nonexistentindex = new xmldb_index('nonexistentindex'); - $nonexistentindex->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('name')); - $this->assertFalse($dbman->find_index_name($table, $nonexistentindex)); - } - - public function testDropIndex() { - $DB = $this->tdb; // do not use global $DB! - - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - $index = new xmldb_index('secondname'); - $index->set_attributes(XMLDB_INDEX_NOTUNIQUE, array('course', 'name')); - $dbman->add_index($table, $index); - - $dbman->drop_index($table, $index); - $this->assertFalse($dbman->find_index_name($table, $index)); - - // Test we are able to drop indexes having hyphens. MDL-22804 - // Create index with hyphens (by hand) - $indexname = 'test-index-with-hyphens'; - switch ($DB->get_dbfamily()) { - case 'mysql': - $indexname = '`' . $indexname . '`'; - break; - default: - $indexname = '"' . $indexname . '"'; - } - $stmt = "CREATE INDEX {$indexname} ON {$DB->get_prefix()}test_table1 (course, name)"; - $DB->change_database_structure($stmt); - $this->assertTrue($dbman->find_index_name($table, $index)); - // Index created, let's drop it using db manager stuff - $index = new xmldb_index('indexname', XMLDB_INDEX_NOTUNIQUE, array('course', 'name')); - $dbman->drop_index($table, $index); - $this->assertFalse($dbman->find_index_name($table, $index)); - } - - public function testAddUniqueKey() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - $key = new xmldb_key('id-course-grade'); - $key->set_attributes(XMLDB_KEY_UNIQUE, array('id', 'course', 'grade')); - $dbman->add_key($table, $key); - } - - public function testAddForeignUniqueKey() { - $dbman = $this->tdb->get_manager(); - - $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'), 'test_table0', array('id')); - $dbman->add_key($table, $key); - } - - public function testDropKey() { - $dbman = $this->tdb->get_manager(); - - $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'), 'test_table0', array('id')); - $dbman->add_key($table, $key); - - $dbman->drop_key($table, $key); - } - - public function testAddForeignKey() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - $this->create_deftable('test_table0'); - - $key = new xmldb_key('course'); - $key->set_attributes(XMLDB_KEY_FOREIGN, array('course'), 'test_table0', array('id')); - $dbman->add_key($table, $key); - } - - public function testDropForeignKey() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table1'); - $this->create_deftable('test_table0'); - - $key = new xmldb_key('course'); - $key->set_attributes(XMLDB_KEY_FOREIGN, array('course'), 'test_table0', array('id')); - $dbman->add_key($table, $key); - - $dbman->drop_key($table, $key); - } - - public function testRenameField() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table0'); - $field = new xmldb_field('type'); - $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course'); - - $dbman->rename_field($table, $field, 'newfieldname'); - - $columns = $DB->get_columns('test_table0'); - - $this->assertFalse(array_key_exists('type', $columns)); - $this->assertTrue(array_key_exists('newfieldname', $columns)); - } - - - public function testIndexExists() { - // Skipping: this is just a test of find_index_name - } - - public function testFindKeyName() { - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table0'); - $key = $table->getKey('primary'); - - // With Mysql, the return value is actually "mdl_test_id_pk" - $result = $dbman->find_key_name($table, $key); - $this->assertTrue(!empty($result)); - } - - public function testDeleteTablesFromXmldbFile() { - global $CFG; - $dbman = $this->tdb->get_manager(); - - $this->create_deftable('test_table1'); - - $this->assertTrue($dbman->table_exists('test_table1')); - - // feed nonexistent file - try { - ob_start(); // hide debug warning - $dbman->delete_tables_from_xmldb_file('fpsoiudfposui'); - ob_end_clean(); - $this->assertTrue(false); - } catch (Exception $e) { - ob_end_clean(); - $this->assertTrue($e instanceof moodle_exception); - } - - // Real file but invalid xml file - $devhack = false; - if (!empty($CFG->xmldbdisablenextprevchecking)) { - $CFG->xmldbdisablenextprevchecking = false; - $devhack = true; - } - try { - ob_start(); // hide debug warning - $dbman->delete_tables_from_xmldb_file($CFG->libdir . '/ddl/simpletest/fixtures/invalid.xml'); - $this->assertTrue(false); - ob_end_clean(); - } catch (Exception $e) { - ob_end_clean(); - $this->assertTrue($e instanceof moodle_exception); - } - if ($devhack) { - $CFG->xmldbdisablenextprevchecking = true; - } - - // Check that the table has not been deleted from DB - $this->assertTrue($dbman->table_exists('test_table1')); - - // Real and valid xml file - //TODO: drop UNSINGED completely in Moodle 2.4 - $dbman->delete_tables_from_xmldb_file($CFG->libdir . '/ddl/simpletest/fixtures/xmldb_table.xml'); - - // Check that the table has been deleted from DB - $this->assertFalse($dbman->table_exists('test_table1')); - } - - public function testInstallFromXmldbFile() { - global $CFG; - $dbman = $this->tdb->get_manager(); - - // feed nonexistent file - try { - ob_start(); // hide debug warning - $dbman->install_from_xmldb_file('fpsoiudfposui'); - ob_end_clean(); - $this->assertTrue(false); - } catch (Exception $e) { - ob_end_clean(); - $this->assertTrue($e instanceof moodle_exception); - } - - // Real but invalid xml file - $devhack = false; - if (!empty($CFG->xmldbdisablenextprevchecking)) { - $CFG->xmldbdisablenextprevchecking = false; - $devhack = true; - } - try { - ob_start(); // hide debug warning - $dbman->install_from_xmldb_file($CFG->libdir.'/ddl/simpletest/fixtures/invalid.xml'); - ob_end_clean(); - $this->assertTrue(false); - } catch (Exception $e) { - ob_end_clean(); - $this->assertTrue($e instanceof moodle_exception); - } - if ($devhack) { - $CFG->xmldbdisablenextprevchecking = true; - } - - // Check that the table has not yet been created in DB - $this->assertFalse($dbman->table_exists('test_table1')); - - // Real and valid xml file - $dbman->install_from_xmldb_file($CFG->libdir.'/ddl/simpletest/fixtures/xmldb_table.xml'); - $this->assertTrue($dbman->table_exists('test_table1')); - } - - public function test_temp_tables() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // Create temp table0 - $table0 = $this->tables['test_table0']; - $dbman->create_temp_table($table0); - $this->assertTrue($dbman->table_exists('test_table0')); - - // Try to create temp table with same name, must throw exception - $dupetable = $this->tables['test_table0']; - try { - $dbman->create_temp_table($dupetable); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_exception); - } - - // Try to create table with same name, must throw exception - $dupetable = $this->tables['test_table0']; - try { - $dbman->create_table($dupetable); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_exception); - } - - // Create another temp table1 - $table1 = $this->tables['test_table1']; - $dbman->create_temp_table($table1); - $this->assertTrue($dbman->table_exists('test_table1')); - - // Get columns and perform some basic tests - $columns = $DB->get_columns('test_table1'); - $this->assertEqual(count($columns), 11); - $this->assertTrue($columns['name'] instanceof database_column_info); - $this->assertEqual($columns['name']->max_length, 30); - $this->assertTrue($columns['name']->has_default); - $this->assertEqual($columns['name']->default_value, 'Moodle'); - - // Insert some records - $inserted = $this->fill_deftable('test_table1'); - $records = $DB->get_records('test_table1'); - $this->assertEqual(count($records), $inserted); - $this->assertEqual($records[1]->course, $this->records['test_table1'][0]->course); - $this->assertEqual($records[1]->secondname, $this->records['test_table1'][0]->secondname); - $this->assertEqual($records[2]->intro, $this->records['test_table1'][1]->intro); - - // Drop table1 - $dbman->drop_table($table1); - $this->assertFalse($dbman->table_exists('test_table1')); - - // Try to drop non-existing temp table, must throw exception - $noetable = $this->tables['test_table1']; - try { - $dbman->drop_table($noetable); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof ddl_table_missing_exception); - } - - // Fill/modify/delete a few table0 records - // TODO: that's - - // Drop table0 - $dbman->drop_table($table0); - $this->assertFalse($dbman->table_exists('test_table0')); - - // Have dropped all these temp tables here, to avoid conflicts with other (normal tables) tests! - } - - public function test_concurrent_temp_tables() { - $DB = $this->tdb; // do not use global $DB! - $dbman = $this->tdb->get_manager(); - - // Define 2 records - $record1 = (object)array( - 'course' => 1, - 'secondname' => '11 important', - 'intro' => '111 important'); - $record2 = (object)array( - 'course' => 2, - 'secondname' => '22 important', - 'intro' => '222 important'); - - // Create temp table1 and insert 1 record (in DB) - $table = $this->tables['test_table1']; - $dbman->create_temp_table($table); - $this->assertTrue($dbman->table_exists('test_table1')); - $inserted = $DB->insert_record('test_table1', $record1); - - // Switch to new connection - $cfg = $DB->export_dbconfig(); - if (!isset($cfg->dboptions)) { - $cfg->dboptions = array(); - } - $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); - $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); - $dbman2 = $DB2->get_manager(); - $this->assertFalse($dbman2->table_exists('test_table1')); // Temp table not exists in DB2 - - // Create temp table1 and insert 1 record (in DB2) - $table = $this->tables['test_table1']; - $dbman2->create_temp_table($table); - $this->assertTrue($dbman2->table_exists('test_table1')); - $inserted = $DB2->insert_record('test_table1', $record2); - - $dbman2->drop_table($table); // Drop temp table before closing DB2 - $this->assertFalse($dbman2->table_exists('test_table1')); - $DB2->dispose(); // Close DB2 - - $this->assertTrue($dbman->table_exists('test_table1')); // Check table continues existing for DB - $dbman->drop_table($table); // Drop temp table - $this->assertFalse($dbman->table_exists('test_table1')); - } - - public function test_reset_sequence() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - - // Drop if exists - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $dbman->create_table($table); - $tablename = $table->getName(); - $this->tables[$tablename] = $table; - - $record = (object)array('id'=>666, 'course'=>10); - $DB->import_record('testtable', $record); - $DB->delete_records('testtable'); // This delete performs one TRUNCATE - - $dbman->reset_sequence($table); // using xmldb object - $this->assertEqual(1, $DB->insert_record('testtable', (object)array('course'=>13))); - - $record = (object)array('id'=>666, 'course'=>10); - $DB->import_record('testtable', $record); - $DB->delete_records('testtable', array()); // This delete performs one DELETE - - $dbman->reset_sequence($table); // using xmldb object - $this->assertEqual(1, $DB->insert_record('testtable', (object)array('course'=>13))); - - $DB->import_record('testtable', $record); - $dbman->reset_sequence($tablename); // using string - $this->assertEqual(667, $DB->insert_record('testtable', (object)array('course'=>13))); - - $dbman->drop_table($table); - } - - public function test_reserved_words() { - $reserved = sql_generator::getAllReservedWords(); - $this->assertTrue(count($reserved) > 1); - } - - public function test_index_max_bytes() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $maxstr = ''; - for($i=0; $i<255; $i++) { - $maxstr .= '言'; // random long string that should fix exactly the limit for one char column - } - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, 255, null, XMLDB_NOTNULL, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name')); - - // Drop if exists - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $dbman->create_table($table); - $tablename = $table->getName(); - $this->tables[$tablename] = $table; - - $rec = new stdClass(); - $rec->name = $maxstr; - - $id = $DB->insert_record($tablename, $rec); - $this->assertTrue(!empty($id)); - - $rec = $DB->get_record($tablename, array('id'=>$id)); - $this->assertIdentical($rec->name, $maxstr); - - $dbman->drop_table($table); - - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, 255+1, null, XMLDB_NOTNULL, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('name', XMLDB_INDEX_NOTUNIQUE, array('name')); - - try { - $dbman->create_table($table); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - } - - public function test_index_composed_max_bytes() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $maxstr = ''; - for($i=0; $i<200; $i++) { - $maxstr .= '言'; - } - $reststr = ''; - for($i=0; $i<133; $i++) { - $reststr .= '言'; - } - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name1', XMLDB_TYPE_CHAR, 200, null, XMLDB_NOTNULL, null); - $table->add_field('name2', XMLDB_TYPE_CHAR, 133, null, XMLDB_NOTNULL, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('name1-name2', XMLDB_INDEX_NOTUNIQUE, array('name1','name2')); - - // Drop if exists - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $dbman->create_table($table); - $tablename = $table->getName(); - $this->tables[$tablename] = $table; - - $rec = new stdClass(); - $rec->name1 = $maxstr; - $rec->name2 = $reststr; - - $id = $DB->insert_record($tablename, $rec); - $this->assertTrue(!empty($id)); - - $rec = $DB->get_record($tablename, array('id'=>$id)); - $this->assertIdentical($rec->name1, $maxstr); - $this->assertIdentical($rec->name2, $reststr); - - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name1', XMLDB_TYPE_CHAR, 201, null, XMLDB_NOTNULL, null); - $table->add_field('name2', XMLDB_TYPE_CHAR, 133, null, XMLDB_NOTNULL, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('name1-name2', XMLDB_INDEX_NOTUNIQUE, array('name1','name2')); - - // Drop if exists - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - - try { - $dbman->create_table($table); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - } - - public function test_char_size_limit() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, xmldb_field::CHAR_MAX_LENGTH, null, XMLDB_NOTNULL, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - - // Drop if exists - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $dbman->create_table($table); - $tablename = $table->getName(); - $this->tables[$tablename] = $table; - - // this has to work in all DBs - $maxstr = ''; - for($i=0; $iname = $maxstr; - - $id = $DB->insert_record($tablename, $rec); - $this->assertTrue(!empty($id)); - - $rec = $DB->get_record($tablename, array('id'=>$id)); - $this->assertIdentical($rec->name, $maxstr); - - - // Following test is supposed to fail in oracle - $maxstr = ''; - for($i=0; $iname = $maxstr; - - try { - $id = $DB->insert_record($tablename, $rec); - $this->assertTrue(!empty($id)); - - $rec = $DB->get_record($tablename, array('id'=>$id)); - $this->assertIdentical($rec->name, $maxstr); - } catch (dml_exception $e) { - if ($DB->get_dbfamily() === 'oracle') { - $this->fail('Oracle does not support text fields larger than 4000 bytes, this is not a big problem for mostly ascii based languages'); - } else { - throw $e; - } - } - - - $table = new xmldb_table('testtable'); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, xmldb_field::CHAR_MAX_LENGTH+1, null, XMLDB_NOTNULL, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - - // Drop if exists - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $tablename = $table->getName(); - $this->tables[$tablename] = $table; - - try { - $dbman->create_table($table); - $this->assertTrue(false); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - } - - // Following methods are not supported == Do not test -/* - public function testRenameIndex() { - // unsupported! - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table0'); - $index = new xmldb_index('course'); - $index->set_attributes(XMLDB_INDEX_UNIQUE, array('course')); - - $this->assertTrue($dbman->rename_index($table, $index, 'newindexname')); - } - - public function testRenameKey() { - //unsupported - $dbman = $this->tdb->get_manager(); - - $table = $this->create_deftable('test_table0'); - $key = new xmldb_key('course'); - $key->set_attributes(XMLDB_KEY_UNIQUE, array('course')); - - $this->assertTrue($dbman->rename_key($table, $key, 'newkeyname')); - } -*/ - -} diff --git a/lib/dml/simpletest/fixtures/clob.txt b/lib/dml/simpletest/fixtures/clob.txt deleted file mode 100644 index 1e073fc9c86..00000000000 --- a/lib/dml/simpletest/fixtures/clob.txt +++ /dev/null @@ -1,197 +0,0 @@ -제 1장 총 칙 - -제 1조 (개요) - -이 약관은 전기통신사업법 및 동법 시행령에 의거 ㈜네오플(이하 "회사")이 제공하는 서비스 (이하 "서비스")의 이용조건, 절차, 의무, 책임사항 및 이용에 필요한 기타사항을 규정합니다. - -제 2조 (목적) - -이 약관은 다음과 같은 내용을 목적으로 합니다. -1. 안정적인 서비스 제공 및 서비스, 시스템의 보호를 목적으로 한다. -2. 이용자의 안정적인 서비스 이용 및 이용자의 데이타 보호를 목적으로 한다. -3. 네트워크 및 서비스 이용자 네트워크의 안정성, 프라이버시, 보안성 유지를 목적으로 한다. -4. 네트워크 및 서비스의 사용 원칙 가이드라인 제시를 목적으로 한다. - -제 3조 (약관의 효력과 변경) - -1. 회사는 귀하가 본 약관 내용에 동의하는 것을 조건으로 서비스를 제공 합니다. -2. 이 약관은 서비스 내에 게시하여 공시함으로써 효력을 발생합니다. -3. 당사의 서비스 제공 행위 및 귀하의 서비스 사용 행위에는 본 약관이 우선적으로 적용될 것입니다. -4. 회사는 이 약관을 개정할 경우에는 적용일자 및 개정사유를 명시하여 현행약관과 함께 초기화면에 그 적용일자 7일이전부터 적용일자 전일까지 공지합니다. -5. 이용자는 회원등록을 취소(회원탈퇴)할 수 있으며, 계속 사용의 경우는 약관 변경에 대한 동의로 간주됩니다. 변경된 약관은 공지와 동시에 그 효력이 발생됩니다. - -제 4조 (약관외 준칙) - -1. 서비스 이용에 관하여는 이 약관을 적용하며, 이 약관에 명시되지 아니 한 사항에 대하여는 전기통신 기본법, 전기통신사업법, 정보통신망 이용 촉진 및 정보보호 등에 관한 법률(이하통신망법), 기타 관련법령 및 회사의 공지,이용안내를 적용 합니다. -2. 이 약관의 공지 및 변경사항은 회사의 지정된 홈페이지(http://www.candybar.co.kr)에 게시하는 방법으로 공지합니다. - -제 5조 (용어의 정의) - -이 약관에서 사용하는 용어의 정의는 다음과 같습니다. -1. 이용자 : 서비스 이용을 신청하고 회사가 이를 승낙하여 회원ID를 발급 받은 자를 말합니다. -2. 가 입 : 회사가 제공하는 양식에 해당 정보를 기입하고, 이 약관에 동의하여 서비스 이용계약을 완료시키는 행위를 말합니다. -3. 회 원 : 회사에 개인 정보를 제공하여 회원 등록을 한 자로서, 회사가 제공하는 서비스를 이용할 수 있는 자를 말합니다. -4. 캔디바 ID : 회원의 서비스 이용을 위하여 회원이 신청하고 회사가 승인하는 문자 및 숫자의 조합을 말합니다. -5. 비밀번호 : 회원ID가 일치하는지를 확인하고 자신의 비밀보호를 위하여 이용자 자신이 선정한 문자와 숫자의 조합을 말합니다. -6. 탈퇴 및 해지 : 회원이 이용계약을 종료 시키는 행위를 말합니다. - -제 2장 서비스 이용 계약 - -제 6조 (이용 계약의 성립) - -1. 이용계약은 당사 소정의 가입신청 양식에서 요구하는 사항을 기록하여 가입을 완료 하는것으로 성립함을 원칙으로 합니다. -2. 이용신청은 이용신청자 자신의 실명으로 하여야 합니다. 실명이 아닌경우는 여러가지 불이익을 받을수 도 있습니다. -3. 이용계약은 서비스 이용희망자의 이용약관 동의 후 이용 신청에 대하여 회사가 승낙함으로써 성립합니다. -4. 서비스 이용 희망자가 14세 미만의 미성년자 또는 한정치산자인 경우 부모등 법정대리인의 동의를 받아 이용신청하며 납입책임자는 법정대리인으로 합니다. 또한 금치산자인 경우에는 법정대리인 을 이용자 및 납입책임자로 하여 신청합니다. - -제 7조 (이용신청의 승낙) -1. 회사는 제 6조의 규정에 의한 서비스 이용희망자에 대하여 업무수행상 또는 기술상 지장이 없는 경우에는 원칙적으로 접수순서에 따라 이용신청을 승낙합니다. -2. 이용자의 이용신청에 대하여 회사가 이를 승낙한 경우, 회사는 회원 ID와 기타 회사가 필요하다고 인정하는 내용을 이용자에게 통지합니다. - -제 8조 (이용신청에 대한 불승낙과 승낙의 보류) -1. 이용자가 신청한 이용 아이디(ID)가 이미 다른 이용자에 의해 쓰여지고 있는 경우 승낙을 하지 아니할 수 있습니다. -2. 회사는 다음 각호에 해당하는 이용신청에 대하여는 승낙을 하지 아니할 수 있습니다. -1) 타인 명의의 신청 -2) 허위의 신청이거나 허위사실을 기재한 경우 예) 주민등록 생성기를 이용한 비실명 가입 -3) 기타 이용신청고객의 귀책사유로 이용승낙이 곤란한 경우 -3. 회사는 전항의 경우에는 이를 이용신청고객에게 가능한 빠른 시일 내에 통지하여야 합니다. -4. 회사는 이용신청고객이 미성년자, 한정치산자인 경우에 법정대리인(부모 등)의 동의없는 이용신청에 대하여 승낙을 보류할 수 있습니다. -5. 회사는 이용신청이 불승낙되거나 승낙을 제한하는 경우에는 이를 이용신청자에게 즉시 통보합니다. - -제 9조 (이용 아이디 관리 및 변경) - -1. 이용 아이디(ID) 및 비밀번호에 대한 모든 관리책임은 이용자에게 있습니다. -2. 이용 아이디(ID)는 다음에 해당하는 경우에는 이용자와 합의하여 변경할 수 있습니다. -(단, 이용 아이디(ID)를 변경할 경우 기존 이용ID는 소멸됩니다.) -1) 이용 아이디(ID)가 이용자의 전화번호 또는 주민등록번호 등으로 등록되어 사생활 침해가 우려 되는 경우 -2) 타인에게 혐오감을 주거나 미풍양속에 어긋나는 경우 -3) 기타 회사가 인정하는 합리적인 사유가 있는 경우 -3. 회사는 이용 아이디(ID)에 의하여 서비스 이용요금 청구 및 게시판 관리 등 제반 이용자 관리 업무를 수행하며 이용자는 이용 아이디(ID)를 공유, 양도 또는 변경할 수 없습니다. 단, 그 사유가 명백하고 회사가 인정하는 경우에는 그러하지 아니 합니다. -4. 이용자가 신청 또는 변경하여 사용하는 이용 아이디(ID) 및 비밀번호에 의하여 발생하는 서비스 이용상의 과실 또는 제3자에 의한 부정사용등에 대한 모든 책임은 이용자에게 있습니다. 단, 회사의 고의 또는 중대한 과실이 있는 경우에는 그러하지 아니합니다. -5. 기타 아이디의 관리 및 변경 등에 관한 사항은 이 약관과 회사의 공지, 이용안내 에서 정하는 바에 의합니다. - -제 3장 계약당사자의 의무 및 책임 - -제 10조 (회사의 의무) - -1. 회사는 이용자로부터 제기되는 의견이나 불만이 정당하다고 인정할 경우에는 즉시 처리하여야 합니다. 즉시 처리가 곤란한 경우에는 그 사유와 처리일정을 서면 또는 전화 등으로 통보합니다. -2. 회사는 서비스 제공과 관련하여 취득한 이용자의 정보를 본인의 동의없이 타인에게 누설 배포할 수 없으며 서비스 관련 업무 이외의 목적으로도 사용할 수 없습니다. -3. 제2항의 경우 관계법령에 의한 규정에 의하여 수사상의 목적으로 관계기관으로부터 요구받은 경우나 정보통신윤리위원회의 요청이 있는 경우 또는 통신망법 24조 1항 '정보통신서비스의 제공에 따른 요금정산을 위하여 필요한 경우'에는 개인정보의 이용 혹은 제 3자에게 제공될수 있으며 그 범위는 회사의 개인정보보호정책을 따릅니다. -4. 회사는 계속적이고 안정적인 서비스 제공을 위하여 설비에 장애가 생기거나 멸실된 경우에는 지체없이 이를 수리 또는 복구하며, 서비스를 다시 이용할 수 있게 된 경우 이 사실을 이용자에게 통지하여야 합니다. 단 천재지변, 비상사태 또는 그밖의 부득이한 경우에는 서비스를 일시 중단하거나 중지할 수 있습니다. -5. 회사는 이용계약의 체결, 계약사항의 변경 및 해지 등 이용자와의 계약에 관련된 절차 및 내용 등에 있어서 이용자에게 편의를 제공하도록 노력합니다. -6. 회사는 이용자가 안전하게 당사서비스를 이용할 수 있도록 이용자의 개인정보(신용정보 포함)보호를 위한 보안시스템을 갖추어야 합니다. -7. 회사는 이용자가 제11조의 이용자의 의무를 위반한 경우 및 고의 또는 중대한 과실로 회사에 손해를 입힌 경우에는 사전 통보 없이 이용계약을 해지하거나 또는 기간을 정하여 서비스의 이용을 중지할 수 있습니다. - -제 11 조 (이용자의 의무) - -1. 이용자는 이 약관에서 규정하는 사항과 서비스 이용안내 또는 주의사항을 준수하여야 하며,기타 회사의 업무수행에 현저한 지장을 초래하는 행위를 하여서는 아니 됩니다. -2. 이용자는 서비스를 이용함에 있어 문제 발생소지 정보의 책임은 이용자에게 있습니다. -3. 이용자는 서비스를 이용하여 얻은 정보를 가공, 판매하는 행위 등 게재 된 자료를 상업적으로 이용할 수 없으며 이를 위반하여 발생하는 제반 문제에 대한 책임은 이용자에게 있습니다. -4. 이용자는 이용계약에 따라 요금을 지불하여야 하며, 서비스 이용요금의 미납으로 인하여 발생되는 모든 문제에 대한 책임은 이용자에게 있습니다. 단, 회사의 고의 또는 중과실의 경우에는 그러하지 아니 합니다. -5. 이용자는 회원가입시 정보는 정확하게 기입하여야 합니다. 주민등록생성기등을 이용하여 허위로 가입을 하거나 주민등록 생성기를 인터넷에 올리거나 이를 이용해 만든 행위에 관하여서도 주민등록법 개정안에 의거하여 엄중히 법적인 제재를 가할 수 있습니다. 또한 이미 제공된 귀하에 대한 정보가 정확한 정보가 되도록 유지, 갱신하여야 하며, 회원은 자신의 캔디바 ID 및 비밀번호를 제3자에게 이용하게 해서는 안됩니다. -6. 이용자는 당사의 사전 승낙없이 서비스를 이용하여 어떠한 영리행위도 할 수 없습니다. -7. 이용자는 당사 서비스를 이용하여 얻은 정보를 당사의 사전승낙 없이 복사, 복제, 변경, 번역, 출판·방송 기타의 방법으로 사용하거나 이를 타인에게 제공할 수 없습니다. -8. 이용자는 당사 서비스 이용과 관련하여 다음 각 호의 행위를 하여서는 안됩니다. -1) 다른 회원의 캔디바 ID를 부정 사용하는 행위 -2) 범죄행위를 목적으로 하거나 기타 범죄행위와 관련된 행위 -3) 선량한 풍속, 기타 사회질서를 해하는 행위 -4) 타인의 명예를 훼손하거나 모욕하는 행위 -5) 타인의 지적재산권 등의 권리를 침해하는 행위 -6) 해킹행위 또는 컴퓨터 바이러스의 유포행위 -7) 타인의 의사에 반하여 광고성 정보 등 일정한 내용을 지속적으로 전송하는 행위 -8) 서비스의 안전적인 운영에 지장을 주거나 줄 우려가 있는 일체의 행위 -9) 당사 사이트에 게시된 정보의 변경 -10) 기타 전기통신법 제53조와 전기통신사업법 시행령 16조(불온통신), 통신사업법 제53조3항에 위배되는 행위 -9. 다른 이용자들에게 피해를 주는 다음 각 호에 해당하는 행위를 하여서는 안됩니다. -1) 회사가 인정하는 서비스의 공식 운영자인 아이디를 사칭하는 내용 -2) 선정적이고 음란한 내용 -3) 반사회적이고 관계법령에 저촉되는 내용 -4) 기타 제3자의 상표권, 저작권에 위배될 가능성이 있는 내용 -5) 비어, 속어라고 판단되는 내용 -6) 욕설이나 노골적인 성 묘사를 하는 내용 -7) 다른 이용자를 희롱하거나, 위협하거나, 특정 이용자에게 지속적으로 고통을 주거나,불편을 주는 행위 -8) 서비스 내 또는 웹사이트 상에서 다른 회사 제품의 광고나 판촉활동을 하는 행위 -9) 저작권자의 허가 없이 저작권 문제가 발생할 소지가 있는 내용을 서비스 내 또는 웹사이트 상에 배포하는 행위 - -제 4장 서비스 이용·제한·정지등 - -제 12조 (서비스의 이용) - -1. 캔디바 서비스의 이용은 기본적으로 무료입니다. 단 회사에서 정한 별도의 유효정보 및 유료서비스에 대해서는 그러지 아니하며, 유료서비스 이용에 관한 사항은 회사가 별도로 정한 약관 및 정책에 따릅니다. -2. 유료 서비스 이용의 결제에 관한 사항(충전방법, 사용, 환불 등)은 바(Bar)정책 이용약관에 따릅니다. -3. 아바타 및 게임 아이템 등 유료서비스의 소유권은 회사에 있으며, 웹상의 사용권은 해당 컨텐츠를 구매한 이용자에게 있습니다. - -제 13조 (서비스 이용시간 및 중지) - -1. 서비스 이용은 회사의 업무상 또는 기술상 특별한 지장이 없는 한 연중 무휴, 1일 24시간을 원칙으로 합니다. 다만, 회사는 서비스의 데이터 베이스별로 이용가능시간을 정할 수 있으며, 이 경우 그 내용은 회사가 정하여 서비스에 게시하거나 별도로 공시하는 바에 따릅니다. -1) 제1항의 규정에도 불구하고 정기점검 등의 필요로 회사가 정한 날 또는 시간은 예외적으로 서비스 이용을 제한할 수 있습니다. -2. 전시,사변,천재지변 또는 이에 준하는 국가비상사태가 발생하거나 발생할 우려가 있는 경우와 전기통신사업법에 의한 기간통신사업자가 전기통신서비스를 중지하는등 기타 부득이한 사유가 있는 경우에는 서비스의 전부 또는 일부를 제한하거나 정지할 수 있습니다. -1) 회사는 제1항의 규정에 의하여 서비스의 전부 또는 일부를 제한하거나 정지한 때에는 지체없이 이용자에게 알려야 합니다. -3. 이용자가 국익 또는 사회적 공익을 저해할 목적이나 범죄적 목적으로 서비스를 이용하고 있다고 판단되는 경우에 회사는 이용자에게 사전 통보 없이 서비스를 중단할 수 있으며 그에 따른 데이터도 복구를 전제로 하지않고 삭제할 수 있습니다. -4. 회사는 이용정지 기간 중에 사유가 해소된 것이 확인된 경우에는 지체없이 서비스를 재개통 또는 이용정지를 해제합니다. - -제 14조 (각종 자료의 저장기간) - -회사는 필요한 경우 서비스의 일정 부분별로 이용자가 게시한 자료나 이용자의 필요에 의해 저장하고 있는 자료에 대해 일정한 게재기간 또는 저장기간을 정할 수 있으며, 필요에 따라 기간을 변경할 수 있습니다. - -제 15조 (게시물의 저작권) - -1. 이용자가 서비스 홈페이지에 게시하거나 등록한 자료의 지적재산권은 이용자에게 귀속됩니다. 단, 회사는 서비스 홈페이지의 게재권을 가지며 비상업적 목적으로는 이용자의 게시물을 활용할 수 있습니다. -2. 이용자는 서비스를 이용하여 얻은 정보를 가공, 판매하는 행위 등 게재 된 자료를 상업적으로 이용할 수 없으며 이를 위반하여 발생하는 제반 문제에 대한 책임은 이용자에게 있습니다. -3. 회사는 이용자 게시물의 내용 검열, 검색 및 관리에 따른 일체의 손해배상 책임을 지지아니 합니다. - -제 5장 개인정보 보호 - -제 16조 개인정보보호 -회사는 관계법령이 정하는 바에 따라 회원등록정보를 포함한 회원의 개인정보를 보호하기 위하여 노력을 합니다. 회원의 개인정보보호에 관하여 관계법령 및 회사가 정하는 개인정보보호정책에 정한 바에 따릅니다. - -제 6장 계약해지 및 이용제한 - -제 17조 (계약의 해지) - -회원이 이용계약을 해지하고자 하는 경우에는 회원 본인이 온라인을 통하여 등록해지신청을 하여야 합니다. - -제 18조 (이용제한) - -1. 회사는 회원이 다음 각 호의 사유에 해당하는 경우 사전통지 없이 회원의 서비스 이용제한 및 적법한 조치를 취할 수 있으며 이용계약을 해지하거나 또는 기간을 정하여 서비스를 중지할 수 있습니다. -1) 회원 가입신청 또는 변경시 허위 내용을 등록한 경우 -2) 타인의 서비스 이용을 방해하거나 그 정보를 도용한 경우 -3) 회사의 운영진, 직원 또는 관계자를 사칭하는 경우 -4) 회사의 사전승락없이 서비스를 이용하여 영업활동을 하는 경우 -5) 회원ID를 타인과 거래하거나 회원ID의 게임상 사이버 자산을 타인과 매매하는 행위를 하는 경우 -6) 회사 프로그램상의 버그를 악용하여 정상적이지 아니한 방법으로 게임상 사이버자산을 취득하는 행위를 하는 경우 -7) 서비스를 통하여 얻은 정보를 회사의 사전승낙없이 서비스 이용 외이 목적으로 복제하거나 이를 출판 및 방송 등에 사용하거나, 제3자에게 제공하는 경우 -8) 회사 또는 제3자의 저작권 등 기타 지적재산권을 침해하는 내용을 전송, 게시, 전자우편 또는 기타의 방법으로 타인에게 유포하는 경우 -9) 공공질서 및 미풍약속에 위반되는 음란한 내용의 정보, 문장, 도형, 음향, 동영상을 전송, 게시, 전자우편 또는 기타의 방법으로 타인에게 유포하는 경우 -10) 심히 모욕적이거나 개인신상에 대한 내용이어서 타인의 명예나 프라이버시를 침해할 수 있는 내용을 전송, 게시, 전자우편 또는 기타의 방법으로 타인에게 유포하는 경우 -11) 서비스에 위해를 가하거나 고의로 방해한 경우 -12) 다른 회원을 희롱, 위협하거나 특정 이용자에게 지속적으로 고통, 불편을 주는 행위를 하는 경우 -13) 범죄와 관련이 있다고 객관적으로 판단되는 행위를 하는 경우 -14) 본 서비스를 이용함에 있어 본 약관 및 기타 회사가 정한 정책 또한 운영 규칙을 위반하는 경우 -15) 기타 관련 법령에 위배하는 행위를 하는 경우 - -제 7장 분쟁조정 및 기타사항 - -제 19조 (손해배상) - -회사는 무료로 제공되는 서비스 이용과 관련하여 회원에게 발생한 어떠한 손해에 관하여도 책임을 지지 않습니다. - -제 20조 (면책조항) - -1. 회사는 천재지변 또는 이에 준하는 불가항력으로 인하여 서비스를 제공 할 수 없는 경우에는 서비스 제공에 관한 책임이 면제됩니다. -2. 회사는 회원의 귀책사유로 인한 서비스 이용의 장애에 대하여 책임을 지지 않습니다. -3. 회사는 회원이 서비스를 이용하여 기대하는 수익을 상실한 것이나 서비스를 통하여 얻은 자료로 인한 손해에 관하여 책임을 지지 않습니다. -4. 회사는 회원이 서비스에 게재한 정보, 자료, 사실의 신뢰도, 정확성 등 내용에 관하여는 책임을 지지 않습니다. -5. 회사는 서비스 이용과 관련하여 가입자에게 발생한 손해 가운데 가입자의 고의, 과실에 의한 손해에 대하여 책임을 지지 않습니다. -6. 회사는 회원간 또는 회원과 제3자간에 서비스를 매개로 하여 물품거래 혹은 금전적 거래등과 관련하여 어떠한 책임도 부담하지 않습니다. - - -제 21조(분쟁조정) - -회사와 이용고객은 개인정보에 관한 분쟁이 있는 경우 신속하고 효과적인 분쟁해결을 위하여 한국정보보호진흥원내의 개인정보분쟁조정위원회에 그 처리를 의뢰할 수 있습니다. - - -제 22조(회사의 소유권) - -1. 회사의 서비스, 소프트웨어, 이미지, 마크, 로고, 디자인, 서비스명칭, 정보 및 상표 등과 관련된 지적재산권 및 기타 권리는 회사에게 소유권이 있습니다. -2. 이용자는 회사가 명시적으로 승인한 경우를 제외하고는 전항의 소정의 각 재산에 대한 전부 또는 일부의 수정, 대여, 대출, 판매, 배포, 제 작, 양도, 재라이센스, 담보권 설정 행위, 상업적 이용 행위를 할 수 없으며, 제3자로 하여금 이와 같은 행위를 하도록 허락할 수 없습니다. diff --git a/lib/dml/simpletest/fixtures/randombinary b/lib/dml/simpletest/fixtures/randombinary deleted file mode 100644 index 91634b696a065a264558e97ccd21841a32525668..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10000 zcmV+rC-2x@xLiKH#fm3ghx7u4)j?Ry=NZA%IuVCVZ9QJ`0s}R2xn(^2oDiZ{`FK4$ zPoufw{`n!YjflOS;}LInrAR$pP>px1I3Hy0c3U`(l<1@!W_rV-)C|31;zn1E5&^`` zQ}yL8TP)eDuRoAe^F!^ zo*E9s#)c(vQAF)qlgVy4d#O8?0b!VJ1@RhYC;vJ=Vauut%U(w+tBghxHDs##T<=N} zrYL~=)D*-~3Zt8$Hn?w9@r^p*JlA|9it}`cJK!%FERSUzZc7SniivEVSH(E`kKHlO ztK{pd*4vzoo$rg~e{^!06p6)CR#Q(zKzKD2X^(u=*MXHFx&FQO>%R;=x9t>evY6|0 z0ob!#5ZDGi7>Fy$jJ6dEKrtur%^1twNdh5p7KL(%yrf`F)DFHG@K?LgrXGWW5|&Y! z-&@i!F`>xVfn!PFsDA9R%9aHZNB2OR~6o!%2vn^fvzO$>%d~_#G z5>kAqBk$7YUGYRo?(m~)A1OE4dr34L0nkUlJHK;NX5!}jsgj;4$y7~Fh&(EqnKbYx z)siUE3YThwp1IR04dyLZZm~WmNv^@H03a}y!Axa8rtSXyrlE9AGHTRcHn#l@XkRcS zf$$1txjVDrEY4(-UD-T2=3abOeV|cM3=>j?^UT1Igp0%3e1P64VUc$;-6^J{Hbg5E?1*{{ykhn__euHIj zp{#I;qmr5)RW_9?aGCnNhMEW7!k$X5KMH_vu$`05>`^oXhAYAPC?YW zLDE8=^vSR=(ss8}N%X?d&~8}Q)7SgAD~b6;6u1fylj!*^!VZ*mg`6+1r1}+|r7P}@ zW}NixBm)J7?O%mo_df-w;7`*{`zL@v55bjl_b^|xse;rVlRpY-#+>)5)*UW!%BU|k zj0xHSds?)engZl=FCvaOk0(8tH*eAe?Z+?^#-MTV+H|ZGDRVn%2X{584V-TqSePNG zrt6l{!G_a%Wu|2V_FUaT6Ddd&olL1-7|e30jb-ZFU8c@^uzP4PJf@WQY*Z3eM6LbW z<$mQ0OZ4j0&d};2TuM%5f%jL%FXF*}perSaw6k2oQ(%ET#tw zCW+SOk3VRqwKA-Gi)%@eC45}Xj>hyvv`tnV21Ynzk*aW^_(x^3x5%2g`T0dD`3rFE{i_ zg`$`qL+P&JYUcN>c8z8k^1dG5ps9lErlaYg<-9Ya<5(|q5xTkhrgp)`h_CYY}z$sM1!OHHf*9fBIMOrAoZJs0A=r90{1 z^&)h!_h3A`H`PnQpboU_zUOQ`h{V_1Qj|RY9l0>-NiXw>zPQj{UaXuzxy}D)9UW-a z%idQ7QDMn0z}u93zv*BIYP3F9PHE7n)yS?oyd_^`W6_LI3?LP!&jvJ9Zbh+)7d6F% zlqtB3I*=A{IL_kfu&o@nEU3L!8V{P1iGXnQqss4e!dtNe+bot^IVH<|FD$4LkB zgDt`rtQPY4nkWB-U6=JRbaEW4Kf$^++K2tz-G#jW>B2kGhQY-Q*E!Y1^J9hX*c%Z% zc1);8HXQFaSedZH|Eho^^6Ycd6^YT%*wJm_lh}C4BAeP7&|Hp)@F^SrhnjJru1tzh z33wq8oZJrH!H|Eg&~&d4HMqf1NNy42$AN#0O40#Pm~szdkQMJv=`mlw;3!%7w-f3dDsA`MeJ0S==OWCd(dytnyEy$#dotw8OY$$2T}}>I;-MsDHMx*Lk8XD%KGqH&@j@W73w;H!N=9%}iWPM2v!- zv9Q3_;8NFm;i$6_FFgA9(EnTH8UU%D@Z_#2l-r0{^$bU47SQ&7sDf$3YA2X-1kn>& zkR}C0-boB|j)S!2KOKgIb2w34391b2Hh9>B_cUG8?%C@mK(%#zugT;X=@B;!i&a2j z4n?XGO@=9EZUV@qg)4(oyh$8xkrDQ>mh~B@28sv zKB4^c3%Fdkx2+cj_uT(qYoq&~SU+t2%;~(2yVNXJ&i!auoblVAn0KD{uRfUFdX+q!=1TWMZV+eCQOlwCBc-n2MY7etrz+V0HCH`&ocnMFHocT&m53Y+_UU_Bo$EcS690UU`py51r_jtF9#UVg#z=Az0Nu-Xzz z%zUw;fpp@^I!f%&z*+0lcn+&_08N=!h?H>~0QZ59E z;T5W5yhc^s-SDCF-b^auN~yncF--yjT)g=%8m5@jwW0RJ)1%r;uKpfTir~N2fCXGIFgrf_kF%1mxCZDYXfe>cC6>ldV#%{W;! z!LNb1qZi+nr26zvp{+Y^Z>Fqe=pi*xZz?a!bscePOoSnwKqUuilaY?W#5qx5rhX_I$OD_ z-{%@M?ML~S*8hDPWwEt7%jqugzm82&gP6)s;cNV>A^lez7>tvkv*oLh9yo{&?u8t2 zW`;~^N}|3Qlb)*p7?l}^w`+ZZ;Ege_=&N$Su0DiTXm;DVel|!C`^J4$f*b4>N$HFU z3gAVXTLk*o4;QVNE{ z9fg<07EK6~wCKr&Gvph*b#KnLLVLlSI2J520)Fh_kpyFDD6f2Ps-Z0YU|_`%UZc~f zKe_uj3r?lEbW-~|=phN&pBBMoMQt5kg_}z%s1;EoX z56>H`fuM5R$)k<$jdPVB^>Q}c^x9*|zX_4H3t-u+!FC@J_nBcB@aR(fg(X{E1Z)h@ zN{4^BfGt$(CE8Z(pvzd)99h)sunLBM-ZJn*b3wqGuX=LMv}NMlQz=DjBG;0=VXgn` z8X916OVp9C_>{%hb)0#I6usIWYVoPBl)pW)kDzE!ZcJOVhBqxK5t)^uPg3zxMPEw4 z1eJ)I3_?3AIn6S}D5VBQQg`^o)cf}l!F^4$|I>un)og^s=NpO07WRk?+om(Un9ScXepbucub?(U%B}ASC1$aLvnoGQYANY436uknYsFFWjfApMAfvpscWvp7-taDTgc zy%_Sf)jTTOWly_`7giux^x)x(N6jMmn#|+dxaKF$=|>9f361<;pD7ywR{=-rgpCp_ zQF|5oIA4%HGr;++shwdzDYe%MW7?=zwK^q&s3pqL%w^mB^VeoI=WflxX+;_1v zDkEo|4x*@Ej$Do=Zw-@%7vPx)LJY&AnZw~7>v_&DC*UKl9U>3f6MO^bMvPUc_V>1wPfv_QX(Sc@Br{3hcuS zB9oe6`7bc<=kLK5}$!y1v3hs zlO!_AN5U^96Z%me+9Oz^lq{B!Cv`>=<>tAe^@xvW5ZJxn1Jg3Gtzq?|Hd-+zz~S?V zZRo{uF=;a(tFAO_`AMVyGxh7IBBMXAqz@W=jve$2J`Bw*5h)Ae2jPIpv6EpoFj`54 zB!BE~3vF*0(Zl!y;{Kd54EN+h!TUy#gZ?BU#2W9eY6mod`AvNwp#Wx)*??#~lJG+H zv#HRmNxT!`9fO4L+M@hv0}y}ZC0@RFS?l?Dn09FLY!}4K23uZ(imrZ}*$!)C$z`%~FDTd$uu=qI&tr;Y;|7cVlYeLU z)W+{1)Kg#_31(5q{x-0ZlvoowNu`IbGJ+6u$T2!?<`ujXJjo0lDtWJ93Z~7!PIW$w zQ`AXJZ)^x1H6h?p4K|&Zr~Wdq_Z2}58F~R>)hU4!B_Jnp=3GLLa58l*GOI2+fImi} zq1Vf+NSFsvDtrRY-md|9UJ3MW-o}DCLDQfOh-{$X2ZVB%6)W$@MY%)%*m>q`^|gU2 z(lWCubQf~R0nk7OUO@CglH1b8?N)fLmi>$gWsxw(9c37Cfh`vn6XxC^;jzMN>xdfD zjD-^K*=CXa7aMM0ABU@BAUV~JovGI&QaQx8d-WkwO(vhIZb(2-Dz1(#&Z6umr2_Gw zt*=Tbcxxc{9Mkc2QyqC>cxAAYR1vWGV}I()k*!GHG6c{<_>GOS|2574Dwu+e%I=e1 zH^FH28Eb7IGw+`*()@THZoy9uL1z2iurAgOs@GB#VU%8^q5Z|`PJbn7w^WBuPMeSf zEbTbuRH&VmzRI2L8#E1gm5rOvGr#tgBq7ea8mM`|Z*R@N!|)G7r=vct*j$hzL+s`v z5}-rJFXaqNBcpy2aRZvWDkv%KUFPiiattM%hMG1DOhr*BA$&DxFL@mc8#m5d-ZWQr zLC{GsCSNfic$1|&sWWSyJZ+4}lA&HdiN8C#vuW$Mv>C>tYF9%PneT%+EVWg=ElDI##*<1mQEyIiPWll)# zD~3|h`7@*x-)d9`tKb#FeBaj@ggm9}sr+z?=#(Y-tI#`M5s~ulLt6HX87aM)v|b6? z_DVPoI7fJ`hP?)bd`}iLhTLB{PE3C$wXB2;W{~l0DPHdQM=q86e8@nI+ik|PL4Td+ z2A5YklqFd|@LR2gdRfGm-es0$$bOx7*u67T+x{PMa2Me(MqF7XK-S}c5IQxV{KW|y zWY zyFfz2nU&l}$ZC8{7p%@fdu!ue#gF4zfL>Wm-?mK(Lf7P7T@Lj()D8PbT|I!blEtVM z&U#P2xXiR}jNH)W#ud3-V5TA7h_EWfboievg5IH z!M=(91N%}~hpWEylB%0n*So;_d(a@XjH&0yOm`3@2KA^zWk;^jH6-dQevK#@fw?N} zijk0)L;bLDi`DTN6zR)YwL{PM`76e3^o#6Kp*ohNLgS3UI17@cUG$(8{LQBFa*6r||3`O`nca9J~o}7nslA&^ZBj4sRtvVI@nmo*9PARjVpWzg#{sI)a?eXxa)M<6<93=q`ENwq0k6_Yx zvNDsUX>EJini?RHiLuB#*$uKpW0eiB`gc*HlIDQkzyaz8z*UtBNRk2F_gyU@2fRVFC=O`fF zv*@5PtEobkj_czCSVKE!yFyf{jZSGw%n{RN`hM0TUzJf{cCE25BjJNKlTFtqCfy8~ zpGV|6DYUt}=vsbIrDk%s1bd@K4V`M5jw4QfU44NdqpB67_ANX+!~;~rO)dxB1d2P1 zwDEifw(gp!v^j?>h-d8Grtq^l|@~7T)k|ysT2+BwyluwVsj*u7}2@LrR&}2|+73 z%w=S}7nB$$BuCiA$Ca)R!Yf~W36>W?ZLW1kS&>T2vd__+FWjtCffjzr%FC+Pi-HnC@gkL z+A-{)-+Q!$=A^2G7**+EQeWBtD%uW{L@1vBX_{Q}BCceK!k3pqOJABG_HMZHUEOdoB#_OW{JaeRkc z=hAoS+J4@=!RI<*3YI=%wb5PIy7jM`(2My9ZOq=%*-AZj$e5yf8&njFu#;DxpodD7iVfhhmpkva2TO-S#qN|l)pmh+wtM)9FTVF>!(+)mRQ?g5RiM_Uo zR!5yx80?g|?teuA@v%G%`js`KWTknm$IvC16B8zq$=IQS*ts zIiK8)yVYGTd?%}f@h5(=dVaOsbD*}a0}p=6e}pUvteEJaHuk-0`NDt)1Q}x>qik# z)29eBGS{ds6cs)E)`{>-A>Wc}Sdmd6=!Kv#Uv7TK>w(>DmM?u2IOAz>B*Epxb-Y+5kxgF)DF4;jn-~M~bIkD^GtL)}ZeSz{<)c8-=aAqV z#&(y+;iwJUQZyXPR9D-I*Malr)r~?(u=^u+SdW0CMx|{-Iis9o*^fc7Z*98ap`4u& z00IyKit`e z;k&KgqR=ip?6&b0Ek@27$6z5d7`mgQJP)ROY$%4ra-C?3B%23n-4a*E${t{2=7d5N=7l=kohcP;|ZV>b^S!OsB-cq^k)MtM2~rGO1>LG&`qx0eZt+#Z*51uGgNr zv6N4|89rt0$6SBxv+GyUu*WYwkj}yVDw@pA$K}%wwSE=BY*Tt4hzQa`j2Yen`>8;LaDX7lQcB92xpBB;7J>ke*n9{F)f=5H{#JODe>(xdzXq2Mpht|M-&4htLu5rwNSSee{jA2gp>pVEL zul!pSrO>6~C&~ZjSK3IrYsQvs^(=Ieb-Gn^0}sL4J_mGVt%#^?7db5 z%$qvmXwSxKSwhHV^u?DnGiL*OPIc1M6NflCvU>LNSi0zwD0_B073ow{V?78*0(7v0 z?~HGiT#{?B%CGyIIC)6=9C4}T5F4o`$P!%)#5}eS2jz1=&Aiu79z@L-n+>p+370$I z&~*&+1r*E9758zHxRo2%7Y0WyCz8R{yHWGCqID(fA8m^)?~W?@V>HZ0Dx))FjD0bZ zw(^qjS3!ikaEM%`!wf9+9#HV!MZzz*yTCs?!lRfoONp89TRw>^U({Sl_sG*jqpPJP zLhak{&|>@mzqZ_WBy}w2xR9WoxoriS{a-~gC3A7@!_p5U@|@tF%Rr2LiThD_NL>?h z+7z;b!V|MIE$0;wm6U}`H60c=ROawknjB$5t(wU8eBfAzCgEux?YW%E(m~zSJPym0 ztdEtO)n*DUi*h>aEN)iZZTz6X^h}#D07=zRcYeH@_Gw^r#6PgZPS9|?fuwFI$Ei`O+Q$=YW;=?U{b@(m18DJYwnTTX@SnWp(;Y*#7tO`EI`^wLB)h~@@pLA~?Hanp6 zDbphGd~bcN!O;;`oJ$74uig7S%C&$7343MNc$FW2;@dL!&GKSD4=*Kf)K`GT3_idF9hn&5u+a>QM%h{7A zuFpFF^D5C9qUt$S80*s~V;;k8RiLKXI^080cL#X3oF}D-(h+Z33kigwS%Mt|R-pa- z9?zq`F*s*F_D(k_HkkIoYwZi%3fHaxoS#P`xtEaBcm#HUoFaQaD^hX#We)Q!$hyW9 z;E&-Hpr))v@J;T2L6j`~oA3aXRAFNtnz34)EEd$|)U9f&KQmzV30kGMwPod12-JdT zyzZ(2>c{e_gh1SeY_JQ1!F9^li{;!kN9? z=}Q!K_{!Lrbgh61IO(|n!#E6taXhHP0 at{iLsq(N(V1Y?5g`FFwGqW5iDCW3ZxVP)9> diff --git a/lib/dml/simpletest/testdml.php b/lib/dml/simpletest/testdml.php deleted file mode 100644 index d3261c9faf3..00000000000 --- a/lib/dml/simpletest/testdml.php +++ /dev/null @@ -1,4628 +0,0 @@ -. - -/** - * @package core - * @subpackage dml - * @copyright 2008 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -class dml_test extends UnitTestCase { - private $tables = array(); - /** @var moodle_database */ - private $tdb; - private $data; - public static $includecoverage = array('lib/dml'); - public static $excludecoverage = array('lib/dml/simpletest'); - - protected $olddebug; - protected $olddisplay; - - function setUp() { - global $DB, $UNITTEST; - - if (isset($UNITTEST->func_test_db)) { - $this->tdb = $UNITTEST->func_test_db; - } else { - $this->tdb = $DB; - } - } - - function tearDown() { - $dbman = $this->tdb->get_manager(); - - foreach ($this->tables as $tablename) { - if ($dbman->table_exists($tablename)) { - $table = new xmldb_table($tablename); - $dbman->drop_table($table); - } - } - $this->tables = array(); - } - - /** - * Get a xmldb_table object for testing, deleting any existing table - * of the same name, for example if one was left over from a previous test - * run that crashed. - * - * @param database_manager $dbman the database_manager to use. - * @param string $suffix table name suffix, use if you need more test tables - * @return xmldb_table the table object. - */ - private function get_test_table($suffix = '') { - $dbman = $this->tdb->get_manager(); - - $tablename = "unit_table"; - if ($suffix !== '') { - $tablename .= $suffix; - } - - $table = new xmldb_table($tablename); - if ($dbman->table_exists($table)) { - $dbman->drop_table($table); - } - $table->setComment("This is a test'n drop table. You can drop it safely"); - $this->tables[$tablename] = $tablename; - return new xmldb_table($tablename); - } - - protected function enable_debugging() { - global $CFG; - - $this->olddebug = $CFG->debug; // Save current debug settings - $this->olddisplay = $CFG->debugdisplay; - $CFG->debug = DEBUG_DEVELOPER; - $CFG->debugdisplay = true; - ob_start(); // hide debug warning - - } - - protected function get_debugging() { - global $CFG; - - $debuginfo = ob_get_contents(); - ob_end_clean(); - $CFG->debug = $this->olddebug; // Restore original debug settings - $CFG->debugdisplay = $this->olddisplay; - - return $debuginfo; - } - - // NOTE: please keep order of test methods here matching the order of moodle_database class methods - - function test_diagnose() { - $DB = $this->tdb; - $result = $DB->diagnose(); - $this->assertNull($result, 'Database self diagnostics failed %s'); - } - - function test_get_server_info() { - $DB = $this->tdb; - $result = $DB->get_server_info(); - $this->assertTrue(is_array($result)); - $this->assertTrue(array_key_exists('description', $result)); - $this->assertTrue(array_key_exists('version', $result)); - } - - public function test_get_in_or_equal() { - $DB = $this->tdb; - - // SQL_PARAMS_QM - IN or = - - // Correct usage of multiple values - $in_values = array('value1', 'value2', '3', 4, null, false, true); - list($usql, $params) = $DB->get_in_or_equal($in_values); - $this->assertEqual('IN ('.implode(',',array_fill(0, count($in_values), '?')).')', $usql); - $this->assertEqual(count($in_values), count($params)); - foreach ($params as $key => $value) { - $this->assertIdentical($in_values[$key], $value); - } - - // Correct usage of single value (in an array) - $in_values = array('value1'); - list($usql, $params) = $DB->get_in_or_equal($in_values); - $this->assertEqual("= ?", $usql); - $this->assertEqual(1, count($params)); - $this->assertEqual($in_values[0], $params[0]); - - // Correct usage of single value - $in_value = 'value1'; - list($usql, $params) = $DB->get_in_or_equal($in_values); - $this->assertEqual("= ?", $usql); - $this->assertEqual(1, count($params)); - $this->assertEqual($in_value, $params[0]); - - // SQL_PARAMS_QM - NOT IN or <> - - // Correct usage of multiple values - $in_values = array('value1', 'value2', 'value3', 'value4'); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false); - $this->assertEqual("NOT IN (?,?,?,?)", $usql); - $this->assertEqual(4, count($params)); - foreach ($params as $key => $value) { - $this->assertEqual($in_values[$key], $value); - } - - // Correct usage of single value (in array() - $in_values = array('value1'); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false); - $this->assertEqual("<> ?", $usql); - $this->assertEqual(1, count($params)); - $this->assertEqual($in_values[0], $params[0]); - - // Correct usage of single value - $in_value = 'value1'; - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, null, false); - $this->assertEqual("<> ?", $usql); - $this->assertEqual(1, count($params)); - $this->assertEqual($in_value, $params[0]); - - // SQL_PARAMS_NAMED - IN or = - - // Correct usage of multiple values - $in_values = array('value1', 'value2', 'value3', 'value4'); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true); - $this->assertEqual(4, count($params)); - reset($in_values); - $ps = array(); - foreach ($params as $key => $value) { - $this->assertEqual(current($in_values), $value); - next($in_values); - $ps[] = ':'.$key; - } - $this->assertEqual("IN (".implode(',', $ps).")", $usql); - - // Correct usage of single values (in array) - $in_values = array('value1'); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual("= :$key", $usql); - $this->assertEqual($in_value, $value); - - // Correct usage of single value - $in_value = 'value1'; - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', true); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual("= :$key", $usql); - $this->assertEqual($in_value, $value); - - // SQL_PARAMS_NAMED - NOT IN or <> - - // Correct usage of multiple values - $in_values = array('value1', 'value2', 'value3', 'value4'); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false); - $this->assertEqual(4, count($params)); - reset($in_values); - $ps = array(); - foreach ($params as $key => $value) { - $this->assertEqual(current($in_values), $value); - next($in_values); - $ps[] = ':'.$key; - } - $this->assertEqual("NOT IN (".implode(',', $ps).")", $usql); - - // Correct usage of single values (in array) - $in_values = array('value1'); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual("<> :$key", $usql); - $this->assertEqual($in_value, $value); - - // Correct usage of single value - $in_value = 'value1'; - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual("<> :$key", $usql); - $this->assertEqual($in_value, $value); - - // make sure the param names are unique - list($usql1, $params1) = $DB->get_in_or_equal(array(1,2,3), SQL_PARAMS_NAMED, 'param'); - list($usql2, $params2) = $DB->get_in_or_equal(array(1,2,3), SQL_PARAMS_NAMED, 'param'); - $params1 = array_keys($params1); - $params2 = array_keys($params2); - $common = array_intersect($params1, $params2); - $this->assertEqual(count($common), 0); - - // Some incorrect tests - - // Incorrect usage passing not-allowed params type - $in_values = array(1, 2, 3); - try { - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_DOLLAR, 'param', false); - $this->fail('An Exception is missing, expected due to not supported SQL_PARAMS_DOLLAR'); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'typenotimplement'); - } - - // Incorrect usage passing empty array - $in_values = array(); - try { - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false); - $this->fail('An Exception is missing, expected due to empty array of items'); - } catch (exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - // Test using $onemptyitems - - // Correct usage passing empty array and $onemptyitems = NULL (equal = true, QM) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, NULL); - $this->assertEqual(' IS NULL', $usql); - $this->assertIdentical(array(), $params); - - // Correct usage passing empty array and $onemptyitems = NULL (equal = false, NAMED) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, NULL); - $this->assertEqual(' IS NOT NULL', $usql); - $this->assertIdentical(array(), $params); - - // Correct usage passing empty array and $onemptyitems = true (equal = true, QM) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, true); - $this->assertEqual('= ?', $usql); - $this->assertIdentical(array(true), $params); - - // Correct usage passing empty array and $onemptyitems = true (equal = false, NAMED) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, true); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual('<> :'.$key, $usql); - $this->assertIdentical($value, true); - - // Correct usage passing empty array and $onemptyitems = -1 (equal = true, QM) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, -1); - $this->assertEqual('= ?', $usql); - $this->assertIdentical(array(-1), $params); - - // Correct usage passing empty array and $onemptyitems = -1 (equal = false, NAMED) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, -1); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual('<> :'.$key, $usql); - $this->assertIdentical($value, -1); - - // Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = true, QM) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_QM, 'param', true, 'onevalue'); - $this->assertEqual('= ?', $usql); - $this->assertIdentical(array('onevalue'), $params); - - // Correct usage passing empty array and $onemptyitems = 'onevalue' (equal = false, NAMED) - $in_values = array(); - list($usql, $params) = $DB->get_in_or_equal($in_values, SQL_PARAMS_NAMED, 'param', false, 'onevalue'); - $this->assertEqual(1, count($params)); - $value = reset($params); - $key = key($params); - $this->assertEqual('<> :'.$key, $usql); - $this->assertIdentical($value, 'onevalue'); - } - - public function test_fix_table_names() { - $DB = new moodle_database_for_testing(); - $prefix = $DB->get_prefix(); - - // Simple placeholder - $placeholder = "{user_123}"; - $this->assertIdentical($prefix."user_123", $DB->public_fix_table_names($placeholder)); - - // wrong table name - $placeholder = "{user-a}"; - $this->assertIdentical($placeholder, $DB->public_fix_table_names($placeholder)); - - // wrong table name - $placeholder = "{123user}"; - $this->assertIdentical($placeholder, $DB->public_fix_table_names($placeholder)); - - // Full SQL - $sql = "SELECT * FROM {user}, {funny_table_name}, {mdl_stupid_table} WHERE {user}.id = {funny_table_name}.userid"; - $expected = "SELECT * FROM {$prefix}user, {$prefix}funny_table_name, {$prefix}mdl_stupid_table WHERE {$prefix}user.id = {$prefix}funny_table_name.userid"; - $this->assertIdentical($expected, $DB->public_fix_table_names($sql)); - } - - function test_fix_sql_params() { - $DB = $this->tdb; - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - // Correct table placeholder substitution - $sql = "SELECT * FROM {{$tablename}}"; - $sqlarray = $DB->fix_sql_params($sql); - $this->assertEqual("SELECT * FROM {$DB->get_prefix()}".$tablename, $sqlarray[0]); - - // Conversions of all param types - $sql = array(); - $sql[SQL_PARAMS_NAMED] = "SELECT * FROM {$DB->get_prefix()}testtable WHERE name = :param1, course = :param2"; - $sql[SQL_PARAMS_QM] = "SELECT * FROM {$DB->get_prefix()}testtable WHERE name = ?, course = ?"; - $sql[SQL_PARAMS_DOLLAR] = "SELECT * FROM {$DB->get_prefix()}testtable WHERE name = \$1, course = \$2"; - - $params = array(); - $params[SQL_PARAMS_NAMED] = array('param1'=>'first record', 'param2'=>1); - $params[SQL_PARAMS_QM] = array('first record', 1); - $params[SQL_PARAMS_DOLLAR] = array('first record', 1); - - list($rsql, $rparams, $rtype) = $DB->fix_sql_params($sql[SQL_PARAMS_NAMED], $params[SQL_PARAMS_NAMED]); - $this->assertIdentical($rsql, $sql[$rtype]); - $this->assertIdentical($rparams, $params[$rtype]); - - list($rsql, $rparams, $rtype) = $DB->fix_sql_params($sql[SQL_PARAMS_QM], $params[SQL_PARAMS_QM]); - $this->assertIdentical($rsql, $sql[$rtype]); - $this->assertIdentical($rparams, $params[$rtype]); - - list($rsql, $rparams, $rtype) = $DB->fix_sql_params($sql[SQL_PARAMS_DOLLAR], $params[SQL_PARAMS_DOLLAR]); - $this->assertIdentical($rsql, $sql[$rtype]); - $this->assertIdentical($rparams, $params[$rtype]); - - - // Malformed table placeholder - $sql = "SELECT * FROM [testtable]"; - $sqlarray = $DB->fix_sql_params($sql); - $this->assertIdentical($sql, $sqlarray[0]); - - - // Mixed param types (colon and dollar) - $sql = "SELECT * FROM {{$tablename}} WHERE name = :param1, course = \$1"; - $params = array('param1' => 'record1', 'param2' => 3); - try { - $DB->fix_sql_params($sql, $params); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Mixed param types (question and dollar) - $sql = "SELECT * FROM {{$tablename}} WHERE name = ?, course = \$1"; - $params = array('param1' => 'record2', 'param2' => 5); - try { - $DB->fix_sql_params($sql, $params); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Too few params in sql - $sql = "SELECT * FROM {{$tablename}} WHERE name = ?, course = ?, id = ?"; - $params = array('record2', 3); - try { - $DB->fix_sql_params($sql, $params); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Too many params in array: no error, just use what is necessary - $params[] = 1; - $params[] = time(); - try { - $sqlarray = $DB->fix_sql_params($sql, $params); - $this->assertTrue(is_array($sqlarray)); - $this->assertEqual(count($sqlarray[1]), 3); - } catch (Exception $e) { - $this->fail("Unexpected ".get_class($e)." exception"); - } - - // Named params missing from array - $sql = "SELECT * FROM {{$tablename}} WHERE name = :name, course = :course"; - $params = array('wrongname' => 'record1', 'course' => 1); - try { - $DB->fix_sql_params($sql, $params); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Duplicate named param in query - this is a very important feature!! - // it helps with debugging of sloppy code - $sql = "SELECT * FROM {{$tablename}} WHERE name = :name, course = :name"; - $params = array('name' => 'record2', 'course' => 3); - try { - $DB->fix_sql_params($sql, $params); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Extra named param is ignored - $sql = "SELECT * FROM {{$tablename}} WHERE name = :name, course = :course"; - $params = array('name' => 'record1', 'course' => 1, 'extrastuff'=>'haha'); - try { - $sqlarray = $DB->fix_sql_params($sql, $params); - $this->assertTrue(is_array($sqlarray)); - $this->assertEqual(count($sqlarray[1]), 2); - } catch (Exception $e) { - $this->fail("Unexpected ".get_class($e)." exception"); - } - - // Params exceeding 30 chars length - $sql = "SELECT * FROM {{$tablename}} WHERE name = :long_placeholder_with_more_than_30"; - $params = array('long_placeholder_with_more_than_30' => 'record1'); - try { - $DB->fix_sql_params($sql, $params); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - // Booleans in NAMED params are casting to 1/0 int - $sql = "SELECT * FROM {{$tablename}} WHERE course = ? OR course = ?"; - $params = array(true, false); - list($sql, $params) = $DB->fix_sql_params($sql, $params); - $this->assertTrue(reset($params) === 1); - $this->assertTrue(next($params) === 0); - - // Booleans in QM params are casting to 1/0 int - $sql = "SELECT * FROM {{$tablename}} WHERE course = :course1 OR course = :course2"; - $params = array('course1' => true, 'course2' => false); - list($sql, $params) = $DB->fix_sql_params($sql, $params); - $this->assertTrue(reset($params) === 1); - $this->assertTrue(next($params) === 0); - - // Booleans in DOLLAR params are casting to 1/0 int - $sql = "SELECT * FROM {{$tablename}} WHERE course = \$1 OR course = \$2"; - $params = array(true, false); - list($sql, $params) = $DB->fix_sql_params($sql, $params); - $this->assertTrue(reset($params) === 1); - $this->assertTrue(next($params) === 0); - - // No data types are touched except bool - $sql = "SELECT * FROM {{$tablename}} WHERE name IN (?,?,?,?,?,?)"; - $inparams = array('abc', 'ABC', NULL, '1', 1, 1.4); - list($sql, $params) = $DB->fix_sql_params($sql, $inparams); - $this->assertIdentical(array_values($params), array_values($inparams)); - } - - public function test_strtok() { - // strtok was previously used by bound emulation, make sure it is not used any more - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $str = 'a?b?c?d'; - $this->assertIdentical(strtok($str, '?'), 'a'); - - $DB->get_records($tablename, array('id'=>1)); - - $this->assertIdentical(strtok('?'), 'b'); - } - - public function test_tweak_param_names() { - // Note the tweak_param_names() method is only available in the oracle driver, - // hence we look for expected results indirectly, by testing various DML methods - // with some "extreme" conditions causing the tweak to happen. - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - // Add some columns with 28 chars in the name - $table->add_field('long_int_columnname_with_28c', XMLDB_TYPE_INTEGER, '10'); - $table->add_field('long_dec_columnname_with_28c', XMLDB_TYPE_NUMBER, '10,2'); - $table->add_field('long_str_columnname_with_28c', XMLDB_TYPE_CHAR, '100'); - // Add some columns with 30 chars in the name - $table->add_field('long_int_columnname_with_30cxx', XMLDB_TYPE_INTEGER, '10'); - $table->add_field('long_dec_columnname_with_30cxx', XMLDB_TYPE_NUMBER, '10,2'); - $table->add_field('long_str_columnname_with_30cxx', XMLDB_TYPE_CHAR, '100'); - - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - - $dbman->create_table($table); - - $this->assertTrue($dbman->table_exists($tablename)); - - // Test insert record - $rec1 = new stdClass(); - $rec1->long_int_columnname_with_28c = 28; - $rec1->long_dec_columnname_with_28c = 28.28; - $rec1->long_str_columnname_with_28c = '28'; - $rec1->long_int_columnname_with_30cxx = 30; - $rec1->long_dec_columnname_with_30cxx = 30.30; - $rec1->long_str_columnname_with_30cxx = '30'; - - // insert_record() - $rec1->id = $DB->insert_record($tablename, $rec1); - $this->assertEqual($rec1, $DB->get_record($tablename, array('id' => $rec1->id))); - - // update_record() - $DB->update_record($tablename, $rec1); - $this->assertEqual($rec1, $DB->get_record($tablename, array('id' => $rec1->id))); - - // set_field() - $rec1->long_int_columnname_with_28c = 280; - $DB->set_field($tablename, 'long_int_columnname_with_28c', $rec1->long_int_columnname_with_28c, - array('id' => $rec1->id, 'long_int_columnname_with_28c' => 28)); - $rec1->long_dec_columnname_with_28c = 280.28; - $DB->set_field($tablename, 'long_dec_columnname_with_28c', $rec1->long_dec_columnname_with_28c, - array('id' => $rec1->id, 'long_dec_columnname_with_28c' => 28.28)); - $rec1->long_str_columnname_with_28c = '280'; - $DB->set_field($tablename, 'long_str_columnname_with_28c', $rec1->long_str_columnname_with_28c, - array('id' => $rec1->id, 'long_str_columnname_with_28c' => '28')); - $rec1->long_int_columnname_with_30cxx = 300; - $DB->set_field($tablename, 'long_int_columnname_with_30cxx', $rec1->long_int_columnname_with_30cxx, - array('id' => $rec1->id, 'long_int_columnname_with_30cxx' => 30)); - $rec1->long_dec_columnname_with_30cxx = 300.30; - $DB->set_field($tablename, 'long_dec_columnname_with_30cxx', $rec1->long_dec_columnname_with_30cxx, - array('id' => $rec1->id, 'long_dec_columnname_with_30cxx' => 30.30)); - $rec1->long_str_columnname_with_30cxx = '300'; - $DB->set_field($tablename, 'long_str_columnname_with_30cxx', $rec1->long_str_columnname_with_30cxx, - array('id' => $rec1->id, 'long_str_columnname_with_30cxx' => '30')); - $this->assertEqual($rec1, $DB->get_record($tablename, array('id' => $rec1->id))); - - // delete_records() - $rec2 = $DB->get_record($tablename, array('id' => $rec1->id)); - $rec2->id = $DB->insert_record($tablename, $rec2); - $this->assertEqual(2, $DB->count_records($tablename)); - $DB->delete_records($tablename, (array) $rec2); - $this->assertEqual(1, $DB->count_records($tablename)); - - // get_recordset() - $rs = $DB->get_recordset($tablename, (array) $rec1); - $iterations = 0; - foreach ($rs as $rec2) { - $iterations++; - } - $rs->close(); - $this->assertEqual(1, $iterations); - $this->assertEqual($rec1, $rec2); - - // get_records() - $recs = $DB->get_records($tablename, (array) $rec1); - $this->assertEqual(1, count($recs)); - $this->assertEqual($rec1, reset($recs)); - - // get_fieldset_select() - $select = 'id = :id AND - long_int_columnname_with_28c = :long_int_columnname_with_28c AND - long_dec_columnname_with_28c = :long_dec_columnname_with_28c AND - long_str_columnname_with_28c = :long_str_columnname_with_28c AND - long_int_columnname_with_30cxx = :long_int_columnname_with_30cxx AND - long_dec_columnname_with_30cxx = :long_dec_columnname_with_30cxx AND - long_str_columnname_with_30cxx = :long_str_columnname_with_30cxx'; - $fields = $DB->get_fieldset_select($tablename, 'long_int_columnname_with_28c', $select, (array)$rec1); - $this->assertEqual(1, count($fields)); - $this->assertEqual($rec1->long_int_columnname_with_28c, reset($fields)); - $fields = $DB->get_fieldset_select($tablename, 'long_dec_columnname_with_28c', $select, (array)$rec1); - $this->assertEqual($rec1->long_dec_columnname_with_28c, reset($fields)); - $fields = $DB->get_fieldset_select($tablename, 'long_str_columnname_with_28c', $select, (array)$rec1); - $this->assertEqual($rec1->long_str_columnname_with_28c, reset($fields)); - $fields = $DB->get_fieldset_select($tablename, 'long_int_columnname_with_30cxx', $select, (array)$rec1); - $this->assertEqual($rec1->long_int_columnname_with_30cxx, reset($fields)); - $fields = $DB->get_fieldset_select($tablename, 'long_dec_columnname_with_30cxx', $select, (array)$rec1); - $this->assertEqual($rec1->long_dec_columnname_with_30cxx, reset($fields)); - $fields = $DB->get_fieldset_select($tablename, 'long_str_columnname_with_30cxx', $select, (array)$rec1); - $this->assertEqual($rec1->long_str_columnname_with_30cxx, reset($fields)); - - // overlapping placeholders (progressive str_replace) - $overlapselect = 'id = :p AND - long_int_columnname_with_28c = :param1 AND - long_dec_columnname_with_28c = :param2 AND - long_str_columnname_with_28c = :param_with_29_characters_long AND - long_int_columnname_with_30cxx = :param_with_30_characters_long_ AND - long_dec_columnname_with_30cxx = :param_ AND - long_str_columnname_with_30cxx = :param__'; - $overlapparams = array( - 'p' => $rec1->id, - 'param1' => $rec1->long_int_columnname_with_28c, - 'param2' => $rec1->long_dec_columnname_with_28c, - 'param_with_29_characters_long' => $rec1->long_str_columnname_with_28c, - 'param_with_30_characters_long_' => $rec1->long_int_columnname_with_30cxx, - 'param_' => $rec1->long_dec_columnname_with_30cxx, - 'param__' => $rec1->long_str_columnname_with_30cxx); - $recs = $DB->get_records_select($tablename, $overlapselect, $overlapparams); - $this->assertEqual(1, count($recs)); - $this->assertEqual($rec1, reset($recs)); - - // execute() - $DB->execute("DELETE FROM {{$tablename}} WHERE $select", (array)$rec1); - $this->assertEqual(0, $DB->count_records($tablename)); - } - - public function test_get_tables() { - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - // Need to test with multiple DBs - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $original_count = count($DB->get_tables()); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - - $dbman->create_table($table); - $this->assertTrue(count($DB->get_tables()) == $original_count + 1); - - $dbman->drop_table($table); - $this->assertTrue(count($DB->get_tables()) == $original_count); - } - - public function test_get_indexes() { - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); - $table->add_index('course-id', XMLDB_INDEX_UNIQUE, array('course', 'id')); - $dbman->create_table($table); - - $indices = $DB->get_indexes($tablename); - $this->assertTrue(is_array($indices)); - $this->assertEqual(count($indices), 2); - // we do not care about index names for now - $first = array_shift($indices); - $second = array_shift($indices); - if (count($first['columns']) == 2) { - $composed = $first; - $single = $second; - } else { - $composed = $second; - $single = $first; - } - $this->assertFalse($single['unique']); - $this->assertTrue($composed['unique']); - $this->assertEqual(1, count($single['columns'])); - $this->assertEqual(2, count($composed['columns'])); - $this->assertEqual('course', $single['columns'][0]); - $this->assertEqual('course', $composed['columns'][0]); - $this->assertEqual('id', $composed['columns'][1]); - } - - public function test_get_columns() { - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, 'lala'); - $table->add_field('description', XMLDB_TYPE_TEXT, 'small', null, null, null, null); - $table->add_field('enumfield', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'test2'); - $table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200); - $table->add_field('onefloat', XMLDB_TYPE_FLOAT, '10,2', null, null, null, 300); - $table->add_field('anotherfloat', XMLDB_TYPE_FLOAT, null, null, null, null, 400); - $table->add_field('negativedfltint', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '-1'); - $table->add_field('negativedfltnumber', XMLDB_TYPE_NUMBER, '10', null, XMLDB_NOTNULL, null, '-2'); - $table->add_field('negativedfltfloat', XMLDB_TYPE_FLOAT, '10', null, XMLDB_NOTNULL, null, '-3'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $columns = $DB->get_columns($tablename); - $this->assertTrue(is_array($columns)); - - $fields = $table->getFields(); - $this->assertEqual(count($columns), count($fields)); - - $field = $columns['id']; - $this->assertEqual('R', $field->meta_type); - $this->assertTrue($field->auto_increment); - $this->assertTrue($field->unique); - - $field = $columns['course']; - $this->assertEqual('I', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertTrue($field->has_default); - $this->assertEqual(0, $field->default_value); - $this->assertTrue($field->not_null); - - $field = $columns['name']; - $this->assertEqual('C', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertEqual(255, $field->max_length); - $this->assertTrue($field->has_default); - $this->assertIdentical('lala', $field->default_value); - $this->assertFalse($field->not_null); - - $field = $columns['description']; - $this->assertEqual('X', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertFalse($field->has_default); - $this->assertIdentical(null, $field->default_value); - $this->assertFalse($field->not_null); - - $field = $columns['enumfield']; - $this->assertEqual('C', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertIdentical('test2', $field->default_value); - $this->assertTrue($field->not_null); - - $field = $columns['onenum']; - $this->assertEqual('N', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertEqual(10, $field->max_length); - $this->assertEqual(2, $field->scale); - $this->assertTrue($field->has_default); - $this->assertEqual(200.0, $field->default_value); - $this->assertFalse($field->not_null); - - $field = $columns['onefloat']; - $this->assertEqual('N', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertTrue($field->has_default); - $this->assertEqual(300.0, $field->default_value); - $this->assertFalse($field->not_null); - - $field = $columns['anotherfloat']; - $this->assertEqual('N', $field->meta_type); - $this->assertFalse($field->auto_increment); - $this->assertTrue($field->has_default); - $this->assertEqual(400.0, $field->default_value); - $this->assertFalse($field->not_null); - - // Test negative defaults in numerical columns - $field = $columns['negativedfltint']; - $this->assertTrue($field->has_default); - $this->assertEqual(-1, $field->default_value); - - $field = $columns['negativedfltnumber']; - $this->assertTrue($field->has_default); - $this->assertEqual(-2, $field->default_value); - - $field = $columns['negativedfltfloat']; - $this->assertTrue($field->has_default); - $this->assertEqual(-3, $field->default_value); - - for ($i = 0; $i < count($columns); $i++) { - if ($i == 0) { - $next_column = reset($columns); - $next_field = reset($fields); - } else { - $next_column = next($columns); - $next_field = next($fields); - } - - $this->assertEqual($next_column->name, $next_field->name); - } - - // Test get_columns for non-existing table returns empty array. MDL-30147 - $columns = $DB->get_columns('xxxx'); - $this->assertEqual(array(), $columns); - - // create something similar to "context_temp" with id column without sequence - $dbman->drop_table($table); - $table = $this->get_test_table(); - $tablename = $table->getName(); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $columns = $DB->get_columns($tablename); - $this->assertFalse($columns['id']->auto_increment); - } - - public function test_get_manager() { - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $this->assertTrue($dbman instanceof database_manager); - } - - public function test_setup_is_unicodedb() { - $DB = $this->tdb; - $this->assertTrue($DB->setup_is_unicodedb()); - } - - public function test_set_debug() { //tests get_debug() too - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $sql = "SELECT * FROM {{$tablename}}"; - - $prevdebug = $DB->get_debug(); - - ob_start(); - $DB->set_debug(true); - $this->assertTrue($DB->get_debug()); - $DB->execute($sql); - $DB->set_debug(false); - $this->assertFalse($DB->get_debug()); - $debuginfo = ob_get_contents(); - ob_end_clean(); - $this->assertFalse($debuginfo === ''); - - ob_start(); - $DB->execute($sql); - $debuginfo = ob_get_contents(); - ob_end_clean(); - $this->assertTrue($debuginfo === ''); - - $DB->set_debug($prevdebug); - } - - public function test_execute() { - $DB = $this->tdb; - $dbman = $this->tdb->get_manager(); - - $table1 = $this->get_test_table('1'); - $tablename1 = $table1->getName(); - $table1->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table1->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table1->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0'); - $table1->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); - $table1->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table1); - - $table2 = $this->get_test_table('2'); - $tablename2 = $table2->getName(); - $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table2); - - $DB->insert_record($tablename1, array('course' => 3, 'name' => 'aaa')); - $DB->insert_record($tablename1, array('course' => 1, 'name' => 'bbb')); - $DB->insert_record($tablename1, array('course' => 7, 'name' => 'ccc')); - $DB->insert_record($tablename1, array('course' => 3, 'name' => 'ddd')); - - // select results are ignored - $sql = "SELECT * FROM {{$tablename1}} WHERE course = :course"; - $this->assertTrue($DB->execute($sql, array('course'=>3))); - - // throw exception on error - $sql = "XXUPDATE SET XSSD"; - try { - $DB->execute($sql); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // update records - $sql = "UPDATE {{$tablename1}} - SET course = 6 - WHERE course = ?"; - $this->assertTrue($DB->execute($sql, array('3'))); - $this->assertEqual($DB->count_records($tablename1, array('course' => 6)), 2); - - // update records with subquery condition - // confirm that the option not using table aliases is cross-db - $sql = "UPDATE {{$tablename1}} - SET course = 0 - WHERE NOT EXISTS ( - SELECT course - FROM {{$tablename2}} tbl2 - WHERE tbl2.course = {{$tablename1}}.course - AND 1 = 0)"; // Really we don't update anything, but verify the syntax is allowed - $this->assertTrue($DB->execute($sql)); - - // insert from one into second table - $sql = "INSERT INTO {{$tablename2}} (course) - - SELECT course - FROM {{$tablename1}}"; - $this->assertTrue($DB->execute($sql)); - $this->assertEqual($DB->count_records($tablename2), 4); - } - - public function test_get_recordset() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $data = array(array('id' => 1, 'course' => 3, 'name' => 'record1', 'onetext'=>'abc'), - array('id' => 2, 'course' => 3, 'name' => 'record2', 'onetext'=>'abcd'), - array('id' => 3, 'course' => 5, 'name' => 'record3', 'onetext'=>'abcde')); - - foreach ($data as $record) { - $DB->insert_record($tablename, $record); - } - - // standard recordset iteration - $rs = $DB->get_recordset($tablename); - $this->assertTrue($rs instanceof moodle_recordset); - reset($data); - foreach($rs as $record) { - $data_record = current($data); - foreach ($record as $k => $v) { - $this->assertEqual($data_record[$k], $v); - } - next($data); - } - $rs->close(); - - // iterator style usage - $rs = $DB->get_recordset($tablename); - $this->assertTrue($rs instanceof moodle_recordset); - reset($data); - while ($rs->valid()) { - $record = $rs->current(); - $data_record = current($data); - foreach ($record as $k => $v) { - $this->assertEqual($data_record[$k], $v); - } - next($data); - $rs->next(); - } - $rs->close(); - - // make sure rewind is ignored - $rs = $DB->get_recordset($tablename); - $this->assertTrue($rs instanceof moodle_recordset); - reset($data); - $i = 0; - foreach($rs as $record) { - $i++; - $rs->rewind(); - if ($i > 10) { - $this->fail('revind not ignored in recordsets'); - break; - } - $data_record = current($data); - foreach ($record as $k => $v) { - $this->assertEqual($data_record[$k], $v); - } - next($data); - } - $rs->close(); - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => '1'); - try { - $rs = $DB->get_recordset($tablename, $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - - // notes: - // * limits are tested in test_get_recordset_sql() - // * where_clause() is used internally and is tested in test_get_records() - } - - public function test_get_recordset_iterator_keys() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, '0'); - $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $data = array(array('id'=> 1, 'course' => 3, 'name' => 'record1'), - array('id'=> 2, 'course' => 3, 'name' => 'record2'), - array('id'=> 3, 'course' => 5, 'name' => 'record3')); - foreach ($data as $record) { - $DB->insert_record($tablename, $record); - } - - // Test repeated numeric keys are returned ok - $rs = $DB->get_recordset($tablename, NULL, NULL, 'course, name, id'); - - reset($data); - $count = 0; - foreach($rs as $key => $record) { - $data_record = current($data); - $this->assertEqual($data_record['course'], $key); - next($data); - $count++; - } - $rs->close(); - $this->assertEqual($count, 3); - - // Test string keys are returned ok - $rs = $DB->get_recordset($tablename, NULL, NULL, 'name, course, id'); - - reset($data); - $count = 0; - foreach($rs as $key => $record) { - $data_record = current($data); - $this->assertEqual($data_record['name'], $key); - next($data); - $count++; - } - $rs->close(); - $this->assertEqual($count, 3); - - // Test numeric not starting in 1 keys are returned ok - $rs = $DB->get_recordset($tablename, NULL, 'id DESC', 'id, course, name'); - - $data = array_reverse($data); - reset($data); - $count = 0; - foreach($rs as $key => $record) { - $data_record = current($data); - $this->assertEqual($data_record['id'], $key); - next($data); - $count++; - } - $rs->close(); - $this->assertEqual($count, 3); - } - - public function test_get_recordset_list() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 2)); - - $rs = $DB->get_recordset_list($tablename, 'course', array(3, 2)); - - $counter = 0; - foreach ($rs as $record) { - $counter++; - } - $this->assertEqual(3, $counter); - $rs->close(); - - $rs = $DB->get_recordset_list($tablename, 'course',array()); /// Must return 0 rows without conditions. MDL-17645 - - $counter = 0; - foreach ($rs as $record) { - $counter++; - } - $rs->close(); - $this->assertEqual(0, $counter); - - // notes: - // * limits are tested in test_get_recordset_sql() - // * where_clause() is used internally and is tested in test_get_records() - } - - public function test_get_recordset_select() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 2)); - - $rs = $DB->get_recordset_select($tablename, ''); - $counter = 0; - foreach ($rs as $record) { - $counter++; - } - $rs->close(); - $this->assertEqual(4, $counter); - - $this->assertTrue($rs = $DB->get_recordset_select($tablename, 'course = 3')); - $counter = 0; - foreach ($rs as $record) { - $counter++; - } - $rs->close(); - $this->assertEqual(2, $counter); - - // notes: - // * limits are tested in test_get_recordset_sql() - } - - public function test_get_recordset_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $inskey1 = $DB->insert_record($tablename, array('course' => 3)); - $inskey2 = $DB->insert_record($tablename, array('course' => 5)); - $inskey3 = $DB->insert_record($tablename, array('course' => 4)); - $inskey4 = $DB->insert_record($tablename, array('course' => 3)); - $inskey5 = $DB->insert_record($tablename, array('course' => 2)); - $inskey6 = $DB->insert_record($tablename, array('course' => 1)); - $inskey7 = $DB->insert_record($tablename, array('course' => 0)); - - $rs = $DB->get_recordset_sql("SELECT * FROM {{$tablename}} WHERE course = ?", array(3)); - $counter = 0; - foreach ($rs as $record) { - $counter++; - } - $rs->close(); - $this->assertEqual(2, $counter); - - // limits - only need to test this case, the rest have been tested by test_get_records_sql() - // only limitfrom = skips that number of records - $rs = $DB->get_recordset_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 2, 0); - $records = array(); - foreach($rs as $key => $record) { - $records[$key] = $record; - } - $rs->close(); - $this->assertEqual(5, count($records)); - $this->assertEqual($inskey3, reset($records)->id); - $this->assertEqual($inskey7, end($records)->id); - - // note: fetching nulls, empties, LOBs already tested by test_insert_record() no needed here - } - - public function test_get_records() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 2)); - - // All records - $records = $DB->get_records($tablename); - $this->assertEqual(4, count($records)); - $this->assertEqual(3, $records[1]->course); - $this->assertEqual(3, $records[2]->course); - $this->assertEqual(5, $records[3]->course); - $this->assertEqual(2, $records[4]->course); - - // Records matching certain conditions - $records = $DB->get_records($tablename, array('course' => 3)); - $this->assertEqual(2, count($records)); - $this->assertEqual(3, $records[1]->course); - $this->assertEqual(3, $records[2]->course); - - // All records sorted by course - $records = $DB->get_records($tablename, null, 'course'); - $this->assertEqual(4, count($records)); - $current_record = reset($records); - $this->assertEqual(4, $current_record->id); - $current_record = next($records); - $this->assertEqual(1, $current_record->id); - $current_record = next($records); - $this->assertEqual(2, $current_record->id); - $current_record = next($records); - $this->assertEqual(3, $current_record->id); - - // All records, but get only one field - $records = $DB->get_records($tablename, null, '', 'id'); - $this->assertFalse(isset($records[1]->course)); - $this->assertTrue(isset($records[1]->id)); - $this->assertEqual(4, count($records)); - - // Booleans into params - $records = $DB->get_records($tablename, array('course' => true)); - $this->assertEqual(0, count($records)); - $records = $DB->get_records($tablename, array('course' => false)); - $this->assertEqual(0, count($records)); - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => '1'); - try { - $records = $DB->get_records($tablename, $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - - // test get_records passing non-existing table - // with params - try { - $records = $DB->get_records('xxxx', array('id' => 0)); - $this->fail('An Exception is missing, expected due to query against non-existing table'); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - if (debugging()) { - // information for developers only, normal users get general error message - $this->assertEqual($e->errorcode, 'ddltablenotexist'); - } - } - // and without params - try { - $records = $DB->get_records('xxxx', array()); - $this->fail('An Exception is missing, expected due to query against non-existing table'); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - if (debugging()) { - // information for developers only, normal users get general error message - $this->assertEqual($e->errorcode, 'ddltablenotexist'); - } - } - - // test get_records passing non-existing column - try { - $records = $DB->get_records($tablename, array('xxxx' => 0)); - $this->fail('An Exception is missing, expected due to query against non-existing column'); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - if (debugging()) { - // information for developers only, normal users get general error message - $this->assertEqual($e->errorcode, 'ddlfieldnotexist'); - } - } - - // note: delegate limits testing to test_get_records_sql() - } - - public function test_get_records_list() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 2)); - - $records = $DB->get_records_list($tablename, 'course', array(3, 2)); - $this->assertTrue(is_array($records)); - $this->assertEqual(3, count($records)); - $this->assertEqual(1, reset($records)->id); - $this->assertEqual(2, next($records)->id); - $this->assertEqual(4, next($records)->id); - - $this->assertIdentical(array(), $records = $DB->get_records_list($tablename, 'course', array())); /// Must return 0 rows without conditions. MDL-17645 - $this->assertEqual(0, count($records)); - - // note: delegate limits testing to test_get_records_sql() - } - - public function test_get_records_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $inskey1 = $DB->insert_record($tablename, array('course' => 3)); - $inskey2 = $DB->insert_record($tablename, array('course' => 5)); - $inskey3 = $DB->insert_record($tablename, array('course' => 4)); - $inskey4 = $DB->insert_record($tablename, array('course' => 3)); - $inskey5 = $DB->insert_record($tablename, array('course' => 2)); - $inskey6 = $DB->insert_record($tablename, array('course' => 1)); - $inskey7 = $DB->insert_record($tablename, array('course' => 0)); - - $table2 = $this->get_test_table("2"); - $tablename2 = $table2->getName(); - $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table2->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table2->add_field('nametext', XMLDB_TYPE_TEXT, 'small', null, null, null, null); - $table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table2); - - $DB->insert_record($tablename2, array('course'=>3, 'nametext'=>'badabing')); - $DB->insert_record($tablename2, array('course'=>4, 'nametext'=>'badabang')); - $DB->insert_record($tablename2, array('course'=>5, 'nametext'=>'badabung')); - $DB->insert_record($tablename2, array('course'=>6, 'nametext'=>'badabong')); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE course = ?", array(3)); - $this->assertEqual(2, count($records)); - $this->assertEqual($inskey1, reset($records)->id); - $this->assertEqual($inskey4, next($records)->id); - - // Awful test, requires debug enabled and sent to browser. Let's do that and restore after test - $this->enable_debugging(); - $records = $DB->get_records_sql("SELECT course AS id, course AS course FROM {{$tablename}}", null); - $this->assertFalse($this->get_debugging() === ''); - $this->assertEqual(6, count($records)); - - // negative limits = no limits - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, -1, -1); - $this->assertEqual(7, count($records)); - - // zero limits = no limits - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 0, 0); - $this->assertEqual(7, count($records)); - - // only limitfrom = skips that number of records - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 2, 0); - $this->assertEqual(5, count($records)); - $this->assertEqual($inskey3, reset($records)->id); - $this->assertEqual($inskey7, end($records)->id); - - // only limitnum = fetches that number of records - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 0, 3); - $this->assertEqual(3, count($records)); - $this->assertEqual($inskey1, reset($records)->id); - $this->assertEqual($inskey3, end($records)->id); - - // both limitfrom and limitnum = skips limitfrom records and fetches limitnum ones - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} ORDER BY id", null, 3, 2); - $this->assertEqual(2, count($records)); - $this->assertEqual($inskey4, reset($records)->id); - $this->assertEqual($inskey5, end($records)->id); - - // both limitfrom and limitnum in query having subqueris - // note the subquery skips records with course = 0 and 3 - $sql = "SELECT * FROM {{$tablename}} - WHERE course NOT IN ( - SELECT course FROM {{$tablename}} - WHERE course IN (0, 3)) - ORDER BY course"; - $records = $DB->get_records_sql($sql, null, 0, 2); // Skip 0, get 2 - $this->assertEqual(2, count($records)); - $this->assertEqual($inskey6, reset($records)->id); - $this->assertEqual($inskey5, end($records)->id); - $records = $DB->get_records_sql($sql, null, 2, 2); // Skip 2, get 2 - $this->assertEqual(2, count($records)); - $this->assertEqual($inskey3, reset($records)->id); - $this->assertEqual($inskey2, end($records)->id); - - // test 2 tables with aliases and limits with order bys - $sql = "SELECT t1.id, t1.course AS cid, t2.nametext - FROM {{$tablename}} t1, {{$tablename2}} t2 - WHERE t2.course=t1.course - ORDER BY t1.course, ". $DB->sql_compare_text('t2.nametext'); - $records = $DB->get_records_sql($sql, null, 2, 2); // Skip courses 3 and 6, get 4 and 5 - $this->assertEqual(2, count($records)); - $this->assertEqual('5', end($records)->cid); - $this->assertEqual('4', reset($records)->cid); - - // test 2 tables with aliases and limits with the highest INT limit works - $records = $DB->get_records_sql($sql, null, 2, PHP_INT_MAX); // Skip course {3,6}, get {4,5} - $this->assertEqual(2, count($records)); - $this->assertEqual('5', end($records)->cid); - $this->assertEqual('4', reset($records)->cid); - - // test 2 tables with aliases and limits with order bys (limit which is highest INT number) - $records = $DB->get_records_sql($sql, null, PHP_INT_MAX, 2); // Skip all courses - $this->assertEqual(0, count($records)); - - // test 2 tables with aliases and limits with order bys (limit which s highest INT number) - $records = $DB->get_records_sql($sql, null, PHP_INT_MAX, PHP_INT_MAX); // Skip all courses - $this->assertEqual(0, count($records)); - - // TODO: Test limits in queries having DISTINCT clauses - - // note: fetching nulls, empties, LOBs already tested by test_update_record() no needed here - } - - public function test_get_records_menu() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 2)); - - $records = $DB->get_records_menu($tablename, array('course' => 3)); - $this->assertTrue(is_array($records)); - $this->assertEqual(2, count($records)); - $this->assertFalse(empty($records[1])); - $this->assertFalse(empty($records[2])); - $this->assertEqual(3, $records[1]); - $this->assertEqual(3, $records[2]); - - // note: delegate limits testing to test_get_records_sql() - } - - public function test_get_records_select_menu() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - - $records = $DB->get_records_select_menu($tablename, "course > ?", array(2)); - $this->assertTrue(is_array($records)); - - $this->assertEqual(3, count($records)); - $this->assertFalse(empty($records[1])); - $this->assertTrue(empty($records[2])); - $this->assertFalse(empty($records[3])); - $this->assertFalse(empty($records[4])); - $this->assertEqual(3, $records[1]); - $this->assertEqual(3, $records[3]); - $this->assertEqual(5, $records[4]); - - // note: delegate limits testing to test_get_records_sql() - } - - public function test_get_records_sql_menu() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - - $records = $DB->get_records_sql_menu("SELECT * FROM {{$tablename}} WHERE course > ?", array(2)); - $this->assertTrue(is_array($records)); - - $this->assertEqual(3, count($records)); - $this->assertFalse(empty($records[1])); - $this->assertTrue(empty($records[2])); - $this->assertFalse(empty($records[3])); - $this->assertFalse(empty($records[4])); - $this->assertEqual(3, $records[1]); - $this->assertEqual(3, $records[3]); - $this->assertEqual(5, $records[4]); - - // note: delegate limits testing to test_get_records_sql() - } - - public function test_get_record() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - - $record = $DB->get_record($tablename, array('id' => 2)); - $this->assertTrue($record instanceof stdClass); - - $this->assertEqual(2, $record->course); - $this->assertEqual(2, $record->id); - } - - - public function test_get_record_select() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - - $record = $DB->get_record_select($tablename, "id = ?", array(2)); - $this->assertTrue($record instanceof stdClass); - - $this->assertEqual(2, $record->course); - - // note: delegates limit testing to test_get_records_sql() - } - - public function test_get_record_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - - // standard use - $record = $DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(2)); - $this->assertTrue($record instanceof stdClass); - $this->assertEqual(2, $record->course); - $this->assertEqual(2, $record->id); - - // backwards compatibility with $ignoremultiple - $this->assertFalse(IGNORE_MISSING); - $this->assertTrue(IGNORE_MULTIPLE); - - // record not found - ignore - $this->assertFalse($DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(666), IGNORE_MISSING)); - $this->assertFalse($DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(666), IGNORE_MULTIPLE)); - - // record not found error - try { - $DB->get_record_sql("SELECT * FROM {{$tablename}} WHERE id = ?", array(666), MUST_EXIST); - $this->fail("Exception expected"); - } catch (dml_missing_record_exception $e) { - $this->assertTrue(true); - } - - $this->enable_debugging(); - $this->assertTrue($DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), IGNORE_MISSING)); - $this->assertFalse($this->get_debugging() === ''); - - // multiple matches ignored - $this->assertTrue($DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), IGNORE_MULTIPLE)); - - // multiple found error - try { - $DB->get_record_sql("SELECT * FROM {{$tablename}}", array(), MUST_EXIST); - $this->fail("Exception expected"); - } catch (dml_multiple_records_exception $e) { - $this->assertTrue(true); - } - } - - public function test_get_field() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $id1 = $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 5)); - - $this->assertEqual(3, $DB->get_field($tablename, 'course', array('id' => $id1))); - $this->assertEqual(3, $DB->get_field($tablename, 'course', array('course' => 3))); - - $this->assertIdentical(false, $DB->get_field($tablename, 'course', array('course' => 11), IGNORE_MISSING)); - try { - $DB->get_field($tablename, 'course', array('course' => 4), MUST_EXIST); - $this->assertFail('Exception expected due to missing record'); - } catch (dml_exception $ex) { - $this->assertTrue(true); - } - - $this->enable_debugging(); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('course' => 5), IGNORE_MULTIPLE)); - $this->assertIdentical($this->get_debugging(), ''); - - $this->enable_debugging(); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('course' => 5), IGNORE_MISSING)); - $this->assertFalse($this->get_debugging() === ''); - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => '1'); - try { - $DB->get_field($tablename, 'course', $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - } - - public function test_get_field_select() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - - $this->assertEqual(3, $DB->get_field_select($tablename, 'course', "id = ?", array(1))); - } - - public function test_get_field_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - - $this->assertEqual(3, $DB->get_field_sql("SELECT course FROM {{$tablename}} WHERE id = ?", array(1))); - } - - public function test_get_fieldset_select() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 6)); - - $fieldset = $DB->get_fieldset_select($tablename, 'course', "course > ?", array(1)); - $this->assertTrue(is_array($fieldset)); - - $this->assertEqual(3, count($fieldset)); - $this->assertEqual(3, $fieldset[0]); - $this->assertEqual(2, $fieldset[1]); - $this->assertEqual(6, $fieldset[2]); - } - - public function test_get_fieldset_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 6)); - - $fieldset = $DB->get_fieldset_sql("SELECT * FROM {{$tablename}} WHERE course > ?", array(1)); - $this->assertTrue(is_array($fieldset)); - - $this->assertEqual(3, count($fieldset)); - $this->assertEqual(2, $fieldset[0]); - $this->assertEqual(3, $fieldset[1]); - $this->assertEqual(4, $fieldset[2]); - } - - public function test_insert_record_raw() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $record = (object)array('course' => 1, 'onechar' => 'xx'); - $before = clone($record); - $result = $DB->insert_record_raw($tablename, $record); - $this->assertIdentical(1, $result); - $this->assertIdentical($record, $before); - - $record = $DB->get_record($tablename, array('course' => 1)); - $this->assertTrue($record instanceof stdClass); - $this->assertIdentical('xx', $record->onechar); - - $result = $DB->insert_record_raw($tablename, array('course' => 2, 'onechar' => 'yy'), false); - $this->assertIdentical(true, $result); - - // note: bulk not implemented yet - $DB->insert_record_raw($tablename, array('course' => 3, 'onechar' => 'zz'), true, true); - $record = $DB->get_record($tablename, array('course' => 3)); - $this->assertTrue($record instanceof stdClass); - $this->assertIdentical('zz', $record->onechar); - - // custom sequence (id) - returnid is ignored - $result = $DB->insert_record_raw($tablename, array('id' => 10, 'course' => 3, 'onechar' => 'bb'), true, false, true); - $this->assertIdentical(true, $result); - $record = $DB->get_record($tablename, array('id' => 10)); - $this->assertTrue($record instanceof stdClass); - $this->assertIdentical('bb', $record->onechar); - - // custom sequence - missing id error - try { - $DB->insert_record_raw($tablename, array('course' => 3, 'onechar' => 'bb'), true, false, true); - $this->assertFail('Exception expected due to missing record'); - } catch (coding_exception $ex) { - $this->assertTrue(true); - } - - // wrong column error - try { - $DB->insert_record_raw($tablename, array('xxxxx' => 3, 'onechar' => 'bb')); - $this->assertFail('Exception expected due to invalid column'); - } catch (dml_exception $ex) { - $this->assertTrue(true); - } - - // create something similar to "context_temp" with id column without sequence - $dbman->drop_table($table); - $table = $this->get_test_table(); - $tablename = $table->getName(); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $record = (object)array('id'=>5, 'course' => 1); - $DB->insert_record_raw($tablename, $record, false, false, true); - $record = $DB->get_record($tablename, array()); - $this->assertEqual(5, $record->id); - } - - public function test_insert_record() { - // All the information in this test is fetched from DB by get_recordset() so we - // have such method properly tested against nulls, empties and friends... - - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 100); - $table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200); - $table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_field('onebinary', XMLDB_TYPE_BINARY, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertIdentical(1, $DB->insert_record($tablename, array('course' => 1), true)); - $record = $DB->get_record($tablename, array('course' => 1)); - $this->assertEqual(1, $record->id); - $this->assertEqual(100, $record->oneint); // Just check column defaults have been applied - $this->assertEqual(200, $record->onenum); - $this->assertIdentical('onestring', $record->onechar); - $this->assertNull($record->onetext); - $this->assertNull($record->onebinary); - - // without returning id, bulk not implemented - $result = $this->assertIdentical(true, $DB->insert_record($tablename, array('course' => 99), false, true)); - $record = $DB->get_record($tablename, array('course' => 99)); - $this->assertEqual(2, $record->id); - $this->assertEqual(99, $record->course); - - // Check nulls are set properly for all types - $record = new stdClass(); - $record->oneint = null; - $record->onenum = null; - $record->onechar = null; - $record->onetext = null; - $record->onebinary = null; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertEqual(0, $record->course); - $this->assertNull($record->oneint); - $this->assertNull($record->onenum); - $this->assertNull($record->onechar); - $this->assertNull($record->onetext); - $this->assertNull($record->onebinary); - - // Check zeros are set properly for all types - $record = new stdClass(); - $record->oneint = 0; - $record->onenum = 0; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertEqual(0, $record->oneint); - $this->assertEqual(0, $record->onenum); - - // Check booleans are set properly for all types - $record = new stdClass(); - $record->oneint = true; // trues - $record->onenum = true; - $record->onechar = true; - $record->onetext = true; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertEqual(1, $record->oneint); - $this->assertEqual(1, $record->onenum); - $this->assertEqual(1, $record->onechar); - $this->assertEqual(1, $record->onetext); - - $record = new stdClass(); - $record->oneint = false; // falses - $record->onenum = false; - $record->onechar = false; - $record->onetext = false; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertEqual(0, $record->oneint); - $this->assertEqual(0, $record->onenum); - $this->assertEqual(0, $record->onechar); - $this->assertEqual(0, $record->onetext); - - // Check string data causes exception in numeric types - $record = new stdClass(); - $record->oneint = 'onestring'; - $record->onenum = 0; - try { - $DB->insert_record($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - $record = new stdClass(); - $record->oneint = 0; - $record->onenum = 'onestring'; - try { - $DB->insert_record($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Check empty string data is stored as 0 in numeric datatypes - $record = new stdClass(); - $record->oneint = ''; // empty string - $record->onenum = 0; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertTrue(is_numeric($record->oneint) && $record->oneint == 0); - - $record = new stdClass(); - $record->oneint = 0; - $record->onenum = ''; // empty string - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0); - - // Check empty strings are set properly in string types - $record = new stdClass(); - $record->oneint = 0; - $record->onenum = 0; - $record->onechar = ''; - $record->onetext = ''; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertTrue($record->onechar === ''); - $this->assertTrue($record->onetext === ''); - - // Check operation ((210.10 + 39.92) - 150.02) against numeric types - $record = new stdClass(); - $record->oneint = ((210.10 + 39.92) - 150.02); - $record->onenum = ((210.10 + 39.92) - 150.02); - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertEqual(100, $record->oneint); - $this->assertEqual(100, $record->onenum); - - // Check various quotes/backslashes combinations in string types - $teststrings = array( - 'backslashes and quotes alone (even): "" \'\' \\\\', - 'backslashes and quotes alone (odd): """ \'\'\' \\\\\\', - 'backslashes and quotes sequences (even): \\"\\" \\\'\\\'', - 'backslashes and quotes sequences (odd): \\"\\"\\" \\\'\\\'\\\''); - foreach ($teststrings as $teststring) { - $record = new stdClass(); - $record->onechar = $teststring; - $record->onetext = $teststring; - $recid = $DB->insert_record($tablename, $record); - $record = $DB->get_record($tablename, array('id' => $recid)); - $this->assertEqual($teststring, $record->onechar); - $this->assertEqual($teststring, $record->onetext); - } - - // Check LOBs in text/binary columns - $clob = file_get_contents(dirname(__FILE__).'/fixtures/clob.txt'); - $blob = file_get_contents(dirname(__FILE__).'/fixtures/randombinary'); - $record = new stdClass(); - $record->onetext = $clob; - $record->onebinary = $blob; - $recid = $DB->insert_record($tablename, $record); - $rs = $DB->get_recordset($tablename, array('id' => $recid)); - $record = $rs->current(); - $rs->close(); - $this->assertEqual($clob, $record->onetext, 'Test CLOB insert (full contents output disabled)'); - $this->assertEqual($blob, $record->onebinary, 'Test BLOB insert (full contents output disabled)'); - - // And "small" LOBs too, just in case - $newclob = substr($clob, 0, 500); - $newblob = substr($blob, 0, 250); - $record = new stdClass(); - $record->onetext = $newclob; - $record->onebinary = $newblob; - $recid = $DB->insert_record($tablename, $record); - $rs = $DB->get_recordset($tablename, array('id' => $recid)); - $record = $rs->current(); - $rs->close(); - $this->assertEqual($newclob, $record->onetext, 'Test "small" CLOB insert (full contents output disabled)'); - $this->assertEqual($newblob, $record->onebinary, 'Test "small" BLOB insert (full contents output disabled)'); - $this->assertEqual(false, $rs->key()); // Ensure recordset key() method to be working ok after closing - - // And "diagnostic" LOBs too, just in case - $newclob = '\'"\\;/ěščřžýáíé'; - $newblob = '\'"\\;/ěščřžýáíé'; - $record = new stdClass(); - $record->onetext = $newclob; - $record->onebinary = $newblob; - $recid = $DB->insert_record($tablename, $record); - $rs = $DB->get_recordset($tablename, array('id' => $recid)); - $record = $rs->current(); - $rs->close(); - $this->assertIdentical($newclob, $record->onetext); - $this->assertIdentical($newblob, $record->onebinary); - $this->assertEqual(false, $rs->key()); // Ensure recordset key() method to be working ok after closing - - // test data is not modified - $record = new stdClass(); - $record->id = -1; // has to be ignored - $record->course = 3; - $record->lalala = 'lalal'; // unused - $before = clone($record); - $DB->insert_record($tablename, $record); - $this->assertEqual($record, $before); - - // make sure the id is always increasing and never reuses the same id - $id1 = $DB->insert_record($tablename, array('course' => 3)); - $id2 = $DB->insert_record($tablename, array('course' => 3)); - $this->assertTrue($id1 < $id2); - $DB->delete_records($tablename, array('id'=>$id2)); - $id3 = $DB->insert_record($tablename, array('course' => 3)); - $this->assertTrue($id2 < $id3); - $DB->delete_records($tablename, array()); - $id4 = $DB->insert_record($tablename, array('course' => 3)); - $this->assertTrue($id3 < $id4); - - // Test saving a float in a CHAR column, and reading it back. - $id = $DB->insert_record($tablename, array('onechar' => 1.0)); - $this->assertEqual(1.0, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onechar' => 1e20)); - $this->assertEqual(1e20, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onechar' => 1e-4)); - $this->assertEqual(1e-4, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onechar' => 1e-5)); - $this->assertEqual(1e-5, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onechar' => 1e-300)); - $this->assertEqual(1e-300, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onechar' => 1e300)); - $this->assertEqual(1e300, $DB->get_field($tablename, 'onechar', array('id' => $id))); - - // Test saving a float in a TEXT column, and reading it back. - $id = $DB->insert_record($tablename, array('onetext' => 1.0)); - $this->assertEqual(1.0, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onetext' => 1e20)); - $this->assertEqual(1e20, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onetext' => 1e-4)); - $this->assertEqual(1e-4, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onetext' => 1e-5)); - $this->assertEqual(1e-5, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onetext' => 1e-300)); - $this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $id = $DB->insert_record($tablename, array('onetext' => 1e300)); - $this->assertEqual(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id))); - } - - public function test_import_record() { - // All the information in this test is fetched from DB by get_recordset() so we - // have such method properly tested against nulls, empties and friends... - - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 100); - $table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200); - $table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_field('onebinary', XMLDB_TYPE_BINARY, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertIdentical(1, $DB->insert_record($tablename, array('course' => 1), true)); - $record = $DB->get_record($tablename, array('course' => 1)); - $this->assertEqual(1, $record->id); - $this->assertEqual(100, $record->oneint); // Just check column defaults have been applied - $this->assertEqual(200, $record->onenum); - $this->assertIdentical('onestring', $record->onechar); - $this->assertNull($record->onetext); - $this->assertNull($record->onebinary); - - // ignore extra columns - $record = (object)array('id'=>13, 'course'=>2, 'xxxx'=>788778); - $before = clone($record); - $this->assertIdentical(true, $DB->import_record($tablename, $record)); - $this->assertIdentical($record, $before); - $records = $DB->get_records($tablename); - $this->assertEqual(2, $records[13]->course); - - // Check nulls are set properly for all types - $record = new stdClass(); - $record->id = 20; - $record->oneint = null; - $record->onenum = null; - $record->onechar = null; - $record->onetext = null; - $record->onebinary = null; - $this->assertTrue($DB->import_record($tablename, $record)); - $record = $DB->get_record($tablename, array('id' => 20)); - $this->assertEqual(0, $record->course); - $this->assertNull($record->oneint); - $this->assertNull($record->onenum); - $this->assertNull($record->onechar); - $this->assertNull($record->onetext); - $this->assertNull($record->onebinary); - - // Check zeros are set properly for all types - $record = new stdClass(); - $record->id = 23; - $record->oneint = 0; - $record->onenum = 0; - $this->assertTrue($DB->import_record($tablename, $record)); - $record = $DB->get_record($tablename, array('id' => 23)); - $this->assertEqual(0, $record->oneint); - $this->assertEqual(0, $record->onenum); - - // Check string data causes exception in numeric types - $record = new stdClass(); - $record->id = 32; - $record->oneint = 'onestring'; - $record->onenum = 0; - try { - $DB->import_record($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - $record = new stdClass(); - $record->id = 35; - $record->oneint = 0; - $record->onenum = 'onestring'; - try { - $DB->import_record($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Check empty strings are set properly in string types - $record = new stdClass(); - $record->id = 44; - $record->oneint = 0; - $record->onenum = 0; - $record->onechar = ''; - $record->onetext = ''; - $this->assertTrue($DB->import_record($tablename, $record)); - $record = $DB->get_record($tablename, array('id' => 44)); - $this->assertTrue($record->onechar === ''); - $this->assertTrue($record->onetext === ''); - - // Check operation ((210.10 + 39.92) - 150.02) against numeric types - $record = new stdClass(); - $record->id = 47; - $record->oneint = ((210.10 + 39.92) - 150.02); - $record->onenum = ((210.10 + 39.92) - 150.02); - $this->assertTrue($DB->import_record($tablename, $record)); - $record = $DB->get_record($tablename, array('id' => 47)); - $this->assertEqual(100, $record->oneint); - $this->assertEqual(100, $record->onenum); - - // Check various quotes/backslashes combinations in string types - $i = 50; - $teststrings = array( - 'backslashes and quotes alone (even): "" \'\' \\\\', - 'backslashes and quotes alone (odd): """ \'\'\' \\\\\\', - 'backslashes and quotes sequences (even): \\"\\" \\\'\\\'', - 'backslashes and quotes sequences (odd): \\"\\"\\" \\\'\\\'\\\''); - foreach ($teststrings as $teststring) { - $record = new stdClass(); - $record->id = $i; - $record->onechar = $teststring; - $record->onetext = $teststring; - $this->assertTrue($DB->import_record($tablename, $record)); - $record = $DB->get_record($tablename, array('id' => $i)); - $this->assertEqual($teststring, $record->onechar); - $this->assertEqual($teststring, $record->onetext); - $i = $i + 3; - } - - // Check LOBs in text/binary columns - $clob = file_get_contents(dirname(__FILE__).'/fixtures/clob.txt'); - $record = new stdClass(); - $record->id = 70; - $record->onetext = $clob; - $record->onebinary = ''; - $this->assertTrue($DB->import_record($tablename, $record)); - $rs = $DB->get_recordset($tablename, array('id' => 70)); - $record = $rs->current(); - $rs->close(); - $this->assertEqual($clob, $record->onetext, 'Test CLOB insert (full contents output disabled)'); - - $blob = file_get_contents(dirname(__FILE__).'/fixtures/randombinary'); - $record = new stdClass(); - $record->id = 71; - $record->onetext = ''; - $record->onebinary = $blob; - $this->assertTrue($DB->import_record($tablename, $record)); - $rs = $DB->get_recordset($tablename, array('id' => 71)); - $record = $rs->current(); - $rs->close(); - $this->assertEqual($blob, $record->onebinary, 'Test BLOB insert (full contents output disabled)'); - - // And "small" LOBs too, just in case - $newclob = substr($clob, 0, 500); - $newblob = substr($blob, 0, 250); - $record = new stdClass(); - $record->id = 73; - $record->onetext = $newclob; - $record->onebinary = $newblob; - $this->assertTrue($DB->import_record($tablename, $record)); - $rs = $DB->get_recordset($tablename, array('id' => 73)); - $record = $rs->current(); - $rs->close(); - $this->assertEqual($newclob, $record->onetext, 'Test "small" CLOB insert (full contents output disabled)'); - $this->assertEqual($newblob, $record->onebinary, 'Test "small" BLOB insert (full contents output disabled)'); - $this->assertEqual(false, $rs->key()); // Ensure recordset key() method to be working ok after closing - } - - public function test_update_record_raw() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $DB->insert_record($tablename, array('course' => 3)); - - $record = $DB->get_record($tablename, array('course' => 1)); - $record->course = 2; - $this->assertTrue($DB->update_record_raw($tablename, $record)); - $this->assertEqual(0, $DB->count_records($tablename, array('course' => 1))); - $this->assertEqual(1, $DB->count_records($tablename, array('course' => 2))); - $this->assertEqual(1, $DB->count_records($tablename, array('course' => 3))); - - $record = $DB->get_record($tablename, array('course' => 3)); - $record->xxxxx = 2; - try { - $DB->update_record_raw($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof moodle_exception); - } - - $record = $DB->get_record($tablename, array('course' => 3)); - unset($record->id); - try { - $DB->update_record_raw($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - } - - public function test_update_record() { - - // All the information in this test is fetched from DB by get_record() so we - // have such method properly tested against nulls, empties and friends... - - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, 100); - $table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null, 200); - $table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null, 'onestring'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_field('onebinary', XMLDB_TYPE_BINARY, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $record = $DB->get_record($tablename, array('course' => 1)); - $record->course = 2; - - $this->assertTrue($DB->update_record($tablename, $record)); - $this->assertFalse($record = $DB->get_record($tablename, array('course' => 1))); - $this->assertTrue($record = $DB->get_record($tablename, array('course' => 2))); - $this->assertEqual(100, $record->oneint); // Just check column defaults have been applied - $this->assertEqual(200, $record->onenum); - $this->assertEqual('onestring', $record->onechar); - $this->assertNull($record->onetext); - $this->assertNull($record->onebinary); - - // Check nulls are set properly for all types - $record->oneint = null; - $record->onenum = null; - $record->onechar = null; - $record->onetext = null; - $record->onebinary = null; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertNull($record->oneint); - $this->assertNull($record->onenum); - $this->assertNull($record->onechar); - $this->assertNull($record->onetext); - $this->assertNull($record->onebinary); - - // Check zeros are set properly for all types - $record->oneint = 0; - $record->onenum = 0; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual(0, $record->oneint); - $this->assertEqual(0, $record->onenum); - - // Check booleans are set properly for all types - $record->oneint = true; // trues - $record->onenum = true; - $record->onechar = true; - $record->onetext = true; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual(1, $record->oneint); - $this->assertEqual(1, $record->onenum); - $this->assertEqual(1, $record->onechar); - $this->assertEqual(1, $record->onetext); - - $record->oneint = false; // falses - $record->onenum = false; - $record->onechar = false; - $record->onetext = false; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual(0, $record->oneint); - $this->assertEqual(0, $record->onenum); - $this->assertEqual(0, $record->onechar); - $this->assertEqual(0, $record->onetext); - - // Check string data causes exception in numeric types - $record->oneint = 'onestring'; - $record->onenum = 0; - try { - $DB->update_record($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - $record->oneint = 0; - $record->onenum = 'onestring'; - try { - $DB->update_record($tablename, $record); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Check empty string data is stored as 0 in numeric datatypes - $record->oneint = ''; // empty string - $record->onenum = 0; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertTrue(is_numeric($record->oneint) && $record->oneint == 0); - - $record->oneint = 0; - $record->onenum = ''; // empty string - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertTrue(is_numeric($record->onenum) && $record->onenum == 0); - - // Check empty strings are set properly in string types - $record->oneint = 0; - $record->onenum = 0; - $record->onechar = ''; - $record->onetext = ''; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertTrue($record->onechar === ''); - $this->assertTrue($record->onetext === ''); - - // Check operation ((210.10 + 39.92) - 150.02) against numeric types - $record->oneint = ((210.10 + 39.92) - 150.02); - $record->onenum = ((210.10 + 39.92) - 150.02); - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual(100, $record->oneint); - $this->assertEqual(100, $record->onenum); - - // Check various quotes/backslashes combinations in string types - $teststrings = array( - 'backslashes and quotes alone (even): "" \'\' \\\\', - 'backslashes and quotes alone (odd): """ \'\'\' \\\\\\', - 'backslashes and quotes sequences (even): \\"\\" \\\'\\\'', - 'backslashes and quotes sequences (odd): \\"\\"\\" \\\'\\\'\\\''); - foreach ($teststrings as $teststring) { - $record->onechar = $teststring; - $record->onetext = $teststring; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual($teststring, $record->onechar); - $this->assertEqual($teststring, $record->onetext); - } - - // Check LOBs in text/binary columns - $clob = file_get_contents(dirname(__FILE__).'/fixtures/clob.txt'); - $blob = file_get_contents(dirname(__FILE__).'/fixtures/randombinary'); - $record->onetext = $clob; - $record->onebinary = $blob; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual($clob, $record->onetext, 'Test CLOB update (full contents output disabled)'); - $this->assertEqual($blob, $record->onebinary, 'Test BLOB update (full contents output disabled)'); - - // And "small" LOBs too, just in case - $newclob = substr($clob, 0, 500); - $newblob = substr($blob, 0, 250); - $record->onetext = $newclob; - $record->onebinary = $newblob; - $DB->update_record($tablename, $record); - $record = $DB->get_record($tablename, array('course' => 2)); - $this->assertEqual($newclob, $record->onetext, 'Test "small" CLOB update (full contents output disabled)'); - $this->assertEqual($newblob, $record->onebinary, 'Test "small" BLOB update (full contents output disabled)'); - - // Test saving a float in a CHAR column, and reading it back. - $id = $DB->insert_record($tablename, array('onechar' => 'X')); - $DB->update_record($tablename, array('id' => $id, 'onechar' => 1.0)); - $this->assertEqual(1.0, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onechar' => 1e20)); - $this->assertEqual(1e20, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onechar' => 1e-4)); - $this->assertEqual(1e-4, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onechar' => 1e-5)); - $this->assertEqual(1e-5, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onechar' => 1e-300)); - $this->assertEqual(1e-300, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onechar' => 1e300)); - $this->assertEqual(1e300, $DB->get_field($tablename, 'onechar', array('id' => $id))); - - // Test saving a float in a TEXT column, and reading it back. - $id = $DB->insert_record($tablename, array('onetext' => 'X')); - $DB->update_record($tablename, array('id' => $id, 'onetext' => 1.0)); - $this->assertEqual(1.0, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onetext' => 1e20)); - $this->assertEqual(1e20, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onetext' => 1e-4)); - $this->assertEqual(1e-4, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onetext' => 1e-5)); - $this->assertEqual(1e-5, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onetext' => 1e-300)); - $this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->update_record($tablename, array('id' => $id, 'onetext' => 1e300)); - $this->assertEqual(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id))); - } - - public function test_set_field() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - // simple set_field - $id1 = $DB->insert_record($tablename, array('course' => 1)); - $id2 = $DB->insert_record($tablename, array('course' => 1)); - $id3 = $DB->insert_record($tablename, array('course' => 3)); - $this->assertTrue($DB->set_field($tablename, 'course', 2, array('id' => $id1))); - $this->assertEqual(2, $DB->get_field($tablename, 'course', array('id' => $id1))); - $this->assertEqual(1, $DB->get_field($tablename, 'course', array('id' => $id2))); - $this->assertEqual(3, $DB->get_field($tablename, 'course', array('id' => $id3))); - $DB->delete_records($tablename, array()); - - // multiple fields affected - $id1 = $DB->insert_record($tablename, array('course' => 1)); - $id2 = $DB->insert_record($tablename, array('course' => 1)); - $id3 = $DB->insert_record($tablename, array('course' => 3)); - $DB->set_field($tablename, 'course', '5', array('course' => 1)); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('id' => $id1))); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('id' => $id2))); - $this->assertEqual(3, $DB->get_field($tablename, 'course', array('id' => $id3))); - $DB->delete_records($tablename, array()); - - // no field affected - $id1 = $DB->insert_record($tablename, array('course' => 1)); - $id2 = $DB->insert_record($tablename, array('course' => 1)); - $id3 = $DB->insert_record($tablename, array('course' => 3)); - $DB->set_field($tablename, 'course', '5', array('course' => 0)); - $this->assertEqual(1, $DB->get_field($tablename, 'course', array('id' => $id1))); - $this->assertEqual(1, $DB->get_field($tablename, 'course', array('id' => $id2))); - $this->assertEqual(3, $DB->get_field($tablename, 'course', array('id' => $id3))); - $DB->delete_records($tablename, array()); - - // all fields - no condition - $id1 = $DB->insert_record($tablename, array('course' => 1)); - $id2 = $DB->insert_record($tablename, array('course' => 1)); - $id3 = $DB->insert_record($tablename, array('course' => 3)); - $DB->set_field($tablename, 'course', 5, array()); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('id' => $id1))); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('id' => $id2))); - $this->assertEqual(5, $DB->get_field($tablename, 'course', array('id' => $id3))); - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => '1'); - try { - $DB->set_field($tablename, 'onechar', 'frog', $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - - // Test saving a float in a CHAR column, and reading it back. - $id = $DB->insert_record($tablename, array('onechar' => 'X')); - $DB->set_field($tablename, 'onechar', 1.0, array('id' => $id)); - $this->assertEqual(1.0, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->set_field($tablename, 'onechar', 1e20, array('id' => $id)); - $this->assertEqual(1e20, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->set_field($tablename, 'onechar', 1e-4, array('id' => $id)); - $this->assertEqual(1e-4, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->set_field($tablename, 'onechar', 1e-5, array('id' => $id)); - $this->assertEqual(1e-5, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->set_field($tablename, 'onechar', 1e-300, array('id' => $id)); - $this->assertEqual(1e-300, $DB->get_field($tablename, 'onechar', array('id' => $id))); - $DB->set_field($tablename, 'onechar', 1e300, array('id' => $id)); - $this->assertEqual(1e300, $DB->get_field($tablename, 'onechar', array('id' => $id))); - - // Test saving a float in a TEXT column, and reading it back. - $id = $DB->insert_record($tablename, array('onetext' => 'X')); - $DB->set_field($tablename, 'onetext', 1.0, array('id' => $id)); - $this->assertEqual(1.0, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->set_field($tablename, 'onetext', 1e20, array('id' => $id)); - $this->assertEqual(1e20, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->set_field($tablename, 'onetext', 1e-4, array('id' => $id)); - $this->assertEqual(1e-4, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->set_field($tablename, 'onetext', 1e-5, array('id' => $id)); - $this->assertEqual(1e-5, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->set_field($tablename, 'onetext', 1e-300, array('id' => $id)); - $this->assertEqual(1e-300, $DB->get_field($tablename, 'onetext', array('id' => $id))); - $DB->set_field($tablename, 'onetext', 1e300, array('id' => $id)); - $this->assertEqual(1e300, $DB->get_field($tablename, 'onetext', array('id' => $id))); - - // Note: All the nulls, booleans, empties, quoted and backslashes tests - // go to set_field_select() because set_field() is just one wrapper over it - } - - public function test_set_field_select() { - - // All the information in this test is fetched from DB by get_field() so we - // have such method properly tested against nulls, empties and friends... - - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('oneint', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null); - $table->add_field('onenum', XMLDB_TYPE_NUMBER, '10,2', null, null, null); - $table->add_field('onechar', XMLDB_TYPE_CHAR, '100', null, null, null); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_field('onebinary', XMLDB_TYPE_BINARY, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - - $this->assertTrue($DB->set_field_select($tablename, 'course', 2, 'id = ?', array(1))); - $this->assertEqual(2, $DB->get_field($tablename, 'course', array('id' => 1))); - - // Check nulls are set properly for all types - $DB->set_field_select($tablename, 'oneint', null, 'id = ?', array(1)); // trues - $DB->set_field_select($tablename, 'onenum', null, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onechar', null, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onetext', null, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onebinary', null, 'id = ?', array(1)); - $this->assertNull($DB->get_field($tablename, 'oneint', array('id' => 1))); - $this->assertNull($DB->get_field($tablename, 'onenum', array('id' => 1))); - $this->assertNull($DB->get_field($tablename, 'onechar', array('id' => 1))); - $this->assertNull($DB->get_field($tablename, 'onetext', array('id' => 1))); - $this->assertNull($DB->get_field($tablename, 'onebinary', array('id' => 1))); - - // Check zeros are set properly for all types - $DB->set_field_select($tablename, 'oneint', 0, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onenum', 0, 'id = ?', array(1)); - $this->assertEqual(0, $DB->get_field($tablename, 'oneint', array('id' => 1))); - $this->assertEqual(0, $DB->get_field($tablename, 'onenum', array('id' => 1))); - - // Check booleans are set properly for all types - $DB->set_field_select($tablename, 'oneint', true, 'id = ?', array(1)); // trues - $DB->set_field_select($tablename, 'onenum', true, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onechar', true, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onetext', true, 'id = ?', array(1)); - $this->assertEqual(1, $DB->get_field($tablename, 'oneint', array('id' => 1))); - $this->assertEqual(1, $DB->get_field($tablename, 'onenum', array('id' => 1))); - $this->assertEqual(1, $DB->get_field($tablename, 'onechar', array('id' => 1))); - $this->assertEqual(1, $DB->get_field($tablename, 'onetext', array('id' => 1))); - - $DB->set_field_select($tablename, 'oneint', false, 'id = ?', array(1)); // falses - $DB->set_field_select($tablename, 'onenum', false, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onechar', false, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onetext', false, 'id = ?', array(1)); - $this->assertEqual(0, $DB->get_field($tablename, 'oneint', array('id' => 1))); - $this->assertEqual(0, $DB->get_field($tablename, 'onenum', array('id' => 1))); - $this->assertEqual(0, $DB->get_field($tablename, 'onechar', array('id' => 1))); - $this->assertEqual(0, $DB->get_field($tablename, 'onetext', array('id' => 1))); - - // Check string data causes exception in numeric types - try { - $DB->set_field_select($tablename, 'oneint', 'onestring', 'id = ?', array(1)); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - try { - $DB->set_field_select($tablename, 'onenum', 'onestring', 'id = ?', array(1)); - $this->fail("Expecting an exception, none occurred"); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - } - - // Check empty string data is stored as 0 in numeric datatypes - $DB->set_field_select($tablename, 'oneint', '', 'id = ?', array(1)); - $field = $DB->get_field($tablename, 'oneint', array('id' => 1)); - $this->assertTrue(is_numeric($field) && $field == 0); - - $DB->set_field_select($tablename, 'onenum', '', 'id = ?', array(1)); - $field = $DB->get_field($tablename, 'onenum', array('id' => 1)); - $this->assertTrue(is_numeric($field) && $field == 0); - - // Check empty strings are set properly in string types - $DB->set_field_select($tablename, 'onechar', '', 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onetext', '', 'id = ?', array(1)); - $this->assertTrue($DB->get_field($tablename, 'onechar', array('id' => 1)) === ''); - $this->assertTrue($DB->get_field($tablename, 'onetext', array('id' => 1)) === ''); - - // Check operation ((210.10 + 39.92) - 150.02) against numeric types - $DB->set_field_select($tablename, 'oneint', ((210.10 + 39.92) - 150.02), 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onenum', ((210.10 + 39.92) - 150.02), 'id = ?', array(1)); - $this->assertEqual(100, $DB->get_field($tablename, 'oneint', array('id' => 1))); - $this->assertEqual(100, $DB->get_field($tablename, 'onenum', array('id' => 1))); - - // Check various quotes/backslashes combinations in string types - $teststrings = array( - 'backslashes and quotes alone (even): "" \'\' \\\\', - 'backslashes and quotes alone (odd): """ \'\'\' \\\\\\', - 'backslashes and quotes sequences (even): \\"\\" \\\'\\\'', - 'backslashes and quotes sequences (odd): \\"\\"\\" \\\'\\\'\\\''); - foreach ($teststrings as $teststring) { - $DB->set_field_select($tablename, 'onechar', $teststring, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onetext', $teststring, 'id = ?', array(1)); - $this->assertEqual($teststring, $DB->get_field($tablename, 'onechar', array('id' => 1))); - $this->assertEqual($teststring, $DB->get_field($tablename, 'onetext', array('id' => 1))); - } - - // Check LOBs in text/binary columns - $clob = file_get_contents(dirname(__FILE__).'/fixtures/clob.txt'); - $blob = file_get_contents(dirname(__FILE__).'/fixtures/randombinary'); - $DB->set_field_select($tablename, 'onetext', $clob, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onebinary', $blob, 'id = ?', array(1)); - $this->assertEqual($clob, $DB->get_field($tablename, 'onetext', array('id' => 1)), 'Test CLOB set_field (full contents output disabled)'); - $this->assertEqual($blob, $DB->get_field($tablename, 'onebinary', array('id' => 1)), 'Test BLOB set_field (full contents output disabled)'); - - // And "small" LOBs too, just in case - $newclob = substr($clob, 0, 500); - $newblob = substr($blob, 0, 250); - $DB->set_field_select($tablename, 'onetext', $newclob, 'id = ?', array(1)); - $DB->set_field_select($tablename, 'onebinary', $newblob, 'id = ?', array(1)); - $this->assertEqual($newclob, $DB->get_field($tablename, 'onetext', array('id' => 1)), 'Test "small" CLOB set_field (full contents output disabled)'); - $this->assertEqual($newblob, $DB->get_field($tablename, 'onebinary', array('id' => 1)), 'Test "small" BLOB set_field (full contents output disabled)'); - - // This is the failure from MDL-24863. This was giving an error on MSSQL, - // which converts the '1' to an integer, which cannot then be compared with - // onetext cast to a varchar. This should be fixed and working now. - $newchar = 'frog'; - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $params = array('onetext' => '1'); - try { - $DB->set_field_select($tablename, 'onechar', $newchar, $DB->sql_compare_text('onetext') . ' = ?', $params); - $this->assertTrue(true, 'No exceptions thrown with numerical text param comparison for text field.'); - } catch (dml_exception $e) { - $this->assertFalse(true, 'We have an unexpected exception.'); - throw $e; - } - - - } - - public function test_count_records() { - $DB = $this->tdb; - - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertEqual(0, $DB->count_records($tablename)); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 4)); - $DB->insert_record($tablename, array('course' => 5)); - - $this->assertEqual(3, $DB->count_records($tablename)); - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => '1'); - try { - $DB->count_records($tablename, $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - } - - public function test_count_records_select() { - $DB = $this->tdb; - - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertEqual(0, $DB->count_records($tablename)); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 4)); - $DB->insert_record($tablename, array('course' => 5)); - - $this->assertEqual(2, $DB->count_records_select($tablename, 'course > ?', array(3))); - } - - public function test_count_records_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertEqual(0, $DB->count_records($tablename)); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 4)); - $DB->insert_record($tablename, array('course' => 5)); - - $this->assertEqual(2, $DB->count_records_sql("SELECT COUNT(*) FROM {{$tablename}} WHERE course > ?", array(3))); - } - - public function test_record_exists() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertEqual(0, $DB->count_records($tablename)); - - $this->assertFalse($DB->record_exists($tablename, array('course' => 3))); - $DB->insert_record($tablename, array('course' => 3)); - - $this->assertTrue($DB->record_exists($tablename, array('course' => 3))); - - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => '1'); - try { - $DB->record_exists($tablename, $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - } - - public function test_record_exists_select() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertEqual(0, $DB->count_records($tablename)); - - $this->assertFalse($DB->record_exists_select($tablename, "course = ?", array(3))); - $DB->insert_record($tablename, array('course' => 3)); - - $this->assertTrue($DB->record_exists_select($tablename, "course = ?", array(3))); - } - - public function test_record_exists_sql() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertEqual(0, $DB->count_records($tablename)); - - $this->assertFalse($DB->record_exists_sql("SELECT * FROM {{$tablename}} WHERE course = ?", array(3))); - $DB->insert_record($tablename, array('course' => 3)); - - $this->assertTrue($DB->record_exists_sql("SELECT * FROM {{$tablename}} WHERE course = ?", array(3))); - } - - public function test_recordset_locks_delete() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - //Setup - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 4)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 6)); - - // Test against db write locking while on an open recordset - $rs = $DB->get_recordset($tablename, array(), null, 'course', 2, 2); // get courses = {3,4} - foreach ($rs as $record) { - $cid = $record->course; - $DB->delete_records($tablename, array('course' => $cid)); - $this->assertFalse($DB->record_exists($tablename, array('course' => $cid))); - } - $rs->close(); - - $this->assertEqual(4, $DB->count_records($tablename, array())); - } - - public function test_recordset_locks_update() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - //Setup - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 4)); - $DB->insert_record($tablename, array('course' => 5)); - $DB->insert_record($tablename, array('course' => 6)); - - // Test against db write locking while on an open recordset - $rs = $DB->get_recordset($tablename, array(), null, 'course', 2, 2); // get courses = {3,4} - foreach ($rs as $record) { - $cid = $record->course; - $DB->set_field($tablename, 'course', 10, array('course' => $cid)); - $this->assertFalse($DB->record_exists($tablename, array('course' => $cid))); - } - $rs->close(); - - $this->assertEqual(2, $DB->count_records($tablename, array('course' => 10))); - } - - public function test_delete_records() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('onetext', XMLDB_TYPE_TEXT, 'big', null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 2)); - - // Delete all records - $this->assertTrue($DB->delete_records($tablename)); - $this->assertEqual(0, $DB->count_records($tablename)); - - // Delete subset of records - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 2)); - - $this->assertTrue($DB->delete_records($tablename, array('course' => 2))); - $this->assertEqual(1, $DB->count_records($tablename)); - - // delete all - $this->assertTrue($DB->delete_records($tablename, array())); - $this->assertEqual(0, $DB->count_records($tablename)); - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext'=>'1'); - try { - $DB->delete_records($tablename, $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - - // test for exception throwing on text conditions being compared. (MDL-24863, unwanted auto conversion of param to int) - $conditions = array('onetext' => 1); - try { - $DB->delete_records($tablename, $conditions); - if (debugging()) { - // only in debug mode - hopefully all devs test code in debug mode... - $this->fail('An Exception is missing, expected due to equating of text fields'); - } - } catch (exception $e) { - $this->assertTrue($e instanceof dml_exception); - $this->assertEqual($e->errorcode, 'textconditionsnotallowed'); - } - } - - public function test_delete_records_select() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 2)); - - $this->assertTrue($DB->delete_records_select($tablename, 'course = ?', array(2))); - $this->assertEqual(1, $DB->count_records($tablename)); - } - - public function test_delete_records_list() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 1)); - $DB->insert_record($tablename, array('course' => 2)); - $DB->insert_record($tablename, array('course' => 3)); - - $this->assertTrue($DB->delete_records_list($tablename, 'course', array(2, 3))); - $this->assertEqual(1, $DB->count_records($tablename)); - - $this->assertTrue($DB->delete_records_list($tablename, 'course', array())); /// Must delete 0 rows without conditions. MDL-17645 - $this->assertEqual(1, $DB->count_records($tablename)); - } - - public function test_object_params() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $o = new stdClass(); // objects without __toString - never worked - try { - $DB->fix_sql_params("SELECT {{$tablename}} WHERE course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - // objects with __toString() forbidden everywhere since 2.3 - $o = new dml_test_object_one(); - try { - $DB->fix_sql_params("SELECT {{$tablename}} WHERE course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $DB->execute("SELECT {{$tablename}} WHERE course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $DB->get_recordset_sql("SELECT {{$tablename}} WHERE course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $DB->get_records_sql("SELECT {{$tablename}} WHERE course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $record = new stdClass(); - $record->course = $o; - $DB->insert_record_raw($tablename, $record); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $record = new stdClass(); - $record->course = $o; - $DB->insert_record($tablename, $record); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $record = new stdClass(); - $record->course = $o; - $DB->import_record($tablename, $record); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $record = new stdClass(); - $record->id = 1; - $record->course = $o; - $DB->update_record_raw($tablename, $record); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $record = new stdClass(); - $record->id = 1; - $record->course = $o; - $DB->update_record($tablename, $record); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $DB->set_field_select($tablename, 'course', 1, "course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - - try { - $DB->delete_records_select($tablename, "course = ? ", array($o)); - $this->fail('coding_exception expected'); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - } - - function test_sql_null_from_clause() { - $DB = $this->tdb; - $sql = "SELECT 1 AS id ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 1); - } - - function test_sql_bitand() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('col1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('col2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('col1' => 3, 'col2' => 10)); - - $sql = "SELECT ".$DB->sql_bitand(10, 3)." AS res ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 2); - - $sql = "SELECT id, ".$DB->sql_bitand('col1', 'col2')." AS res FROM {{$tablename}}"; - $result = $DB->get_records_sql($sql); - $this->assertEqual(count($result), 1); - $this->assertEqual(reset($result)->res, 2); - - $sql = "SELECT id, ".$DB->sql_bitand('col1', '?')." AS res FROM {{$tablename}}"; - $result = $DB->get_records_sql($sql, array(10)); - $this->assertEqual(count($result), 1); - $this->assertEqual(reset($result)->res, 2); - } - - function test_sql_bitnot() { - $DB = $this->tdb; - - $not = $DB->sql_bitnot(2); - $notlimited = $DB->sql_bitand($not, 7); // might be positive or negative number which can not fit into PHP INT! - - $sql = "SELECT $notlimited AS res ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 5); - } - - function test_sql_bitor() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('col1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('col2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('col1' => 3, 'col2' => 10)); - - $sql = "SELECT ".$DB->sql_bitor(10, 3)." AS res ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 11); - - $sql = "SELECT id, ".$DB->sql_bitor('col1', 'col2')." AS res FROM {{$tablename}}"; - $result = $DB->get_records_sql($sql); - $this->assertEqual(count($result), 1); - $this->assertEqual(reset($result)->res, 11); - - $sql = "SELECT id, ".$DB->sql_bitor('col1', '?')." AS res FROM {{$tablename}}"; - $result = $DB->get_records_sql($sql, array(10)); - $this->assertEqual(count($result), 1); - $this->assertEqual(reset($result)->res, 11); - } - - function test_sql_bitxor() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('col1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('col2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('col1' => 3, 'col2' => 10)); - - $sql = "SELECT ".$DB->sql_bitxor(10, 3)." AS res ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 9); - - $sql = "SELECT id, ".$DB->sql_bitxor('col1', 'col2')." AS res FROM {{$tablename}}"; - $result = $DB->get_records_sql($sql); - $this->assertEqual(count($result), 1); - $this->assertEqual(reset($result)->res, 9); - - $sql = "SELECT id, ".$DB->sql_bitxor('col1', '?')." AS res FROM {{$tablename}}"; - $result = $DB->get_records_sql($sql, array(10)); - $this->assertEqual(count($result), 1); - $this->assertEqual(reset($result)->res, 9); - } - - function test_sql_modulo() { - $DB = $this->tdb; - $sql = "SELECT ".$DB->sql_modulo(10, 7)." AS res ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 3); - } - - function test_sql_ceil() { - $DB = $this->tdb; - $sql = "SELECT ".$DB->sql_ceil(665.666)." AS res ".$DB->sql_null_from_clause(); - $this->assertEqual($DB->get_field_sql($sql), 666); - } - - function test_cast_char2int() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table1 = $this->get_test_table("1"); - $tablename1 = $table1->getName(); - - $table1->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table1->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table1->add_field('nametext', XMLDB_TYPE_TEXT, 'small', null, null, null, null); - $table1->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table1); - - $DB->insert_record($tablename1, array('name'=>'0100', 'nametext'=>'0200')); - $DB->insert_record($tablename1, array('name'=>'10', 'nametext'=>'20')); - - $table2 = $this->get_test_table("2"); - $tablename2 = $table2->getName(); - $table2->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table2->add_field('res', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table2->add_field('restext', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table2->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table2); - - $DB->insert_record($tablename2, array('res'=>100, 'restext'=>200)); - - // casting varchar field - $sql = "SELECT * - FROM {".$tablename1."} t1 - JOIN {".$tablename2."} t2 ON ".$DB->sql_cast_char2int("t1.name")." = t2.res "; - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 1); - // also test them in order clauses - $sql = "SELECT * FROM {{$tablename1}} ORDER BY ".$DB->sql_cast_char2int('name'); - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 2); - $this->assertEqual(reset($records)->name, '10'); - $this->assertEqual(next($records)->name, '0100'); - - // casting text field - $sql = "SELECT * - FROM {".$tablename1."} t1 - JOIN {".$tablename2."} t2 ON ".$DB->sql_cast_char2int("t1.nametext", true)." = t2.restext "; - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 1); - // also test them in order clauses - $sql = "SELECT * FROM {{$tablename1}} ORDER BY ".$DB->sql_cast_char2int('nametext', true); - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 2); - $this->assertEqual(reset($records)->nametext, '20'); - $this->assertEqual(next($records)->nametext, '0200'); - } - - function test_cast_char2real() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('nametext', XMLDB_TYPE_TEXT, 'small', null, null, null, null); - $table->add_field('res', XMLDB_TYPE_NUMBER, '12, 7', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'10.10', 'nametext'=>'10.10', 'res'=>5.1)); - $DB->insert_record($tablename, array('name'=>'91.10', 'nametext'=>'91.10', 'res'=>666)); - $DB->insert_record($tablename, array('name'=>'011.10','nametext'=>'011.10','res'=>10.1)); - - // casting varchar field - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_cast_char2real('name')." > res"; - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 2); - // also test them in order clauses - $sql = "SELECT * FROM {{$tablename}} ORDER BY ".$DB->sql_cast_char2real('name'); - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 3); - $this->assertEqual(reset($records)->name, '10.10'); - $this->assertEqual(next($records)->name, '011.10'); - $this->assertEqual(next($records)->name, '91.10'); - - // casting text field - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_cast_char2real('nametext', true)." > res"; - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 2); - // also test them in order clauses - $sql = "SELECT * FROM {{$tablename}} ORDER BY ".$DB->sql_cast_char2real('nametext', true); - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 3); - $this->assertEqual(reset($records)->nametext, '10.10'); - $this->assertEqual(next($records)->nametext, '011.10'); - $this->assertEqual(next($records)->nametext, '91.10'); - } - - function sql_compare_text() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'abcd', 'description'=>'abcd')); - $DB->insert_record($tablename, array('name'=>'abcdef', 'description'=>'bbcdef')); - $DB->insert_record($tablename, array('name'=>'aaaabb', 'description'=>'aaaacccccccccccccccccc')); - - $sql = "SELECT * FROM {{$tablename}} WHERE name = ".$DB->sql_compare_text('description'); - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 1); - - $sql = "SELECT * FROM {{$tablename}} WHERE name = ".$DB->sql_compare_text('description', 4); - $records = $DB->get_records_sql($sql); - $this->assertEqual(count($records), 2); - } - - function test_unique_index_collation_trouble() { - // note: this is a work in progress, we should probably move this to ddl test - - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $table->add_index('name', XMLDB_INDEX_UNIQUE, array('name')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'aaa')); - - try { - $DB->insert_record($tablename, array('name'=>'AAA')); - } catch (Exception $e) { - //TODO: ignore case insensitive uniqueness problems for now - //$this->fail("Unique index is case sensitive - this may cause problems in some tables"); - } - - try { - $DB->insert_record($tablename, array('name'=>'aäa')); - $DB->insert_record($tablename, array('name'=>'aáa')); - $this->assertTrue(true); - } catch (Exception $e) { - $family = $DB->get_dbfamily(); - if ($family === 'mysql' or $family === 'mssql') { - $this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages. This is usually caused by accent insensitive default collation."); - } else { - // this should not happen, PostgreSQL and Oracle do not support accent insensitive uniqueness. - $this->fail("Unique index is accent insensitive, this may cause problems for non-ascii languages."); - } - throw($e); - } - } - - function test_sql_binary_equal() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'aaa')); - $DB->insert_record($tablename, array('name'=>'aáa')); - $DB->insert_record($tablename, array('name'=>'aäa')); - $DB->insert_record($tablename, array('name'=>'bbb')); - $DB->insert_record($tablename, array('name'=>'BBB')); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE name = ?", array('aaa')); - $this->assertEqual(count($records), 1, 'SQL operator "=" is expected to be accent sensitive'); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE name = ?", array('bbb')); - $this->assertEqual(count($records), 1, 'SQL operator "=" is expected to be case sensitive'); - } - - function test_sql_like() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'SuperDuperRecord')); - $DB->insert_record($tablename, array('name'=>'Nodupor')); - $DB->insert_record($tablename, array('name'=>'ouch')); - $DB->insert_record($tablename, array('name'=>'ouc_')); - $DB->insert_record($tablename, array('name'=>'ouc%')); - $DB->insert_record($tablename, array('name'=>'aui')); - $DB->insert_record($tablename, array('name'=>'aüi')); - $DB->insert_record($tablename, array('name'=>'aÜi')); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', false); - $records = $DB->get_records_sql($sql, array("%dup_r%")); - $this->assertEqual(count($records), 2); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true); - $records = $DB->get_records_sql($sql, array("%dup%")); - $this->assertEqual(count($records), 1); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?'); // defaults - $records = $DB->get_records_sql($sql, array("%dup%")); - $this->assertEqual(count($records), 1); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true); - $records = $DB->get_records_sql($sql, array("ouc\\_")); - $this->assertEqual(count($records), 1); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true, true, false, '|'); - $records = $DB->get_records_sql($sql, array($DB->sql_like_escape("ouc%", '|'))); - $this->assertEqual(count($records), 1); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true, true); - $records = $DB->get_records_sql($sql, array('aui')); - $this->assertEqual(count($records), 1); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true, true, true); // NOT LIKE - $records = $DB->get_records_sql($sql, array("%o%")); - $this->assertEqual(count($records), 3); - - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', false, true, true); // NOT ILIKE - $records = $DB->get_records_sql($sql, array("%D%")); - $this->assertEqual(count($records), 6); - - // TODO: we do not require accent insensitivness yet, just make sure it does not throw errors - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', true, false); - $records = $DB->get_records_sql($sql, array('aui')); - //$this->assertEqual(count($records), 2, 'Accent insensitive LIKE searches may not be supported in all databases, this is not a problem.'); - $sql = "SELECT * FROM {{$tablename}} WHERE ".$DB->sql_like('name', '?', false, false); - $records = $DB->get_records_sql($sql, array('aui')); - //$this->assertEqual(count($records), 3, 'Accent insensitive LIKE searches may not be supported in all databases, this is not a problem.'); - } - - function test_coalesce() { - $DB = $this->tdb; - - // Testing not-null ocurrences, return 1st - $sql = "SELECT COALESCE('returnthis', 'orthis', 'orwhynotthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array())); - $sql = "SELECT COALESCE(:paramvalue, 'orthis', 'orwhynotthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => 'returnthis'))); - - // Testing null ocurrences, return 2nd - $sql = "SELECT COALESCE(null, 'returnthis', 'orthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array())); - $sql = "SELECT COALESCE(:paramvalue, 'returnthis', 'orthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => null))); - $sql = "SELECT COALESCE(null, :paramvalue, 'orthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => 'returnthis'))); - - // Testing null ocurrences, return 3rd - $sql = "SELECT COALESCE(null, null, 'returnthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array())); - $sql = "SELECT COALESCE(null, :paramvalue, 'returnthis') AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => null))); - $sql = "SELECT COALESCE(null, null, :paramvalue) AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('returnthis', $DB->get_field_sql($sql, array('paramvalue' => 'returnthis'))); - - // Testing all null ocurrences, return null - // Note: under mssql, if all elements are nulls, at least one must be a "typed" null, hence - // we cannot test this in a cross-db way easily, so next 2 tests are using - // different queries depending of the DB family - $customnull = $DB->get_dbfamily() == 'mssql' ? 'CAST(null AS varchar)' : 'null'; - $sql = "SELECT COALESCE(null, null, " . $customnull . ") AS test" . $DB->sql_null_from_clause(); - $this->assertNull($DB->get_field_sql($sql, array())); - $sql = "SELECT COALESCE(null, :paramvalue, " . $customnull . ") AS test" . $DB->sql_null_from_clause(); - $this->assertNull($DB->get_field_sql($sql, array('paramvalue' => null))); - - // Check there are not problems with whitespace strings - $sql = "SELECT COALESCE(null, '', null) AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('', $DB->get_field_sql($sql, array())); - $sql = "SELECT COALESCE(null, :paramvalue, null) AS test" . $DB->sql_null_from_clause(); - $this->assertEqual('', $DB->get_field_sql($sql, array('paramvalue' => ''))); - } - - function test_sql_concat() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - /// Testing all sort of values - $sql = "SELECT ".$DB->sql_concat("?", "?", "?")." AS fullname ". $DB->sql_null_from_clause(); - // string, some unicode chars - $params = array('name', 'áéíóú', 'name3'); - $this->assertEqual('nameáéíóúname3', $DB->get_field_sql($sql, $params)); - // string, spaces and numbers - $params = array('name', ' ', 12345); - $this->assertEqual('name 12345', $DB->get_field_sql($sql, $params)); - // float, empty and strings - $params = array(123.45, '', 'test'); - $this->assertEqual('123.45test', $DB->get_field_sql($sql, $params)); - // only integers - $params = array(12, 34, 56); - $this->assertEqual('123456', $DB->get_field_sql($sql, $params)); - // float, null and strings - $params = array(123.45, null, 'test'); - $this->assertNull($DB->get_field_sql($sql, $params), 'ANSI behaviour: Concatenating NULL must return NULL - But in Oracle :-(. [%s]'); // Concatenate NULL with anything result = NULL - - /// Testing fieldnames + values and also integer fieldnames - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('description'=>'áéíóú')); - $DB->insert_record($tablename, array('description'=>'dxxx')); - $DB->insert_record($tablename, array('description'=>'bcde')); - - // fieldnames and values mixed - $sql = 'SELECT id, ' . $DB->sql_concat('description', "'harcoded'", '?', '?') . ' AS result FROM {' . $tablename . '}'; - $records = $DB->get_records_sql($sql, array(123.45, 'test')); - $this->assertEqual(count($records), 3); - $this->assertEqual($records[1]->result, 'áéíóúharcoded123.45test'); - // integer fieldnames and values - $sql = 'SELECT id, ' . $DB->sql_concat('id', "'harcoded'", '?', '?') . ' AS result FROM {' . $tablename . '}'; - $records = $DB->get_records_sql($sql, array(123.45, 'test')); - $this->assertEqual(count($records), 3); - $this->assertEqual($records[1]->result, '1harcoded123.45test'); - // all integer fieldnames - $sql = 'SELECT id, ' . $DB->sql_concat('id', 'id', 'id') . ' AS result FROM {' . $tablename . '}'; - $records = $DB->get_records_sql($sql, array()); - $this->assertEqual(count($records), 3); - $this->assertEqual($records[1]->result, '111'); - - } - - function test_concat_join() { - $DB = $this->tdb; - $sql = "SELECT ".$DB->sql_concat_join("' '", array("?", "?", "?"))." AS fullname ".$DB->sql_null_from_clause(); - $params = array("name", "name2", "name3"); - $result = $DB->get_field_sql($sql, $params); - $this->assertEqual("name name2 name3", $result); - } - - function test_sql_fullname() { - $DB = $this->tdb; - $sql = "SELECT ".$DB->sql_fullname(':first', ':last')." AS fullname ".$DB->sql_null_from_clause(); - $params = array('first'=>'Firstname', 'last'=>'Surname'); - $this->assertEqual("Firstname Surname", $DB->get_field_sql($sql, $params)); - } - - function sql_sql_order_by_text() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('description'=>'abcd')); - $DB->insert_record($tablename, array('description'=>'dxxx')); - $DB->insert_record($tablename, array('description'=>'bcde')); - - $sql = "SELECT * FROM {{$tablename}} ORDER BY ".$DB->sql_order_by_text('description'); - $records = $DB->get_records_sql($sql); - $first = array_shift($records); - $this->assertEqual(1, $first->id); - $second = array_shift($records); - $this->assertEqual(3, $second->id); - $last = array_shift($records); - $this->assertEqual(2, $last->id); - } - - function test_sql_substring() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $string = 'abcdefghij'; - - $DB->insert_record($tablename, array('name'=>$string)); - - $sql = "SELECT id, ".$DB->sql_substr("name", 5)." AS name FROM {{$tablename}}"; - $record = $DB->get_record_sql($sql); - $this->assertEqual(substr($string, 5-1), $record->name); - - $sql = "SELECT id, ".$DB->sql_substr("name", 5, 2)." AS name FROM {{$tablename}}"; - $record = $DB->get_record_sql($sql); - $this->assertEqual(substr($string, 5-1, 2), $record->name); - - try { - // silence php warning ;-) - @$DB->sql_substr("name"); - $this->fail("Expecting an exception, none occurred"); - } catch (Exception $e) { - $this->assertTrue($e instanceof coding_exception); - } - } - - function test_sql_length() { - $DB = $this->tdb; - $this->assertEqual($DB->get_field_sql( - "SELECT ".$DB->sql_length("'aeiou'").$DB->sql_null_from_clause()), 5); - $this->assertEqual($DB->get_field_sql( - "SELECT ".$DB->sql_length("'áéíóú'").$DB->sql_null_from_clause()), 5); - } - - function test_sql_position() { - $DB = $this->tdb; - $this->assertEqual($DB->get_field_sql( - "SELECT ".$DB->sql_position("'ood'", "'Moodle'").$DB->sql_null_from_clause()), 2); - $this->assertEqual($DB->get_field_sql( - "SELECT ".$DB->sql_position("'Oracle'", "'Moodle'").$DB->sql_null_from_clause()), 0); - } - - function test_sql_empty() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('namenotnull', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'default value'); - $table->add_field('namenotnullnodeflt', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'', 'namenotnull'=>'')); - $DB->insert_record($tablename, array('name'=>null)); - $DB->insert_record($tablename, array('name'=>'lalala')); - $DB->insert_record($tablename, array('name'=>0)); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE name = '".$DB->sql_empty()."'"); - $this->assertEqual(count($records), 1); - $record = reset($records); - $this->assertEqual($record->name, ''); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE namenotnull = '".$DB->sql_empty()."'"); - $this->assertEqual(count($records), 1); - $record = reset($records); - $this->assertEqual($record->namenotnull, ''); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE namenotnullnodeflt = '".$DB->sql_empty()."'"); - $this->assertEqual(count($records), 4); - $record = reset($records); - $this->assertEqual($record->namenotnullnodeflt, ''); - } - - function test_sql_isempty() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); - $table->add_field('namenull', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null); - $table->add_field('descriptionnull', XMLDB_TYPE_TEXT, 'big', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'', 'namenull'=>'', 'description'=>'', 'descriptionnull'=>'')); - $DB->insert_record($tablename, array('name'=>'??', 'namenull'=>null, 'description'=>'??', 'descriptionnull'=>null)); - $DB->insert_record($tablename, array('name'=>'la', 'namenull'=>'la', 'description'=>'la', 'descriptionnull'=>'lalala')); - $DB->insert_record($tablename, array('name'=>0, 'namenull'=>0, 'description'=>0, 'descriptionnull'=>0)); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isempty($tablename, 'name', false, false)); - $this->assertEqual(count($records), 1); - $record = reset($records); - $this->assertEqual($record->name, ''); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isempty($tablename, 'namenull', true, false)); - $this->assertEqual(count($records), 1); - $record = reset($records); - $this->assertEqual($record->namenull, ''); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isempty($tablename, 'description', false, true)); - $this->assertEqual(count($records), 1); - $record = reset($records); - $this->assertEqual($record->description, ''); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isempty($tablename, 'descriptionnull', true, true)); - $this->assertEqual(count($records), 1); - $record = reset($records); - $this->assertEqual($record->descriptionnull, ''); - } - - function test_sql_isnotempty() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); - $table->add_field('namenull', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('description', XMLDB_TYPE_TEXT, 'big', null, XMLDB_NOTNULL, null, null); - $table->add_field('descriptionnull', XMLDB_TYPE_TEXT, 'big', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'', 'namenull'=>'', 'description'=>'', 'descriptionnull'=>'')); - $DB->insert_record($tablename, array('name'=>'??', 'namenull'=>null, 'description'=>'??', 'descriptionnull'=>null)); - $DB->insert_record($tablename, array('name'=>'la', 'namenull'=>'la', 'description'=>'la', 'descriptionnull'=>'lalala')); - $DB->insert_record($tablename, array('name'=>0, 'namenull'=>0, 'description'=>0, 'descriptionnull'=>0)); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isnotempty($tablename, 'name', false, false)); - $this->assertEqual(count($records), 3); - $record = reset($records); - $this->assertEqual($record->name, '??'); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isnotempty($tablename, 'namenull', true, false)); - $this->assertEqual(count($records), 2); // nulls aren't comparable (so they aren't "not empty"). SQL expected behaviour - $record = reset($records); - $this->assertEqual($record->namenull, 'la'); // so 'la' is the first non-empty 'namenull' record - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isnotempty($tablename, 'description', false, true)); - $this->assertEqual(count($records), 3); - $record = reset($records); - $this->assertEqual($record->description, '??'); - - $records = $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE ".$DB->sql_isnotempty($tablename, 'descriptionnull', true, true)); - $this->assertEqual(count($records), 2); // nulls aren't comparable (so they aren't "not empty"). SQL expected behaviour - $record = reset($records); - $this->assertEqual($record->descriptionnull, 'lalala'); // so 'lalala' is the first non-empty 'descriptionnull' record - } - - function test_sql_regex() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('name'=>'lalala')); - $DB->insert_record($tablename, array('name'=>'holaaa')); - $DB->insert_record($tablename, array('name'=>'aouch')); - - $sql = "SELECT * FROM {{$tablename}} WHERE name ".$DB->sql_regex()." ?"; - $params = array('a$'); - if ($DB->sql_regex_supported()) { - $records = $DB->get_records_sql($sql, $params); - $this->assertEqual(count($records), 2); - } else { - $this->assertTrue(true, 'Regexp operations not supported. Test skipped'); - } - - $sql = "SELECT * FROM {{$tablename}} WHERE name ".$DB->sql_regex(false)." ?"; - $params = array('.a'); - if ($DB->sql_regex_supported()) { - $records = $DB->get_records_sql($sql, $params); - $this->assertEqual(count($records), 1); - } else { - $this->assertTrue(true, 'Regexp operations not supported. Test skipped'); - } - - } - - /** - * Test some more complex SQL syntax which moodle uses and depends on to work - * useful to determine if new database libraries can be supported. - */ - public function test_get_records_sql_complicated() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('content', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, XMLDB_NOTNULL); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => 3, 'content' => 'hello', 'name'=>'xyz')); - $DB->insert_record($tablename, array('course' => 3, 'content' => 'world', 'name'=>'abc')); - $DB->insert_record($tablename, array('course' => 5, 'content' => 'hello', 'name'=>'def')); - $DB->insert_record($tablename, array('course' => 2, 'content' => 'universe', 'name'=>'abc')); - - // test grouping by expressions in the query. MDL-26819. Note that there are 4 ways: - // - By column position (GROUP by 1) - Not supported by mssql & oracle - // - By column name (GROUP by course) - Supported by all, but leading to wrong results - // - By column alias (GROUP by casecol) - Not supported by mssql & oracle - // - By complete expression (GROUP BY CASE ...) - 100% cross-db, this test checks it - $sql = "SELECT (CASE WHEN course = 3 THEN 1 ELSE 0 END) AS casecol, - COUNT(1) AS countrecs, - MAX(name) AS maxname - FROM {{$tablename}} - GROUP BY CASE WHEN course = 3 THEN 1 ELSE 0 END - ORDER BY casecol DESC"; - $result = array( - 1 => (object)array('casecol' => 1, 'countrecs' => 2, 'maxname' => 'xyz'), - 0 => (object)array('casecol' => 0, 'countrecs' => 2, 'maxname' => 'def')); - $records = $DB->get_records_sql($sql, null); - $this->assertEqual($result, $records); - - // another grouping by CASE expression just to ensure it works ok for multiple WHEN - $sql = "SELECT CASE name - WHEN 'xyz' THEN 'last' - WHEN 'def' THEN 'mid' - WHEN 'abc' THEN 'first' - END AS casecol, - COUNT(1) AS countrecs, - MAX(name) AS maxname - FROM {{$tablename}} - GROUP BY CASE name - WHEN 'xyz' THEN 'last' - WHEN 'def' THEN 'mid' - WHEN 'abc' THEN 'first' - END - ORDER BY casecol DESC"; - $result = array( - 'mid' => (object)array('casecol' => 'mid', 'countrecs' => 1, 'maxname' => 'def'), - 'last' => (object)array('casecol' => 'last', 'countrecs' => 1, 'maxname' => 'xyz'), - 'first'=> (object)array('casecol' => 'first', 'countrecs' => 2, 'maxname' => 'abc')); - $records = $DB->get_records_sql($sql, null); - $this->assertEqual($result, $records); - - // test limits in queries with DISTINCT/ALL clauses and multiple whitespace. MDL-25268 - $sql = "SELECT DISTINCT course - FROM {{$tablename}} - ORDER BY course"; - // only limitfrom - $records = $DB->get_records_sql($sql, null, 1); - $this->assertEqual(2, count($records)); - $this->assertEqual(3, reset($records)->course); - $this->assertEqual(5, next($records)->course); - // only limitnum - $records = $DB->get_records_sql($sql, null, 0, 2); - $this->assertEqual(2, count($records)); - $this->assertEqual(2, reset($records)->course); - $this->assertEqual(3, next($records)->course); - // both limitfrom and limitnum - $records = $DB->get_records_sql($sql, null, 2, 2); - $this->assertEqual(1, count($records)); - $this->assertEqual(5, reset($records)->course); - - // we have sql like this in moodle, this syntax breaks on older versions of sqlite for example.. - $sql = "SELECT a.id AS id, a.course AS course - FROM {{$tablename}} a - JOIN (SELECT * FROM {{$tablename}}) b ON a.id = b.id - WHERE a.course = ?"; - - $records = $DB->get_records_sql($sql, array(3)); - $this->assertEqual(2, count($records)); - $this->assertEqual(1, reset($records)->id); - $this->assertEqual(2, next($records)->id); - - // do NOT try embedding sql_xxxx() helper functions in conditions array of count_records(), they don't break params/binding! - $count = $DB->count_records_select($tablename, "course = :course AND ".$DB->sql_compare_text('content')." = :content", array('course' => 3, 'content' => 'hello')); - $this->assertEqual(1, $count); - - // test int x string comparison - $sql = "SELECT * - FROM {{$tablename}} c - WHERE name = ?"; - $this->assertEqual(count($DB->get_records_sql($sql, array(10))), 0); - $this->assertEqual(count($DB->get_records_sql($sql, array("10"))), 0); - $DB->insert_record($tablename, array('course' => 7, 'content' => 'xx', 'name'=>'1')); - $DB->insert_record($tablename, array('course' => 7, 'content' => 'yy', 'name'=>'2')); - $this->assertEqual(count($DB->get_records_sql($sql, array(1))), 1); - $this->assertEqual(count($DB->get_records_sql($sql, array("1"))), 1); - $this->assertEqual(count($DB->get_records_sql($sql, array(10))), 0); - $this->assertEqual(count($DB->get_records_sql($sql, array("10"))), 0); - $DB->insert_record($tablename, array('course' => 7, 'content' => 'xx', 'name'=>'1abc')); - $this->assertEqual(count($DB->get_records_sql($sql, array(1))), 1); - $this->assertEqual(count($DB->get_records_sql($sql, array("1"))), 1); - } - - function test_onelevel_commit() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $transaction = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $this->assertEqual(0, $DB->count_records($tablename)); - $DB->insert_record($tablename, $data); - $this->assertEqual(1, $DB->count_records($tablename)); - $transaction->allow_commit(); - $this->assertEqual(1, $DB->count_records($tablename)); - } - - function test_onelevel_rollback() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - // this might in fact encourage ppl to migrate from myisam to innodb - - $transaction = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $this->assertEqual(0, $DB->count_records($tablename)); - $DB->insert_record($tablename, $data); - $this->assertEqual(1, $DB->count_records($tablename)); - try { - $transaction->rollback(new Exception('test')); - $this->fail('transaction rollback must rethrow exception'); - } catch (Exception $e) { - } - $this->assertEqual(0, $DB->count_records($tablename)); - } - - function test_nested_transactions() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - // two level commit - $this->assertFalse($DB->is_transaction_started()); - $transaction1 = $DB->start_delegated_transaction(); - $this->assertTrue($DB->is_transaction_started()); - $data = (object)array('course'=>3); - $DB->insert_record($tablename, $data); - $transaction2 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>4); - $DB->insert_record($tablename, $data); - $transaction2->allow_commit(); - $this->assertTrue($DB->is_transaction_started()); - $transaction1->allow_commit(); - $this->assertFalse($DB->is_transaction_started()); - $this->assertEqual(2, $DB->count_records($tablename)); - - $DB->delete_records($tablename); - - // rollback from top level - $transaction1 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $DB->insert_record($tablename, $data); - $transaction2 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>4); - $DB->insert_record($tablename, $data); - $transaction2->allow_commit(); - try { - $transaction1->rollback(new Exception('test')); - $this->fail('transaction rollback must rethrow exception'); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'Exception'); - } - $this->assertEqual(0, $DB->count_records($tablename)); - - $DB->delete_records($tablename); - - // rollback from nested level - $transaction1 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $DB->insert_record($tablename, $data); - $transaction2 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>4); - $DB->insert_record($tablename, $data); - try { - $transaction2->rollback(new Exception('test')); - $this->fail('transaction rollback must rethrow exception'); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'Exception'); - } - $this->assertEqual(2, $DB->count_records($tablename)); // not rolled back yet - try { - $transaction1->allow_commit(); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - $this->assertEqual(2, $DB->count_records($tablename)); // not rolled back yet - // the forced rollback is done from the default_exception handler and similar places, - // let's do it manually here - $this->assertTrue($DB->is_transaction_started()); - $DB->force_transaction_rollback(); - $this->assertFalse($DB->is_transaction_started()); - $this->assertEqual(0, $DB->count_records($tablename)); // finally rolled back - - $DB->delete_records($tablename); - } - - function test_transactions_forbidden() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->transactions_forbidden(); - $transaction = $DB->start_delegated_transaction(); - $data = (object)array('course'=>1); - $DB->insert_record($tablename, $data); - try { - $DB->transactions_forbidden(); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - // the previous test does not force rollback - $transaction->allow_commit(); - $this->assertFalse($DB->is_transaction_started()); - $this->assertEqual(1, $DB->count_records($tablename)); - } - - function test_wrong_transactions() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - - // wrong order of nested commits - $transaction1 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $DB->insert_record($tablename, $data); - $transaction2 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>4); - $DB->insert_record($tablename, $data); - try { - $transaction1->allow_commit(); - $this->fail('wrong order of commits must throw exception'); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - try { - $transaction2->allow_commit(); - $this->fail('first wrong commit forces rollback'); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - // this is done in default exception handler usually - $this->assertTrue($DB->is_transaction_started()); - $this->assertEqual(2, $DB->count_records($tablename)); // not rolled back yet - $DB->force_transaction_rollback(); - $this->assertEqual(0, $DB->count_records($tablename)); - $DB->delete_records($tablename); - - - // wrong order of nested rollbacks - $transaction1 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $DB->insert_record($tablename, $data); - $transaction2 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>4); - $DB->insert_record($tablename, $data); - try { - // this first rollback should prevent all other rollbacks - $transaction1->rollback(new Exception('test')); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'Exception'); - } - try { - $transaction2->rollback(new Exception('test')); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'Exception'); - } - try { - $transaction1->rollback(new Exception('test')); - } catch (Exception $e) { - // the rollback was used already once, no way to use it again - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - // this is done in default exception handler usually - $this->assertTrue($DB->is_transaction_started()); - $DB->force_transaction_rollback(); - $DB->delete_records($tablename); - - - // unknown transaction object - $transaction1 = $DB->start_delegated_transaction(); - $data = (object)array('course'=>3); - $DB->insert_record($tablename, $data); - $transaction2 = new moodle_transaction($DB); - try { - $transaction2->allow_commit(); - $this->fail('foreign transaction must fail'); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - try { - $transaction1->allow_commit(); - $this->fail('first wrong commit forces rollback'); - } catch (Exception $e) { - $this->assertEqual(get_class($e), 'dml_transaction_exception'); - } - $DB->force_transaction_rollback(); - $DB->delete_records($tablename); - } - - function test_concurent_transactions() { - // Notes about this test: - // 1- MySQL needs to use one engine with transactions support (InnoDB). - // 2- MSSQL needs to have enabled versioning for read committed - // transactions (ALTER DATABASE xxx SET READ_COMMITTED_SNAPSHOT ON) - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $transaction = $DB->start_delegated_transaction(); - $data = (object)array('course'=>1); - $this->assertEqual(0, $DB->count_records($tablename)); - $DB->insert_record($tablename, $data); - $this->assertEqual(1, $DB->count_records($tablename)); - - //open second connection - $cfg = $DB->export_dbconfig(); - if (!isset($cfg->dboptions)) { - $cfg->dboptions = array(); - } - $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); - $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); - - // second instance should not see pending inserts - $this->assertEqual(0, $DB2->count_records($tablename)); - $data = (object)array('course'=>2); - $DB2->insert_record($tablename, $data); - $this->assertEqual(1, $DB2->count_records($tablename)); - - // first should see the changes done from second - $this->assertEqual(2, $DB->count_records($tablename)); - - // now commit and we should see it finally in second connections - $transaction->allow_commit(); - $this->assertEqual(2, $DB2->count_records($tablename)); - - // let's try delete all is also working on (this checks MDL-29198) - // initially both connections see all the records in the table (2) - $this->assertEqual(2, $DB->count_records($tablename)); - $this->assertEqual(2, $DB2->count_records($tablename)); - $transaction = $DB->start_delegated_transaction(); - - // delete all from within transaction - $DB->delete_records($tablename); - - // transactional $DB, sees 0 records now - $this->assertEqual(0, $DB->count_records($tablename)); - - // others ($DB2) get no changes yet - $this->assertEqual(2, $DB2->count_records($tablename)); - - // now commit and we should see changes - $transaction->allow_commit(); - $this->assertEqual(0, $DB2->count_records($tablename)); - - $DB2->dispose(); - } - - public function test_session_locks() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - // Open second connection - $cfg = $DB->export_dbconfig(); - if (!isset($cfg->dboptions)) { - $cfg->dboptions = array(); - } - $DB2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); - $DB2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); - - // Testing that acquiring a lock efectively locks - // Get a session lock on connection1 - $rowid = rand(100, 200); - $timeout = 1; - $DB->get_session_lock($rowid, $timeout); - - // Try to get the same session lock on connection2 - try { - $DB2->get_session_lock($rowid, $timeout); - $DB2->release_session_lock($rowid); // Should not be excuted, but here for safety - $this->fail('An Exception is missing, expected due to session lock acquired.'); - } catch (exception $e) { - $this->assertTrue($e instanceof dml_sessionwait_exception); - $DB->release_session_lock($rowid); // Release lock on connection1 - } - - // Testing that releasing a lock efectively frees - // Get a session lock on connection1 - $rowid = rand(100, 200); - $timeout = 1; - $DB->get_session_lock($rowid, $timeout); - // Release the lock on connection1 - $DB->release_session_lock($rowid); - - // Get the just released lock on connection2 - $DB2->get_session_lock($rowid, $timeout); - // Release the lock on connection2 - $DB2->release_session_lock($rowid); - - $DB2->dispose(); - } - - public function test_bound_param_types() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('content', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, XMLDB_NOTNULL); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertTrue($DB->insert_record($tablename, array('name' => '1', 'content'=>'xx'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 2, 'content'=>'yy'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'somestring', 'content'=>'zz'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'aa', 'content'=>'1'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'bb', 'content'=>2))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'cc', 'content'=>'sometext'))); - - - // Conditions in CHAR columns - $this->assertTrue($DB->record_exists($tablename, array('name'=>1))); - $this->assertTrue($DB->record_exists($tablename, array('name'=>'1'))); - $this->assertFalse($DB->record_exists($tablename, array('name'=>111))); - $this->assertTrue($DB->get_record($tablename, array('name'=>1))); - $this->assertTrue($DB->get_record($tablename, array('name'=>'1'))); - $this->assertFalse($DB->get_record($tablename, array('name'=>111))); - $sqlqm = "SELECT * - FROM {{$tablename}} - WHERE name = ?"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, array(1))); - $this->assertEqual(1, count($records)); - $this->assertTrue($records = $DB->get_records_sql($sqlqm, array('1'))); - $this->assertEqual(1, count($records)); - $records = $DB->get_records_sql($sqlqm, array(222)); - $this->assertEqual(0, count($records)); - $sqlnamed = "SELECT * - FROM {{$tablename}} - WHERE name = :name"; - $this->assertTrue($records = $DB->get_records_sql($sqlnamed, array('name' => 2))); - $this->assertEqual(1, count($records)); - $this->assertTrue($records = $DB->get_records_sql($sqlnamed, array('name' => '2'))); - $this->assertEqual(1, count($records)); - - // Conditions in TEXT columns always must be performed with the sql_compare_text - // helper function on both sides of the condition - $sqlqm = "SELECT * - FROM {{$tablename}} - WHERE " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text('?'); - $this->assertTrue($records = $DB->get_records_sql($sqlqm, array('1'))); - $this->assertEqual(1, count($records)); - $this->assertTrue($records = $DB->get_records_sql($sqlqm, array(1))); - $this->assertEqual(1, count($records)); - $sqlnamed = "SELECT * - FROM {{$tablename}} - WHERE " . $DB->sql_compare_text('content') . " = " . $DB->sql_compare_text(':content'); - $this->assertTrue($records = $DB->get_records_sql($sqlnamed, array('content' => 2))); - $this->assertEqual(1, count($records)); - $this->assertTrue($records = $DB->get_records_sql($sqlnamed, array('content' => '2'))); - $this->assertEqual(1, count($records)); - } - - public function test_bound_param_reserved() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0'); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $DB->insert_record($tablename, array('course' => '1')); - - // make sure reserved words do not cause fatal problems in query parameters - - $DB->execute("UPDATE {{$tablename}} SET course = 1 WHERE ID = :select", array('select'=>1)); - $DB->get_records_sql("SELECT * FROM {{$tablename}} WHERE course = :select", array('select'=>1)); - $rs = $DB->get_recordset_sql("SELECT * FROM {{$tablename}} WHERE course = :select", array('select'=>1)); - $rs->close(); - $DB->get_fieldset_sql("SELECT id FROM {{$tablename}} WHERE course = :select", array('select'=>1)); - $DB->set_field_select($tablename, 'course', '1', "id = :select", array('select'=>1)); - $DB->delete_records_select($tablename, "id = :select", array('select'=>1)); - } - - public function test_limits_and_offsets() { - $DB = $this->tdb; - $dbman = $DB->get_manager(); - - if (false) $DB = new moodle_database (); - - $table = $this->get_test_table(); - $tablename = $table->getName(); - - $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); - $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null); - $table->add_field('content', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, XMLDB_NOTNULL); - $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); - $dbman->create_table($table); - - $this->assertTrue($DB->insert_record($tablename, array('name' => 'a', 'content'=>'one'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'b', 'content'=>'two'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'c', 'content'=>'three'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'd', 'content'=>'four'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'e', 'content'=>'five'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'f', 'content'=>'six'))); - - $sqlqm = "SELECT * - FROM {{$tablename}}"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 4)); - $this->assertEqual(2, count($records)); - $this->assertEqual('e', reset($records)->name); - $this->assertEqual('f', end($records)->name); - - $sqlqm = "SELECT * - FROM {{$tablename}}"; - $this->assertFalse($records = $DB->get_records_sql($sqlqm, null, 8)); - - $sqlqm = "SELECT * - FROM {{$tablename}}"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 0, 4)); - $this->assertEqual(4, count($records)); - $this->assertEqual('a', reset($records)->name); - $this->assertEqual('d', end($records)->name); - - $sqlqm = "SELECT * - FROM {{$tablename}}"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 0, 8)); - $this->assertEqual(6, count($records)); - $this->assertEqual('a', reset($records)->name); - $this->assertEqual('f', end($records)->name); - - $sqlqm = "SELECT * - FROM {{$tablename}}"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 1, 4)); - $this->assertEqual(4, count($records)); - $this->assertEqual('b', reset($records)->name); - $this->assertEqual('e', end($records)->name); - - $sqlqm = "SELECT * - FROM {{$tablename}}"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 4, 4)); - $this->assertEqual(2, count($records)); - $this->assertEqual('e', reset($records)->name); - $this->assertEqual('f', end($records)->name); - - $sqlqm = "SELECT t.*, t.name AS test - FROM {{$tablename}} t - ORDER BY t.id ASC"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 4, 4)); - $this->assertEqual(2, count($records)); - $this->assertEqual('e', reset($records)->name); - $this->assertEqual('f', end($records)->name); - - $sqlqm = "SELECT DISTINCT t.name, t.name AS test - FROM {{$tablename}} t - ORDER BY t.name DESC"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 4, 4)); - $this->assertEqual(2, count($records)); - $this->assertEqual('b', reset($records)->name); - $this->assertEqual('a', end($records)->name); - - $sqlqm = "SELECT 1 - FROM {{$tablename}} t - WHERE t.name = 'a'"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 0, 1)); - $this->assertEqual(1, count($records)); - - $sqlqm = "SELECT 'constant' - FROM {{$tablename}} t - WHERE t.name = 'a'"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 0, 8)); - $this->assertEqual(1, count($records)); - - $this->assertTrue($DB->insert_record($tablename, array('name' => 'a', 'content'=>'one'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'b', 'content'=>'two'))); - $this->assertTrue($DB->insert_record($tablename, array('name' => 'c', 'content'=>'three'))); - - $sqlqm = "SELECT t.name, COUNT(DISTINCT t2.id) AS count, 'Test' AS teststring - FROM {{$tablename}} t - LEFT JOIN ( - SELECT t.id, t.name - FROM {{$tablename}} t - ) t2 ON t2.name = t.name - GROUP BY t.name - ORDER BY t.name ASC"; - $this->assertTrue($records = $DB->get_records_sql($sqlqm)); - $this->assertEqual(6, count($records)); // a,b,c,d,e,f - $this->assertEqual(2, reset($records)->count); // a has 2 records now - $this->assertEqual(1, end($records)->count); // f has 1 record still - - $this->assertTrue($records = $DB->get_records_sql($sqlqm, null, 0, 2)); - $this->assertEqual(2, count($records)); - $this->assertEqual(2, reset($records)->count); - $this->assertEqual(2, end($records)->count); - } -} - -/** - * This class is not a proper subclass of moodle_database. It is - * intended to be used only in unit tests, in order to gain access to the - * protected methods of moodle_database, and unit test them. - */ -class moodle_database_for_testing extends moodle_database { - protected $prefix = 'mdl_'; - - public function public_fix_table_names($sql) { - return $this->fix_table_names($sql); - } - - public function driver_installed(){} - public function get_dbfamily(){} - protected function get_dbtype(){} - protected function get_dblibrary(){} - public function get_name(){} - public function get_configuration_help(){} - public function get_configuration_hints(){} - public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dboptions=null){} - public function get_server_info(){} - protected function allowed_param_types(){} - public function get_last_error(){} - public function get_tables($usecache=true){} - public function get_indexes($table){} - public function get_columns($table, $usecache=true){} - protected function normalise_value($column, $value){} - public function set_debug($state){} - public function get_debug(){} - public function set_logging($state){} - public function change_database_structure($sql){} - public function execute($sql, array $params=null){} - public function get_recordset_sql($sql, array $params=null, $limitfrom=0, $limitnum=0){} - public function get_records_sql($sql, array $params=null, $limitfrom=0, $limitnum=0){} - public function get_fieldset_sql($sql, array $params=null){} - public function insert_record_raw($table, $params, $returnid=true, $bulk=false, $customsequence=false){} - public function insert_record($table, $dataobject, $returnid=true, $bulk=false){} - public function import_record($table, $dataobject){} - public function update_record_raw($table, $params, $bulk=false){} - public function update_record($table, $dataobject, $bulk=false){} - public function set_field_select($table, $newfield, $newvalue, $select, array $params=null){} - public function delete_records_select($table, $select, array $params=null){} - public function sql_concat(){} - public function sql_concat_join($separator="' '", $elements=array()){} - public function sql_substr($expr, $start, $length=false){} - public function begin_transaction() {} - public function commit_transaction() {} - public function rollback_transaction() {} -} - - -/** - * Dumb test class with toString() returrning 1. - */ -class dml_test_object_one { - public function __toString() { - return 1; - } -}