Make WordPress Core


Ignore:
Timestamp:
05/01/2018 02:04:25 AM (8 years ago)
Author:
peterwilsoncc
Message:

Cron API: Return meaningful values from cron functions.

Return values added to Cron API functions to indicate outcome:

  • wp_schedule_single_event(), wp_schedule_event(), wp_reschedule_event() and wp_unschedule_event(): boolean indicating success or failure,
  • wp_clear_scheduled_hook(): integer indicating number of jobs cleared (zero or more), false if one or more jobs fail to clear,
  • wp_unschedule_hook(): integer indicating number of jobs cleared (zero or more), false if the jobs fail to clear,
  • spawn_cron(): boolean indicating whether job spawned,
  • wp_cron(): integer indicating number of jobs spawned (zero or more), false if one or more jobs fail to spawned,
  • _set_cron_array(): boolean outcome of update_option().

Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/cron.php

    r42343 r43050  
    3030                $timestamp = strtotime( '+1 hour' );
    3131
    32                 wp_schedule_single_event( $timestamp, $hook );
     32                $scheduled = wp_schedule_single_event( $timestamp, $hook );
     33                $this->assertTrue( $scheduled );
    3334                $this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
    3435
     
    4445                $args      = array( 'foo' );
    4546
    46                 wp_schedule_single_event( $timestamp, $hook, $args );
     47                $scheduled = wp_schedule_single_event( $timestamp, $hook, $args );
     48                $this->assertTrue( $scheduled );
    4749                // this returns the timestamp only if we provide matching args
    4850                $this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
     
    6163                $timestamp = strtotime( '+1 hour' );
    6264
    63                 wp_schedule_event( $timestamp, $recur, $hook );
     65                $scheduled = wp_schedule_event( $timestamp, $recur, $hook );
     66                $this->assertTrue( $scheduled );
    6467                // it's scheduled for the right time
    6568                $this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
     
    7578                $args      = array( 'foo' );
    7679
    77                 wp_schedule_event( $timestamp, 'hourly', $hook, $args );
     80                $scheduled = wp_schedule_event( $timestamp, 'hourly', $hook, $args );
     81                $this->assertTrue( $scheduled );
    7882                // this returns the timestamp only if we provide matching args
    7983                $this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
     
    9599
    96100                // now unschedule it and make sure it's gone
    97                 wp_unschedule_event( $timestamp, $hook );
     101                $unscheduled = wp_unschedule_event( $timestamp, $hook );
     102                $this->assertTrue( $unscheduled );
    98103                $this->assertEquals( false, wp_next_scheduled( $hook ) );
    99104        }
     
    114119
    115120                // clear the schedule for the no args events and make sure it's gone
    116                 wp_clear_scheduled_hook( $hook );
     121                $hook_unscheduled = wp_clear_scheduled_hook( $hook );
     122                $this->assertSame( 2, $hook_unscheduled );
    117123                $this->assertFalse( wp_next_scheduled( $hook ) );
    118124                // the args events should still be there
     
    123129                wp_clear_scheduled_hook( $hook, $args );
    124130                $this->assertFalse( wp_next_scheduled( $hook, $args ) );
     131        }
     132
     133        function test_clear_undefined_schedule() {
     134                $hook = __FUNCTION__;
     135                $args = array( 'arg1' );
     136
     137                wp_schedule_single_event( strtotime( '+1 hour' ), $hook, $args );
     138                wp_schedule_single_event( strtotime( '+2 hour' ), $hook, $args );
     139
     140                // clear the schedule for no args events and ensure no events are cleared.
     141                $hook_unscheduled = wp_clear_scheduled_hook( $hook );
     142                $this->assertSame( 0, $hook_unscheduled );
    125143        }
    126144
     
    207225
    208226                // clear the schedule and make sure it's gone.
    209                 wp_unschedule_hook( $hook );
     227                $unschedule_hook = wp_unschedule_hook( $hook );
     228                $this->assertSame( 4, $unschedule_hook );
     229                $this->assertFalse( wp_next_scheduled( $hook ) );
     230        }
     231
     232        function test_unschedule_undefined_hook() {
     233                $hook           = __FUNCTION__;
     234                $unrelated_hook = __FUNCTION__ . '_two';
     235
     236                // Attempt to clear schedule on non-existant hook.
     237                $unschedule_hook = wp_unschedule_hook( $hook );
     238                $this->assertSame( 0, $unschedule_hook );
     239                $this->assertFalse( wp_next_scheduled( $hook ) );
     240
     241                // Repeat tests with populated cron array.
     242                wp_schedule_single_event( strtotime( '+1 hour' ), $unrelated_hook );
     243                wp_schedule_single_event( strtotime( '+2 hour' ), $unrelated_hook );
     244
     245                $unschedule_hook = wp_unschedule_hook( $hook );
     246                $this->assertSame( 0, $unschedule_hook );
    210247                $this->assertFalse( wp_next_scheduled( $hook ) );
    211248        }
     
    222259
    223260                // first one works
    224                 wp_schedule_single_event( $ts1, $hook, $args );
     261                $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
    225262                // second one is ignored
    226                 wp_schedule_single_event( $ts2, $hook, $args );
     263                $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
    227264
    228265                // the next event should be at +5 minutes, not +3
     
    241278
    242279                // first one works
    243                 wp_schedule_single_event( $ts1, $hook, $args );
     280                $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
    244281                // second works too
    245                 wp_schedule_single_event( $ts2, $hook, $args );
     282                $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
    246283
    247284                // the next event should be at +3 minutes, even though that one was scheduled second
     
    260297
    261298                // first one works
    262                 wp_schedule_single_event( $ts1, $hook, $args );
     299                $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
    263300                // second works too
    264                 wp_schedule_single_event( $ts2, $hook, $args );
     301                $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
    265302
    266303                // the next event should be at +3 minutes
Note: See TracChangeset for help on using the changeset viewer.