diff --git a/tests/migrations/migrations_check_config_added_test.php b/tests/migrations/migrations_check_config_added_test.php new file mode 100644 index 0000000000..25898778e8 --- /dev/null +++ b/tests/migrations/migrations_check_config_added_test.php @@ -0,0 +1,133 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class migrations_check_config_added_test extends phpbb_test_case +{ + public function setUp(): void + { + global $phpbb_root_path; + + // Get schema data from file + $this->schema_data = file_get_contents($phpbb_root_path . 'install/schemas/schema_data.sql'); + } + + public function get_config_options_from_migrations() + { + global $phpbb_root_path, $phpEx; + + $this->config = new \phpbb\config\config([ + 'search_type' => '\phpbb\search\fulltext_mysql', + ]); + + $this->db = $this->createMock('\phpbb\db\driver\driver_interface'); + $factory = new \phpbb\db\tools\factory(); + $this->db_tools = $factory->get($this->db); + $this->table_prefix = 'phpbb_'; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $phpEx; + + $this->container = new phpbb_mock_container_builder(); + + $this->migrator = new \phpbb\db\migrator( + $this->container, + $this->config, + $this->db, + $this->db_tools, + 'phpbb_migrations', + $this->phpbb_root_path, + $this->php_ext, + $this->table_prefix, + [], + new \phpbb\db\migration\helper() + ); + + $this->extension_manager = new phpbb_mock_extension_manager( + $this->phpbb_root_path, + [], + $this->container + ); + + // Get all migrations + $migrations = $this->extension_manager + ->get_finder() + ->core_path('phpbb/db/migration/data/') + ->extension_directory('/migrations') + ->get_classes(); + + $config_names = $config_removed = []; + foreach ($migrations as $key => $class) + { + // Filter non-migration files + if (!$this->migrator::is_migration($class)) + { + unset($migrations[$key]); + continue; + } + + // Create migration object instance + $migration = new $class($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix); + + // $step[0] - action (config.*|if|custom|etc), $step[1][1] - action when wrapped with 'if' action + // $step[1] - action parameters for non-'if'-wrapped actions (0 => config_name and 1 => config_value) + // $step[1][0] - configuration option to add/update/remove (config_name) + foreach ($migration->update_data() as $migration_name => $step) + { + if ($step[0] == 'if') + { + $step = $step[1][1]; + } + + // Filter out actions different from config.* + if ($step[0] == 'custom' || strpos($step[0], 'config') === false) + { + continue; + } + + // Exclude removed configuration options and filter them out + if ($step[0] == 'config.remove') + { + if (!isset($config_removed[$step[1][0]])) + { + $config_removed[$step[1][0]] = true; + } + + continue; + } + + $action = explode('.', $step[0]); + $method = $action[1]; + $config_name = $step[1][0]; + + // Fill error entries for configuration options which were not added to shema_data.sql + if (!isset($config_names[$config_name]) && !isset($config_removed[$config_name])) + { + $config_names[$config_name] = [$config_name, $class]; + } + } + } + + return $config_names; + } + + /** + * @dataProvider get_config_options_from_migrations + */ + public function test_config_option_exists_in_schema_data($config_name, $class) + { + $message = 'Migration: %1$s, config_name: %2$s; not added to shema_data.sql'; + + $this->assertNotFalse(strpos($this->schema_data, $config_name), + sprintf($message, $class, $config_name) + ); + } +}