From 804b5ccb8e1cc590aa2fc40eb9011df23bcf3fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Mon, 28 Oct 2024 09:13:47 +0100 Subject: [PATCH] fix: order tests and alerts by test id again --- includes/features/class-test-runs-page.php | 76 ++++++++++++++----- .../list-tables/class-tests-list-table.php | 2 +- includes/models/class-test.php | 2 +- 3 files changed, 59 insertions(+), 21 deletions(-) diff --git a/includes/features/class-test-runs-page.php b/includes/features/class-test-runs-page.php index 49355cc..f264486 100644 --- a/includes/features/class-test-runs-page.php +++ b/includes/features/class-test-runs-page.php @@ -81,29 +81,14 @@ public function render_page() { $run = Test_Run::get_item( $run_id ); if ( $run ) { - $alerts = Alert::get_items_by_test_run( $run_id ); - list($alert_id, $alert) = $this->get_alert( $alerts ); - $service = new Test_Run_Service(); $service->update_latest_alert_for_all_tests( $run ); + $tests = $this->prepare_tests( maybe_unserialize( $run->tests ) ); + $alerts = $this->prepare_alerts( $run_id, $tests ); + + list($alert_id, $alert) = $this->get_alert( $alerts ); $test = $alert ? Test::get_item_by_post_id( $alert->post_id ) : null; - $tests = maybe_unserialize( $run->tests ); - if ( is_array( $tests ) && count( $tests ) > 0 && ! is_array( $tests[0] ) ) { - $tests = array_map( function( $test ) { - $test = (int) $test; - $post_id = Test::get_post_id( $test ); - return [ - 'id' => $test, - 'post_id' => $post_id, - 'post_title' => get_the_title( $post_id ), - 'permalink' => get_permalink( $post_id ), - ]; - }, $tests ); - } - usort( $tests, function( $a, $b ) { - return $a['post_id'] > $b['post_id'] ? -1 : 1; - } ); $is_receipt = 'receipt' === $alert_id; @@ -148,6 +133,59 @@ public function render_page() { }//end if } + /** + * Prepare alerts. + * + * @param array $tests Tests. + */ + private function prepare_tests( $tests ) { + if ( is_array( $tests ) && count( $tests ) > 0 && ! is_array( $tests[0] ) ) { + $tests = array_map( function( $test ) { + $test = (int) $test; + $post_id = Test::get_post_id( $test ); + return [ + 'id' => $test, + 'post_id' => $post_id, + 'post_title' => get_the_title( $post_id ), + 'permalink' => get_permalink( $post_id ), + ]; + }, $tests ); + } + usort( $tests, function( $a, $b ) { + return $a['id'] > $b['id'] ? -1 : 1; + } ); + return $tests; + } + + /** + * Prepare alerts. + * + * @param int $run_id Run ID. + * @param array $tests Tests. + */ + private function prepare_alerts( $run_id, $tests ) { + $alerts = Alert::get_items_by_test_run( $run_id ); + $alerts_by_post_id = []; + foreach ( $alerts as $alert ) { + $alerts_by_post_id[ $alert->post_id ][] = $alert; + } + $sorted_alerts = []; + foreach ( $tests as $test ) { + if ( isset( $alerts_by_post_id[ $test['post_id'] ] ) ) { + $sorted_alerts = array_merge( $sorted_alerts, $alerts_by_post_id[ $test['post_id'] ] ); + unset( $alerts_by_post_id[ $test['post_id'] ] ); + } + } + $remaining_alerts = array_values( $alerts_by_post_id ); + usort( $remaining_alerts, function( $a, $b ) { + return $a[0]->post_id > $b[0]->post_id ? -1 : 1; + } ); + foreach ( $remaining_alerts as $remaining_alert ) { + $sorted_alerts = array_merge( $sorted_alerts, $remaining_alert ); + } + return $sorted_alerts; + } + /** * Get alert. * diff --git a/includes/list-tables/class-tests-list-table.php b/includes/list-tables/class-tests-list-table.php index f44354b..4fef971 100644 --- a/includes/list-tables/class-tests-list-table.php +++ b/includes/list-tables/class-tests-list-table.php @@ -288,7 +288,7 @@ public function prepare_items() { $order = isset( $_REQUEST['order'] ) && 'asc' === $_REQUEST['order'] ? 'ASC' : 'DESC'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's the list order by parameter. - $order_by = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'post_id'; + $order_by = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'id'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- It's the list search query parameter. $search_query = isset( $_POST['s'] ) && '' !== $_POST['s'] ? sanitize_text_field( wp_unslash( $_POST['s'] ) ) : null; diff --git a/includes/models/class-test.php b/includes/models/class-test.php index 5a8bffd..3f3f76d 100644 --- a/includes/models/class-test.php +++ b/includes/models/class-test.php @@ -71,7 +71,7 @@ public static function get_items( $args = [], $return_count = false ) { $whitelist_orderby = [ 'id', 'post_title', 'status', 'base_screenshot_date' ]; $whitelist_order = [ 'ASC', 'DESC' ]; - $orderby = in_array( $args['orderby'], $whitelist_orderby, true ) ? $args['orderby'] : 'post_id'; + $orderby = in_array( $args['orderby'], $whitelist_orderby, true ) ? $args['orderby'] : 'id'; $order = in_array( $args['order'], $whitelist_order, true ) ? $args['order'] : 'DESC'; if ( 'status' === $orderby ) {