From 66c34d357874d62a2e3d0a180e91ba80b45e0372 Mon Sep 17 00:00:00 2001 From: Matteo Scaramuccia Date: Thu, 13 Sep 2018 21:55:17 +0200 Subject: [PATCH] MDL-63319 ddl: Improved rename_field() coverage DB API should allow to rename fields, including renaming those columns named using a reserved word. --- lib/ddl/tests/ddl_test.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/ddl/tests/ddl_test.php b/lib/ddl/tests/ddl_test.php index 2f42a6276e113..d79d4304346be 100644 --- a/lib/ddl/tests/ddl_test.php +++ b/lib/ddl/tests/ddl_test.php @@ -1545,12 +1545,45 @@ public function testRenameField() { $field = new xmldb_field('type'); $field->set_attributes(XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'general', 'course'); + // 1. Rename the 'type' field into a generic new valid name. + // This represents the standard use case. $dbman->rename_field($table, $field, 'newfieldname'); $columns = $DB->get_columns('test_table0'); $this->assertArrayNotHasKey('type', $columns); $this->assertArrayHasKey('newfieldname', $columns); + $field->setName('newfieldname'); + + // 2. Rename the 'newfieldname' field into a reserved word, for testing purposes. + // This represents a questionable use case: we should support it but discourage the use of it on peer reviewing. + $dbman->rename_field($table, $field, 'where'); + + $columns = $DB->get_columns('test_table0'); + + $this->assertArrayNotHasKey('newfieldname', $columns); + $this->assertArrayHasKey('where', $columns); + + // 3. Create a table with a column name named w/ a reserved word and get rid of it. + // This represents a "recovering" use case: a field name could be a reserved word in the future, at least for a DB type. + $table = new xmldb_table('test_table_res_word'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('where', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + $dbman->create_table($table); + $dbman->table_exists('test_table_res_word'); + + $columns = $DB->get_columns('test_table_res_word'); + $this->assertArrayHasKey('where', $columns); + $field = $table->getField('where'); + + $dbman->rename_field($table, $field, 'newfieldname'); + + $columns = $DB->get_columns('test_table_res_word'); + + $this->assertArrayNotHasKey('where', $columns); + $this->assertArrayHasKey('newfieldname', $columns); } public function testIndexExists() {