From 80f1967b37bfc50cde29531a0b625d64fe79b0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Mon, 28 Oct 2024 17:02:37 +0100 Subject: [PATCH] fix(TestRun): ensure correct pagination --- includes/features/class-test-runs-page.php | 40 +++++++++++++++------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/includes/features/class-test-runs-page.php b/includes/features/class-test-runs-page.php index f264486..17a74ed 100644 --- a/includes/features/class-test-runs-page.php +++ b/includes/features/class-test-runs-page.php @@ -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, @@ -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. *