Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.01% |
65 / 67 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| migrations_check_config_added_test | |
97.01% |
65 / 67 |
|
66.67% |
2 / 3 |
13 | |
0.00% |
0 / 1 |
| setUp | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_config_names | |
96.67% |
58 / 60 |
|
0.00% |
0 / 1 |
10 | |||
| test_config_option_exists_in_schema_data | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | class migrations_check_config_added_test extends phpbb_test_case |
| 15 | { |
| 16 | /** @var \phpbb\config\config */ |
| 17 | protected $config; |
| 18 | |
| 19 | /** @var \Symfony\Component\DependencyInjection\ContainerInterface */ |
| 20 | protected $container; |
| 21 | |
| 22 | /** @var \phpbb\db\driver\driver_interface */ |
| 23 | protected $db; |
| 24 | |
| 25 | /** @var \Doctrine\DBAL\Connection */ |
| 26 | protected $db_doctrine; |
| 27 | |
| 28 | /** @var \phpbb\db\tools\tools_interface */ |
| 29 | protected $db_tools; |
| 30 | |
| 31 | /** @var \phpbb\extension\manager */ |
| 32 | protected $extension_manager; |
| 33 | |
| 34 | /** @var \phpbb\db\migrator */ |
| 35 | protected $migrator; |
| 36 | |
| 37 | /** @var string */ |
| 38 | protected $table_prefix; |
| 39 | |
| 40 | /** @var string */ |
| 41 | protected $phpbb_root_path; |
| 42 | |
| 43 | /** @var string */ |
| 44 | protected $php_ext; |
| 45 | |
| 46 | /** @var string */ |
| 47 | protected $schema_data; |
| 48 | |
| 49 | protected function setUp(): void |
| 50 | { |
| 51 | global $phpbb_root_path; |
| 52 | |
| 53 | // Get schema data from file |
| 54 | $this->schema_data = file_get_contents($phpbb_root_path . 'install/schemas/schema_data.sql'); |
| 55 | } |
| 56 | |
| 57 | protected function get_config_names() |
| 58 | { |
| 59 | global $phpbb_root_path, $phpEx; |
| 60 | |
| 61 | $this->table_prefix = 'phpbb_'; |
| 62 | $this->phpbb_root_path = $phpbb_root_path; |
| 63 | $this->php_ext = $phpEx; |
| 64 | |
| 65 | $this->config = new \phpbb\config\config([ |
| 66 | 'search_type' => '\phpbb\search\fulltext_mysql', |
| 67 | ]); |
| 68 | |
| 69 | $this->db = $this->createMock('\phpbb\db\driver\driver_interface'); |
| 70 | $this->db_doctrine = $this->createMock(\Doctrine\DBAL\Connection::class); |
| 71 | $factory = new \phpbb\db\tools\factory(); |
| 72 | $this->db_tools = $factory->get($this->db_doctrine); |
| 73 | $this->db_tools->set_table_prefix($this->table_prefix); |
| 74 | |
| 75 | $tools = [ |
| 76 | new \phpbb\db\migration\tool\config($this->config), |
| 77 | ]; |
| 78 | |
| 79 | $this->container = new phpbb_mock_container_builder(); |
| 80 | |
| 81 | $this->migrator = new \phpbb\db\migrator( |
| 82 | $this->container, |
| 83 | $this->config, |
| 84 | $this->db, |
| 85 | $this->db_tools, |
| 86 | 'phpbb_migrations', |
| 87 | $this->phpbb_root_path, |
| 88 | $this->php_ext, |
| 89 | $this->table_prefix, |
| 90 | [], |
| 91 | $tools, |
| 92 | new \phpbb\db\migration\helper() |
| 93 | ); |
| 94 | $this->container->set('migrator', $this->migrator); |
| 95 | |
| 96 | $this->extension_manager = new phpbb_mock_extension_manager( |
| 97 | $this->phpbb_root_path, |
| 98 | [], |
| 99 | $this->container |
| 100 | ); |
| 101 | |
| 102 | // Get all migrations |
| 103 | $migrations = $this->extension_manager |
| 104 | ->get_finder() |
| 105 | ->core_path('phpbb/db/migration/data/') |
| 106 | ->extension_directory('/migrations') |
| 107 | ->get_classes(); |
| 108 | |
| 109 | $config_names = $config_removed = []; |
| 110 | foreach ($migrations as $key => $class) |
| 111 | { |
| 112 | // Filter non-migration files |
| 113 | if (!$this->migrator::is_migration($class)) |
| 114 | { |
| 115 | unset($migrations[$key]); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | // Create migration object instance |
| 120 | $migration = $this->migrator->get_migration($class); |
| 121 | |
| 122 | // $step[0] - action (config.*|if|custom|etc), $step[1][1] - action when wrapped with 'if' action |
| 123 | // $step[1] - action parameters for non-'if'-wrapped actions (0 => config_name and 1 => config_value) |
| 124 | // $step[1][0] - configuration option to add/update/remove (config_name) |
| 125 | foreach ($migration->update_data() as $migration_name => $step) |
| 126 | { |
| 127 | if ($step[0] == 'if') |
| 128 | { |
| 129 | $step = $step[1][1]; |
| 130 | } |
| 131 | |
| 132 | // Filter out actions different from config.* |
| 133 | if ($step[0] == 'custom' || strpos($step[0], 'config') === false) |
| 134 | { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | $config_name = $step[1][0]; |
| 139 | // Exclude removed configuration options and filter them out |
| 140 | if ($step[0] == 'config.remove') |
| 141 | { |
| 142 | if (!isset($config_removed[$config_name])) |
| 143 | { |
| 144 | $config_removed[$config_name] = true; |
| 145 | } |
| 146 | |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // Fill error entries for configuration options which were not added to schema_data.sql |
| 151 | if (!isset($config_names[$config_name])) |
| 152 | { |
| 153 | $config_names[$config_name] = [$config_name, $class]; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Drop configuration options which were removed by config.remove |
| 159 | $config_names = array_diff_key($config_names, $config_removed); |
| 160 | |
| 161 | return array_combine(array_column($config_names, 0), array_column($config_names, 1)); |
| 162 | } |
| 163 | |
| 164 | public function test_config_option_exists_in_schema_data() |
| 165 | { |
| 166 | $message = 'Migration: %1$s, config_name: %2$s; not added to schema_data.sql'; |
| 167 | foreach ($this->get_config_names() as $config_name => $class) |
| 168 | { |
| 169 | $this->assertNotFalse(strpos($this->schema_data, $config_name), |
| 170 | sprintf($message, $class, $config_name) |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | } |