diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php index 2125b75fc50..8f2b7e7d82a 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-sync.php @@ -515,11 +515,15 @@ class WC_Admin_Reports_Sync { if ( $range_size > $batch_size ) { // If the current batch range is larger than a single batch, // split the range into $queue_batch_size chunks. - $chunk_size = ceil( $range_size / $batch_size ); + $chunk_size = (int) ceil( $range_size / $batch_size ); for ( $i = 0; $i < $batch_size; $i++ ) { - $batch_start = $range_start + ( $i * $chunk_size ); - $batch_end = min( $range_end, $range_start + ( $chunk_size * ( $i + 1 ) ) - 1 ); + $batch_start = (int) ( $range_start + ( $i * $chunk_size ) ); + $batch_end = (int) min( $range_end, $range_start + ( $chunk_size * ( $i + 1 ) ) - 1 ); + + if ( $batch_start > $range_end ) { + return; + } self::queue()->schedule_single( $action_timestamp, diff --git a/plugins/woocommerce-admin/tests/batch-queue.php b/plugins/woocommerce-admin/tests/batch-queue.php index 283be7ac8b3..266cfa975f6 100644 --- a/plugins/woocommerce-admin/tests/batch-queue.php +++ b/plugins/woocommerce-admin/tests/batch-queue.php @@ -18,7 +18,7 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { * * @var integer */ - public $queue_batch_size = 10; + public $queue_batch_size = 100; /** * Customers batch size. @@ -79,24 +79,26 @@ class WC_Tests_Reports_Regenerate_Batching extends WC_REST_Unit_Test_Case { */ public function test_queue_batches_splits_into_batches_correctly() { $num_customers = 1234; // 1234 / 5 = 247 batches - $num_batches = ceil( $num_customers / $this->customers_batch_size ); + $num_batches = (int) ceil( $num_customers / $this->customers_batch_size ); + $chunk_size = (int) ceil( $num_batches / $this->queue_batch_size ); + $num_chunks = (int) ceil( $num_batches / $chunk_size ); WC_Admin_Reports_Sync::queue_batches( 1, $num_batches, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ); - $this->assertCount( $this->queue_batch_size, $this->queue->actions ); + $this->assertCount( $num_chunks, $this->queue->actions ); $this->assertArraySubset( array( 'hook' => WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION, - 'args' => array( 1, 25, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ), + 'args' => array( 1, $chunk_size, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ), ), $this->queue->actions[0] ); $this->assertArraySubset( array( 'hook' => WC_Admin_Reports_Sync::QUEUE_BATCH_ACTION, - 'args' => array( 226, 247, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ), + 'args' => array( 247, 247, WC_Admin_Reports_Sync::CUSTOMERS_IMPORT_BATCH_ACTION ), ), - $this->queue->actions[ $this->queue_batch_size - 1 ] + $this->queue->actions[ $num_chunks - 1 ] ); }