diff --git a/tests/.cacti-version b/tests/.cacti-version index 0848465..c971a7a 100644 --- a/tests/.cacti-version +++ b/tests/.cacti-version @@ -1 +1 @@ -1.2.31 +1.2.x diff --git a/tests/Integration/QuicktreeInstallHooksTest.php b/tests/Integration/QuicktreeInstallHooksTest.php new file mode 100644 index 0000000..16bf6d6 --- /dev/null +++ b/tests/Integration/QuicktreeInstallHooksTest.php @@ -0,0 +1,52 @@ +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'); +}); diff --git a/tests/Unit/QuicktreeCheckUpgradeTest.php b/tests/Unit/QuicktreeCheckUpgradeTest.php new file mode 100644 index 0000000..48dc143 --- /dev/null +++ b/tests/Unit/QuicktreeCheckUpgradeTest.php @@ -0,0 +1,83 @@ +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'], + )); +}); diff --git a/tests/Unit/QuicktreeConfigSettingsAndArraysTest.php b/tests/Unit/QuicktreeConfigSettingsAndArraysTest.php new file mode 100644 index 0000000..b74bac1 --- /dev/null +++ b/tests/Unit/QuicktreeConfigSettingsAndArraysTest.php @@ -0,0 +1,61 @@ +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'); +}); diff --git a/tests/Unit/QuicktreeLifecycleTest.php b/tests/Unit/QuicktreeLifecycleTest.php new file mode 100644 index 0000000..d3a3751 --- /dev/null +++ b/tests/Unit/QuicktreeLifecycleTest.php @@ -0,0 +1,40 @@ + 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(); +}); diff --git a/tests/Unit/QuicktreePageLocationTest.php b/tests/Unit/QuicktreePageLocationTest.php new file mode 100644 index 0000000..82582d1 --- /dev/null +++ b/tests/Unit/QuicktreePageLocationTest.php @@ -0,0 +1,49 @@ +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'); +}); diff --git a/tests/Unit/QuicktreeSetupTableTest.php b/tests/Unit/QuicktreeSetupTableTest.php new file mode 100644 index 0000000..e2a7f4e --- /dev/null +++ b/tests/Unit/QuicktreeSetupTableTest.php @@ -0,0 +1,40 @@ +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'); +}); diff --git a/tests/Unit/QuicktreeVersionTest.php b/tests/Unit/QuicktreeVersionTest.php new file mode 100644 index 0000000..b513d2f --- /dev/null +++ b/tests/Unit/QuicktreeVersionTest.php @@ -0,0 +1,23 @@ +toBeArray(); + expect($info)->toHaveKey('name'); + expect($info)->toHaveKey('version'); + expect($info['name'])->toBe('quicktree'); +}); diff --git a/tests/bootstrap-unit.php b/tests/bootstrap-unit.php index 4879462..8a618f3 100644 --- a/tests/bootstrap-unit.php +++ b/tests/bootstrap-unit.php @@ -125,9 +125,18 @@ function db_fetch_cell($sql) { } } +$GLOBALS['__test_db_fetch_cell_prepared_return'] = ''; + if (!function_exists('db_fetch_cell_prepared')) { function db_fetch_cell_prepared($sql, $params = array()) { - return ''; + $GLOBALS['__test_db_calls'][] = array('fn' => 'db_fetch_cell_prepared', 'sql' => $sql, 'params' => $params); + return $GLOBALS['__test_db_fetch_cell_prepared_return']; + } +} + +if (!function_exists('quicktree_test_set_db_fetch_cell_prepared_return')) { + function quicktree_test_set_db_fetch_cell_prepared_return($value) { + $GLOBALS['__test_db_fetch_cell_prepared_return'] = $value; } } @@ -155,14 +164,17 @@ function api_plugin_db_table_create($plugin, $table, $data) { } } +$GLOBALS['__test_config_options'] = array(); + if (!function_exists('read_config_option')) { function read_config_option($name, $force = false) { - return ''; + return isset($GLOBALS['__test_config_options'][$name]) ? $GLOBALS['__test_config_options'][$name] : ''; } } if (!function_exists('set_config_option')) { function set_config_option($name, $value) { + $GLOBALS['__test_config_options'][$name] = $value; } } @@ -242,6 +254,34 @@ function sql_save($array, $table, $key = 'id') { } } +$GLOBALS['__test_registered_hooks'] = array(); +$GLOBALS['__test_registered_realms'] = array(); + +if (!function_exists('api_plugin_register_hook')) { + function api_plugin_register_hook($name, $hook, $function, $file, $enabled = 1) { + $GLOBALS['__test_registered_hooks'][] = array( + 'name' => $name, + 'hook' => $hook, + 'function' => $function, + 'file' => $file, + 'enabled' => $enabled, + ); + return true; + } +} + +if (!function_exists('api_plugin_register_realm')) { + function api_plugin_register_realm($name, $file, $description, $enabled = 1) { + $GLOBALS['__test_registered_realms'][] = array( + 'name' => $name, + 'file' => $file, + 'description' => $description, + 'enabled' => $enabled, + ); + return true; + } +} + if (!defined('CACTI_PATH_BASE')) { define('CACTI_PATH_BASE', $GLOBALS['config']['base_path']); }