From 75c5fe9459260e91e8887cc68e845865a0d6490b Mon Sep 17 00:00:00 2001 From: rxu Date: Wed, 25 Jun 2025 22:24:50 +0700 Subject: [PATCH] [ticket/17525] Add index names test for generated database schema PHPBB-17525 --- phpBB/phpbb/db/doctrine/table_helper.php | 2 +- tests/dbal/migrator_test.php | 55 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index 448597df3d..70ac39078f 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -127,7 +127,7 @@ class table_helper * Maps short table names for the purpose of prefixing tables' index names. * * @param array $additional_tables Additional table names without prefix to add to the map. - * @param array $table_prefix Tables prefix. + * @param string $table_prefix Tables prefix. * * @return array Pairs of table names and their short name representations. * @psalm-return array{string, string} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index f48ad35275..ac502f1816 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -440,4 +440,59 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar1')); $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar2')); } + + public function test_schema_generator(): array + { + global $phpbb_root_path, $phpEx; + + $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $phpEx); + $finder = $finder_factory->get(); + $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); + + $schema_generator = new \phpbb\db\migration\schema_generator( + $migrator_classes, + $this->config, + $this->db, + $this->db_tools, + $phpbb_root_path, + $phpEx, + 'phpbb_', + self::get_core_tables() + ); + $db_table_schema = $schema_generator->get_schema(); + + $this->assertNotEmpty($db_table_schema); + + return $db_table_schema; + } + + /** + * @depends test_schema_generator + */ + public function test_table_indexes(array $db_table_schema) + { + $table_keys = []; + foreach ($db_table_schema as $table_name => $table_data) + { + if (isset($table_data['KEYS'])) + { + foreach ($table_data['KEYS'] as $key_name => $key_data) + { + $table_keys[$table_name][] = $key_name; + } + } + } + + $this->assertNotEmpty($table_keys); + + $short_table_names = \phpbb\db\doctrine\table_helper::map_short_table_names([], 'phpbb_'); + foreach ($table_keys as $table_name => $key_names) + { + $index_prefix = $short_table_names[$table_name] . '_'; + foreach ($key_names as $key_name) + { + $this->assertFalse(strpos($key_name, $index_prefix), "$key_name does not contain $index_prefix"); + } + } + } }