forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
683 lines (562 loc) · 21.5 KB
/
cron.php
File metadata and controls
683 lines (562 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
<?php
/**
* Test the cron scheduling functions
*
* @group cron
*/
class Tests_Cron extends WP_UnitTestCase {
/**
* @var array Cron array for testing preflight filters.
*/
private $preflight_cron_array;
/**
* @var int Timestamp of now() + 30 minutes;
*/
private $plus_thirty_minutes;
function setUp() {
parent::setUp();
// make sure the schedule is clear
_set_cron_array( array() );
$this->preflight_cron_array = array();
$this->plus_thirty_minutes = strtotime( '+30 minutes' );
}
function tearDown() {
// make sure the schedule is clear
_set_cron_array( array() );
parent::tearDown();
}
function test_wp_get_schedule_empty() {
// nothing scheduled
$hook = __FUNCTION__;
$this->assertFalse( wp_get_schedule( $hook ) );
}
function test_schedule_event_single() {
// schedule an event and make sure it's returned by wp_next_scheduled
$hook = __FUNCTION__;
$timestamp = strtotime( '+1 hour' );
$scheduled = wp_schedule_single_event( $timestamp, $hook );
$this->assertTrue( $scheduled );
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
// it's a non recurring event
$this->assertEquals( '', wp_get_schedule( $hook ) );
}
function test_schedule_event_single_args() {
// schedule an event with arguments and make sure it's returned by wp_next_scheduled
$hook = 'event';
$timestamp = strtotime( '+1 hour' );
$args = array( 'foo' );
$scheduled = wp_schedule_single_event( $timestamp, $hook, $args );
$this->assertTrue( $scheduled );
// this returns the timestamp only if we provide matching args
$this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
// these don't match so return nothing
$this->assertEquals( false, wp_next_scheduled( $hook ) );
$this->assertEquals( false, wp_next_scheduled( $hook, array( 'bar' ) ) );
// it's a non recurring event
$this->assertEquals( '', wp_get_schedule( $hook, $args ) );
}
function test_schedule_event() {
// schedule an event and make sure it's returned by wp_next_scheduled
$hook = __FUNCTION__;
$recur = 'hourly';
$timestamp = strtotime( '+1 hour' );
$scheduled = wp_schedule_event( $timestamp, $recur, $hook );
$this->assertTrue( $scheduled );
// it's scheduled for the right time
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
// it's a recurring event
$this->assertEquals( $recur, wp_get_schedule( $hook ) );
}
function test_schedule_event_args() {
// schedule an event and make sure it's returned by wp_next_scheduled
$hook = 'event';
$timestamp = strtotime( '+1 hour' );
$recur = 'hourly';
$args = array( 'foo' );
$scheduled = wp_schedule_event( $timestamp, 'hourly', $hook, $args );
$this->assertTrue( $scheduled );
// this returns the timestamp only if we provide matching args
$this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
// these don't match so return nothing
$this->assertEquals( false, wp_next_scheduled( $hook ) );
$this->assertEquals( false, wp_next_scheduled( $hook, array( 'bar' ) ) );
$this->assertEquals( $recur, wp_get_schedule( $hook, $args ) );
}
function test_unschedule_event() {
// schedule an event and make sure it's returned by wp_next_scheduled
$hook = __FUNCTION__;
$timestamp = strtotime( '+1 hour' );
wp_schedule_single_event( $timestamp, $hook );
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
// now unschedule it and make sure it's gone
$unscheduled = wp_unschedule_event( $timestamp, $hook );
$this->assertTrue( $unscheduled );
$this->assertEquals( false, wp_next_scheduled( $hook ) );
}
function test_clear_schedule() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
// schedule several events with and without arguments
wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
// make sure they're returned by wp_next_scheduled()
$this->assertTrue( wp_next_scheduled( $hook ) > 0 );
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule for the no args events and make sure it's gone
$hook_unscheduled = wp_clear_scheduled_hook( $hook );
$this->assertSame( 2, $hook_unscheduled );
$this->assertFalse( wp_next_scheduled( $hook ) );
// the args events should still be there
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule for the args events and make sure they're gone too
// note: wp_clear_scheduled_hook() expects args passed directly, rather than as an array
wp_clear_scheduled_hook( $hook, $args );
$this->assertFalse( wp_next_scheduled( $hook, $args ) );
}
function test_clear_undefined_schedule() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
wp_schedule_single_event( strtotime( '+1 hour' ), $hook, $args );
wp_schedule_single_event( strtotime( '+2 hour' ), $hook, $args );
// clear the schedule for no args events and ensure no events are cleared.
$hook_unscheduled = wp_clear_scheduled_hook( $hook );
$this->assertSame( 0, $hook_unscheduled );
}
function test_clear_schedule_multiple_args() {
$hook = __FUNCTION__;
$args = array( 'arg1', 'arg2' );
// schedule several events with and without arguments
wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
// make sure they're returned by wp_next_scheduled()
$this->assertTrue( wp_next_scheduled( $hook ) > 0 );
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule for the no args events and make sure it's gone
wp_clear_scheduled_hook( $hook );
$this->assertFalse( wp_next_scheduled( $hook ) );
// the args events should still be there
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule for the args events and make sure they're gone too
// note: wp_clear_scheduled_hook() used to expect args passed directly, rather than as an array pre WP 3.0
wp_clear_scheduled_hook( $hook, $args );
$this->assertFalse( wp_next_scheduled( $hook, $args ) );
}
/**
* @ticket 10468
*/
function test_clear_schedule_new_args() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$multi_hook = __FUNCTION__ . '_multi';
$multi_args = array( 'arg2', 'arg3' );
// schedule several events with and without arguments
wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
wp_schedule_single_event( strtotime( '+5 hour' ), $multi_hook, $multi_args );
wp_schedule_single_event( strtotime( '+6 hour' ), $multi_hook, $multi_args );
// make sure they're returned by wp_next_scheduled()
$this->assertTrue( wp_next_scheduled( $hook ) > 0 );
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule for the no args events and make sure it's gone
wp_clear_scheduled_hook( $hook );
$this->assertFalse( wp_next_scheduled( $hook ) );
// the args events should still be there
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule for the args events and make sure they're gone too
// wp_clear_scheduled_hook() should take args as an array like the other functions.
wp_clear_scheduled_hook( $hook, $args );
$this->assertFalse( wp_next_scheduled( $hook, $args ) );
// clear the schedule for the args events and make sure they're gone too
// wp_clear_scheduled_hook() should take args as an array like the other functions and does from WP 3.0
wp_clear_scheduled_hook( $multi_hook, $multi_args );
$this->assertFalse( wp_next_scheduled( $multi_hook, $multi_args ) );
}
/**
* @ticket 18997
*/
function test_unschedule_hook() {
$hook = __FUNCTION__;
$args = array( rand_str() );
// schedule several events with and without arguments.
wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
// make sure they're returned by wp_next_scheduled().
$this->assertTrue( wp_next_scheduled( $hook ) > 0 );
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
// clear the schedule and make sure it's gone.
$unschedule_hook = wp_unschedule_hook( $hook );
$this->assertSame( 4, $unschedule_hook );
$this->assertFalse( wp_next_scheduled( $hook ) );
}
function test_unschedule_undefined_hook() {
$hook = __FUNCTION__;
$unrelated_hook = __FUNCTION__ . '_two';
// Attempt to clear schedule on non-existant hook.
$unschedule_hook = wp_unschedule_hook( $hook );
$this->assertSame( 0, $unschedule_hook );
$this->assertFalse( wp_next_scheduled( $hook ) );
// Repeat tests with populated cron array.
wp_schedule_single_event( strtotime( '+1 hour' ), $unrelated_hook );
wp_schedule_single_event( strtotime( '+2 hour' ), $unrelated_hook );
$unschedule_hook = wp_unschedule_hook( $hook );
$this->assertSame( 0, $unschedule_hook );
$this->assertFalse( wp_next_scheduled( $hook ) );
}
/**
* @ticket 6966
*/
function test_duplicate_event() {
// duplicate events close together should be skipped
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+5 minutes' );
$ts2 = strtotime( '+3 minutes' );
// first one works
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// second one is ignored
$this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
// the next event should be at +5 minutes, not +3
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
}
/**
* @ticket 6966
*/
function test_not_duplicate_event() {
// duplicate events far apart should work normally
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+30 minutes' );
$ts2 = strtotime( '+3 minutes' );
// first one works
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// second works too
$this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
// the next event should be at +3 minutes, even though that one was scheduled second
$this->assertEquals( $ts2, wp_next_scheduled( $hook, $args ) );
wp_unschedule_event( $ts2, $hook, $args );
// following event at +30 minutes should be there too
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
}
function test_not_duplicate_event_reversed() {
// duplicate events far apart should work normally regardless of order
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+3 minutes' );
$ts2 = strtotime( '+30 minutes' );
// first one works
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// second works too
$this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
// the next event should be at +3 minutes
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
wp_unschedule_event( $ts1, $hook, $args );
// following event should be there too
$this->assertEquals( $ts2, wp_next_scheduled( $hook, $args ) );
}
/**
* Ensure the pre_scheduled_event filter prevents
* modification of the cron_array_option.
*
* @ticket 32656
*/
function test_pre_schedule_event_filter() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+30 minutes' );
$ts2 = strtotime( '+3 minutes' );
$expected = _get_cron_array();
add_filter( 'pre_schedule_event', array( $this, '_filter_pre_schedule_event_filter' ), 10, 2 );
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
$this->assertTrue( wp_schedule_event( $ts2, 'hourly', $hook ) );
// Check cron option is unchanged.
$this->assertSame( $expected, _get_cron_array() );
$expected_preflight[ $ts2 ][ $hook ][ md5( serialize( array() ) ) ] = array(
'schedule' => 'hourly',
'interval' => HOUR_IN_SECONDS,
'args' => array(),
);
$expected_preflight[ $ts1 ][ $hook ][ md5( serialize( $args ) ) ] = array(
'schedule' => false,
'interval' => 0,
'args' => $args,
);
$this->assertSame( $expected_preflight, $this->preflight_cron_array );
}
/**
* Filter the scheduling of events to use the preflight array.
*/
function _filter_pre_schedule_event_filter( $null, $event ) {
$key = md5( serialize( $event->args ) );
$this->preflight_cron_array[ $event->timestamp ][ $event->hook ][ $key ] = array(
'schedule' => $event->schedule,
'interval' => isset( $event->interval ) ? $event->interval : 0,
'args' => $event->args,
);
uksort( $this->preflight_cron_array, 'strnatcasecmp' );
return true;
}
/**
* Ensure the pre_reschedule_event filter prevents
* modification of the cron_array_option.
*
* @ticket 32656
*/
function test_pre_reschedule_event_filter() {
$hook = __FUNCTION__;
$ts1 = strtotime( '+30 minutes' );
// Add an event
$this->assertTrue( wp_schedule_event( $ts1, 'hourly', $hook ) );
$expected = _get_cron_array();
// Add preflight filter.
add_filter( 'pre_reschedule_event', '__return_true' );
// Reschedule event with preflight filter in place.
wp_reschedule_event( $ts1, 'daily', $hook );
// Check cron option is unchanged.
$this->assertSame( $expected, _get_cron_array() );
}
/**
* Ensure the pre_unschedule_event filter prevents
* modification of the cron_array_option.
*
* @ticket 32656
*/
function test_pre_unschedule_event_filter() {
$hook = __FUNCTION__;
$ts1 = strtotime( '+30 minutes' );
// Add an event
$this->assertTrue( wp_schedule_event( $ts1, 'hourly', $hook ) );
$expected = _get_cron_array();
// Add preflight filter.
add_filter( 'pre_unschedule_event', '__return_true' );
// Unschedule event with preflight filter in place.
wp_unschedule_event( $ts1, $hook );
// Check cron option is unchanged.
$this->assertSame( $expected, _get_cron_array() );
}
/**
* Ensure the clearing scheduled hooks filter prevents
* modification of the cron_array_option.
*
* @ticket 32656
*/
function test_pre_clear_scheduled_hook_filters() {
$hook = __FUNCTION__;
$ts1 = strtotime( '+30 minutes' );
// Add an event
$this->assertTrue( wp_schedule_event( $ts1, 'hourly', $hook ) );
$expected = _get_cron_array();
// Add preflight filters.
add_filter( 'pre_clear_scheduled_hook', '__return_true' );
add_filter( 'pre_unschedule_hook', '__return_zero' );
// Unschedule event with preflight filter in place.
wp_clear_scheduled_hook( $hook );
// Check cron option is unchanged.
$this->assertSame( $expected, _get_cron_array() );
// Unschedule all events with preflight filter in place.
wp_unschedule_hook( $hook );
// Check cron option is unchanged.
$this->assertSame( $expected, _get_cron_array() );
}
/**
* Ensure the preflight hooks for scheduled events
* return a filtered value as expected.
*
* @ticket 32656
*/
function test_pre_scheduled_event_hooks() {
add_filter( 'pre_get_scheduled_event', array( $this, 'filter_pre_scheduled_event_hooks' ) );
$actual = wp_get_scheduled_event( 'preflight_event', array(), $this->plus_thirty_minutes );
$actual2 = wp_next_scheduled( 'preflight_event', array() );
$expected = (object) array(
'hook' => 'preflight_event',
'timestamp' => $this->plus_thirty_minutes,
'schedule' => false,
'args' => array(),
);
$this->assertEquals( $expected, $actual );
$this->assertEquals( $expected->timestamp, $actual2 );
}
function filter_pre_scheduled_event_hooks() {
return (object) array(
'hook' => 'preflight_event',
'timestamp' => $this->plus_thirty_minutes,
'schedule' => false,
'args' => array(),
);
}
/**
* Ensure wp_get_scheduled_event() returns the expected one off events.
*
* When no timestamp is specified, the next event should be returned.
* When a timestamp is specified, a particular event should be returned.
*
* @ticket 45976.
*/
function test_get_scheduled_event_singles() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts_late = strtotime( '+30 minutes' );
$ts_next = strtotime( '+3 minutes' );
$expected1 = (object) array(
'hook' => $hook,
'timestamp' => $ts_late,
'schedule' => false,
'args' => $args,
);
$expected2 = (object) array(
'hook' => $hook,
'timestamp' => $ts_next,
'schedule' => false,
'args' => $args,
);
// Schedule late running event.
wp_schedule_single_event( $ts_late, $hook, $args );
// Schedule next running event.
wp_schedule_single_event( $ts_next, $hook, $args );
// Late running, timestamp specified.
$this->assertEquals( $expected1, wp_get_scheduled_event( $hook, $args, $ts_late ) );
// Next running, timestamp specified.
$this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args, $ts_next ) );
// Next running, no timestamp specified.
$this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
}
/**
* Ensure wp_get_scheduled_event() returns the expected recurring events.
*
* When no timestamp is specified, the next event should be returned.
* When a timestamp is specified, a particular event should be returned.
*
* @ticket 45976.
*/
function test_get_scheduled_event_recurring() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts_late = strtotime( '+30 minutes' );
$ts_next = strtotime( '+3 minutes' );
$schedule = 'hourly';
$interval = HOUR_IN_SECONDS;
$expected1 = (object) array(
'hook' => $hook,
'timestamp' => $ts_late,
'schedule' => $schedule,
'args' => $args,
'interval' => $interval,
);
$expected2 = (object) array(
'hook' => $hook,
'timestamp' => $ts_next,
'schedule' => $schedule,
'args' => $args,
'interval' => $interval,
);
// Schedule late running event.
wp_schedule_event( $ts_late, $schedule, $hook, $args );
// Schedule next running event.
wp_schedule_event( $ts_next, $schedule, $hook, $args );
// Late running, timestamp specified.
$this->assertEquals( $expected1, wp_get_scheduled_event( $hook, $args, $ts_late ) );
// Next running, timestamp specified.
$this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args, $ts_next ) );
// Next running, no timestamp specified.
$this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
}
/**
* Ensure wp_get_scheduled_event() returns false when expected.
*
* @ticket 45976.
*/
function test_get_scheduled_event_false() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts = strtotime( '+3 minutes' );
// No scheduled events.
// - With timestamp
$this->assertFalse( wp_get_scheduled_event( $hook, $args, $ts ) );
// - Get next, none scheduled.
$this->assertFalse( wp_get_scheduled_event( $hook, $args ) );
// Schedule an event.
wp_schedule_event( $ts, $hook, $args );
// - unregistered timestamp
$this->assertFalse( wp_get_scheduled_event( $hook, $args, strtotime( '+30 minutes' ) ) );
// - invalid timestamp.
$this->assertFalse( wp_get_scheduled_event( $hook, $args, 'Words Fail!' ) );
}
/**
* Ensure any past event counts as a duplicate.
*
* @ticket 44818
*/
function test_duplicate_past_event() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '-14 minutes' );
$ts2 = strtotime( '+5 minutes' );
$ts3 = strtotime( '-2 minutes' );
// First event scheduled successfully.
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// Second event fails.
$this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
// Third event fails.
$this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
}
/**
* Ensure any near future event counts as a duplicate.
*
* @ticket 44818
*/
function test_duplicate_near_future_event() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+4 minutes' );
$ts2 = strtotime( '-15 minutes' );
$ts3 = strtotime( '+12 minutes' );
// First event scheduled successfully.
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// Second event fails.
$this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
// Third event fails.
$this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
}
/**
* Duplicate future events are disallowed.
*
* @ticket 44818
*/
function test_duplicate_future_event() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+15 minutes' );
$ts2 = strtotime( '-600 seconds', $ts1 );
$ts3 = strtotime( '+600 seconds', $ts1 );
// First event scheduled successfully.
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// Events within ten minutes should fail.
$this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
$this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
}
/**
* Future events are allowed.
*
* @ticket 44818
*/
function test_not_duplicate_future_event() {
$hook = __FUNCTION__;
$args = array( 'arg1' );
$ts1 = strtotime( '+15 minutes' );
$ts2 = strtotime( '-601 seconds', $ts1 );
$ts3 = strtotime( '+601 seconds', $ts1 );
// First event scheduled successfully.
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
// Events over ten minutes should work.
$this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
$this->assertTrue( wp_schedule_single_event( $ts3, $hook, $args ) );
}
}