Skip to content
Merged
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
2 changes: 1 addition & 1 deletion tests/.cacti-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.31
1.2.x
52 changes: 52 additions & 0 deletions tests/Integration/QuicktreeInstallHooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Integration coverage for plugin_quicktree_install(): verifies every hook
* and the realm the plugin depends on at runtime are actually registered,
* together with its table/config-option provisioning, in a single
* end-to-end pass.
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

beforeEach(function () {
$GLOBALS['__test_registered_hooks'] = array();
$GLOBALS['__test_registered_realms'] = array();
$GLOBALS['__test_config_options'] = array();
});

it('registers every hook quicktree depends on, its realm, and provisions its table/config option', function () {
expect(plugin_quicktree_install())->toBeTrue();

$hooks = array();
foreach ($GLOBALS['__test_registered_hooks'] as $registered) {
$hooks[$registered['hook']] = $registered;
}

foreach (array(
'top_header_tabs',
'top_graph_header_tabs',
'config_arrays',
'config_settings',
'draw_navigation_text',
'graph_buttons',
'graph_buttons_thumbnails',
'page_head',
) as $expected) {
expect($hooks)->toHaveKey($expected);
expect($hooks[$expected]['name'])->toBe('quicktree');
expect($hooks[$expected]['file'])->toBe('setup.php');
}

expect($GLOBALS['__test_registered_realms'])->toHaveCount(1);
expect($GLOBALS['__test_registered_realms'][0]['file'])->toBe('quicktree.php');

expect(read_config_option('quicktree_pagestyle'))->toBe('0');
});
83 changes: 83 additions & 0 deletions tests/Unit/QuicktreeCheckUpgradeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for quicktree_check_upgrade() in setup.php: the page-guard
* (only certain entry points run the check) and the version-drift branch
* (re-enables the page_head hook, re-runs the table setup, and updates
* plugin_config).
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

beforeEach(function () {
$GLOBALS['__test_db_calls'] = array();
$GLOBALS['__test_registered_hooks'] = array();
$GLOBALS['__test_db_fetch_cell_prepared_return'] = '';
unset($_SERVER['PHP_SELF']);
});

it('does nothing when the current page is not in the allowed list', function () {
$_SERVER['PHP_SELF'] = '/cacti/some_other_page.php';

quicktree_check_upgrade();

expect($GLOBALS['__test_db_calls'])->toBeEmpty();
expect($GLOBALS['__test_registered_hooks'])->toBeEmpty();
});

it('runs the version check when PHP_SELF is not set at all (e.g. CLI)', function () {
quicktree_check_upgrade();

expect($GLOBALS['__test_db_calls'])->not->toBeEmpty();
});

it('does nothing further when the stored version already matches', function () {
$_SERVER['PHP_SELF'] = '/cacti/plugins.php';

$info = plugin_quicktree_version();
quicktree_test_set_db_fetch_cell_prepared_return($info['version']);

quicktree_check_upgrade();

expect($GLOBALS['__test_registered_hooks'])->toBeEmpty();

$updates = array_filter($GLOBALS['__test_db_calls'], function ($call) {
return $call['fn'] === 'db_execute_prepared';
});

expect($updates)->toBeEmpty();
});

it('re-enables the page_head hook and updates plugin_config when the version drifts', function () {
$_SERVER['PHP_SELF'] = '/cacti/quicktree.php';

quicktree_test_set_db_fetch_cell_prepared_return('0.0.0');

quicktree_check_upgrade();

$pageHeadHooks = array_filter($GLOBALS['__test_registered_hooks'], function ($hook) {
return $hook['hook'] === 'page_head' && $hook['enabled'] === 1;
});

expect($pageHeadHooks)->not->toBeEmpty();

$updates = array_values(array_filter($GLOBALS['__test_db_calls'], function ($call) {
return $call['fn'] === 'db_execute_prepared' && stripos($call['sql'], 'UPDATE plugin_config') !== false;
}));

expect($updates)->toHaveCount(1);
expect($updates[0]['params'])->toBe(array(
plugin_quicktree_version()['version'],
plugin_quicktree_version()['longname'],
plugin_quicktree_version()['author'],
plugin_quicktree_version()['homepage'],
plugin_quicktree_version()['name'],
));
});
61 changes: 61 additions & 0 deletions tests/Unit/QuicktreeConfigSettingsAndArraysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for quicktree_config_settings() and quicktree_config_arrays()
* in setup.php.
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

beforeEach(function () {
$GLOBALS['__test_config_options'] = array();
unset($_SERVER['PHP_SELF']);
});

