/
Test_WP_Background_Process.php
672 lines (598 loc) · 24.8 KB
/
Test_WP_Background_Process.php
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
<?php
/**
* Unit tests for WP_Background_Process.
*
* @package WP-Background-Processing
*/
require_once __DIR__ . '/fixtures/Test_Batch_Data.php';
use PHPUnit\Framework\MockObject\MockObject;
/**
* Class Test_WP_Background_Process
*/
class Test_WP_Background_Process extends WP_UnitTestCase {
/**
* Instance of WP_Background_Process
*
* @var MockObject|WP_Background_Process|(WP_Background_Process&MockObject)
*/
private $wpbp;
/**
* Performs set up before each test.
*
* @return void
*/
public function setUp(): void {
parent::setUp();
$this->wpbp = $this->getMockForAbstractClass( WP_Background_Process::class );
$this->wpbp->expects( $this->any() )
->method( 'task' )
->will( $this->returnValue( false ) );
}
/**
* Get a property value from WPBP regardless of accessibility.
*
* @param string $name
*
* @return mixed
*/
private function getWPBPProperty( string $name ) {
try {
$property = new ReflectionProperty( 'WP_Background_Process', $name );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
$property->setAccessible( true );
return $property->getValue( $this->wpbp );
}
/**
* Set a property value on WPBP regardless of accessibility.
*
* @param string $name
* @param mixed $value
*
* @return mixed
*/
private function setWPBPProperty( string $name, $value ) {
try {
$property = new ReflectionProperty( 'WP_Background_Process', $name );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
$property->setAccessible( true );
return $property->setValue( $this->wpbp, $value );
}
/**
* Execute a method of WPBP regardless of accessibility.
*
* @param string $name Method name.
* @param mixed $args None, one or more args to pass to method.
*
* @return mixed
*/
private function executeWPBPMethod( string $name, ...$args ) {
try {
$method = new ReflectionMethod( 'WP_Background_Process', $name );
$method->setAccessible( true );
return $method->invoke( $this->wpbp, ...$args );
} catch ( Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
}
/**
* Test push_to_queue.
*
* @return void
*/
public function test_push_to_queue() {
$this->assertClassHasAttribute( 'data', 'WP_Background_Process', 'class has data property' );
$this->assertEmpty( $this->getWPBPProperty( 'data' ) );
$this->wpbp->push_to_queue( 'wibble' );
$this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
$this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
$this->wpbp->push_to_queue( 'wobble' );
$this->assertEquals( array( 'wibble', 'wobble' ), $this->getWPBPProperty( 'data' ) );
}
/**
* Test save.
*
* @return void
*/
public function test_save() {
$this->assertClassHasAttribute( 'data', 'WP_Background_Process', 'class has data property' );
$this->assertEmpty( $this->getWPBPProperty( 'data' ) );
$this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
$this->wpbp->push_to_queue( 'wibble' );
$this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
$this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
$this->wpbp->save();
$this->assertEmpty( $this->getWPBPProperty( 'data' ), 'data emptied after save' );
$this->assertNotEmpty( $this->wpbp->get_batches(), 'batches exist after save' );
}
/**
* Test get_batches.
*
* @return void
*/
public function test_get_batches() {
$this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
$this->wpbp->push_to_queue( 'wibble' );
$this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
$this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
$this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
$this->wpbp->push_to_queue( 'wobble' );
$this->assertEquals( array( 'wibble', 'wobble' ), $this->getWPBPProperty( 'data' ) );
$this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
$this->wpbp->save();
$first_batch = $this->wpbp->get_batches();
$this->assertNotEmpty( $first_batch );
$this->assertCount( 1, $first_batch );
$this->wpbp->push_to_queue( 'more wibble' );
$this->wpbp->save();
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->wpbp->push_to_queue( 'Wibble wobble all day long.' );
$this->wpbp->save();
$this->assertCount( 3, $this->wpbp->get_batches() );
$this->assertEquals( $first_batch, $this->wpbp->get_batches( 1 ) );
$this->assertNotEquals( $first_batch, $this->wpbp->get_batches( 2 ) );
$this->assertCount( 2, $this->wpbp->get_batches( 2 ) );
$this->assertCount( 3, $this->wpbp->get_batches( 3 ) );
$this->assertCount( 3, $this->wpbp->get_batches( 5 ) );
}
/**
* Test get_batch.
*
* @return void
*/
public function test_get_batch() {
$this->assertEmpty( $this->executeWPBPMethod( 'get_batch' ), 'no batches until save' );
$this->wpbp->push_to_queue( 'wibble' );
$this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
$this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
$this->assertEmpty( $this->executeWPBPMethod( 'get_batch' ), 'no batches until save' );
$this->wpbp->push_to_queue( 'wobble' );
$this->assertEquals( array( 'wibble', 'wobble' ), $this->getWPBPProperty( 'data' ) );
$this->assertEmpty( $this->executeWPBPMethod( 'get_batch' ), 'no batches until save' );
$this->wpbp->save();
$first_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertNotEmpty( $first_batch );
$this->assertInstanceOf( 'stdClass', $first_batch );
$this->assertEquals( array( 'wibble', 'wobble' ), $first_batch->data );
$this->wpbp->push_to_queue( 'more wibble' );
$this->wpbp->save();
$second_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertNotEmpty( $second_batch );
$this->assertInstanceOf( 'stdClass', $second_batch );
$this->assertEquals( $first_batch, $second_batch, 'same 1st batch returned until deleted' );
$this->wpbp->delete( $first_batch->key );
$second_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertNotEmpty( $second_batch );
$this->assertInstanceOf( 'stdClass', $second_batch );
$this->assertNotEquals( $first_batch, $second_batch, '2nd batch returned as 1st deleted' );
$this->assertEquals( array( 'more wibble' ), $second_batch->data );
// Tests using a custom class for the $item.
$this->wpbp->delete( $second_batch->key );
$batch_data_object = new Test_Batch_Data();
$this->wpbp->push_to_queue( $batch_data_object );
$this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
$this->assertEquals( array( $batch_data_object ), $this->getWPBPProperty( 'data' ) );
$this->wpbp->save();
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
// Explicitly set allowed classes to Test_Batch_Data.
$this->setWPBPProperty( 'allowed_batch_data_classes', array( Test_Batch_Data::class ) );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
// Allow a different class.
$this->setWPBPProperty( 'allowed_batch_data_classes', array( stdClass::class ) );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] );
// Disallow all classes.
$this->setWPBPProperty( 'allowed_batch_data_classes', false );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] );
// Allow everything.
$this->setWPBPProperty( 'allowed_batch_data_classes', true );
$third_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertCount( 1, $third_batch->data );
$this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
}
/**
* Test cancel.
*
* @return void
*/
public function test_cancel() {
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertFalse( $this->wpbp->is_cancelled() );
$this->wpbp->cancel();
$this->assertTrue( $this->wpbp->is_cancelled() );
}
/**
* Test pause.
*
* @return void
*/
public function test_pause() {
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertFalse( $this->wpbp->is_paused() );
$this->wpbp->pause();
$this->assertTrue( $this->wpbp->is_paused() );
}
/**
* Test resume.
*
* @return void
*/
public function test_resume() {
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertFalse( $this->wpbp->is_paused() );
$this->wpbp->pause();
$this->assertTrue( $this->wpbp->is_paused() );
$this->wpbp->resume();
$this->assertFalse( $this->wpbp->is_paused() );
}
/**
* Test delete.
*
* @return void
*/
public function test_delete() {
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->wpbp->push_to_queue( 'wobble' );
$this->wpbp->save();
$this->assertCount( 2, $this->wpbp->get_batches() );
$first_batch = $this->executeWPBPMethod( 'get_batch' );
$this->wpbp->delete( $first_batch->key );
$this->assertCount( 1, $this->wpbp->get_batches() );
$second_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertNotEquals( $first_batch, $second_batch, '2nd batch returned as 1st deleted' );
}
/**
* Test delete_all.
*
* @return void
*/
public function test_delete_all() {
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->wpbp->push_to_queue( 'wobble' );
$this->wpbp->save();
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->wpbp->delete_all();
$this->assertCount( 0, $this->wpbp->get_batches() );
}
/**
* Test update.
*
* @return void
*/
public function test_update() {
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->wpbp->push_to_queue( 'wobble' );
$this->wpbp->save();
$this->assertCount( 2, $this->wpbp->get_batches() );
$first_batch = $this->executeWPBPMethod( 'get_batch' );
$this->wpbp->update( $first_batch->key, array( 'Wibble wobble all day long!' ) );
$this->assertCount( 2, $this->wpbp->get_batches() );
$updated_batch = $this->executeWPBPMethod( 'get_batch' );
$this->assertNotEquals( $first_batch, $updated_batch, 'fetched updated batch different to 1st fetch' );
$this->assertEquals( array( 'Wibble wobble all day long!' ), $updated_batch->data, 'fetched updated batch has expected data' );
}
/**
* Test maybe_handle when cancelling.
*
* @return void
*/
public function test_maybe_handle_cancelled() {
// Cancelled status results in cleared batches and action fired.
$cancelled_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
$cancelled_fired = true;
} );
// Paused action should not be fired though.
$paused_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
$paused_fired = true;
} );
// Completed action should not be fired though.
$completed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
$completed_fired = true;
} );
add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->wpbp->push_to_queue( 'wobble' );
$this->wpbp->save();
$this->assertCount( 2, $this->wpbp->get_batches() );
update_site_option( $this->executeWPBPMethod( 'get_status_key' ), WP_Background_Process::STATUS_CANCELLED );
$this->assertTrue( $this->wpbp->is_cancelled(), 'is_cancelled' );
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
$this->wpbp->maybe_handle();
$this->assertCount( 0, $this->wpbp->get_batches() );
$this->assertTrue( $cancelled_fired, 'cancelled action fired' );
$this->assertFalse( $paused_fired, 'paused action still not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
}
/**
* Test maybe_handle when pausing and resuming.
*
* @return void
*/
public function test_maybe_handle_paused_resumed() {
// Cancelled action should not be fired.
$cancelled_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
$cancelled_fired = true;
} );
// Paused action should fire and batches remain intact.
$paused_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
$paused_fired = true;
} );
// Resumed action should fire on resume before batches handled.
$resumed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) {
$resumed_fired = true;
} );
// Completed action should fire after batches handled.
$completed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
$completed_fired = true;
} );
add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->wpbp->push_to_queue( 'wobble' );
$this->wpbp->save();
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->wpbp->pause();
$this->assertTrue( $this->wpbp->is_paused(), 'is_paused' );
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $resumed_fired, 'resumed action not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
$this->wpbp->maybe_handle();
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
$this->assertTrue( $paused_fired, 'paused action fired' );
$this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
// Reset for resume and ensure dispatch does nothing to that maybe_handle can be monitored.
$paused_fired = false;
add_filter( 'pre_http_request', '__return_true' );
$this->wpbp->resume();
remove_filter( 'pre_http_request', '__return_true' );
$this->assertFalse( $this->wpbp->is_paused(), 'not is_paused after resume' );
$this->assertCount( 2, $this->wpbp->get_batches() );
$this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertTrue( $resumed_fired, 'resumed action fired' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
// Don't expect resumed to be fired again, and batches to be handled with valid security.
$resumed_fired = false;
$_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
$this->wpbp->maybe_handle();
$this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' );
$this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
$this->assertTrue( $completed_fired, 'completed action fired' );
}
/**
* Test maybe_handle when handling a single batch.
*
* @return void
*/
public function test_maybe_handle_single_batch() {
// Cancelled action should not be fired.
$cancelled_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
$cancelled_fired = true;
} );
// Paused action should not be fired.
$paused_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
$paused_fired = true;
} );
// Resumed action should not be fired.
$resumed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) {
$resumed_fired = true;
} );
// Completed action should fire after batches handled.
$completed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
$completed_fired = true;
} );
add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $resumed_fired, 'resumed action not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
$_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
$this->wpbp->maybe_handle();
$this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' );
$this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
$this->assertTrue( $completed_fired, 'completed action fired' );
}
/**
* Test maybe_handle when handling nothing.
*
* @return void
*/
public function test_maybe_handle_nothing() {
// Cancelled action should not be fired.
$cancelled_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
$cancelled_fired = true;
} );
// Paused action should not be fired.
$paused_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
$paused_fired = true;
} );
// Resumed action should not be fired.
$resumed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) {
$resumed_fired = true;
} );
// Completed action should not be fired.
$completed_fired = false;
add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
$completed_fired = true;
} );
add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
$this->assertCount( 0, $this->wpbp->get_batches() );
$this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $resumed_fired, 'resumed action not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
$this->wpbp->maybe_handle();
$this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' );
$this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
$this->assertFalse( $paused_fired, 'paused action not fired yet' );
$this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
$this->assertFalse( $completed_fired, 'completed action not fired yet' );
}
/**
* Test is_processing.
*
* @return void
*/
public function test_is_processing() {
$this->assertFalse( $this->wpbp->is_processing(), 'not processing yet' );
$this->executeWPBPMethod( 'lock_process' );
$this->assertTrue( $this->wpbp->is_processing(), 'processing' );
// With batches to be processed, maybe_handle does nothing as "another instance is processing".
add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertCount( 1, $this->wpbp->get_batches() );
$this->wpbp->maybe_handle();
$this->assertCount( 1, $this->wpbp->get_batches() );
// Unlock and maybe_handle can process the batch.
$this->executeWPBPMethod( 'unlock_process' );
$this->assertFalse( $this->wpbp->is_processing(), 'not processing yet' );
$this->assertCount( 1, $this->wpbp->get_batches() );
$_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
$this->wpbp->maybe_handle();
$this->assertCount( 0, $this->wpbp->get_batches() );
$this->assertFalse( $this->wpbp->is_processing(), 'not left processing on complete' );
}
/**
* Test is_queued.
*
* @return void
*/
public function test_is_queued() {
$this->assertFalse( $this->wpbp->is_queued(), 'nothing queued until save' );
$this->wpbp->push_to_queue( 'wibble' );
$this->assertFalse( $this->wpbp->is_queued(), 'nothing queued until save' );
$this->wpbp->save();
$this->assertTrue( $this->wpbp->is_queued(), 'queued items exist' );
$this->wpbp->push_to_queue( 'wobble' );
$this->wpbp->save();
$this->assertTrue( $this->wpbp->is_queued(), 'queued items exist' );
$this->wpbp->delete_all();
$this->assertFalse( $this->wpbp->is_queued(), 'queue emptied' );
}
/**
* Test is_active.
*
* @return void
*/
public function test_is_active() {
$this->assertFalse( $this->wpbp->is_active(), 'not queued, processing, paused or cancelling' );
// Queued.
$this->wpbp->push_to_queue( 'wibble' );
$this->assertFalse( $this->wpbp->is_active(), 'nothing queued until save' );
$this->wpbp->save();
$this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' );
$this->wpbp->delete_all();
$this->assertFalse( $this->wpbp->is_active(), 'queue emptied, so no longer active' );
// Processing.
$this->executeWPBPMethod( 'lock_process' );
$this->assertTrue( $this->wpbp->is_active(), 'processing, so now active' );
$this->executeWPBPMethod( 'unlock_process' );
$this->assertFalse( $this->wpbp->is_active(), 'not processing, so no longer active' );
// Paused.
$this->wpbp->pause();
$this->assertTrue( $this->wpbp->is_active(), 'paused, so now active' );
$this->wpbp->resume();
$this->assertFalse( $this->wpbp->is_active(), 'not paused, nothing queued, so no longer active' );
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' );
$this->wpbp->pause();
$this->assertTrue( $this->wpbp->is_active(), 'paused, so still active' );
add_filter( 'pre_http_request', '__return_true' );
$this->wpbp->resume();
remove_filter( 'pre_http_request', '__return_true' );
$this->assertTrue( $this->wpbp->is_active(), 'resumed but with queued items, so still active' );
$this->wpbp->delete_all();
$this->assertFalse( $this->wpbp->is_active(), 'queue emptied, so no longer active' );
// Cancelled.
add_filter( 'pre_http_request', '__return_true' );
$this->wpbp->cancel();
remove_filter( 'pre_http_request', '__return_true' );
$this->assertTrue( $this->wpbp->is_active(), 'cancelling, so now active' );
add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
$this->wpbp->maybe_handle();
$this->assertFalse( $this->wpbp->is_active(), 'cancel handled, so no longer active' );
$this->wpbp->push_to_queue( 'wibble' );
$this->wpbp->save();
$this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' );
add_filter( 'pre_http_request', '__return_true' );
$this->wpbp->cancel();
remove_filter( 'pre_http_request', '__return_true' );
$this->assertTrue( $this->wpbp->is_active(), 'cancelling, so still active' );
$this->wpbp->maybe_handle();
$this->assertFalse( $this->wpbp->is_active(), 'cancel handled, queue emptied, so no longer active' );
}
/**
* Test get_cron_interval.
*
* @return void
*/
public function test_get_cron_interval() {
// Default value.
$this->assertEquals( 5, $this->wpbp->get_cron_interval() );
// Override via property (usually on subclass).
$this->wpbp->cron_interval = 3;
$this->assertEquals( 3, $this->wpbp->get_cron_interval() );
// Override via filter.
$callback = function ( $interval ) {
return 1;
};
add_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
$this->assertEquals( 1, $this->wpbp->get_cron_interval() );
remove_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
$this->assertEquals( 3, $this->wpbp->get_cron_interval() );
unset( $this->wpbp->cron_interval );
$this->assertEquals( 5, $this->wpbp->get_cron_interval() );
}
}