mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-32112 validate field name restrictions in sql_generator
This commit is contained in:
parent
c856a1f60b
commit
6cfade8f5a
2 changed files with 56 additions and 1 deletions
|
@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -701,7 +701,16 @@ class xmldb_field extends xmldb_object {
|
|||
*/
|
||||
function validateDefinition(xmldb_table $xmldb_table=null) {
|
||||
if (!$xmldb_table) {
|
||||
return 'Invalid xmldb_field->validateDefinition() call, $xmldb_table si required.';
|
||||
return 'Invalid xmldb_field->validateDefinition() call, $xmldb_table is required.';
|
||||
}
|
||||
|
||||
$name = $this->getName();
|
||||
if (strlen($name) > 30) {
|
||||
return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name is too long.'
|
||||
.' Limit is 30 chars.';
|
||||
}
|
||||
if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) {
|
||||
return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name includes invalid characters.';
|
||||
}
|
||||
|
||||
switch ($this->getType()) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue