From e10d775c889857d0c6b1e9ce2d0c831b5e1bc7a9 Mon Sep 17 00:00:00 2001 From: Caleb Burks Date: Tue, 18 Jan 2022 15:55:02 -0500 Subject: [PATCH] Correctly register recurring events during option updates (#246) --- includes/wp-adapter.php | 2 +- tests/tests/class-wp-adapter-tests.php | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/includes/wp-adapter.php b/includes/wp-adapter.php index dfa53214..e3140222 100644 --- a/includes/wp-adapter.php +++ b/includes/wp-adapter.php @@ -303,7 +303,7 @@ function pre_update_cron_option( $new_value, $old_value ) { 'args' => $event_to_add['args'], ]; - if ( ! empty( $item['value']['schedule'] ) ) { + if ( ! empty( $event_to_add['schedule'] ) ) { $wp_event['schedule'] = $event_to_add['schedule']; $wp_event['interval'] = $event_to_add['interval']; } diff --git a/tests/tests/class-wp-adapter-tests.php b/tests/tests/class-wp-adapter-tests.php index c43881c0..9348b420 100644 --- a/tests/tests/class-wp-adapter-tests.php +++ b/tests/tests/class-wp-adapter-tests.php @@ -244,20 +244,28 @@ public function test_pre_update_cron_option() { $update_result = Cron_Control\pre_update_cron_option( 'not array', [ 'old array' ] ); $this->assertEquals( [ 'old array' ], $update_result ); - // Schedule an event, and leave one unsaved. + // Schedule one event, and leave two unsaved. $default_args = [ 'timestamp' => time() + 100, 'args' => [ 'some', 'args' ] ]; - $event_to_add = $this->create_unsaved_event( array_merge( $default_args, [ 'action' => 'test_pre_update_cron_option_new' ] ) ); $existing_event = $this->create_unsaved_event( array_merge( $default_args, [ 'action' => 'test_pre_update_cron_option_existing' ] ) ); $existing_event->save(); - // Mock the scenario of sending a fresh event into the mix. + $event_to_add = $this->create_unsaved_event( array_merge( $default_args, [ 'action' => 'test_pre_update_cron_option_new' ] ) ); + $recurring_event_to_add = $this->create_unsaved_event( array_merge( $default_args, [ + 'action' => 'test_pre_update_cron_option_new_recurring', + 'schedule' => 'hourly', + 'interval' => HOUR_IN_SECONDS, + ] ) ); + + // Mock the scenario of sending a fresh events into the mix. $existing_option = Events::format_events_for_wp( [ $existing_event ] ); - $new_option = Events::format_events_for_wp( [ $existing_event, $event_to_add ] ); + $new_option = Events::format_events_for_wp( [ $existing_event, $event_to_add, $recurring_event_to_add ] ); $update_result = Cron_Control\pre_update_cron_option( $new_option, $existing_option ); $this->assertEquals( $existing_option, $update_result, 'return value is always the prev value' ); $added_event = Event::find( [ 'action' => 'test_pre_update_cron_option_new' ] ); - $this->assertEquals( $event_to_add->get_action(), $added_event->get_action(), 'event was registered' ); + $this->assertEquals( $event_to_add->get_action(), $added_event->get_action(), 'single event was registered' ); + $added_recurring_event = Event::find( [ 'action' => 'test_pre_update_cron_option_new_recurring' ] ); + $this->assertEquals( $recurring_event_to_add->get_schedule(), $added_recurring_event->get_schedule(), 'recurring event was registered' ); // Mock the scenario of deleting an event from the mix. $existing_option = Events::format_events_for_wp( [ $existing_event, $added_event ] ); @@ -308,6 +316,10 @@ private function create_unsaved_event( array $args ) { $event->set_timestamp( $args['timestamp'] ); $event->set_args( $args['args'] ); + if ( isset( $args['schedule'] ) ) { + $event->set_schedule( $args['schedule'], $args['interval'] ); + } + return $event; } }