Permalink
Browse files

MDL-28080 DML unit tests - cover tweak_param_names() and the new 30 c…

…hars limit exception
  • Loading branch information...
1 parent a4c5837 commit a2c3c37cb6f37f71aecd4cfdbd851a87092e8f6d @stronk7 stronk7 committed Sep 1, 2011
Showing with 143 additions and 0 deletions.
  1. +143 −0 lib/dml/simpletest/testdml.php
@@ -385,6 +385,16 @@ function test_fix_sql_params() {
$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);
@@ -413,6 +423,139 @@ function test_fix_sql_params() {
$this->assertIdentical(array_values($params), array_values($inparams));
}
+ 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();

0 comments on commit a2c3c37

Please sign in to comment.