issue7338_prior_globals = [ 'database_hostname' => $database_hostname ?? null, 'database_port' => $database_port ?? null, 'database_default' => $database_default ?? null, 'database_sessions' => $database_sessions ?? [], ]; }); afterEach(function () { global $database_hostname, $database_port, $database_default, $database_sessions; $prior = $this->issue7338_prior_globals; $database_hostname = $prior['database_hostname']; $database_port = $prior['database_port']; $database_default = $prior['database_default']; $database_sessions = $prior['database_sessions']; }); function issue7338_seed(PDO $conn): void { $conn->exec('DROP TABLE IF EXISTS aggregate_graph_templates'); $conn->exec('DROP TABLE IF EXISTS aggregate_graphs'); $conn->exec('DROP TABLE IF EXISTS aggregate_graphs_items'); $conn->exec('CREATE TABLE aggregate_graph_templates (id INTEGER PRIMARY KEY, graph_template_id INTEGER, gprint_prefix TEXT, graph_type TEXT, total INTEGER, total_type INTEGER, total_prefix TEXT, order_type INTEGER)'); $conn->exec('CREATE TABLE aggregate_graphs (id INTEGER PRIMARY KEY, aggregate_template_id INTEGER, local_graph_id INTEGER, title_format TEXT)'); $conn->exec('CREATE TABLE aggregate_graphs_items (aggregate_graph_id INTEGER, local_graph_id INTEGER, sequence INTEGER)'); } test('a missing aggregate template stops the migration before any row is written', function () { $conn = new FakeMySQLPDO(); issue7338_seed($conn); issue7338_wire_default_connection($conn); // aggregate_graph_templates is empty: id 9999 cannot resolve. $_REQUEST['aggregate_template_id'] = '9999'; api_aggregate_convert_template([42, 43]); $graphs_count = (int) $conn->query('SELECT COUNT(*) AS c FROM aggregate_graphs')->fetch(PDO::FETCH_ASSOC)['c']; $items_count = (int) $conn->query('SELECT COUNT(*) AS c FROM aggregate_graphs_items')->fetch(PDO::FETCH_ASSOC)['c']; expect($graphs_count)->toBe(0); expect($items_count)->toBe(0); unset($_REQUEST['aggregate_template_id']); }); test('an existing aggregate template id that does not match any row also stops the migration', function () { $conn = new FakeMySQLPDO(); issue7338_seed($conn); issue7338_wire_default_connection($conn); $conn->exec("INSERT INTO aggregate_graph_templates VALUES (5, 1, 'in_out_', 'area', 1, 1, 'Total ', 1)"); // A real template exists, but the requested id (6) is not it. $_REQUEST['aggregate_template_id'] = '6'; api_aggregate_convert_template([42]); $graphs_count = (int) $conn->query('SELECT COUNT(*) AS c FROM aggregate_graphs')->fetch(PDO::FETCH_ASSOC)['c']; expect($graphs_count)->toBe(0); unset($_REQUEST['aggregate_template_id']); });