diff --git a/tests/php/test_eventdetail_modal_submit_action.php b/tests/php/test_eventdetail_modal_submit_action.php new file mode 100644 index 0000000000..8de72e883e --- /dev/null +++ b/tests/php/test_eventdetail_modal_submit_action.php @@ -0,0 +1,154 @@ +. If the Save button also had name="action" +// value="save", a native submit (e.g. Enter in a text field) posts both +// entries, and PHP keeps the LAST one ('save'). web/includes/actions/ +// eventdetail.php only acts when $action == 'eventdetail', so nothing was +// saved. The Save button must therefore have no name/value. +// +// This is a source-level test: the modal is parsed as text, no DB, no web +// server, no PHP rendering. +// +// Usage: php test_eventdetail_modal_submit_action.php [zoneminder-root] +// + +$root = isset($argv[1]) && $argv[1] !== '' ? $argv[1] : __DIR__ . '/../..'; + +$failures = 0; +function check($cond, $message) { + global $failures; + echo ($cond ? 'ok' : 'FAIL') . ' - ' . $message . "\n"; + if (!$cond) $failures++; +} + +function extract_attr($tag, $name) { + // Extract a double- or single-quoted attribute value from a tag string. + if (preg_match('/\b' . $name . '\s*=\s*"([^"]*)"/i', $tag, $m)) + return $m[1]; + if (preg_match("/\b" . $name . "\s*=\s*'([^']*)'/i", $tag, $m)) + return $m[1]; + return null; +} + +$modal_path = $root . '/web/ajax/modals/eventdetail.php'; +if (!is_file($modal_path)) { + echo "FAIL - modal file not found: $modal_path\n"; + exit(1); +} +$modal = file_get_contents($modal_path); +if ($modal === false) { + echo "FAIL - could not read: $modal_path\n"; + exit(1); +} + +// +// 1. Collect every element that would be submitted with name="action": +// hidden inputs and submit buttons carrying name="action". +// + +// Hidden inputs with name="action". The form tag's action="..." attribute +// cannot match because we anchor on ]*>/i', $modal, $input_tags)) { + foreach ($input_tags[0] as $tag) { + if (strcasecmp((string)extract_attr($tag, 'name'), 'action') === 0 + and strcasecmp((string)extract_attr($tag, 'type'), 'hidden') === 0) { + $hidden_values[] = (string)extract_attr($tag, 'value'); + } + } +} +check(count($hidden_values) >= 1, + 'modal has at least one hidden input with name="action" (found ' . count($hidden_values) . ')'); + +// All +