-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: DRY cycle request context handling #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
somethingwithproof
wants to merge
2
commits into
Cacti:develop
from
somethingwithproof:fix/dry-context-23
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
| /* | ||
| +-------------------------------------------------------------------------+ | ||
| | Copyright (C) 2004-2026 The Cacti Group | | ||
| | | | ||
| | This program is free software; you can redistribute it and/or | | ||
| | modify it under the terms of the GNU General Public License | | ||
| | as published by the Free Software Foundation; either version 2 | | ||
| | of the License, or (at your option) any later version. | | ||
| +-------------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| if (!function_exists('cycle_get_default_tree_id')) { | ||
| function cycle_get_default_tree_id($tree_id) { | ||
| if (empty($tree_id)) { | ||
| $tree_id = db_fetch_cell('SELECT id | ||
| FROM graph_tree | ||
| ORDER BY name | ||
| LIMIT 1'); | ||
| } | ||
|
|
||
| return $tree_id; | ||
| } | ||
| } | ||
|
|
||
| if (!function_exists('cycle_get_request_context')) { | ||
| function cycle_get_request_context() { | ||
| $context = array( | ||
| 'legend' => get_request_var('legend'), | ||
| 'tree_id' => get_request_var('tree_id'), | ||
| 'leaf_id' => get_request_var('leaf_id'), | ||
| 'graphs' => get_request_var('graphs'), | ||
| 'cols' => get_request_var('cols'), | ||
| 'rfilter' => get_request_var('rfilter'), | ||
| 'id' => get_request_var('id'), | ||
| 'width' => get_request_var('width'), | ||
| 'height' => get_request_var('height') | ||
| ); | ||
|
|
||
| $context['tree_id'] = cycle_get_default_tree_id($context['tree_id']); | ||
| $context['id'] = (empty($context['id']) ? -1 : $context['id']); | ||
|
|
||
| return $context; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
| /* | ||
| +-------------------------------------------------------------------------+ | ||
| | Copyright (C) 2004-2026 The Cacti Group | | ||
| | | | ||
| | Regression checks for shared cycle request-context helpers | | ||
| | | | ||
| | Run: php tests/test_request_context.php | | ||
| +-------------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| $pass = 0; | ||
| $fail = 0; | ||
| $request_values = array(); | ||
| $db_queries = array(); | ||
|
|
||
| function assert_true($label, $value) { | ||
| global $pass, $fail; | ||
|
|
||
| if ($value) { | ||
| echo "PASS $label\n"; | ||
| $pass++; | ||
| } else { | ||
| echo "FAIL $label\n"; | ||
| $fail++; | ||
| } | ||
| } | ||
|
|
||
| function get_request_var($key) { | ||
| global $request_values; | ||
|
|
||
| return (array_key_exists($key, $request_values) ? $request_values[$key] : ''); | ||
| } | ||
|
|
||
| function db_fetch_cell($sql) { | ||
| global $db_queries; | ||
| $db_queries[] = $sql; | ||
|
|
||
| return 777; | ||
| } | ||
|
|
||
| require_once __DIR__ . '/../cycle_helpers.php'; | ||
|
|
||
| $request_values = array( | ||
| 'legend' => 'true', | ||
| 'tree_id' => '', | ||
| 'leaf_id' => 19, | ||
| 'graphs' => 8, | ||
| 'cols' => 3, | ||
| 'rfilter' => 'prod', | ||
| 'id' => '', | ||
| 'width' => 500, | ||
| 'height' => 200 | ||
| ); | ||
| $db_queries = array(); | ||
|
|
||
| $context = cycle_get_request_context(); | ||
|
|
||
| assert_true('empty tree_id falls back to first tree query', $context['tree_id'] === 777); | ||
| assert_true('empty id normalizes to -1', $context['id'] === -1); | ||
| assert_true('legend is preserved', $context['legend'] === 'true'); | ||
| assert_true('fallback query executes once', count($db_queries) === 1); | ||
|
|
||
| $request_values['tree_id'] = 123; | ||
| $request_values['id'] = 456; | ||
| $db_queries = array(); | ||
|
|
||
| $context = cycle_get_request_context(); | ||
|
|
||
| assert_true('provided tree_id is preserved', $context['tree_id'] === 123); | ||
| assert_true('provided id is preserved', $context['id'] === 456); | ||
| assert_true('no fallback query when tree_id exists', count($db_queries) === 0); | ||
|
|
||
| $cycle_source = file_get_contents(__DIR__ . '/../cycle.php'); | ||
| assert_true('cycle.php is readable', $cycle_source !== false); | ||
| $cycle_source = ($cycle_source === false ? '' : $cycle_source); | ||
|
|
||
| assert_true( | ||
| 'cycle.php uses shared request context helper', | ||
| preg_match('/cycle_get_request_context\\s*\\(/', $cycle_source) === 1 | ||
| ); | ||
|
|
||
| echo "\n"; | ||
| echo "Results: $pass passed, $fail failed\n"; | ||
|
|
||
| exit($fail > 0 ? 1 : 0); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in
31dfe04: replaced the count-based source assertion with a readability check plus a non-counted helper-usage assertion to avoid implementation-detail coupling.