Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tests/test_layout_wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| Regression checks for shared layout wrapper routing in upses.php |
| |
| Run: php tests/test_layout_wrapper.php |
+-------------------------------------------------------------------------+
*/

$pass = 0;
$fail = 0;
$events = array();

function assert_true($label, $value) {
global $pass, $fail;

if ($value) {
echo "PASS $label\n";
$pass++;
} else {
echo "FAIL $label\n";
$fail++;
}
}

function top_header() {
global $events;
$events[] = 'top_header';
}

function bottom_footer() {
global $events;
$events[] = 'bottom_footer';
}

require_once __DIR__ . '/../ui_helpers.php';

$events = array();
$layout_invocations = 0;

apcupsd_render_with_layout(function () use (&$events, &$layout_invocations) {
$layout_invocations++;
$events[] = 'content';
});

assert_true('layout callback executes exactly once', $layout_invocations === 1);
assert_true('layout wrapper call order is top->content->bottom', $events === array('top_header', 'content', 'bottom_footer'));

$upses_source = file_get_contents(__DIR__ . '/../upses.php');

assert_true(
"edit action uses shared layout helper",
strpos($upses_source, "apcupsd_render_with_layout('ups_edit');") !== false
);
assert_true(
"default action uses shared layout helper",
strpos($upses_source, "apcupsd_render_with_layout('upses');") !== false
);

echo "\n";
echo "Results: $pass passed, $fail failed\n";

exit($fail > 0 ? 1 : 0);
19 changes: 19 additions & 0 deletions ui_helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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('apcupsd_render_with_layout')) {
function apcupsd_render_with_layout($renderer) {
top_header();
call_user_func($renderer);
bottom_footer();
}
}
13 changes: 3 additions & 10 deletions upses.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_once($config['base_path'] . '/lib/api_data_source.php');
require_once($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/utility.php');
require_once(__DIR__ . '/ui_helpers.php');

$ups_actions = array(
1 => __('Delete', 'apcupsd'),
Expand Down Expand Up @@ -205,18 +206,10 @@

break;
case 'edit':
top_header();

ups_edit();

bottom_footer();
apcupsd_render_with_layout('ups_edit');
break;
default:
top_header();

upses();

bottom_footer();
apcupsd_render_with_layout('upses');
break;
}

Expand Down
Loading