1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-12 09:34:54 +02:00

Issue #5443 - PHP 8.4 test fixes.

This commit is contained in:
camer0n
2025-03-17 13:44:04 -07:00
parent d58f199f28
commit 1b54602be1
17 changed files with 362 additions and 158 deletions

View File

@@ -144,81 +144,159 @@
$this->assertEquals($expected, $input);
}
public function testPluginScripts()
{
public function testPluginScripts()
{
$core = e107::getPlug()->getCorePluginList();
$exclude = [
'forum/forum_post.php',
'forum/forum_viewtopic.php',
'forum/index.php',
'online/online_menu.php',
'pm/pm.php',
'poll/admin_config.php',
'rss_menu/rss.php',
'tagcloud/tagcloud_menu.php',
'tinymce4/wysiwyg.php',
];
$focus = [];
$core = e107::getPlug()->getCorePluginList();
$errors = [];
$exclude = array(
'forum/forum_post.php',
'forum/forum_viewtopic.php', // needs a major cleanup.
'forum/index.php',
'online/online_menu.php', // FIXME missing template for member/new
'pm/pm.php', // FIXME contains exit, needs rework.
'poll/admin_config.php', // FIXME convert to admin-ui
'rss_menu/rss.php', // FIXME rework code into class.
'tagcloud/tagcloud_menu.php', // FIXME - strange (PHP bug?), doesn't see the render() method.
'tinymce4/wysiwyg.php', // javascript generator.
);
foreach ($core as $plug) {
$path = realpath(e107::getFolder('plugins') . $plug);
if ($path === false) {
fwrite(STDOUT, "Plugin directory not found: {$plug}\n");
continue;
}
//DEBUG A SPECIFIC FILE : dir => file
// $focus = array('tagcloud'=>'tagcloud_menu.php');
$file[$plug] = scandir($path);
unset($file[$plug][0], $file[$plug][1]);
sort($file[$plug]);
if (!empty($focus) && !isset($focus[$plug])) {
unset($file[$plug]);
continue;
}
foreach($core as $plug)
{
$parm = null; //
e107::plugLan($plug, 'global'); // load global language (usually done in class2.php)
e107::getConfig()->setPref('plug_installed/'.$plug, 1); // simulate installed.
e107::plugLan($plug, 'global');
e107::getConfig()->setPref('plug_installed/' . $plug, 1);
}
$path = e_PLUGIN.$plug;
$files = scandir($path);
unset($files[0], $files[1]); // . and ..
foreach ($file as $plug => $files) {
$pluginFiles = [];
foreach ($files as $f) {
$filePath = realpath(e107::getFolder('plugins') . $plug) . DIRECTORY_SEPARATOR . $f;
if (!empty($focus) && $f !== $focus[$plug]) {
continue;
}
if (is_dir($filePath) || strpos($f, '_sql.php') !== false || strpos($f, '.php') === false || in_array($plug . '/' . $f, $exclude)) {
continue;
}
if (!file_exists($filePath)) {
fwrite(STDOUT, "File not found: {$plug}/{$f}\n");
continue;
}
$pluginFiles[$plug . '/' . $f] = $filePath;
}
sort($files);
if (empty($pluginFiles)) {
fwrite(STDOUT, "No testable files found for plugin: {$plug}\n");
continue;
}
if(!empty($focus) && !isset($focus[$plug]))
{
continue;
}
foreach($files as $f)
{
$filePath = $path.'/'.$f;
if(!empty($focus) && $f !== $focus[$plug])
{
continue;
}
if(is_dir($filePath) || (strpos($f, '_sql.php') !== false) || strpos($f, '.php') === false || in_array($plug.'/'.$f, $exclude))
{
continue;
}
// echo " --- ".$filePath." --- \n";
if(empty($focus))
{
ob_start();
}
require_once( $filePath);
if(empty($focus))
{
ob_end_clean();
}
// echo $plug.'/'.$f."\n";
}
}
}
fwrite(STDOUT, "Testing plugin: {$plug}\n");
foreach ($pluginFiles as $relativePath => $_) {
fwrite(STDOUT, " - $relativePath\n");
}
// Build the command
$requireStatements = '';
$firstFilePath = reset($pluginFiles);
$e107Root = realpath(dirname($firstFilePath) . '/../../');
$class2Path = $e107Root . '/class2.php';
if ($class2Path === false || !file_exists($class2Path)) {
fwrite(STDOUT, "Error: Could not locate class2.php at $class2Path\n");
$errors[] = "Error: Could not locate class2.php for plugin {$plug}";
continue;
}
$lanAdminPath = $e107Root . '/e107_languages/English/admin/lan_admin.php';
if (!file_exists($lanAdminPath)) {
fwrite(STDOUT, "Error: Could not locate lan_admin.php at $lanAdminPath\n");
$errors[] = "Error: Could not locate lan_admin.php for plugin {$plug}";
continue;
}
$requireStatements .= "error_reporting(E_ALL); ini_set('display_errors', 1); ";
$requireStatements .= "require_once '" . addslashes($class2Path) . "'; ";
$requireStatements .= "require_once '" . addslashes($lanAdminPath) . "'; ";
$requireStatements .= "e107::plugLan('" . addslashes($plug) . "', 'global'); ";
$requireStatements .= "e107::getConfig()->setPref('plug_installed/" . addslashes($plug) . "', 1); ";
foreach ($pluginFiles as $relativePath => $filePath) {
$requireStatements .= "echo 'START: " . addslashes($relativePath) . "\\n'; ";
$requireStatements .= "require_once '" . addslashes($filePath) . "'; ";
$requireStatements .= "echo 'END: " . addslashes($relativePath) . "\\n'; ";
}
$runCommand = sprintf('php -r %s 1>NUL 2>&1', escapeshellarg($requireStatements));
// Execute and capture errors
exec($runCommand, $runOutput, $runExitCode);
if ($runExitCode !== 0 || !empty($runOutput)) {
$output = implode("\n", $runOutput);
if (!empty($output)) {
if (preg_match('/(Parse error|Fatal error|Warning|Notice):.*in\s+([^\s]+)\s+on\s+line\s+(\d+)/i', $output, $match)) {
$errorMessage = $match[0];
$errorFile = $match[2];
$relativePath = array_search($errorFile, $pluginFiles) ?: $plug . '/unknown';
$error = "Error in {$relativePath}: $errorMessage";
fwrite(STDOUT, "$error\n");
$errors[] = $error;
} else {
$firstLine = strtok($output, "\n");
$error = "Error in {$plug}: $firstLine";
fwrite(STDOUT, "$error\n");
$errors[] = $error;
}
} else {
// Sequentially check files to find the error
$lastGoodFile = null;
foreach ($pluginFiles as $relativePath => $filePath) {
$testCommand = sprintf('php -r %s 1>NUL 2>&1', escapeshellarg(
"error_reporting(E_ALL); ini_set('display_errors', 1); " .
"require_once '" . addslashes($class2Path) . "'; " .
"require_once '" . addslashes($lanAdminPath) . "'; " .
"e107::plugLan('" . addslashes($plug) . "', 'global'); " .
"e107::getConfig()->setPref('plug_installed/" . addslashes($plug) . "', 1); " .
"require_once '" . addslashes($filePath) . "';"
));
exec($testCommand, $testOutput, $testExitCode);
if ($testExitCode !== 0) {
$errorOutput = !empty($testOutput) ? implode("\n", $testOutput) : "Syntax error detected (exit code $testExitCode)";
if (preg_match('/(Parse error|Fatal error|Warning|Notice):.*in\s+([^\s]+)\s+on\s+line\s+(\d+)/i', $errorOutput, $match)) {
$errorMessage = $match[0];
} else {
$errorMessage = $errorOutput;
}
$error = "Error in {$relativePath}: $errorMessage";
fwrite(STDOUT, "$error\n");
$errors[] = $error;
break;
}
$lastGoodFile = $relativePath;
}
if (empty($errors) && $lastGoodFile) {
$error = "Error after {$lastGoodFile}: Syntax error detected (exit code $runExitCode)";
fwrite(STDOUT, "$error\n");
$errors[] = $error;
}
}
}
}
if (!empty($errors)) {
self::fail("Errors found in plugin scripts:\n" . implode("\n", $errors));
}
}