From 5d120cd0c8abfc9579e5805e85ea6e027885a543 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 15 Jan 2024 22:50:43 +0000 Subject: [PATCH] [tests] put tests inside transactions --- core/install.php | 2 ++ core/testcase.php | 16 +++------------- ext/mime/main.php | 9 ++++----- ext/upgrade/main.php | 9 ++++----- tests/bootstrap.php | 5 +++++ 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/core/install.php b/core/install.php index d82e55cb..5f8deff8 100644 --- a/core/install.php +++ b/core/install.php @@ -293,6 +293,8 @@ function create_tables(Database $db): void if ($db->is_transaction_open()) { $db->commit(); } + // Ensure that we end this code in a transaction (for testing) + $db->begin_transaction(); } catch (\PDOException $e) { throw new InstallerException( "PDO Error:", diff --git a/core/testcase.php b/core/testcase.php index 984e0f1a..458ec870 100644 --- a/core/testcase.php +++ b/core/testcase.php @@ -17,8 +17,6 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { global $_tracer, $database; $_tracer->begin(get_called_class()); $database->begin_transaction(); - self::create_user(self::$admin_name); - self::create_user(self::$user_name); parent::setUpBeforeClass(); } @@ -37,6 +35,7 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { } // Set up a clean environment for each test + $database->execute("SAVEPOINT test_start"); self::log_out(); foreach ($database->get_col("SELECT id FROM images") as $image_id) { send_event(new ImageDeletionEvent(Image::by_id((int)$image_id), true)); @@ -48,7 +47,8 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { public function tearDown(): void { - global $_tracer; + global $_tracer, $database; + $database->execute("ROLLBACK TO test_start"); $_tracer->end(); # test $_tracer->end(); # $this->getName() } @@ -63,16 +63,6 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { $_tracer->flush("data/test-trace.json"); } - protected static function create_user(string $name): void - { - if (is_null(User::by_name($name))) { - $userPage = new UserPage(); - $userPage->onUserCreation(new UserCreationEvent($name, $name, $name, "$name@$name.com", false)); - // @phpstan-ignore-next-line - ??? - assert(!is_null(User::by_name($name)), "Creation of user $name failed"); - } - } - private static function check_args(?array $args): array { if (!$args) { diff --git a/ext/mime/main.php b/ext/mime/main.php index c093d0c5..32afb536 100644 --- a/ext/mime/main.php +++ b/ext/mime/main.php @@ -30,11 +30,9 @@ class MimeSystem extends Extension // adjustment needs to be made to the mime types. if ($this->get_version(self::VERSION) < 1) { - if ($database->is_transaction_open()) { - // Each of these commands could hit a lot of data, combining - // them into one big transaction would not be a good idea. - $database->commit(); - } + // Each of these commands could hit a lot of data, combining + // them into one big transaction would not be a good idea. + $database->commit(); $database->set_timeout(null); // These updates can take a little bit $extensions = $database->get_col_iterable("SELECT DISTINCT ext FROM images"); @@ -55,6 +53,7 @@ class MimeSystem extends Extension } $this->set_version(self::VERSION, 1); + $database->begin_transaction(); } } diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 522be95f..639f0cc0 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -193,11 +193,9 @@ class Upgrade extends Extension if ($this->get_version("db_version") < 21) { log_info("upgrade", "Setting predictable media values for known file types"); - if ($database->is_transaction_open()) { - // Each of these commands could hit a lot of data, combining - // them into one big transaction would not be a good idea. - $database->commit(); - } + // Each of these commands could hit a lot of data, combining + // them into one big transaction would not be a good idea. + $database->commit(); $database->execute("UPDATE images SET lossless = :t, video = :t WHERE ext IN ('swf')", ["t" => true]); $database->execute("UPDATE images SET lossless = :f, video = :f, audio = :t WHERE ext IN ('mp3')", ["t" => true, "f" => false]); $database->execute("UPDATE images SET lossless = :f, video = :f, audio = :f WHERE ext IN ('jpg','jpeg')", ["f" => false]); @@ -206,6 +204,7 @@ class Upgrade extends Extension $database->execute("UPDATE images SET audio = :f WHERE ext IN ('webp')", ["f" => false]); $database->execute("UPDATE images SET lossless = :f, video = :t WHERE ext IN ('flv','mp4','m4v','ogv','webm')", ["t" => true, "f" => false]); $this->set_version("db_version", 21); + $database->begin_transaction(); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 05dcb497..098a8839 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -37,4 +37,9 @@ $config->set_bool("nice_urls", true); send_event(new DatabaseUpgradeEvent()); send_event(new InitExtEvent()); $user = User::by_id($config->get_int("anon_id", 0)); +$userPage = new UserPage(); +$userPage->onUserCreation(new UserCreationEvent("demo", "demo", "demo", "demo@demo.com", false)); +$userPage->onUserCreation(new UserCreationEvent("test", "test", "test", "test@test.com", false)); +$database->commit(); $_tracer->end(); +