diff --git a/changelog.d/8527.issue b/changelog.d/8527.issue new file mode 100644 index 0000000000..ae6fc1a4e0 --- /dev/null +++ b/changelog.d/8527.issue @@ -0,0 +1 @@ +Keep graph item groups and their legends within the selected instance graph diff --git a/lib/functions.php b/lib/functions.php index 206d96a336..44dcdb8d63 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -4274,7 +4274,7 @@ function get_graph_group(int $graph_template_item_id) : array { $params[] = $graph_item['graph_template_id']; $sql_where = 'graph_template_id = ? AND local_graph_id = 0'; } else { - $params[] = $graph_item['sequence']; + $params[] = $graph_item['local_graph_id']; $sql_where = 'local_graph_id = ?'; } diff --git a/tests/Unit/Ui/Graph/GraphGroupIdentityTest.php b/tests/Unit/Ui/Graph/GraphGroupIdentityTest.php new file mode 100644 index 0000000000..e9ec648072 --- /dev/null +++ b/tests/Unit/Ui/Graph/GraphGroupIdentityTest.php @@ -0,0 +1,22 @@ +toBe([101, 102, 103, 104]); + expect($groups['template'])->toBe([301, 302]); + expect($groups['non_parent'])->toBe([]); +}); diff --git a/tests/Unit/fixtures/graph_group_probe.php b/tests/Unit/fixtures/graph_group_probe.php new file mode 100644 index 0000000000..a01c53ce5e --- /dev/null +++ b/tests/Unit/fixtures/graph_group_probe.php @@ -0,0 +1,59 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); +$database->exec('CREATE TABLE graph_templates_item (id INTEGER PRIMARY KEY, sequence INTEGER, local_graph_id INTEGER, graph_template_id INTEGER, graph_type_id INTEGER, text_format TEXT, hard_return TEXT)'); +$insert = $database->prepare('INSERT INTO graph_templates_item VALUES (?, ?, ?, ?, ?, ?, ?)'); + +foreach ([ + [101, 7, 42, 5, 4, 'Series', ''], + [102, 8, 42, 5, 1, '|95:bytes:0:current:2|', ''], + [103, 9, 42, 5, 1, '|sum:0:current:2:auto|', ''], + [104, 10, 42, 5, 9, 'Last', 'on'], + [105, 11, 42, 5, 9, 'Next group', ''], + [201, 8, 7, 5, 9, 'Another graph', ''], + [301, 7, 0, 9, 4, 'Template series', ''], + [302, 8, 0, 9, 9, 'Template last', 'on'], + [303, 9, 0, 10, 9, 'Another template', ''], +] as $row) { + $insert->execute($row); +} +function db_fetch_row_prepared($sql, $params = []) { + global $database; + $statement = $database->prepare($sql); + $statement->execute($params); + + return $statement->fetch(PDO::FETCH_ASSOC); +} +function db_fetch_assoc_prepared($sql, $params = []) { + global $database; + $statement = $database->prepare($sql); + $statement->execute($params); + + return $statement->fetchAll(PDO::FETCH_ASSOC); +} +require_once dirname(__DIR__, 3) . '/lib/functions.php'; +$graph_item_types = [1 => 'COMMENT', 4 => 'LINE1', 9 => 'GPRINT']; +$groups = [ + 'instance' => array_values(get_graph_group(101)), + 'template' => array_values(get_graph_group(301)), + 'non_parent' => array_values(get_graph_group(104)), +]; + +if ($groups !== ['instance' => [101, 102, 103, 104], 'template' => [301, 302], 'non_parent' => []]) { + throw new RuntimeException('Graph grouping mixed identities or crossed its group boundary: ' . json_encode($groups)); +} +print json_encode($groups, JSON_THROW_ON_ERROR); diff --git a/tests/tools/run_request_coverage.sh b/tests/tools/run_request_coverage.sh index 956d0fa866..a1e5b9ef49 100644 --- a/tests/tools/run_request_coverage.sh +++ b/tests/tools/run_request_coverage.sh @@ -49,3 +49,5 @@ run_php_probe tests/fixtures/Core/PackageArchive.php '^(lib/package\.php|tests/f run_probe tests/e2e/installer-coverage.sh '^(lib/installer\.php|cli/(install_cacti|upgrade_database)\.php|install/(functions|preflight|report|background|install)\.php|tests/e2e/installer)' 18277 run_php_probe tests/fixtures/Installer/RetiredThemeCleanup.php '^(install/(functions\.php|upgrades/1_3_0\.php)|tests/fixtures/Installer/RetiredThemeCleanup\.php)' + +run_php_probe tests/Unit/fixtures/graph_group_probe.php '^(lib/functions\.php|tests/Unit/fixtures/graph_group_probe\.php)' diff --git a/tests/tools/test_request_coverage_dispatch.py b/tests/tools/test_request_coverage_dispatch.py index d7a6a29843..e7d405f459 100644 --- a/tests/tools/test_request_coverage_dispatch.py +++ b/tests/tools/test_request_coverage_dispatch.py @@ -40,7 +40,7 @@ class RequestDispatchTest(unittest.TestCase): self.dispatch('lib/rrd.php', 'FallbackSourceIdentity.php') def test_account_change_runs_the_native_audit_probe(self): - self.dispatch('lib/functions.php', 'UserAdministrationAuditCoverage.php') + self.dispatch('lib/functions.php', ['UserAdministrationAuditCoverage.php', 'graph_group_probe.php']) def test_auth_change_runs_the_native_auth_probe(self): self.dispatch('lib/auth.php', 'BoundaryContracts.php') @@ -80,6 +80,9 @@ class RequestDispatchTest(unittest.TestCase): path = repository / 'tests/fixtures' / script path.parent.mkdir(parents=True, exist_ok=True) path.write_text('