From fa5c1ba199d77d4232f4bf2578dc278b71d201ee Mon Sep 17 00:00:00 2001 From: Jash Ambaliya <87864439+AJ0070@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:06:15 +0530 Subject: [PATCH] fix: report delete in the reports ajax reports a failed delete instead of success refs #7907 (#7908) --- tests/php/test_ajax_reports_delete_result.php | 62 +++++++++++++++++++ web/ajax/reports.php | 4 +- 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/php/test_ajax_reports_delete_result.php diff --git a/tests/php/test_ajax_reports_delete_result.php b/tests/php/test_ajax_reports_delete_result.php new file mode 100644 index 0000000000..f422cad04c --- /dev/null +++ b/tests/php/test_ajax_reports_delete_result.php @@ -0,0 +1,62 @@ +delete() and ignore its result, so a +// failed DELETE was reported as success. deleteRequest() now reports the failure like +// snapshots.php does. The function is extracted from the ajax script (which cannot be +// included because of its top-level request handling) and run against a stub Report. +// +// Run as: php tests/php/test_ajax_reports_delete_result.php [root] (root defaults to the repo) + +namespace ZM { + class Report { + private $id; + public function __construct($id) { + $this->id = ((int)$id === 0) ? 0 : $id; + } + public function Id() { return $this->id; } + public function canEdit() { return $GLOBALS['reportCanEdit']; } + public function delete() { return $GLOBALS['reportDeleteResult']; } + } +} + +namespace { + $failures = 0; + + function check($name, $got, $want) { + global $failures; + if ($got === $want) { + echo "ok - $name\n"; + } else { + $failures++; + echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; + } + } + + $root = isset($argv[1]) ? $argv[1] : __DIR__.'/../..'; + $path = $root.'/web/ajax/reports.php'; + $src = file_get_contents($path); + if (!preg_match('/^function deleteRequest\(.*?\n\}/ms', $src, $m)) { + echo "Could not find deleteRequest() in $path\n"; + exit(1); + } + eval($m[0]); + + $GLOBALS['reportCanEdit'] = true; + $GLOBALS['reportDeleteResult'] = true; + check('a delete that works reports nothing', deleteRequest(4), array()); + + $GLOBALS['reportDeleteResult'] = false; + $messages = deleteRequest(4); + check('a failed delete is reported', count($messages) > 0, true); + check('a failed delete mentions the report id', strpos(implode(' ', $messages), '4') !== false, true); + + $GLOBALS['reportDeleteResult'] = true; + check('a missing report is reported', deleteRequest(0), array('Report not found.')); + + $GLOBALS['reportCanEdit'] = false; + $messages = deleteRequest(7); + check('a denied delete reports one message', count($messages), 1); + check('a denied delete mentions permission', strpos($messages[0], 'You do not have permission') === 0, true); + + echo $failures ? "\n$failures failed\n" : "\nall passed\n"; + exit($failures ? 1 : 0); +} diff --git a/web/ajax/reports.php b/web/ajax/reports.php index 0960e36cda..88b33a4348 100644 --- a/web/ajax/reports.php +++ b/web/ajax/reports.php @@ -110,8 +110,8 @@ function deleteRequest($id) { $message[] = 'Report not found.'; } else if (!$report->canEdit()) { $message[] = 'You do not have permission to delete report '.$report->Id(); - } else { - $report->delete(); + } else if ( !$report->delete() ) { + $message[] = 'Report '.$id.' could not be deleted.'; } return $message;