-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcron.feature
More file actions
655 lines (565 loc) · 19.6 KB
/
cron.feature
File metadata and controls
655 lines (565 loc) · 19.6 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
Feature: Manage WP-Cron events and schedules
Background:
Given a WP install
And I run `wp config set DISABLE_WP_CRON false --raw --type=constant --anchor="/* Add any custom values between this line and the \"stop editing\" line. */" --placement=after`
Scenario: Scheduling and then deleting an event
When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 5 minutes' --0=banana`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_1'
"""
When I run `wp cron event list --format=csv --fields=hook,recurrence,args`
Then STDOUT should be CSV containing:
| hook | recurrence | args |
| wp_cli_test_event_1 | Non-repeating | ["banana"] |
When I run `wp cron event list --fields=hook,next_run_relative | grep wp_cli_test_event_1`
Then STDOUT should contain:
"""
1 hour
"""
When I run `wp cron event list --hook=wp_cli_test_event_1 --format=count`
Then STDOUT should be:
"""
1
"""
When I run `wp cron event list --hook=apple --format=count`
Then STDOUT should be:
"""
0
"""
When I run `wp cron event delete wp_cli_test_event_1`
Then STDOUT should contain:
"""
Success: Deleted a total of 1 cron event.
"""
When I run `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_1
"""
Scenario: Scheduling and then running an event
When I run `wp cron event schedule wp_cli_test_event_3 '-1 minutes'`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_3'
"""
When I run `wp cron event schedule wp_cli_test_event_4`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_4'
"""
When I run `wp cron event list --format=csv --fields=hook,recurrence`
Then STDOUT should be CSV containing:
| hook | recurrence |
| wp_cli_test_event_3 | Non-repeating |
When I run `wp cron event run wp_cli_test_event_3`
Then STDOUT should not be empty
When I run `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_3
"""
Scenario: Scheduling, running, and deleting duplicate events
When I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=banana`
And I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=bar`
Then STDOUT should not be empty
When I run `wp cron event list --format=csv --fields=hook,recurrence,args`
Then STDOUT should be CSV containing:
| hook | recurrence | args |
| wp_cli_test_event_5 | Non-repeating | ["banana"] |
| wp_cli_test_event_5 | Non-repeating | ["bar"] |
When I run `wp cron event run wp_cli_test_event_5`
Then STDOUT should contain:
"""
Executed the cron event 'wp_cli_test_event_5'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_cli_test_event_5'
"""
And STDOUT should contain:
"""
Success: Executed a total of 2 cron events.
"""
When I run `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_5
"""
When I try `wp cron event run wp_cli_test_event_5`
Then STDERR should be:
"""
Error: Invalid cron event 'wp_cli_test_event_5'
"""
When I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=banana`
And I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=bar`
Then STDOUT should not be empty
When I run `wp cron event list`
Then STDOUT should contain:
"""
wp_cli_test_event_5
"""
When I run `wp cron event delete wp_cli_test_event_5`
Then STDOUT should be:
"""
Success: Deleted a total of 2 cron events.
"""
When I run `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_5
"""
When I try `wp cron event delete wp_cli_test_event_5`
Then STDERR should be:
"""
Error: Invalid cron event 'wp_cli_test_event_5'
"""
Scenario: Cron event with missing recurrence should be non-repeating.
Given a wp-content/mu-plugins/schedule.php file:
"""
<?php
add_filter(
'cron_schedules',
function( $schedules ) {
$schedules['test_schedule'] = array(
'interval' => 3600,
'display' => __( 'Every Hour' ),
);
return $schedules;
}
);
"""
When I run `wp cron event schedule wp_cli_test_event "1 hour" test_schedule`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event'
"""
When I run `wp cron event list --hook=wp_cli_test_event --fields=hook,recurrence`
Then STDOUT should be a table containing rows:
| hook | recurrence |
| wp_cli_test_event | 1 hour |
When I run `rm wp-content/mu-plugins/schedule.php`
Then the return code should be 0
When I run `wp cron event list --hook=wp_cli_test_event --fields=hook,recurrence`
Then STDOUT should be a table containing rows:
| hook | recurrence |
| wp_cli_test_event | Non-repeating |
Scenario: Scheduling and then running a re-occurring event
When I run `wp cron event schedule wp_cli_test_event_4 now hourly`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_4'
"""
When I run `wp cron event list --format=csv --fields=hook,recurrence`
Then STDOUT should be CSV containing:
| hook | recurrence |
| wp_cli_test_event_4 | 1 hour |
When I run `wp cron event run wp_cli_test_event_4`
Then STDOUT should not be empty
When I run `wp cron event list`
Then STDOUT should contain:
"""
wp_cli_test_event_4
"""
Scenario: Scheduling and then deleting a recurring event
When I run `wp cron event schedule wp_cli_test_event_2 now daily`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_2'
"""
When I run `wp cron event list --format=csv --fields=hook,recurrence`
Then STDOUT should be CSV containing:
| hook | recurrence |
| wp_cli_test_event_2 | 1 day |
When I run `wp cron event delete wp_cli_test_event_2`
Then STDOUT should contain:
"""
Success: Deleted a total of 1 cron event.
"""
When I run `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_2
"""
Scenario: Listing cron schedules
When I run `wp cron schedule list --format=csv --fields=name,interval`
Then STDOUT should be CSV containing:
| name | interval |
| hourly | 3600 |
Scenario: Testing WP-Cron
Given a php.ini file:
"""
error_log = {RUN_DIR}/server.log
log_errors = on
"""
And I launch in the background `wp server --host=localhost --port=8080 --config=php.ini`
And a wp-content/mu-plugins/set_cron_site_url.php file:
"""
<?php
add_filter( 'cron_request', static function ( $cron_request_array ) {
$cron_request_array['url'] = 'http://localhost:8080';
$cron_request_array['args']['sslverify'] = false;
return $cron_request_array;
} );
"""
When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 5 minutes' --0=banana`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_1'
"""
When I run `wp cron test`
Then STDOUT should contain:
"""
Success: WP-Cron spawning is working as expected.
"""
And STDERR should not contain:
"""
Error:
"""
# Normally we would simply check for the log file to not exist. However, when running with Xdebug for code coverage purposes,
# the following warning might be added to the log file:
# PHP Warning: JIT is incompatible with third party extensions that override zend_execute_ex(). JIT disabled. in Unknown on line 0
# This workaround checks for any other possible entries in the log file.
When I run `awk '!/JIT/' {RUN_DIR}/server.log 2>/dev/null || true`
Then STDOUT should be empty
Scenario: Run multiple cron events
When I try `wp cron event run`
Then STDERR should be:
"""
Error: Please specify one or more cron events, or use --due-now/--all.
"""
When I run `wp cron event run wp_version_check wp_update_plugins`
Then STDOUT should contain:
"""
Executed the cron event 'wp_version_check'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_update_plugins'
"""
And STDOUT should contain:
"""
Success: Executed a total of 2 cron events.
"""
# WP throws a notice here for older versions of core.
When I try `wp cron event run --all`
Then STDOUT should contain:
"""
Executed the cron event 'wp_version_check'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_update_plugins'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_update_themes'
"""
And STDOUT should contain:
"""
Success: Executed a total of
"""
# Fails on WordPress 4.9 because `wp cron event run --due-now`
# executes the "wp_privacy_delete_old_export_files" event there.
@require-wp-5.0
Scenario: Run currently scheduled events
# Disable web-triggered cron so spawn_cron() cannot set doing_cron between
# steps - wp cron event run always defines DOING_CRON so this has no effect
# on the commands being tested.
When I run `wp config set DISABLE_WP_CRON true --raw`
# WP throws a notice here for older versions of core.
And I try `wp cron event run --all`
Then STDOUT should contain:
"""
Executed the cron event 'wp_version_check'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_update_plugins'
"""
And STDOUT should contain:
"""
Executed the cron event 'wp_update_themes'
"""
And STDOUT should contain:
"""
Success: Executed a total of
"""
When I run `wp cron event run --due-now`
Then STDOUT should contain:
"""
Executed a total of 0 cron events
"""
When I run `wp cron event schedule wp_cli_test_event_1 now hourly`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_1'
"""
When I run `wp cron event run --due-now`
Then STDOUT should contain:
"""
Executed the cron event 'wp_cli_test_event_1'
"""
And STDOUT should contain:
"""
Executed a total of 1 cron event
"""
When I run `wp cron event run --due-now`
Then STDOUT should contain:
"""
Executed a total of 0 cron events
"""
Scenario: Don't trigger cron when ALTERNATE_WP_CRON is defined
Given a alternate-wp-cron.php file:
"""
<?php
define( 'ALTERNATE_WP_CRON', true );
"""
And a wp-cli.yml file:
"""
require:
- alternate-wp-cron.php
"""
And a php.ini file:
"""
error_log = {RUN_DIR}/server.log
log_errors = on
"""
And I launch in the background `wp server --host=localhost --port=8080 --config=php.ini`
And a wp-content/mu-plugins/set_cron_site_url.php file:
"""
<?php
add_filter( 'cron_request', static function ( $cron_request_array ) {
$cron_request_array['url'] = str_replace( home_url(), 'http://localhost:8080', $cron_request_array['url'] );
$cron_request_array['args']['sslverify'] = false;
return $cron_request_array;
} );
"""
When I run `wp eval 'var_export( ALTERNATE_WP_CRON );'`
Then STDOUT should be:
"""
true
"""
When I run `wp option get home`
Then STDOUT should be:
"""
https://example.com
"""
When I try `wp cron test`
Then STDOUT should contain:
"""
Success: WP-Cron spawning is working as expected.
"""
And STDERR should contain:
"""
Warning: The ALTERNATE_WP_CRON constant is set to true. WP-Cron spawning is not asynchronous.
"""
Scenario: Listing duplicated cron events
When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty
When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 6 minutes' hourly`
Then STDOUT should not be empty
When I run `wp cron event list --format=ids`
Then STDOUT should contain:
"""
wp_cli_test_event_1 wp_cli_test_event_1
"""
Scenario: Scheduling an event with non-numerically indexed arguments
When I try `wp cron event schedule wp_cli_test_args_event '+10 minutes' --foo=banana --bar=apple`
Then STDOUT should not be empty
And STDERR should be:
"""
Warning: Numeric keys should be used for the hook arguments.
"""
When I run `wp cron event list --format=csv --fields=hook,recurrence,args`
Then STDOUT should be CSV containing:
| hook | recurrence | args |
| wp_cli_test_args_event | Non-repeating | {"foo":"banana","bar":"apple"} |
Scenario: Warn when an invalid cron event is detected
Given a WP install
And a update.php file:
"""
<?php
$val = array(
1647914509 => array(
'postindexer_secondpass_cron' => array(
'40cd750bba9870f18aada2478b24840a' => array(
'schedule' => '5mins',
'args' => array(),
'interval' => 100,
),
),
),
'wp_batch_split_terms' => array(
1442323165 => array(
'40cd750bba9870f18aada2478b24840a' => array(
'schedule' => false,
'args' => array()
)
)
)
);
update_option( 'cron', $val );
"""
And I run `wp eval-file update.php`
When I try `wp cron event list`
Then STDOUT should contain:
"""
postindexer_secondpass_cron
"""
And STDERR should contain:
"""
Warning: Ignoring incorrectly registered cron event "wp_batch_split_terms".
"""
Scenario: Delete multiple cron events
When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty
When I run `wp cron event schedule wp_cli_test_event_2 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty
When I try `wp cron event delete`
Then STDERR should be:
"""
Error: Please specify one or more cron events, or use --due-now/--all.
"""
# WP throws a notice here for older versions of core.
When I try `wp cron event delete --all`
Then STDOUT should contain:
"""
Success: Deleted a total of
"""
When I try `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_1
"""
And STDOUT should not contain:
"""
wp_cli_test_event_2
"""
When I run `wp cron event schedule wp_cli_test_event_1 now hourly`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_1'
"""
When I run `wp cron event schedule wp_cli_test_event_2 now hourly`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_2'
"""
When I run `wp cron event schedule wp_cli_test_event_2 '+1 hour 5 minutes' hourly`
Then STDOUT should contain:
"""
Success: Scheduled event with hook 'wp_cli_test_event_2'
"""
When I run `wp cron event delete wp_cli_test_event_2 --due-now`
Then STDOUT should contain:
"""
Deleted a total of 1 cron event
"""
When I try `wp cron event list`
Then STDOUT should contain:
"""
wp_cli_test_event_2
"""
When I run `wp cron event list --hook=wp_cli_test_event_2 --format=count`
Then STDOUT should be:
"""
1
"""
When I run `wp cron event delete --due-now`
Then STDOUT should contain:
"""
Success: Deleted a total of
"""
When I try `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_1
"""
And STDOUT should contain:
"""
wp_cli_test_event_2
"""
When I run `wp cron event schedule wp_cli_test_event_1 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty
When I run `wp cron event schedule wp_cli_test_event_2 '+1 hour 5 minutes' hourly`
Then STDOUT should not be empty
When I run `wp cron event delete --all --exclude=wp_cli_test_event_1`
Then STDOUT should contain:
"""
Success: Deleted a total of
"""
When I try `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_2
"""
And STDOUT should contain:
"""
wp_cli_test_event_1
"""
Scenario: A valid combination of parameters should be present
When I try `wp cron event delete --due-now --all`
Then STDERR should be:
"""
Error: Please use either --due-now or --all.
"""
When I try `wp cron event delete wp_cli_test_event_1 --all`
Then STDERR should be:
"""
Error: Please either specify cron events, or use --all.
"""
Scenario: Delete a specific cron event by matching args
When I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=banana`
And I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=bar`
Then STDOUT should not be empty
When I run `wp cron event list --format=csv --fields=hook,recurrence,args`
Then STDOUT should be CSV containing:
| hook | recurrence | args |
| wp_cli_test_event_5 | Non-repeating | ["banana"] |
| wp_cli_test_event_5 | Non-repeating | ["bar"] |
When I run `wp cron event delete wp_cli_test_event_5 --match-args='["banana"]'`
Then STDOUT should be:
"""
Success: Deleted a total of 1 cron event.
"""
When I run `wp cron event list --format=csv --fields=hook,args`
Then STDOUT should be CSV containing:
| hook | args |
| wp_cli_test_event_5 | ["bar"] |
And STDOUT should not contain:
"""
["banana"]
"""
When I run `wp cron event delete wp_cli_test_event_5 --match-args=bar`
Then STDOUT should be:
"""
Success: Deleted a total of 1 cron event.
"""
When I try `wp cron event list`
Then STDOUT should not contain:
"""
wp_cli_test_event_5
"""
When I run `wp cron event schedule wp_cli_test_event_5 '+20 minutes' --0=banana`
Then STDOUT should not be empty
When I try `wp cron event delete wp_cli_test_event_5 --match-args='["banana"]' --all`
Then STDERR should be:
"""
Error: The --match-args parameter cannot be combined with --all or --due-now.
"""
When I try `wp cron event delete wp_cli_test_event_5 --match-args='["banana"]' --due-now`
Then STDERR should be:
"""
Error: The --match-args parameter cannot be combined with --all or --due-now.
"""
When I try `wp cron event delete --match-args='["banana"]'`
Then STDERR should be:
"""
Error: The --match-args parameter requires exactly one hook name.
"""
When I try `wp cron event delete wp_cli_test_event_5 wp_cli_test_event_5 --match-args='["banana"]'`
Then STDERR should be:
"""
Error: The --match-args parameter requires exactly one hook name.
"""