mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-25 04:27:56 +09:00
* test: shared hand-off + mutation infrastructure for feature PRs Consolidates infection/infection dev dep, allow-plugins entry, baseline infection.json5, tests/HandOff/HandOffHelpers.php with reusable stubs (cacti_log capture, temp-file fixtures, minimal-zip builder), HandOff testsuite registration, and the documented pattern. Feature PRs that add hand-off coverage rebase onto this and contribute only ONE HandOffTest file plus an optional infection.json5 filter override. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: consolidate per-feature hand-off suites into shared infra Each test guards on its feature file and skips when not present on develop. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(test): replace eval stub, unify log buffer ref, guard ZipArchive add Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(ci): correct infection vendor path in infection.json5 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * ci(phpstan): baseline upstream undefined-variable and isset errors Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: add HandOff and Integration testsuites; add regression guards - phpunit.xml: add Integration testsuite alongside existing Unit/HandOff - tests/Integration/: DbDumpIntegrationTest, PingIntegrationTest (from develop), CactiProcessIntegrationTest, SqlScriptsIntegrationTest - tests/HandOff/RegressionGuardTest: guards against backsliding on shell_exec, password-in-argv, RLIKE concat, and open-redirect patterns Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * chore(test): pest test infrastructure and PSR-4 autoload Add Pest 2 dev dependency, regenerate composer.lock under PHP 8.1, declare PSR-4 autoload for the Cacti namespace, and seed the test bootstrap with three unit tests plus an orb integration check. Pest stays on the v2 line because v3 transitively requires PHP 8.2 and breaks the 8.1 matrix entry. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * chore(phpstan): catch up baseline with 11 upstream-detected entries Cacti Commit Audit fails on develop with 11 PHPStan errors that are not yet in phpstan-baseline.neon. Append the matching entries so the PR-A branch passes Run PHPStan at Level 6. Same set of entries already exists on PR #7077; this is a transient catch-up that upstream will absorb when those entries land on develop. Files: aggregate_graphs.php, color_templates.php, graph_templates.php, graphs.php, lib/html.php (3 entries). Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * chore(test): drop composer.lock; let CI resolve per PHP version The lock file pinned brianium/paratest v7.3.1 which only supports PHP 8.1-8.3, so the 8.4 matrix entry rejected the lock with "Your lock file does not contain a compatible set of packages". A single lock cannot satisfy 8.1 (paratest 7.3.x) and 8.4 (paratest 7.4.x) at the same time because pestphp/pest 2.36.0 is the last 2.x release that supports 8.1 and the 8.1-compatible paratest tag predates 8.4 support. Match upstream develop's behaviour: no committed lock; let `composer install` in CI resolve per matrix entry. Pest stays on the v2 line in composer.json so 8.1 still gets a compatible Pest. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(test): apply Copilot review feedback for test infra Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * chore(test): drop psr-4 autoload and align deps with canonical PRs Match the flat un-namespaced lib/CactiX.php convention used by #7088, #7073, #7077, #7075. Drop symfony/process (added by #7073) and symfony/validator (added by #7077) to avoid composer.json conflicts at merge time. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: skip integration suites and regression guards pending feature PRs Guards integration tests and regression guards from #7083 against absence of CactiProcess and other feature-PR hardening, so the consolidated foundation PR is green on develop. Suites activate automatically once their feature PRs merge. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(ci): drop infection/infection to unbreak PHP 8.4 matrix infection/infection ^0.27 transitively pins thecodingmachine/safe ^2.1.2, resolving to v2.5.0. Its generated stubs use implicit-nullable parameter declarations, which PHP 8.4 emits as deprecations into cacti.log and trips the log-quiet assertion. infection has no config or test wiring in this repo, so removing the dev dep clears the transitive pin without losing functionality. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> Co-authored-by: TheWitness <thewitness@cacti.net>
141 lines
4.7 KiB
PHP
141 lines
4.7 KiB
PHP
<?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. |
|
|
+-------------------------------------------------------------------------+
|
|
| Cacti: The Complete RRDtool-based Graphing Solution |
|
|
+-------------------------------------------------------------------------+
|
|
*/
|
|
|
|
/*
|
|
* Shared helpers for tests/HandOff/<Feature>HandOffTest.php files.
|
|
*
|
|
* Hand-off tests assert behaviour at boundaries the unit tests cannot reach:
|
|
* a real filesystem path, a real ZIP entry, a real cacti_log() call site.
|
|
* The helpers here build those fixtures and capture side effects so the
|
|
* per-feature test files stay focused on the assertion they care about.
|
|
*
|
|
* Re-including this file is safe; every declaration is guarded so the
|
|
* second include in a test run is a no-op.
|
|
*/
|
|
|
|
if (!function_exists('cacti_handoff_stub_cacti_log')) {
|
|
|
|
/*
|
|
* Register a cacti_log() shim that records every call into a static
|
|
* buffer. Returns the buffer by reference so the caller can both clear
|
|
* it and assert on it without re-reaching into the function.
|
|
*
|
|
* Tests that load real Cacti code which already declares cacti_log()
|
|
* should call cacti_handoff_clear_log_buffer() instead and read the
|
|
* buffer directly via cacti_handoff_get_log_buffer().
|
|
*/
|
|
function &cacti_handoff_stub_cacti_log(): array {
|
|
if (!function_exists('cacti_log')) {
|
|
function cacti_log(string $string, bool $output = false, string $environ = 'CMDPHP', int $level = 0): bool {
|
|
cacti_handoff_record_log_line($string, $environ, $level);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return cacti_handoff_log_buffer_ref();
|
|
}
|
|
|
|
/*
|
|
* Internal sink used by the eval'd cacti_log shim. Kept separate so
|
|
* the stored shape stays a single hashmap per call regardless of how
|
|
* cacti_log was invoked.
|
|
*/
|
|
function cacti_handoff_record_log_line(string $string, string $environ = 'CMDPHP', int $level = 0): void {
|
|
$buffer = &cacti_handoff_log_buffer_ref();
|
|
$buffer[] = array(
|
|
'string' => $string,
|
|
'environ' => $environ,
|
|
'level' => $level,
|
|
);
|
|
}
|
|
|
|
/*
|
|
* Single static buffer shared by the stub, the getter, and the
|
|
* clearer. A by-reference accessor avoids duplicating the static.
|
|
*/
|
|
function &cacti_handoff_log_buffer_ref(): array {
|
|
static $buffer = array();
|
|
return $buffer;
|
|
}
|
|
|
|
function cacti_handoff_get_log_buffer(): array {
|
|
return cacti_handoff_log_buffer_ref();
|
|
}
|
|
|
|
function cacti_handoff_clear_log_buffer(): void {
|
|
$buffer = &cacti_handoff_log_buffer_ref();
|
|
$buffer = array();
|
|
}
|
|
|
|
/*
|
|
* Write $contents to a unique temp path with the given extension,
|
|
* register a shutdown cleanup, and return the path. The shutdown
|
|
* handler tolerates files already removed by the test itself.
|
|
*/
|
|
function cacti_handoff_temp_file(string $contents, string $extension = '.tmp'): string {
|
|
$base = tempnam(sys_get_temp_dir(), 'cacti_handoff_');
|
|
if ($base === false) {
|
|
throw new RuntimeException('cacti_handoff_temp_file: tempnam failed');
|
|
}
|
|
|
|
$path = $base . $extension;
|
|
if (rename($base, $path) === false) {
|
|
@unlink($base);
|
|
throw new RuntimeException('cacti_handoff_temp_file: rename failed');
|
|
}
|
|
|
|
if (file_put_contents($path, $contents) === false) {
|
|
@unlink($path);
|
|
throw new RuntimeException('cacti_handoff_temp_file: write failed');
|
|
}
|
|
|
|
register_shutdown_function(static function () use ($path) {
|
|
if (is_file($path)) {
|
|
@unlink($path);
|
|
}
|
|
});
|
|
|
|
return $path;
|
|
}
|
|
|
|
/*
|
|
* Build a minimal valid ZIP archive containing the supplied
|
|
* path => content entries and return the temp path. Uses ext-zip
|
|
* which Cacti already requires; no shell-out, no third-party lib.
|
|
*/
|
|
function cacti_handoff_make_zip(array $entries): string {
|
|
if (!class_exists('ZipArchive')) {
|
|
throw new RuntimeException('cacti_handoff_make_zip: ext-zip not loaded');
|
|
}
|
|
|
|
$path = cacti_handoff_temp_file('', '.zip');
|
|
|
|
$zip = new ZipArchive();
|
|
if ($zip->open($path, ZipArchive::OVERWRITE) !== true) {
|
|
throw new RuntimeException('cacti_handoff_make_zip: open failed');
|
|
}
|
|
|
|
foreach ($entries as $name => $contents) {
|
|
if ($zip->addFromString((string) $name, (string) $contents) === false) {
|
|
$zip->close();
|
|
throw new RuntimeException('cacti_handoff_make_zip: failed to add ZIP entry "' . (string) $name . '"');
|
|
}
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
return $path;
|
|
}
|
|
}
|