Skip to content

Commit

Permalink
fix: order tests and alerts by test id again
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Oct 28, 2024
1 parent 788fa16 commit 804b5cc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
76 changes: 57 additions & 19 deletions includes/features/class-test-runs-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/list-tables/class-tests-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion includes/models/class-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit 804b5cc

Please sign in to comment.