PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.2
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / deliciousbrains / wp-background-processing / tests / Test_WP_Background_Process.php

Test_WP_Background_Process.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.2, at vendor/deliciousbrains/wp-background-processing/tests/Test_WP_Background_Process.php

680 lines 25.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Unit tests for Imagify_WP_Background_Process.
4 *
5 * @package WP-Background-Processing
6 */
7
8 require_once __DIR__ . '/fixtures/Test_Batch_Data.php';
9
10 use PHPUnit\Framework\MockObject\MockObject;
11
12 /**
13 * Class Test_WP_Background_Process
14 */
15 class Test_WP_Background_Process extends WP_UnitTestCase {
16 /**
17 * Instance of Imagify_WP_Background_Process
18 *
19 * @var MockObject|Imagify_WP_Background_Process|(Imagify_WP_Background_Process&MockObject)
20 */
21 private $wpbp;
22
23 /**
24 * Performs set up before each test.
25 *
26 * @return void
27 */
28 public function setUp(): void {
29 parent::setUp();
30
31 $this->wpbp = $this->getMockForAbstractClass( Imagify_WP_Background_Process::class );
32
33 $this->wpbp->expects( $this->any() )
34 ->method( 'task' )
35 ->will( $this->returnValue( false ) );
36 }
37
38 /**
39 * Get a property value from WPBP regardless of accessibility.
40 *
41 * @param string $name
42 *
43 * @return mixed
44 */
45 private function getWPBPProperty( string $name ) {
46 try {
47 $property = new ReflectionProperty( 'Imagify_WP_Background_Process', $name );
48 } catch ( Exception $e ) {
49 return new WP_Error( $e->getCode(), $e->getMessage() );
50 }
51 $property->setAccessible( true );
52
53 return $property->getValue( $this->wpbp );
54 }
55
56 /**
57 * Set a property value on WPBP regardless of accessibility.
58 *
59 * @param string $name
60 * @param mixed $value
61 *
62 * @return mixed
63 */
64 private function setWPBPProperty( string $name, $value ) {
65 try {
66 $property = new ReflectionProperty( 'Imagify_WP_Background_Process', $name );
67 } catch ( Exception $e ) {
68 return new WP_Error( $e->getCode(), $e->getMessage() );
69 }
70 $property->setAccessible( true );
71
72 return $property->setValue( $this->wpbp, $value );
73 }
74
75 /**
76 * Execute a method of WPBP regardless of accessibility.
77 *
78 * @param string $name Method name.
79 * @param mixed $args None, one or more args to pass to method.
80 *
81 * @return mixed
82 */
83 private function executeWPBPMethod( string $name, ...$args ) {
84 try {
85 $method = new ReflectionMethod( 'Imagify_WP_Background_Process', $name );
86 $method->setAccessible( true );
87
88 return $method->invoke( $this->wpbp, ...$args );
89 } catch ( Exception $e ) {
90 return new WP_Error( $e->getCode(), $e->getMessage() );
91 }
92 }
93
94 /**
95 * Test push_to_queue.
96 *
97 * @return void
98 */
99 public function test_push_to_queue() {
100 $this->assertClassHasAttribute( 'data', 'Imagify_WP_Background_Process', 'class has data property' );
101 $this->assertEmpty( $this->getWPBPProperty( 'data' ) );
102
103 $this->wpbp->push_to_queue( 'wibble' );
104 $this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
105 $this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
106
107 $this->wpbp->push_to_queue( 'wobble' );
108 $this->assertEquals( array( 'wibble', 'wobble' ), $this->getWPBPProperty( 'data' ) );
109 }
110
111 /**
112 * Test save.
113 *
114 * @return void
115 */
116 public function test_save() {
117 $this->assertClassHasAttribute( 'data', 'Imagify_WP_Background_Process', 'class has data property' );
118 $this->assertEmpty( $this->getWPBPProperty( 'data' ) );
119 $this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
120
121 $this->wpbp->push_to_queue( 'wibble' );
122 $this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
123 $this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
124 $this->wpbp->save();
125 $this->assertEmpty( $this->getWPBPProperty( 'data' ), 'data emptied after save' );
126 $this->assertNotEmpty( $this->wpbp->get_batches(), 'batches exist after save' );
127 }
128
129 /**
130 * Test get_batches.
131 *
132 * @return void
133 */
134 public function test_get_batches() {
135 $this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
136
137 $this->wpbp->push_to_queue( 'wibble' );
138 $this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
139 $this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
140 $this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
141
142 $this->wpbp->push_to_queue( 'wobble' );
143 $this->assertEquals( array( 'wibble', 'wobble' ), $this->getWPBPProperty( 'data' ) );
144 $this->assertEmpty( $this->wpbp->get_batches(), 'no batches until save' );
145
146 $this->wpbp->save();
147 $first_batch = $this->wpbp->get_batches();
148 $this->assertNotEmpty( $first_batch );
149 $this->assertCount( 1, $first_batch );
150
151 $this->wpbp->push_to_queue( 'more wibble' );
152 $this->wpbp->save();
153 $this->assertCount( 2, $this->wpbp->get_batches() );
154
155 $this->wpbp->push_to_queue( 'Wibble wobble all day long.' );
156 $this->wpbp->save();
157 $this->assertCount( 3, $this->wpbp->get_batches() );
158
159 $this->assertEquals( $first_batch, $this->wpbp->get_batches( 1 ) );
160 $this->assertNotEquals( $first_batch, $this->wpbp->get_batches( 2 ) );
161 $this->assertCount( 2, $this->wpbp->get_batches( 2 ) );
162 $this->assertCount( 3, $this->wpbp->get_batches( 3 ) );
163 $this->assertCount( 3, $this->wpbp->get_batches( 5 ) );
164 }
165
166 /**
167 * Test get_batch.
168 *
169 * @return void
170 */
171 public function test_get_batch() {
172 $this->assertEmpty( $this->executeWPBPMethod( 'get_batch' ), 'no batches until save' );
173
174 $this->wpbp->push_to_queue( 'wibble' );
175 $this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
176 $this->assertEquals( array( 'wibble' ), $this->getWPBPProperty( 'data' ) );
177 $this->assertEmpty( $this->executeWPBPMethod( 'get_batch' ), 'no batches until save' );
178
179 $this->wpbp->push_to_queue( 'wobble' );
180 $this->assertEquals( array( 'wibble', 'wobble' ), $this->getWPBPProperty( 'data' ) );
181 $this->assertEmpty( $this->executeWPBPMethod( 'get_batch' ), 'no batches until save' );
182
183 $this->wpbp->save();
184 $first_batch = $this->executeWPBPMethod( 'get_batch' );
185 $this->assertNotEmpty( $first_batch );
186 $this->assertInstanceOf( 'stdClass', $first_batch );
187 $this->assertEquals( array( 'wibble', 'wobble' ), $first_batch->data );
188
189 $this->wpbp->push_to_queue( 'more wibble' );
190 $this->wpbp->save();
191 $second_batch = $this->executeWPBPMethod( 'get_batch' );
192 $this->assertNotEmpty( $second_batch );
193 $this->assertInstanceOf( 'stdClass', $second_batch );
194 $this->assertEquals( $first_batch, $second_batch, 'same 1st batch returned until deleted' );
195
196 $this->wpbp->delete( $first_batch->key );
197 $second_batch = $this->executeWPBPMethod( 'get_batch' );
198 $this->assertNotEmpty( $second_batch );
199 $this->assertInstanceOf( 'stdClass', $second_batch );
200 $this->assertNotEquals( $first_batch, $second_batch, '2nd batch returned as 1st deleted' );
201 $this->assertEquals( array( 'more wibble' ), $second_batch->data );
202
203 // Tests using a custom class for the $item.
204 $this->wpbp->delete( $second_batch->key );
205 $batch_data_object = new Test_Batch_Data();
206 $this->wpbp->push_to_queue( $batch_data_object );
207 $this->assertNotEmpty( $this->getWPBPProperty( 'data' ) );
208 $this->assertEquals( array( $batch_data_object ), $this->getWPBPProperty( 'data' ) );
209 $this->wpbp->save();
210 $third_batch = $this->executeWPBPMethod( 'get_batch' );
211 $this->assertCount( 1, $third_batch->data );
212 $this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
213
214 // Explicitly set allowed classes to Test_Batch_Data.
215 $this->setWPBPProperty( 'allowed_batch_data_classes', array( Test_Batch_Data::class ) );
216 $third_batch = $this->executeWPBPMethod( 'get_batch' );
217 $this->assertCount( 1, $third_batch->data );
218 $this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
219
220 // Allow a different class.
221 $this->setWPBPProperty( 'allowed_batch_data_classes', array( stdClass::class ) );
222 $third_batch = $this->executeWPBPMethod( 'get_batch' );
223 $this->assertCount( 1, $third_batch->data );
224 $this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] );
225
226 // Disallow all classes.
227 $this->setWPBPProperty( 'allowed_batch_data_classes', false );
228 $third_batch = $this->executeWPBPMethod( 'get_batch' );
229 $this->assertCount( 1, $third_batch->data );
230 $this->assertInstanceOf( __PHP_Incomplete_Class::class, $third_batch->data[0] );
231
232 // Allow everything.
233 $this->setWPBPProperty( 'allowed_batch_data_classes', true );
234 $third_batch = $this->executeWPBPMethod( 'get_batch' );
235 $this->assertCount( 1, $third_batch->data );
236 $this->assertInstanceOf( Test_Batch_Data::class, $third_batch->data[0] );
237 }
238
239 /**
240 * Test cancel.
241 *
242 * @return void
243 */
244 public function test_cancel() {
245 $this->wpbp->push_to_queue( 'wibble' );
246 $this->wpbp->save();
247 $this->assertFalse( $this->wpbp->is_cancelled() );
248 $this->wpbp->cancel();
249 $this->assertTrue( $this->wpbp->is_cancelled() );
250 }
251
252 /**
253 * Test pause.
254 *
255 * @return void
256 */
257 public function test_pause() {
258 $this->wpbp->push_to_queue( 'wibble' );
259 $this->wpbp->save();
260 $this->assertFalse( $this->wpbp->is_paused() );
261 $this->wpbp->pause();
262 $this->assertTrue( $this->wpbp->is_paused() );
263 }
264
265 /**
266 * Test resume.
267 *
268 * @return void
269 */
270 public function test_resume() {
271 $this->wpbp->push_to_queue( 'wibble' );
272 $this->wpbp->save();
273 $this->assertFalse( $this->wpbp->is_paused() );
274 $this->wpbp->pause();
275 $this->assertTrue( $this->wpbp->is_paused() );
276 $this->wpbp->resume();
277 $this->assertFalse( $this->wpbp->is_paused() );
278 }
279
280 /**
281 * Test delete.
282 *
283 * @return void
284 */
285 public function test_delete() {
286 $this->wpbp->push_to_queue( 'wibble' );
287 $this->wpbp->save();
288 $this->assertCount( 1, $this->wpbp->get_batches() );
289 $this->wpbp->push_to_queue( 'wobble' );
290 $this->wpbp->save();
291 $this->assertCount( 2, $this->wpbp->get_batches() );
292 $first_batch = $this->executeWPBPMethod( 'get_batch' );
293 $this->wpbp->delete( $first_batch->key );
294 $this->assertCount( 1, $this->wpbp->get_batches() );
295 $second_batch = $this->executeWPBPMethod( 'get_batch' );
296 $this->assertNotEquals( $first_batch, $second_batch, '2nd batch returned as 1st deleted' );
297 }
298
299 /**
300 * Test delete_all.
301 *
302 * @return void
303 */
304 public function test_delete_all() {
305 $this->wpbp->push_to_queue( 'wibble' );
306 $this->wpbp->save();
307 $this->assertCount( 1, $this->wpbp->get_batches() );
308 $this->wpbp->push_to_queue( 'wobble' );
309 $this->wpbp->save();
310 $this->assertCount( 2, $this->wpbp->get_batches() );
311 $this->wpbp->delete_all();
312 $this->assertCount( 0, $this->wpbp->get_batches() );
313 }
314
315 /**
316 * Test update.
317 *
318 * @return void
319 */
320 public function test_update() {
321 $this->wpbp->push_to_queue( 'wibble' );
322 $this->wpbp->save();
323 $this->assertCount( 1, $this->wpbp->get_batches() );
324 $this->wpbp->push_to_queue( 'wobble' );
325 $this->wpbp->save();
326 $this->assertCount( 2, $this->wpbp->get_batches() );
327 $first_batch = $this->executeWPBPMethod( 'get_batch' );
328 $this->wpbp->update( $first_batch->key, array( 'Wibble wobble all day long!' ) );
329 $this->assertCount( 2, $this->wpbp->get_batches() );
330 $updated_batch = $this->executeWPBPMethod( 'get_batch' );
331 $this->assertNotEquals( $first_batch, $updated_batch, 'fetched updated batch different to 1st fetch' );
332 $this->assertEquals(
333 array( 'Wibble wobble all day long!' ),
334 $updated_batch->data,
335 'fetched updated batch has expected data'
336 );
337 }
338
339 /**
340 * Test maybe_handle when cancelling.
341 *
342 * @return void
343 */
344 public function test_maybe_handle_cancelled() {
345 // Cancelled status results in cleared batches and action fired.
346 $cancelled_fired = false;
347 add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
348 $cancelled_fired = true;
349 } );
350 // Paused action should not be fired though.
351 $paused_fired = false;
352 add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
353 $paused_fired = true;
354 } );
355 // Completed action should not be fired though.
356 $completed_fired = false;
357 add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
358 $completed_fired = true;
359 } );
360 add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
361 $this->wpbp->push_to_queue( 'wibble' );
362 $this->wpbp->save();
363 $this->assertCount( 1, $this->wpbp->get_batches() );
364 $this->wpbp->push_to_queue( 'wobble' );
365 $this->wpbp->save();
366 $this->assertCount( 2, $this->wpbp->get_batches() );
367 update_site_option( $this->executeWPBPMethod( 'get_status_key' ), Imagify_WP_Background_Process::STATUS_CANCELLED );
368 $this->assertTrue( $this->wpbp->is_cancelled(), 'is_cancelled' );
369 $this->assertCount( 2, $this->wpbp->get_batches() );
370 $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
371 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
372 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
373 $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
374 $this->wpbp->maybe_handle();
375 $this->assertCount( 0, $this->wpbp->get_batches() );
376 $this->assertTrue( $cancelled_fired, 'cancelled action fired' );
377 $this->assertFalse( $paused_fired, 'paused action still not fired yet' );
378 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
379 }
380
381 /**
382 * Test maybe_handle when pausing and resuming.
383 *
384 * @return void
385 */
386 public function test_maybe_handle_paused_resumed() {
387 // Cancelled action should not be fired.
388 $cancelled_fired = false;
389 add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
390 $cancelled_fired = true;
391 } );
392 // Paused action should fire and batches remain intact.
393 $paused_fired = false;
394 add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
395 $paused_fired = true;
396 } );
397 // Resumed action should fire on resume before batches handled.
398 $resumed_fired = false;
399 add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) {
400 $resumed_fired = true;
401 } );
402 // Completed action should fire after batches handled.
403 $completed_fired = false;
404 add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
405 $completed_fired = true;
406 } );
407 add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
408 $this->wpbp->push_to_queue( 'wibble' );
409 $this->wpbp->save();
410 $this->assertCount( 1, $this->wpbp->get_batches() );
411 $this->wpbp->push_to_queue( 'wobble' );
412 $this->wpbp->save();
413 $this->assertCount( 2, $this->wpbp->get_batches() );
414 $this->wpbp->pause();
415 $this->assertTrue( $this->wpbp->is_paused(), 'is_paused' );
416 $this->assertCount( 2, $this->wpbp->get_batches() );
417 $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
418 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
419 $this->assertFalse( $resumed_fired, 'resumed action not fired yet' );
420 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
421 $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
422 $this->wpbp->maybe_handle();
423 $this->assertCount( 2, $this->wpbp->get_batches() );
424 $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
425 $this->assertTrue( $paused_fired, 'paused action fired' );
426 $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
427 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
428
429 // Reset for resume and ensure dispatch does nothing to that maybe_handle can be monitored.
430 $paused_fired = false;
431 add_filter( 'pre_http_request', '__return_true' );
432 $this->wpbp->resume();
433 remove_filter( 'pre_http_request', '__return_true' );
434 $this->assertFalse( $this->wpbp->is_paused(), 'not is_paused after resume' );
435 $this->assertCount( 2, $this->wpbp->get_batches() );
436 $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
437 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
438 $this->assertTrue( $resumed_fired, 'resumed action fired' );
439 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
440
441 // Don't expect resumed to be fired again, and batches to be handled with valid security.
442 $resumed_fired = false;
443 $this->wpbp->maybe_handle();
444 $this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' );
445 $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
446 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
447 $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
448 $this->assertTrue( $completed_fired, 'completed action fired' );
449 }
450
451 /**
452 * Test maybe_handle when handling a single batch.
453 *
454 * @return void
455 */
456 public function test_maybe_handle_single_batch() {
457 // Cancelled action should not be fired.
458 $cancelled_fired = false;
459 add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
460 $cancelled_fired = true;
461 } );
462 // Paused action should not be fired.
463 $paused_fired = false;
464 add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
465 $paused_fired = true;
466 } );
467 // Resumed action should not be fired.
468 $resumed_fired = false;
469 add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) {
470 $resumed_fired = true;
471 } );
472 // Completed action should fire after batches handled.
473 $completed_fired = false;
474 add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
475 $completed_fired = true;
476 } );
477 add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
478 $this->wpbp->push_to_queue( 'wibble' );
479 $this->wpbp->save();
480 $this->assertCount( 1, $this->wpbp->get_batches() );
481 $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
482 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
483 $this->assertFalse( $resumed_fired, 'resumed action not fired yet' );
484 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
485
486 $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
487 $this->wpbp->maybe_handle();
488 $this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' );
489 $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
490 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
491 $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
492 $this->assertTrue( $completed_fired, 'completed action fired' );
493 }
494
495 /**
496 * Test maybe_handle when handling nothing.
497 *
498 * @return void
499 */
500 public function test_maybe_handle_nothing() {
501 // Cancelled action should not be fired.
502 $cancelled_fired = false;
503 add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) {
504 $cancelled_fired = true;
505 } );
506 // Paused action should not be fired.
507 $paused_fired = false;
508 add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) {
509 $paused_fired = true;
510 } );
511 // Resumed action should not be fired.
512 $resumed_fired = false;
513 add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) {
514 $resumed_fired = true;
515 } );
516 // Completed action should not be fired.
517 $completed_fired = false;
518 add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) {
519 $completed_fired = true;
520 } );
521 add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
522 $this->assertCount( 0, $this->wpbp->get_batches() );
523 $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' );
524 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
525 $this->assertFalse( $resumed_fired, 'resumed action not fired yet' );
526 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
527
528 $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
529 $this->wpbp->maybe_handle();
530 $this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' );
531 $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' );
532 $this->assertFalse( $paused_fired, 'paused action not fired yet' );
533 $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' );
534 $this->assertFalse( $completed_fired, 'completed action not fired yet' );
535 }
536
537 /**
538 * Test is_processing.
539 *
540 * @return void
541 */
542 public function test_is_processing() {
543 $this->assertFalse( $this->wpbp->is_processing(), 'not processing yet' );
544 $this->executeWPBPMethod( 'lock_process' );
545 $this->assertTrue( $this->wpbp->is_processing(), 'processing' );
546
547 // With batches to be processed, maybe_handle does nothing as "another instance is processing".
548 add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
549 $this->wpbp->push_to_queue( 'wibble' );
550 $this->wpbp->save();
551 $this->assertCount( 1, $this->wpbp->get_batches() );
552 $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
553 $this->wpbp->maybe_handle();
554 $this->assertCount( 1, $this->wpbp->get_batches() );
555
556 // Unlock and maybe_handle can process the batch.
557 $this->executeWPBPMethod( 'unlock_process' );
558 $this->assertFalse( $this->wpbp->is_processing(), 'not processing yet' );
559 $this->assertCount( 1, $this->wpbp->get_batches() );
560 $this->wpbp->maybe_handle();
561 $this->assertCount( 0, $this->wpbp->get_batches() );
562 $this->assertFalse( $this->wpbp->is_processing(), 'not left processing on complete' );
563 }
564
565 /**
566 * Test is_queued.
567 *
568 * @return void
569 */
570 public function test_is_queued() {
571 $this->assertFalse( $this->wpbp->is_queued(), 'nothing queued until save' );
572
573 $this->wpbp->push_to_queue( 'wibble' );
574 $this->assertFalse( $this->wpbp->is_queued(), 'nothing queued until save' );
575
576 $this->wpbp->save();
577 $this->assertTrue( $this->wpbp->is_queued(), 'queued items exist' );
578
579 $this->wpbp->push_to_queue( 'wobble' );
580 $this->wpbp->save();
581 $this->assertTrue( $this->wpbp->is_queued(), 'queued items exist' );
582
583 $this->wpbp->delete_all();
584 $this->assertFalse( $this->wpbp->is_queued(), 'queue emptied' );
585 }
586
587 /**
588 * Test is_active.
589 *
590 * @return void
591 */
592 public function test_is_active() {
593 $this->assertFalse( $this->wpbp->is_active(), 'not queued, processing, paused or cancelling' );
594
595 // Queued.
596 $this->wpbp->push_to_queue( 'wibble' );
597 $this->assertFalse( $this->wpbp->is_active(), 'nothing queued until save' );
598
599 $this->wpbp->save();
600 $this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' );
601
602 $this->wpbp->delete_all();
603 $this->assertFalse( $this->wpbp->is_active(), 'queue emptied, so no longer active' );
604
605 // Processing.
606 $this->executeWPBPMethod( 'lock_process' );
607 $this->assertTrue( $this->wpbp->is_active(), 'processing, so now active' );
608
609 $this->executeWPBPMethod( 'unlock_process' );
610 $this->assertFalse( $this->wpbp->is_active(), 'not processing, so no longer active' );
611
612 // Paused.
613 $this->wpbp->pause();
614 $this->assertTrue( $this->wpbp->is_active(), 'paused, so now active' );
615
616 $this->wpbp->resume();
617 $this->assertFalse( $this->wpbp->is_active(), 'not paused, nothing queued, so no longer active' );
618
619 $this->wpbp->push_to_queue( 'wibble' );
620 $this->wpbp->save();
621 $this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' );
622 $this->wpbp->pause();
623 $this->assertTrue( $this->wpbp->is_active(), 'paused, so still active' );
624 add_filter( 'pre_http_request', '__return_true' );
625 $this->wpbp->resume();
626 remove_filter( 'pre_http_request', '__return_true' );
627 $this->assertTrue( $this->wpbp->is_active(), 'resumed but with queued items, so still active' );
628 $this->wpbp->delete_all();
629 $this->assertFalse( $this->wpbp->is_active(), 'queue emptied, so no longer active' );
630
631 // Cancelled.
632 add_filter( 'pre_http_request', '__return_true' );
633 $this->wpbp->cancel();
634 remove_filter( 'pre_http_request', '__return_true' );
635 $this->assertTrue( $this->wpbp->is_active(), 'cancelling, so now active' );
636
637 add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' );
638 $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) );
639 $this->wpbp->maybe_handle();
640 $this->assertFalse( $this->wpbp->is_active(), 'cancel handled, so no longer active' );
641
642 $this->wpbp->push_to_queue( 'wibble' );
643 $this->wpbp->save();
644 $this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' );
645 add_filter( 'pre_http_request', '__return_true' );
646 $this->wpbp->cancel();
647 remove_filter( 'pre_http_request', '__return_true' );
648 $this->assertTrue( $this->wpbp->is_active(), 'cancelling, so still active' );
649 $this->wpbp->maybe_handle();
650 $this->assertFalse( $this->wpbp->is_active(), 'cancel handled, queue emptied, so no longer active' );
651 }
652
653 /**
654 * Test get_cron_interval.
655 *
656 * @return void
657 */
658 public function test_get_cron_interval() {
659 // Default value.
660 $this->assertEquals( 5, $this->wpbp->get_cron_interval() );
661
662 // Override via property (usually on subclass).
663 $this->wpbp->cron_interval = 3;
664 $this->assertEquals( 3, $this->wpbp->get_cron_interval() );
665
666 // Override via filter.
667 $callback = function ( $interval ) {
668 return 1;
669 };
670 add_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
671 $this->assertEquals( 1, $this->wpbp->get_cron_interval() );
672
673 remove_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback );
674 $this->assertEquals( 3, $this->wpbp->get_cron_interval() );
675
676 unset( $this->wpbp->cron_interval );
677 $this->assertEquals( 5, $this->wpbp->get_cron_interval() );
678 }
679 }
680