Skip to content

Commit

Permalink
MDL-32112 validate field name restrictions in sql_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Mar 24, 2012
1 parent c856a1f commit 6cfade8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
46 changes: 46 additions & 0 deletions lib/ddl/simpletest/testddl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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');
Expand Down Expand Up @@ -310,6 +311,51 @@ public function test_create_table() {
$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');
}

}

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/xmldb/xmldb_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,16 @@ function readableInfo() {
*/
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()) {
Expand Down

0 comments on commit 6cfade8

Please sign in to comment.