|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Knowledge Base extension for the phpBB Forum Software package |
| 5 | + * |
| 6 | + * @copyright (c) 2026, phpBB Modders, https://www.phpbbmodders.com/ |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbbmodders\knowledgebase\migrations; |
| 12 | + |
| 13 | +require_once __DIR__ . '/kb_extensions_test.php'; |
| 14 | +require_once dirname(__DIR__) . '/migrations/version_1_0_0.php'; |
| 15 | +require_once dirname(__DIR__) . '/migrations/version_1_0_3.php'; |
| 16 | +require_once dirname(__DIR__) . '/migrations/version_2_0_2.php'; |
| 17 | +require_once dirname(__DIR__) . '/migrations/version_2_0_15.php'; |
| 18 | +require_once dirname(__DIR__) . '/migrations/version_2_0_30.php'; |
| 19 | +require_once dirname(__DIR__) . '/migrations/version_2_0_31.php'; |
| 20 | +if (!defined('CONFIG_TABLE')) define('CONFIG_TABLE', 'phpbb_config'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Migrations that stopped a fresh PostgreSQL board from enabling the extension. |
| 24 | + * |
| 25 | + * version_2_0_30 adds kb_m_change_author after the seven options |
| 26 | + * version_1_0_0 inserted with explicit ids. On PostgreSQL those inserts leave |
| 27 | + * the table's sequence at 1, so the new option must not take its id from it. |
| 28 | + * |
| 29 | + * version_2_0_31 rebuilds the search-match index that version_1_0_3 created |
| 30 | + * as plain un_mtch on PostgreSQL, where phpBB 3.3's tools drop it as |
| 31 | + * {table}_un_mtch. |
| 32 | + */ |
| 33 | +class kb_options_id_migration_test extends \PHPUnit\Framework\TestCase |
| 34 | +{ |
| 35 | + private $db; |
| 36 | + private $tools; |
| 37 | + private $sqlite_fixture; |
| 38 | + |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + if ($this->tools instanceof \phpbb\db\tools\doctrine) $this->tools->get_connection()->close(); |
| 42 | + if ($this->db) $this->db->sql_close(); |
| 43 | + if ($this->sqlite_fixture) @unlink($this->sqlite_fixture); |
| 44 | + } |
| 45 | + |
| 46 | + public function engines() |
| 47 | + { |
| 48 | + return [['sqlite'], ['mysql'], ['postgres']]; |
| 49 | + } |
| 50 | + |
| 51 | + private function connect(string $engine): void |
| 52 | + { |
| 53 | + if ($engine === 'sqlite') |
| 54 | + { |
| 55 | + $this->db = new \phpbb\db\driver\sqlite3(); |
| 56 | + if (class_exists(\phpbb\db\tools\doctrine::class)) |
| 57 | + { |
| 58 | + $this->sqlite_fixture = tempnam(sys_get_temp_dir(), 'kb-options-'); |
| 59 | + $this->assertTrue($this->db->sql_connect($this->sqlite_fixture, '', '', '')); |
| 60 | + $this->tools = new \phpbb\db\tools\doctrine(\phpbb\db\doctrine\connection_factory::get_connection_from_params('sqlite3', $this->sqlite_fixture)); |
| 61 | + $this->tools->set_table_prefix('phpbb_'); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + $this->assertTrue($this->db->sql_connect(':memory:', '', '', '')); |
| 66 | + $this->tools = new \phpbb\db\tools\sqlite3($this->db); |
| 67 | + } |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + $port = getenv('KB_TEST_' . strtoupper($engine) . '_PORT'); |
| 72 | + if (!$port) $this->markTestSkipped('Set the fixture database port to run integration tests'); |
| 73 | + $this->assertTrue(ctype_digit($port)); |
| 74 | + $this->db = $engine === 'mysql' ? new \phpbb\db\driver\mysqli() : new \phpbb\db\driver\postgres(); |
| 75 | + $this->assertIsObject($this->db->sql_connect('127.0.0.1', $engine === 'mysql' ? 'root' : 'postgres', '', 'kb_fixture', (int) $port)); |
| 76 | + if (class_exists(\phpbb\db\tools\doctrine::class)) |
| 77 | + { |
| 78 | + $this->tools = new \phpbb\db\tools\doctrine(\phpbb\db\doctrine\connection_factory::get_connection_from_params( |
| 79 | + $engine === 'mysql' ? 'mysqli' : 'postgres', '127.0.0.1', $engine === 'mysql' ? 'root' : 'postgres', '', 'kb_fixture', $port |
| 80 | + )); |
| 81 | + $this->tools->set_table_prefix('phpbb_'); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + $this->tools = $engine === 'mysql' ? new \phpbb\db\tools\tools($this->db) : new \phpbb\db\tools\postgres($this->db); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** @dataProvider engines */ |
| 90 | + public function test_the_author_permission_is_added_after_the_first_options_on_a_fresh_board($engine) |
| 91 | + { |
| 92 | + $this->connect($engine); |
| 93 | + foreach (['phpbb_kb_options', 'phpbb_kb_users', 'phpbb_kb_groups'] as $table) |
| 94 | + { |
| 95 | + if ($this->tools->sql_table_exists($table)) $this->tools->sql_table_drop($table); |
| 96 | + if ($this->db->get_sql_layer() === 'postgres') $this->db->sql_query('DROP SEQUENCE IF EXISTS ' . $table . '_seq'); |
| 97 | + } |
| 98 | + |
| 99 | + // The options table and its first seven options, exactly as version_1_0_0 makes them |
| 100 | + $config = new \phpbb\config\config([]); |
| 101 | + $first = new version_1_0_0($config, $this->db, $this->tools, '', 'php', 'phpbb_', []); |
| 102 | + $this->tools->sql_create_table('phpbb_kb_options', $first->update_schema()['add_tables']['phpbb_kb_options']); |
| 103 | + $first->update_kb_options_table(); |
| 104 | + |
| 105 | + (new version_2_0_30($config, $this->db, $this->tools, '', 'php', 'phpbb_', []))->add_permission(); |
| 106 | + |
| 107 | + $result = $this->db->sql_query('SELECT auth_option_id, auth_option FROM phpbb_kb_options ORDER BY auth_option_id'); |
| 108 | + $options = []; |
| 109 | + while ($row = $this->db->sql_fetchrow($result)) |
| 110 | + { |
| 111 | + $options[(int) $row['auth_option_id']] = $row['auth_option']; |
| 112 | + } |
| 113 | + $this->db->sql_freeresult($result); |
| 114 | + |
| 115 | + $this->assertSame('kb_m_change_author', $options[8] ?? null); |
| 116 | + $this->assertCount(8, $options); |
| 117 | + |
| 118 | + // Running it again adds nothing |
| 119 | + (new version_2_0_30($config, $this->db, $this->tools, '', 'php', 'phpbb_', []))->add_permission(); |
| 120 | + $result = $this->db->sql_query('SELECT COUNT(*) AS total FROM phpbb_kb_options'); |
| 121 | + $this->assertSame(8, (int) $this->db->sql_fetchfield('total')); |
| 122 | + $this->db->sql_freeresult($result); |
| 123 | + |
| 124 | + $this->tools->sql_table_drop('phpbb_kb_options'); |
| 125 | + if ($this->db->get_sql_layer() === 'postgres') $this->db->sql_query('DROP SEQUENCE IF EXISTS phpbb_kb_options_seq'); |
| 126 | + } |
| 127 | + |
| 128 | + /** @dataProvider engines */ |
| 129 | + public function test_the_search_match_index_is_rebuilt_after_version_1_0_3_made_it($engine) |
| 130 | + { |
| 131 | + $this->connect($engine); |
| 132 | + $table = 'phpbb_kb_src_wrdmtch'; |
| 133 | + foreach ([$table, 'phpbb_kb_articles'] as $name) |
| 134 | + { |
| 135 | + if ($this->tools->sql_table_exists($name)) $this->tools->sql_table_drop($name); |
| 136 | + } |
| 137 | + // version_1_0_3 also drops a kb_articles column |
| 138 | + $this->tools->sql_create_table('phpbb_kb_articles', ['COLUMNS' => ['article_id' => ['UINT', 0], 'bbcode_bitfield' => ['VCHAR:32', '']]]); |
| 139 | + |
| 140 | + // The table as version_1_0_0, 1_0_3 (schema and data), 2_0_2 and 2_0_15 leave it |
| 141 | + $config = new \phpbb\config\config([]); |
| 142 | + $migration = fn($class) => new $class($config, $this->db, $this->tools, '', 'php', 'phpbb_', []); |
| 143 | + $this->tools->sql_create_table($table, $migration(version_1_0_0::class)->update_schema()['add_tables'][$table]); |
| 144 | + $this->tools->perform_schema_changes($migration(version_1_0_3::class)->update_schema()); |
| 145 | + $migration(version_1_0_3::class)->add_key(); |
| 146 | + $this->tools->perform_schema_changes($migration(version_2_0_2::class)->update_schema()); |
| 147 | + $this->tools->perform_schema_changes($migration(version_2_0_15::class)->update_schema()); |
| 148 | + $this->db->sql_query('INSERT INTO ' . $table . ' (article_id, word_id, title_match, descr_match) VALUES (1, 1, 0, 1)'); |
| 149 | + |
| 150 | + $migration(version_2_0_31::class)->rebuild_match_index(); |
| 151 | + // Run again (an enable retried): still fine |
| 152 | + $migration(version_2_0_31::class)->rebuild_match_index(); |
| 153 | + |
| 154 | + // The match survives, and the four-column unique key holds |
| 155 | + $result = $this->db->sql_query('SELECT COUNT(*) AS total FROM ' . $table); |
| 156 | + $this->assertSame(1, (int) $this->db->sql_fetchfield('total')); |
| 157 | + $this->db->sql_freeresult($result); |
| 158 | + $this->db->sql_return_on_error(true); |
| 159 | + $duplicate = $this->db->sql_query('INSERT INTO ' . $table . ' (article_id, word_id, title_match, descr_match) VALUES (1, 1, 0, 1)'); |
| 160 | + $this->db->sql_return_on_error(false); |
| 161 | + $this->assertFalse($duplicate, 'a duplicate match should be refused by the unique index'); |
| 162 | + $this->assertNotFalse($this->db->sql_query('INSERT INTO ' . $table . ' (article_id, word_id, title_match, descr_match) VALUES (1, 1, 0, 0)')); |
| 163 | + |
| 164 | + $this->tools->sql_table_drop($table); |
| 165 | + $this->tools->sql_table_drop('phpbb_kb_articles'); |
| 166 | + } |
| 167 | +} |
0 commit comments