diff --git a/tests/php/test_events_search_order_ties.php b/tests/php/test_events_search_order_ties.php new file mode 100644 index 0000000000..d004831df3 --- /dev/null +++ b/tests/php/test_events_search_order_ties.php @@ -0,0 +1,32 @@ += 3, true); +foreach ($m[0] as $i => $stmt) { + $has_order = (strpos($stmt, 'ORDER BY') !== false); + if (!$has_order) continue; + check("query ".($i + 1)." orders ties by E.Id", (bool)preg_match("/'E\\.Id'\s*;\s*$/", $stmt) || (bool)preg_match('/\$order_by/', $stmt), true); + check("query ".($i + 1)." has no ORDER BY without the tiebreak", (bool)preg_match("/\?\s*' ORDER BY '\s*\.\s*\\\$sort\s*:\s*''/", $stmt), false); +} + +echo $failures ? "\n$failures failed\n" : "\nall passed\n"; +exit($failures ? 1 : 0); diff --git a/web/ajax/events.php b/web/ajax/events.php index 5285522916..75cbfbaeb7 100644 --- a/web/ajax/events.php +++ b/web/ajax/events.php @@ -346,7 +346,7 @@ function queryRequest($filter, $search, $advsearch, $sort, $offset, $order, $lim LEFT JOIN Tags AS T ON T.Id = ET.TagId '.$where.' GROUP BY E.Id, Monitor - '.($sort?' ORDER BY '.$sort:''); + '.' ORDER BY '.($sort ? $sort.', ' : '').'E.Id'; if ((int)($filter->limit()) and !$has_post_sql_conditions) { $sql .= ' LIMIT '.(int)($filter->limit()); @@ -418,7 +418,7 @@ function queryRequest($filter, $search, $advsearch, $sort, $offset, $order, $lim LEFT JOIN Tags AS T ON T.Id = ET.TagId WHERE M.Deleted = false AND ('.$search_filter->sql().') GROUP BY E.Id' - .($sort ? ' ORDER BY '.$sort : ''); + .' ORDER BY '.($sort ? $sort.', ' : '').'E.Id'; $filtered_rows = dbFetchAll($sql); ZM\Debug('Have ' . count($filtered_rows) . ' events matching search filter: '.$sql);