Skip to content

Commit e10d775

Browse files
authored
Correctly register recurring events during option updates (#246)
1 parent 426ca25 commit e10d775

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

includes/wp-adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function pre_update_cron_option( $new_value, $old_value ) {
303303
'args' => $event_to_add['args'],
304304
];
305305

306-
if ( ! empty( $item['value']['schedule'] ) ) {
306+
if ( ! empty( $event_to_add['schedule'] ) ) {
307307
$wp_event['schedule'] = $event_to_add['schedule'];
308308
$wp_event['interval'] = $event_to_add['interval'];
309309
}

tests/tests/class-wp-adapter-tests.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,20 +244,28 @@ public function test_pre_update_cron_option() {
244244
$update_result = Cron_Control\pre_update_cron_option( 'not array', [ 'old array' ] );
245245
$this->assertEquals( [ 'old array' ], $update_result );
246246

247-
// Schedule an event, and leave one unsaved.
247+
// Schedule one event, and leave two unsaved.
248248
$default_args = [ 'timestamp' => time() + 100, 'args' => [ 'some', 'args' ] ];
249-
$event_to_add = $this->create_unsaved_event( array_merge( $default_args, [ 'action' => 'test_pre_update_cron_option_new' ] ) );
250249
$existing_event = $this->create_unsaved_event( array_merge( $default_args, [ 'action' => 'test_pre_update_cron_option_existing' ] ) );
251250
$existing_event->save();
252251

253-
// Mock the scenario of sending a fresh event into the mix.
252+
$event_to_add = $this->create_unsaved_event( array_merge( $default_args, [ 'action' => 'test_pre_update_cron_option_new' ] ) );
253+
$recurring_event_to_add = $this->create_unsaved_event( array_merge( $default_args, [
254+
'action' => 'test_pre_update_cron_option_new_recurring',
255+
'schedule' => 'hourly',
256+
'interval' => HOUR_IN_SECONDS,
257+
] ) );
258+
259+
// Mock the scenario of sending a fresh events into the mix.
254260
$existing_option = Events::format_events_for_wp( [ $existing_event ] );
255-
$new_option = Events::format_events_for_wp( [ $existing_event, $event_to_add ] );
261+
$new_option = Events::format_events_for_wp( [ $existing_event, $event_to_add, $recurring_event_to_add ] );
256262
$update_result = Cron_Control\pre_update_cron_option( $new_option, $existing_option );
257263

258264
$this->assertEquals( $existing_option, $update_result, 'return value is always the prev value' );
259265
$added_event = Event::find( [ 'action' => 'test_pre_update_cron_option_new' ] );
260-
$this->assertEquals( $event_to_add->get_action(), $added_event->get_action(), 'event was registered' );
266+
$this->assertEquals( $event_to_add->get_action(), $added_event->get_action(), 'single event was registered' );
267+
$added_recurring_event = Event::find( [ 'action' => 'test_pre_update_cron_option_new_recurring' ] );
268+
$this->assertEquals( $recurring_event_to_add->get_schedule(), $added_recurring_event->get_schedule(), 'recurring event was registered' );
261269

262270
// Mock the scenario of deleting an event from the mix.
263271
$existing_option = Events::format_events_for_wp( [ $existing_event, $added_event ] );
@@ -308,6 +316,10 @@ private function create_unsaved_event( array $args ) {
308316
$event->set_timestamp( $args['timestamp'] );
309317
$event->set_args( $args['args'] );
310318

319+
if ( isset( $args['schedule'] ) ) {
320+
$event->set_schedule( $args['schedule'], $args['interval'] );
321+
}
322+
311323
return $event;
312324
}
313325
}

0 commit comments

Comments
 (0)