From 6cf0f60c7dc4e7e21bdd5ff1179232c6437b84c7 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Fri, 8 Nov 2013 13:17:47 +1100 Subject: [PATCH] MDL-29071 ddl: unittest for objects name length --- lib/ddl/tests/ddl_test.php | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/ddl/tests/ddl_test.php b/lib/ddl/tests/ddl_test.php index 3484b043b5d..77b60864fc9 100644 --- a/lib/ddl/tests/ddl_test.php +++ b/lib/ddl/tests/ddl_test.php @@ -1865,6 +1865,59 @@ class ddl_testcase extends database_driver_testcase { } } + public function test_object_name() { + $gen = $this->tdb->get_manager()->generator; + + // This will form short object name and max length should not be exceeded. + $table = 'tablename'; + $fields = 'id'; + $suffix = 'pk'; + for ($i=0; $i<12; $i++) { + $this->assertLessThanOrEqual($gen->names_max_length, + strlen($gen->getNameForObject($table, $fields, $suffix)), + 'Generated object name is too long. $i = '.$i); + } + + // This will form too long object name always and it must be trimmed to exactly 30 chars. + $table = 'aaaa_bbbb_cccc_dddd_eeee_ffff_gggg'; + $fields = 'aaaaa,bbbbb,ccccc,ddddd'; + $suffix = 'idx'; + for ($i=0; $i<12; $i++) { + $this->assertEquals($gen->names_max_length, + strlen($gen->getNameForObject($table, $fields, $suffix)), + 'Generated object name is too long. $i = '.$i); + } + + // Same test without suffix. + $table = 'bbbb_cccc_dddd_eeee_ffff_gggg_hhhh'; + $fields = 'aaaaa,bbbbb,ccccc,ddddd'; + $suffix = ''; + for ($i=0; $i<12; $i++) { + $this->assertEquals($gen->names_max_length, + strlen($gen->getNameForObject($table, $fields, $suffix)), + 'Generated object name is too long. $i = '.$i); + } + + // This must only trim name when counter is 10 or more. + $table = 'cccc_dddd_eeee_ffff_gggg_hhhh_iiii'; + $fields = 'id'; + $suffix = 'idx'; + // Since we don't know how long prefix is, loop to generate tablename that gives exactly maxlengh-1 length. + // Skip this test if prefix is too long. + while (strlen($table) && strlen($gen->prefix.preg_replace('/_/','',$table).'_id_'.$suffix) >= $gen->names_max_length) { + $table = rtrim(substr($table, 0, strlen($table) - 1), '_'); + } + if (strlen($table)) { + $this->assertEquals($gen->names_max_length - 1, + strlen($gen->getNameForObject($table, $fields, $suffix))); + for ($i=0; $i<12; $i++) { + $this->assertEquals($gen->names_max_length, + strlen($gen->getNameForObject($table, $fields, $suffix)), + 'Generated object name is too long. $i = '.$i); + } + } + } + // Following methods are not supported == Do not test /* public function testRenameIndex() {