it('adds the misc tab and quicktree settings when no settings exist yet', function () {
global $tabs, $settings;

$tabs = array();
$settings = array();

quicktree_config_settings();

expect($tabs['misc'])->toBe('Misc');
expect($settings['misc'])->toHaveKey('quicktree_pagestyle');
expect($settings['misc']['quicktree_pagestyle']['method'])->toBe('drop_array');
});

it('merges into an existing misc settings array without clobbering it', function () {
global $tabs, $settings;

$tabs = array();
$settings = array('misc' => array('other_setting' => array('friendly_name' => 'Other')));

quicktree_config_settings();

expect($settings['misc'])->toHaveKey('other_setting');
expect($settings['misc'])->toHaveKey('quicktree_pagestyle');
});

it('adds the Management menu entry only when the page style enables the console view', function () {
global $menu;

$menu = array(__('Management') => array());

$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '0';
quicktree_config_arrays();
expect($menu[__('Management')])->not->toHaveKey('plugins/quicktree/quicktree.php?location=console');

$menu = array(__('Management') => array());

$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '1';
quicktree_config_arrays();
expect($menu[__('Management')])->toHaveKey('plugins/quicktree/quicktree.php?location=console');
});
40 changes: 40 additions & 0 deletions tests/Unit/QuicktreeLifecycleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for the plugin lifecycle contract functions in setup.php:
* plugin_quicktree_uninstall(), plugin_quicktree_check_config(), and
* plugin_quicktree_upgrade().
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

beforeEach(function () {
$GLOBALS['__test_db_calls'] = array();
$GLOBALS['__test_db_fetch_cell_prepared_return'] = '';
unset($_SERVER['PHP_SELF']);
});

it('does not error on uninstall', function () {
expect(fn () => plugin_quicktree_uninstall())->not->toThrow(Throwable::class);
});

it('reports the config as always valid', function () {
expect(plugin_quicktree_check_config())->toBeTrue();
});

it('runs the upgrade check and reports false', function () {
$_SERVER['PHP_SELF'] = '/cacti/plugins.php';

$info = plugin_quicktree_version();
quicktree_test_set_db_fetch_cell_prepared_return($info['version']);

// version matches => quicktree_check_upgrade() takes the no-drift path.
expect(plugin_quicktree_upgrade())->toBeFalse();
});
49 changes: 49 additions & 0 deletions tests/Unit/QuicktreePageLocationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for quicktree_page_location() in setup.php.
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

beforeEach(function () {
$GLOBALS['__test_config_options'] = array();
});

it('always returns the preferred location when both tab and console are enabled', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '2';

expect(quicktree_page_location('tab'))->toBe('tab');
expect(quicktree_page_location('console'))->toBe('console');
});

it('returns tab when only the tab is enabled and tab is preferred', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '0';

expect(quicktree_page_location('tab'))->toBe('tab');
});

it('falls back to console when only the console menu is enabled but tab is preferred', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '1';

expect(quicktree_page_location('tab'))->toBe('console');
});

it('returns console when preferred is console and only console is enabled', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '1';

expect(quicktree_page_location('console'))->toBe('console');
});

it('falls back to tab when preferred is console but only the tab is enabled', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '0';

expect(quicktree_page_location('console'))->toBe('tab');
});
40 changes: 40 additions & 0 deletions tests/Unit/QuicktreeSetupTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for quicktree_setup_table() in setup.php.
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

beforeEach(function () {
$GLOBALS['__test_config_options'] = array();
});

it('defaults the page style to Tab when unset', function () {
quicktree_setup_table();

expect(read_config_option('quicktree_pagestyle'))->toBe('0');
});

it('defaults the page style to Tab when the stored value is out of range', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '5';

quicktree_setup_table();

expect(read_config_option('quicktree_pagestyle'))->toBe('0');
});

it('leaves a valid stored page style untouched', function () {
$GLOBALS['__test_config_options']['quicktree_pagestyle'] = '2';

quicktree_setup_table();

expect(read_config_option('quicktree_pagestyle'))->toBe('2');
});
23 changes: 23 additions & 0 deletions tests/Unit/QuicktreeVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
*/

/*
* Unit coverage for plugin_quicktree_version() in setup.php.
*/

beforeAll(function () {
require_once __DIR__ . '/../../setup.php';
});

it('parses the plugin INFO file into an info array', function () {
$info = plugin_quicktree_version();

expect($info)->toBeArray();
expect($info)->toHaveKey('name');
expect($info)->toHaveKey('version');
expect($info['name'])->toBe('quicktree');
});
Loading
Loading