MDL-32112 validate field name restrictions in sql_generator

This commit is contained in:
Petr Skoda 2012-03-19 18:44:33 +01:00
parent c856a1f60b
commit 6cfade8f5a
2 changed files with 56 additions and 1 deletions

View file

@ -15,6 +15,7 @@ require_once($CFG->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');
@ -310,6 +311,51 @@ class ddl_test extends UnitTestCase {
$this->assertTrue($e instanceof ddl_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');
}
}
/**