diff --git a/phpBB/phpbb/db/driver/mysqli.php b/phpBB/phpbb/db/driver/mysqli.php index e474b2584e..6182801da6 100644 --- a/phpBB/phpbb/db/driver/mysqli.php +++ b/phpBB/phpbb/db/driver/mysqli.php @@ -155,7 +155,9 @@ class mysqli extends \phpbb\db\driver\mysql_base switch ($status) { case 'begin': - return @mysqli_autocommit($this->db_connect_id, false); + @mysqli_autocommit($this->db_connect_id, false); + $result = @mysqli_begin_transaction($this->db_connect_id); + return $result; break; case 'commit': diff --git a/tests/dbal/write_test.php b/tests/dbal/write_test.php index f0093ff869..f94aebd85b 100644 --- a/tests/dbal/write_test.php +++ b/tests/dbal/write_test.php @@ -73,6 +73,67 @@ class phpbb_dbal_write_test extends phpbb_database_test_case $db->sql_freeresult($result); } + public function test_delete_rollback() + { + $db = $this->new_dbal(); + + $is_myisam = false; + if ($db->get_sql_layer() === 'mysqli') + { + $table_status = $db->get_table_status('phpbb_config'); + $is_myisam = isset($table_status['Engine']) && $table_status['Engine'] === 'MyISAM'; + } + + $db->sql_transaction('begin'); + + $sql = "DELETE FROM phpbb_config + WHERE config_name = 'config1'"; + $db->sql_query($sql); + + // Rollback and check that nothing was changed + $db->sql_transaction('rollback'); + + $sql = 'SELECT * + FROM phpbb_config'; + $result = $db->sql_query($sql); + $rows = $db->sql_fetchrowset($result); + $db->sql_freeresult($result); + + if (!$is_myisam) + { + $this->assertEquals(2, count($rows)); + $this->assertEquals('config1', $rows[0]['config_name']); + } + else + { + // Rollback does not work on MyISAM + $this->assertEquals(1, count($rows)); + $this->assertEquals('config2', $rows[0]['config_name']); + + // Restore deleted config value on MyISAM + $sql = "INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('config1', 'foo', 0)"; + $db->sql_query($sql); + } + + $db->sql_transaction('begin'); + + $sql = "DELETE FROM phpbb_config + WHERE config_name = 'config1'"; + $db->sql_query($sql); + + // Commit and check that data was actually changed + $db->sql_transaction('commit'); + + $sql = 'SELECT * + FROM phpbb_config'; + $result = $db->sql_query($sql); + $rows = $db->sql_fetchrowset($result); + $db->sql_freeresult($result); + + $this->assertEquals(1, count($rows)); + $this->assertEquals('config2', $rows[0]['config_name']); + } + public function test_multiple_insert() { $db = $this->new_dbal(); diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index c46bbe11c3..a7054d5635 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -285,7 +285,7 @@ abstract class phpbb_database_test_case extends TestCase return $this->createDefaultDBConnection($manager->get_pdo(), 'testdb'); } - public function new_dbal() + public function new_dbal() : \phpbb\db\driver\driver_interface { $config = $this->get_database_config();