From 5eaabb1c39303d202f9c7f1af34700325d83e491 Mon Sep 17 00:00:00 2001 From: rxu Date: Fri, 4 Jul 2025 00:08:28 +0700 Subject: [PATCH] [ticket/17525] Fix handling index name prefix logic for renaming PHPBB-17525 --- phpBB/develop/create_schema_files.php | 1 + phpBB/phpbb/db/doctrine/table_helper.php | 3 +- .../v400/rename_duplicated_index_names.php | 58 ++++++++------- phpBB/phpbb/db/tools/doctrine.php | 70 ++++++++++--------- phpBB/phpbb/db/tools/tools_interface.php | 28 +++++--- phpBB/phpbb/install/helper/database.php | 1 + .../install_database/task/add_tables.php | 1 + .../task/create_schema_file.php | 1 + tests/captcha/qa_test.php | 6 +- tests/console/user/base.php | 3 +- tests/dbal/auto_increment_test.php | 3 + tests/dbal/db_tools_test.php | 3 + tests/dbal/migrator_test.php | 3 + tests/extension/manager_test.php | 3 +- tests/extension/metadata_manager_test.php | 3 +- .../migrations_check_config_added_test.php | 8 ++- tests/migrator/convert_timezones_test.php | 12 +++- .../migrator/get_callable_from_step_test.php | 4 +- tests/migrator/schema_generator_test.php | 8 ++- tests/notification/convert_test.php | 3 +- tests/search/native_test.php | 3 +- .../phpbb_database_test_case.php | 1 + ...phpbb_database_test_connection_manager.php | 6 +- .../phpbb_functional_test_case.php | 1 + 24 files changed, 149 insertions(+), 84 deletions(-) diff --git a/phpBB/develop/create_schema_files.php b/phpBB/develop/create_schema_files.php index e7c44a7f34..a8c3fd8647 100644 --- a/phpBB/develop/create_schema_files.php +++ b/phpBB/develop/create_schema_files.php @@ -58,6 +58,7 @@ $db_doctrine = $ref->newInstanceWithoutConstructor(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($db_doctrine, true); +$db_tools->set_table_prefix($table_prefix); $tables_data = \Symfony\Component\Yaml\Yaml::parseFile($phpbb_root_path . '/config/default/container/tables.yml'); $tables = []; diff --git a/phpBB/phpbb/db/doctrine/table_helper.php b/phpBB/phpbb/db/doctrine/table_helper.php index ae80e23a50..9c638a2ea4 100644 --- a/phpBB/phpbb/db/doctrine/table_helper.php +++ b/phpBB/phpbb/db/doctrine/table_helper.php @@ -13,6 +13,7 @@ namespace phpbb\db\doctrine; use InvalidArgumentException; +use phpbb\db\tools\doctrine as doctrine_dbtools; class table_helper { @@ -137,7 +138,7 @@ class table_helper $short_table_names_map = []; foreach ($table_names as $table_name) { - $short_table_names_map[$table_name] = self::generate_shortname(str_replace($table_prefix, '', $table_name)); + $short_table_names_map[$table_name] = self::generate_shortname(doctrine_dbtools::remove_prefix($table_name, $table_prefix)); } return $short_table_names_map; diff --git a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php index dc053a0f77..9249c10744 100644 --- a/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php +++ b/phpBB/phpbb/db/migration/data/v400/rename_duplicated_index_names.php @@ -15,9 +15,15 @@ namespace phpbb\db\migration\data\v400; use phpbb\db\migration\migration; use phpbb\db\doctrine\table_helper; +use phpbb\db\tools\doctrine as doctrine_dbtools; class rename_duplicated_index_names extends migration { + /** + * @var array + */ + protected $table_keys = []; + public static function depends_on() { return [ @@ -28,24 +34,33 @@ class rename_duplicated_index_names extends migration public function update_schema() { $rename_index = []; - $is_prefixed_index = false; - $tables_index_names = $this->get_tables_index_names(); - $short_table_names = table_helper::map_short_table_names(array_keys($tables_index_names), $this->table_prefix); - - foreach ($tables_index_names as $table_name => $key_names) + if (empty($this->table_keys)) { + $this->get_tables_index_names(); + } + $short_table_names = table_helper::map_short_table_names(array_keys($this->table_keys), $this->table_prefix); + + foreach ($this->table_keys as $table_name => $key_names) + { + $prefixless_table_name = doctrine_dbtools::remove_prefix($table_name, $this->table_prefix); foreach ($key_names as $key_name) { - $prefixless_table_name = strpos($table_name, $this->table_prefix) === 0 ? substr($table_name, strlen($this->table_prefix)) : $table_name; - - // Check if there's at least one index name is prefixed, otherwise we operate on generated database schema - $is_prefixed_index = $is_prefixed_index || (strpos($key_name, $table_name) === 0); - - // If key name is prefixed by its table name (with or without tables prefix), remove that key name prefix. - $cleaned_key_name = !$is_prefixed_index ? $key_name : str_replace(strpos($key_name, $table_name) === 0 ? $table_name . '_' : $prefixless_table_name . '_', '', $key_name); + // If 'old' key name is already new format, do not rename it + if (doctrine_dbtools::is_prefixed($key_name, $short_table_names[$table_name])) + { + continue; + } + // If 'old' key name is prefixed by its table name with and/or without table name common prefix + // (f.e. 'phpbb_log_log_time'), remove it to prefix with the relevant table's short name + $cleaned_key_name = $key_name; + foreach ([$table_name, $prefixless_table_name] as $prefix) + { + $cleaned_key_name = doctrine_dbtools::remove_prefix($cleaned_key_name, $prefix); + } $key_name_new = $short_table_names[$table_name] . '_' . $cleaned_key_name; - $rename_index[$table_name][$key_name !== $cleaned_key_name ? $key_name : $cleaned_key_name] = $key_name_new; + + $rename_index[$table_name][$key_name] = $key_name_new; } } @@ -89,7 +104,6 @@ class rename_duplicated_index_names extends migration public function get_tables_index_names() { - $table_keys = []; $schema_manager = $this->db_tools->get_connection()->createSchemaManager(); $table_names = $schema_manager->listTableNames(); @@ -98,32 +112,28 @@ class rename_duplicated_index_names extends migration foreach ($table_names as $table_name) { $indices = $schema_manager->listTableIndexes($table_name); - - $index_names = array_keys( - array_filter($indices, function (\Doctrine\DBAL\Schema\Index $index) + $indices = array_keys(array_filter($indices, + function (\Doctrine\DBAL\Schema\Index $index) { return !$index->isPrimary(); }) ); - if (!empty($index_names)) + if (!empty($indices)) { - $table_keys[$table_name] = $index_names; + $this->table_keys[$table_name] = $indices; } } } else { - $db_table_schema = $this->get_schema(); - foreach ($db_table_schema as $table_name => $table_data) + foreach ($this->get_schema() as $table_name => $table_data) { if (isset($table_data['KEYS'])) { - $table_keys[$table_name] = array_keys($table_data['KEYS']); + $this->table_keys[$table_name] = array_keys($table_data['KEYS']); } } } - - return $table_keys; } } diff --git a/phpBB/phpbb/db/tools/doctrine.php b/phpBB/phpbb/db/tools/doctrine.php index 3853eae9cb..2fd72a5f38 100644 --- a/phpBB/phpbb/db/tools/doctrine.php +++ b/phpBB/phpbb/db/tools/doctrine.php @@ -49,6 +49,11 @@ class doctrine implements tools_interface */ private $return_statements; + /** + * @var string + */ + private $table_prefix; + /** * Database tools constructors. * @@ -94,6 +99,14 @@ class doctrine implements tools_interface return $this->get_schema_manager()->createSchema(); } + /** + * {@inheritDoc} + */ + public function set_table_prefix($table_prefix): void + { + $this->table_prefix = $table_prefix; + } + /** * {@inheritDoc} */ @@ -168,7 +181,6 @@ class doctrine implements tools_interface */ public function sql_index_exists(string $table_name, string $index_name): bool { - $index_name = self::normalize_index_name($table_name, $index_name); return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, true)); } @@ -177,7 +189,6 @@ class doctrine implements tools_interface */ public function sql_unique_index_exists(string $table_name, string $index_name): bool { - $index_name = self::normalize_index_name($table_name, $index_name); return $this->asset_exists($index_name, $this->get_filtered_index_list($table_name, false)); } @@ -332,7 +343,6 @@ class doctrine implements tools_interface */ public function sql_create_index(string $table_name, string $index_name, $column) { - $index_name = self::normalize_index_name($table_name, $index_name); return $this->alter_schema( function (Schema $schema) use ($table_name, $index_name, $column): void { @@ -346,8 +356,6 @@ class doctrine implements tools_interface */ public function sql_rename_index(string $table_name, string $index_name_old, string $index_name_new) { - $index_name_old = self::normalize_index_name($table_name, $index_name_old); - $index_name_new = self::normalize_index_name($table_name, $index_name_new); return $this->alter_schema( function (Schema $schema) use ($table_name, $index_name_old, $index_name_new): void { @@ -361,7 +369,6 @@ class doctrine implements tools_interface */ public function sql_index_drop(string $table_name, string $index_name) { - $index_name = self::normalize_index_name($table_name, $index_name); return $this->alter_schema( function (Schema $schema) use ($table_name, $index_name): void { @@ -375,7 +382,6 @@ class doctrine implements tools_interface */ public function sql_create_unique_index(string $table_name, string $index_name, $column) { - $index_name = self::normalize_index_name($table_name, $index_name); return $this->alter_schema( function (Schema $schema) use ($table_name, $index_name, $column): void { @@ -415,34 +421,18 @@ class doctrine implements tools_interface /** * {@inheritDoc} */ - public static function normalize_index_name(string $table_name, string $index_name, bool $remove_prefix = false): string + public static function add_prefix(string $name, string $prefix): string { - $prefixless_table_name = self::remove_prefix($table_name); - $short_table_name = table_helper::generate_shortname($prefixless_table_name); - if (!self::is_prefixed($index_name, $short_table_name . '_') - && !self::is_prefixed($index_name, $prefixless_table_name . '_') - && !self::is_prefixed($index_name, $table_name . '_') - ) - { - $index_name = $short_table_name . '_' . $index_name; - } - else if ($remove_prefix) - { - $index_name = self::remove_prefix($index_name); - } - - return $index_name; + return strpos($prefix, '_', -1) ? $prefix . $name : $prefix . '_' . $name; } /** * {@inheritDoc} */ - public static function remove_prefix(string $name): string + public static function remove_prefix(string $name, string $prefix = ''): string { - $prefix_end_pos = strpos($name, '_'); - $prefixless_name = substr($name, $prefix_end_pos + 1); - - return $prefixless_name; + $prefix = strpos($prefix, '_', -1) ? $prefix : $prefix . '_'; + return $prefix && self::is_prefixed($name, $prefix) ? substr($name, strlen($prefix)) : $name; } /** @@ -676,6 +666,7 @@ class doctrine implements tools_interface } $table = $schema->createTable($table_name); + $short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix)); $dbms_name = $this->get_schema_manager()->getDatabasePlatform()->getName(); foreach ($table_data['COLUMNS'] as $column_name => $column_data) @@ -701,7 +692,7 @@ class doctrine implements tools_interface foreach ($table_data['KEYS'] as $key_name => $key_data) { $columns = (is_array($key_data[1])) ? $key_data[1] : [$key_data[1]]; - $key_name = self::normalize_index_name($table_name, $key_name); + $key_name = !self::is_prefixed($key_name, $short_table_name) ? self::add_prefix($key_name, $short_table_name) : $key_name; // Supports key columns defined with there length $columns = array_map(function (string $column) @@ -897,7 +888,8 @@ class doctrine implements tools_interface { $columns = (is_array($column)) ? $column : [$column]; $table = $schema->getTable($table_name); - $index_name = self::normalize_index_name($table_name, $index_name); + $short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix)); + $index_name = !self::is_prefixed($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : $index_name; if ($safe_check && $table->hasIndex($index_name)) { @@ -919,7 +911,13 @@ class doctrine implements tools_interface protected function schema_rename_index(Schema $schema, string $table_name, string $index_name_old, string $index_name_new, bool $safe_check = false): void { $table = $schema->getTable($table_name); - $index_name_new = self::normalize_index_name($table_name, $index_name_new); + $short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix)); + + if (!$table->hasIndex($index_name_old)) + { + $index_name_old = !self::is_prefixed($index_name_old, $short_table_name) ? self::add_prefix($index_name_old, $short_table_name) : self::remove_prefix($index_name_old, $short_table_name); + } + $index_name_new = !self::is_prefixed($index_name_new, $short_table_name) ? self::add_prefix($index_name_new, $short_table_name) : $index_name_new; if ($safe_check && !$table->hasIndex($index_name_old)) { @@ -942,7 +940,8 @@ class doctrine implements tools_interface { $columns = (is_array($column)) ? $column : [$column]; $table = $schema->getTable($table_name); - $index_name = self::normalize_index_name($table_name, $index_name); + $short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix)); + $index_name = !self::is_prefixed($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : $index_name; if ($safe_check && $table->hasIndex($index_name)) { @@ -963,7 +962,12 @@ class doctrine implements tools_interface protected function schema_index_drop(Schema $schema, string $table_name, string $index_name, bool $safe_check = false): void { $table = $schema->getTable($table_name); - $index_name = self::normalize_index_name($table_name, $index_name); + $short_table_name = table_helper::generate_shortname(self::remove_prefix($table_name, $this->table_prefix)); + + if (!$table->hasIndex($index_name)) + { + $index_name = !self::is_prefixed($index_name, $short_table_name) ? self::add_prefix($index_name, $short_table_name) : self::remove_prefix($index_name, $short_table_name); + } if ($safe_check && !$table->hasIndex($index_name)) { diff --git a/phpBB/phpbb/db/tools/tools_interface.php b/phpBB/phpbb/db/tools/tools_interface.php index a3fdae8004..36472f12c5 100644 --- a/phpBB/phpbb/db/tools/tools_interface.php +++ b/phpBB/phpbb/db/tools/tools_interface.php @@ -235,24 +235,25 @@ interface tools_interface public function get_connection(): \Doctrine\DBAL\Connection; /** - * Adds short table name prefix to the index name if needed + * Adds prefix to a string if needed * - * @param string $table_name Table name with tables prefix - * @param string $index_name Index name - * @param bool $remove_prefix Flag indicating to remove short table name prefix if exists + * @param string $name Table name with tables prefix + * @param string $prefix Prefix to add * - * @return string Prefixed index name + * @return string Prefixed name */ - public static function normalize_index_name(string $table_name, string $index_name, bool $remove_prefix = false): string; + public static function add_prefix(string $name, string $prefix): string; /** - * Removes prefix from string if exists + * Removes prefix from string if exists. If prefix is empty, + * the first part of the string ending with underscore will be removed. * - * @param string $name String to remove the prefix from + * @param string $name String to remove the prefix from + * @param string $prefix Prefix to remove * * @return string Prefixless string */ - public static function remove_prefix(string $name): string; + public static function remove_prefix(string $name, string $prefix = ''): string; /** * Tests if a string is prefixed with the prefix defined @@ -263,4 +264,13 @@ interface tools_interface * @return bool True if a string id prefixed with the prefix defined, false otherwise */ public static function is_prefixed(string $name, string $prefix): bool; + + /** + * Sets table prefix + * + * @param string $table_prefix String to test vs prefix + * + * @return void + */ + public function set_table_prefix(string $table_prefix): void; } diff --git a/phpBB/phpbb/install/helper/database.php b/phpBB/phpbb/install/helper/database.php index ddf4a503b7..91fd9c7aa8 100644 --- a/phpBB/phpbb/install/helper/database.php +++ b/phpBB/phpbb/install/helper/database.php @@ -393,6 +393,7 @@ class database $doctrine_db = connection_factory::get_connection_from_params($dbms, $dbhost, $dbuser, $dbpass, $dbname, $dbport); $db_tools_factory = new \phpbb\db\tools\factory(); $db_tools = $db_tools_factory->get($doctrine_db); + $db_tools->set_table_prefix($table_prefix); $tables = $db_tools->sql_list_tables(); $tables = array_map('strtolower', $tables); $table_intersect = array_intersect($tables, $table_ary); diff --git a/phpBB/phpbb/install/module/install_database/task/add_tables.php b/phpBB/phpbb/install/module/install_database/task/add_tables.php index 0d7e81b9e8..dfe276b6c9 100644 --- a/phpBB/phpbb/install/module/install_database/task/add_tables.php +++ b/phpBB/phpbb/install/module/install_database/task/add_tables.php @@ -98,6 +98,7 @@ class add_tables extends task_base $this->schema_file_path = $phpbb_root_path . 'store/schema.json'; $this->table_prefix = $this->config->get('table_prefix'); $this->change_prefix = $this->config->get('change_table_prefix', true); + $this->db_tools->set_table_prefix($this->table_prefix); parent::__construct(true); } diff --git a/phpBB/phpbb/install/module/install_database/task/create_schema_file.php b/phpBB/phpbb/install/module/install_database/task/create_schema_file.php index e75bca65dc..20615c134a 100644 --- a/phpBB/phpbb/install/module/install_database/task/create_schema_file.php +++ b/phpBB/phpbb/install/module/install_database/task/create_schema_file.php @@ -145,6 +145,7 @@ class create_schema_file extends \phpbb\install\task_base $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->db_doctrine, true); + $db_tools->set_table_prefix($table_prefix); $tables_data = \Symfony\Component\Yaml\Yaml::parseFile($this->phpbb_root_path . '/config/default/container/tables.yml'); $tables = []; foreach ($tables_data['parameters'] as $parameter => $table) diff --git a/tests/captcha/qa_test.php b/tests/captcha/qa_test.php index 1c61a7d10c..f3b34302bc 100644 --- a/tests/captcha/qa_test.php +++ b/tests/captcha/qa_test.php @@ -25,7 +25,7 @@ class phpbb_captcha_qa_test extends \phpbb_database_test_case protected function setUp(): void { - global $db, $request, $phpbb_container; + global $db, $request, $phpbb_container, $table_prefix; $db = $this->new_dbal(); $db_doctrine = $this->new_doctrine_dbal(); @@ -35,7 +35,9 @@ class phpbb_captcha_qa_test extends \phpbb_database_test_case $request = new \phpbb_mock_request(); $phpbb_container = new \phpbb_mock_container_builder(); $factory = new \phpbb\db\tools\factory(); - $phpbb_container->set('dbal.tools', $factory->get($db_doctrine)); + $db_tools = $factory->get($db_doctrine); + $db_tools->set_table_prefix($table_prefix); + $phpbb_container->set('dbal.tools', $db_tools); $this->qa = new \phpbb\captcha\plugins\qa('phpbb_captcha_questions', 'phpbb_captcha_answers', 'phpbb_qa_confirm'); } diff --git a/tests/console/user/base.php b/tests/console/user/base.php index 511cfff9a3..1707ac2edb 100644 --- a/tests/console/user/base.php +++ b/tests/console/user/base.php @@ -32,7 +32,7 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case protected function setUp(): void { - global $auth, $db, $cache, $config, $user, $phpbb_dispatcher, $phpbb_container, $phpbb_root_path, $phpEx; + global $auth, $db, $cache, $config, $user, $phpbb_dispatcher, $phpbb_container, $phpbb_root_path, $phpEx, $table_prefix; $phpbb_dispatcher = new \phpbb\event\dispatcher(); $phpbb_container = new phpbb_mock_container_builder(); @@ -141,6 +141,7 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case $factory = new \phpbb\db\tools\factory(); $db_doctrine = $this->new_doctrine_dbal(); $db_tools = $factory->get($db_doctrine); + $db_tools->set_table_prefix($table_prefix); $migrator = new \phpbb\db\migrator( $phpbb_container, $config, diff --git a/tests/dbal/auto_increment_test.php b/tests/dbal/auto_increment_test.php index e238859afc..5d34cb49b8 100644 --- a/tests/dbal/auto_increment_test.php +++ b/tests/dbal/auto_increment_test.php @@ -26,12 +26,15 @@ class phpbb_dbal_auto_increment_test extends phpbb_database_test_case protected function setUp(): void { + global $table_prefix; + parent::setUp(); $this->db = $this->new_dbal(); $this->db_doctrine = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); $this->tools = $factory->get($this->db_doctrine); + $this->tools->set_table_prefix($table_prefix); $this->table_data = array( 'COLUMNS' => array( diff --git a/tests/dbal/db_tools_test.php b/tests/dbal/db_tools_test.php index 42ce4e924b..5bd0705daa 100644 --- a/tests/dbal/db_tools_test.php +++ b/tests/dbal/db_tools_test.php @@ -35,12 +35,15 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case protected function setUp(): void { + parent::setUp(); + $table_prefix = 'prefix_'; $this->db = $this->new_dbal(); $this->doctrine_db = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); $this->tools = $factory->get($this->doctrine_db); + $this->tools->set_table_prefix($table_prefix); $this->table_data = array( 'COLUMNS' => array( diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 4207e388de..f4b80fc5e9 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -53,12 +53,15 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case protected function setUp(): void { + global $table_prefix; + parent::setUp(); $this->db = $this->new_dbal(); $this->doctrine_db = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); $this->db_tools = $factory->get($this->doctrine_db); + $this->db_tools->set_table_prefix($table_prefix); $this->config = new \phpbb\config\db($this->db, new phpbb_mock_cache, 'phpbb_config'); diff --git a/tests/extension/manager_test.php b/tests/extension/manager_test.php index 73ac5a51de..e659d37a51 100644 --- a/tests/extension/manager_test.php +++ b/tests/extension/manager_test.php @@ -154,6 +154,7 @@ class phpbb_extension_manager_test extends phpbb_database_test_case { $phpbb_root_path = __DIR__ . './../../phpBB/'; $php_ext = 'php'; + $table_prefix = 'phpbb_'; $config = new \phpbb\config\config(array('version' => PHPBB_VERSION)); $db = $this->new_dbal(); @@ -162,7 +163,7 @@ class phpbb_extension_manager_test extends phpbb_database_test_case $factory = new \phpbb\db\tools\factory(); $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $php_ext); $db_tools = $factory->get($db_doctrine); - $table_prefix = 'phpbb_'; + $db_tools->set_table_prefix($table_prefix); $container = new phpbb_mock_container_builder(); diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 095efd3a69..b32b67c124 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -37,6 +37,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case { parent::setUp(); + $this->table_prefix = 'phpbb_'; $this->config = new \phpbb\config\config(array( 'version' => '3.1.0', )); @@ -45,13 +46,13 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $factory = new \phpbb\db\tools\factory(); $this->db_tools = $factory->get($this->db_doctrine); + $this->db_tools->set_table_prefix($this->table_prefix); $finder_factory = $this->createMock('\phpbb\finder\factory'); $this->phpbb_root_path = __DIR__ . '/'; $this->phpEx = 'php'; $this->cache = new \phpbb\cache\service(new phpbb_mock_cache(), $this->config, $this->db, $phpbb_dispatcher, $this->phpbb_root_path, $this->phpEx); - $this->table_prefix = 'phpbb_'; $container = new phpbb_mock_container_builder(); $cache_path = $this->phpbb_root_path . 'cache/twig'; diff --git a/tests/migrations/migrations_check_config_added_test.php b/tests/migrations/migrations_check_config_added_test.php index 1472f9e370..5cde980cde 100644 --- a/tests/migrations/migrations_check_config_added_test.php +++ b/tests/migrations/migrations_check_config_added_test.php @@ -55,6 +55,10 @@ class migrations_check_config_added_test extends phpbb_test_case { global $phpbb_root_path, $phpEx; + $this->table_prefix = 'phpbb_'; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $phpEx; + $this->config = new \phpbb\config\config([ 'search_type' => '\phpbb\search\fulltext_mysql', ]); @@ -63,9 +67,7 @@ class migrations_check_config_added_test extends phpbb_test_case $this->db_doctrine = $this->createMock(\Doctrine\DBAL\Connection::class); $factory = new \phpbb\db\tools\factory(); $this->db_tools = $factory->get($this->db_doctrine); - $this->table_prefix = 'phpbb_'; - $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $phpEx; + $this->db_tools->set_table_prefix($this->table_prefix); $tools = [ new \phpbb\db\migration\tool\config($this->config), diff --git a/tests/migrator/convert_timezones_test.php b/tests/migrator/convert_timezones_test.php index 7bc434e33c..9b7e80101b 100644 --- a/tests/migrator/convert_timezones_test.php +++ b/tests/migrator/convert_timezones_test.php @@ -18,10 +18,13 @@ class phpbb_migrator_convert_timezones_test extends phpbb_database_test_case public function getDataSet() { + global $table_prefix; + $this->db = $this->new_dbal(); $this->db_doctrine = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->db_doctrine); + $db_tools->set_table_prefix($table_prefix); // user_dst doesn't exist anymore, must re-add it to test this $db_tools->sql_column_add('phpbb_users', 'user_dst', array('BOOL', 1)); @@ -55,16 +58,18 @@ class phpbb_migrator_convert_timezones_test extends phpbb_database_test_case { parent::setUp(); - global $phpbb_root_path, $phpEx; + global $phpbb_root_path, $phpEx, $table_prefix; $this->db = $this->new_dbal(); $this->db_doctrine = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); + $db_tools = $factory->get($this->db_doctrine); + $db_tools->set_table_prefix($table_prefix); $this->migration = new \phpbb\db\migration\data\v310\timezone( new \phpbb\config\config(array()), $this->db, - $factory->get($this->db_doctrine), + $db_tools, $phpbb_root_path, $phpEx, 'phpbb_', @@ -84,6 +89,8 @@ class phpbb_migrator_convert_timezones_test extends phpbb_database_test_case public function test_convert() { + global $table_prefix; + $this->migration->update_timezones(0); $sql = 'SELECT user_id, user_timezone @@ -98,6 +105,7 @@ class phpbb_migrator_convert_timezones_test extends phpbb_database_test_case $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->db_doctrine); + $db_tools->set_table_prefix($table_prefix); // Remove the user_dst field again $db_tools->sql_column_remove('phpbb_users', 'user_dst'); diff --git a/tests/migrator/get_callable_from_step_test.php b/tests/migrator/get_callable_from_step_test.php index 1200568046..f8a92c742a 100644 --- a/tests/migrator/get_callable_from_step_test.php +++ b/tests/migrator/get_callable_from_step_test.php @@ -23,6 +23,8 @@ class get_callable_from_step_test extends phpbb_database_test_case $db = $this->new_dbal(); $db_doctrine = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); + $db_tools = $factory->get($db_doctrine); + $db_tools->set_table_prefix($table_prefix); $user = $this->getMockBuilder('\phpbb\user')->disableOriginalConstructor()->getMock(); $user->ip = '127.0.0.1'; $module_manager = new \phpbb\module\module_manager( @@ -38,7 +40,7 @@ class get_callable_from_step_test extends phpbb_database_test_case new phpbb_mock_container_builder(), new \phpbb\config\config(array()), $db, - $factory->get($db_doctrine), + $db_tools, 'phpbb_migrations', $phpbb_root_path, $php_ext, diff --git a/tests/migrator/schema_generator_test.php b/tests/migrator/schema_generator_test.php index 04c73f925e..943029304f 100644 --- a/tests/migrator/schema_generator_test.php +++ b/tests/migrator/schema_generator_test.php @@ -30,14 +30,16 @@ class schema_generator_test extends phpbb_test_case parent::setUp(); + $this->table_prefix = 'phpbb_'; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $phpEx; + $this->config = new \phpbb\config\config(array()); $this->db = new \phpbb\db\driver\sqlite3(); $this->doctrine_db = \phpbb\db\doctrine\connection_factory::get_connection(new phpbb_mock_config_php_file()); $factory = new \phpbb\db\tools\factory(); $this->db_tools = $factory->get($this->doctrine_db); - $this->table_prefix = 'phpbb_'; - $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $phpEx; + $this->db_tools->set_table_prefix($this->table_prefix); } protected function get_schema_generator(array $class_names) diff --git a/tests/notification/convert_test.php b/tests/notification/convert_test.php index 6c1f7b496e..2a7de4ea84 100644 --- a/tests/notification/convert_test.php +++ b/tests/notification/convert_test.php @@ -25,12 +25,13 @@ class phpbb_notification_convert_test extends phpbb_database_test_case { parent::setUp(); - global $phpbb_root_path, $phpEx; + global $phpbb_root_path, $phpEx, $table_prefix; $this->db = $this->new_dbal(); $this->doctrine_db = $this->new_doctrine_dbal(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->doctrine_db); + $db_tools->set_table_prefix($table_prefix); $core_tables = self::get_core_tables(); // Add user_notify_type column for testing this migration and set type diff --git a/tests/search/native_test.php b/tests/search/native_test.php index 0d8d8e6810..edb0cc43fc 100644 --- a/tests/search/native_test.php +++ b/tests/search/native_test.php @@ -25,7 +25,7 @@ class phpbb_search_native_test extends phpbb_search_test_case protected function setUp(): void { - global $phpbb_root_path, $phpEx, $config, $cache; + global $phpbb_root_path, $phpEx, $config, $cache, $table_prefix; parent::setUp(); @@ -41,6 +41,7 @@ class phpbb_search_native_test extends phpbb_search_test_case $this->db = $this->new_dbal(); $tools_factory = new \phpbb\db\tools\factory(); $this->db_tools = $tools_factory->get($this->new_doctrine_dbal()); + $this->db_tools->set_table_prefix($table_prefix); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native'); $config['fulltext_native_min_chars'] = 2; diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index 09de77bc35..76e77add44 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -88,6 +88,7 @@ abstract class phpbb_database_test_case extends TestCase $doctrine = \phpbb\db\doctrine\connection_factory::get_connection(new phpbb_mock_config_php_file()); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($doctrine, true); + $db_tools->set_table_prefix($table_prefix); $schema_generator = new \phpbb\db\migration\schema_generator($classes, new \phpbb\config\config(array()), $db, $db_tools, $phpbb_root_path, $phpEx, $table_prefix, self::get_core_tables()); file_put_contents(self::$schema_file, json_encode($schema_generator->get_schema())); diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index db73248f6c..8759a1f8c0 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -327,6 +327,8 @@ class phpbb_database_test_connection_manager */ protected function load_schema_from_file($directory, \phpbb\db\driver\driver_interface $db, \Doctrine\DBAL\Connection $doctrine) { + global $table_prefix; + $schema = $this->dbms['SCHEMA']; if ($this->config['dbms'] == 'phpbb\db\driver\mysql') @@ -363,7 +365,7 @@ class phpbb_database_test_connection_manager } else { - global $phpbb_root_path, $phpEx, $table_prefix; + global $phpbb_root_path, $phpEx; $finder = new \phpbb\finder\finder(null, false, $phpbb_root_path, $phpEx); $classes = $finder->core_path('phpbb/db/migration/data/') @@ -373,6 +375,7 @@ class phpbb_database_test_connection_manager $doctrine = \phpbb\db\doctrine\connection_factory::get_connection(new phpbb_mock_config_php_file()); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($doctrine, true); + $db_tools->set_table_prefix($table_prefix); $tables = phpbb_database_test_case::get_core_tables(); $schema_generator = new \phpbb\db\migration\schema_generator($classes, new \phpbb\config\config(array()), $db, $db_tools, $phpbb_root_path, $phpEx, $table_prefix, $tables); @@ -381,6 +384,7 @@ class phpbb_database_test_connection_manager $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($doctrine); + $db_tools->set_table_prefix($table_prefix); foreach ($db_table_schema as $table_name => $table_data) { $db_tools->sql_create_table( diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index c42e0aa60b..d093768375 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -286,6 +286,7 @@ class phpbb_functional_test_case extends phpbb_test_case $factory = new \phpbb\db\tools\factory(); $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $phpEx); $db_tools = $factory->get($db_doctrine); + $db_tools->set_table_prefix(self::$config['table_prefix']); $container = new phpbb_mock_container_builder(); $migrator = new \phpbb\db\migrator(