Skip to content

Commit

Permalink
fix(TestRun): ensure correct pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Oct 28, 2024
1 parent 804b5cc commit 80f1967
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions includes/features/class-test-runs-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,7 @@ public function render_page() {

$is_receipt = 'receipt' === $alert_id;

if ( $is_receipt ) {
$current_pagination = count( $alerts );
$prev_alert_id = isset( $alerts[ count( $alerts ) - 1 ] ) ? $alerts[ count( $alerts ) - 1 ]->id : 0;
$next_alert_id = 0;
} else {
$current_pagination = Alert::get_pagination_current_position( $alert_id, $run_id );
$prev_alert_id = Alert::get_pagination_prev_alert_id( $alert_id, $run_id );
$next_alert_id = Alert::get_pagination_next_alert_id( $alert_id, $run_id );

if ( ! $next_alert_id ) {
$next_alert_id = 'receipt';
}
}
list( $current_pagination, $prev_alert_id, $next_alert_id ) = $this->get_pagination( $alerts, $alert_id );

vrts()->component('test-run-page', [
'run' => $run,
Expand Down Expand Up @@ -186,6 +174,32 @@ private function prepare_alerts( $run_id, $tests ) {
return $sorted_alerts;
}

/**
* Get pagination.
*
* @param array $alerts Alerts.
* @param int $alert_id Alert ID.
*/
private function get_pagination( $alerts, $alert_id ) {
if ( 'receipt' === $alert_id ) {
$current_pagination = count( $alerts );
$prev_alert_id = $alerts[ count( $alerts ) - 1 ]->id ?? 0;
$next_alert_id = 0;
} else {
$current_index = ( array_keys( array_filter( $alerts, function ( $alert ) use ( $alert_id ) {
return $alert->id === $alert_id;
} ) )[0] ?? 0 );
$prev_alert_id = $alerts[ $current_index - 1 ]->id ?? 0;
$next_alert_id = $alerts[ $current_index + 1 ]->id ?? 0;
$current_pagination = $current_index + 1;

if ( ! $next_alert_id ) {
$next_alert_id = 'receipt';
}
}
return [ $current_pagination, $prev_alert_id, $next_alert_id ];
}

/**
* Get alert.
*
Expand Down

0 comments on commit 80f1967

Please sign in to comment.