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.2.8, at vendor/deliciousbrains/wp-background-processing/tests/Test_WP_Background_Process.php
| 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( array( 'Wibble wobble all day long!' ), $updated_batch->data, 'fetched updated batch has expected data' ); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Test maybe_handle when cancelling. |
| 337 | * |
| 338 | * @return void |
| 339 | */ |
| 340 | public function test_maybe_handle_cancelled() { |
| 341 | // Cancelled status results in cleared batches and action fired. |
| 342 | $cancelled_fired = false; |
| 343 | add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) { |
| 344 | $cancelled_fired = true; |
| 345 | } ); |
| 346 | // Paused action should not be fired though. |
| 347 | $paused_fired = false; |
| 348 | add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) { |
| 349 | $paused_fired = true; |
| 350 | } ); |
| 351 | // Completed action should not be fired though. |
| 352 | $completed_fired = false; |
| 353 | add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) { |
| 354 | $completed_fired = true; |
| 355 | } ); |
| 356 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' ); |
| 357 | $this->wpbp->push_to_queue( 'wibble' ); |
| 358 | $this->wpbp->save(); |
| 359 | $this->assertCount( 1, $this->wpbp->get_batches() ); |
| 360 | $this->wpbp->push_to_queue( 'wobble' ); |
| 361 | $this->wpbp->save(); |
| 362 | $this->assertCount( 2, $this->wpbp->get_batches() ); |
| 363 | update_site_option( $this->executeWPBPMethod( 'get_status_key' ), Imagify_WP_Background_Process::STATUS_CANCELLED ); |
| 364 | $this->assertTrue( $this->wpbp->is_cancelled(), 'is_cancelled' ); |
| 365 | $this->assertCount( 2, $this->wpbp->get_batches() ); |
| 366 | $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' ); |
| 367 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 368 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 369 | $this->wpbp->maybe_handle(); |
| 370 | $this->assertCount( 0, $this->wpbp->get_batches() ); |
| 371 | $this->assertTrue( $cancelled_fired, 'cancelled action fired' ); |
| 372 | $this->assertFalse( $paused_fired, 'paused action still not fired yet' ); |
| 373 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Test maybe_handle when pausing and resuming. |
| 378 | * |
| 379 | * @return void |
| 380 | */ |
| 381 | public function test_maybe_handle_paused_resumed() { |
| 382 | // Cancelled action should not be fired. |
| 383 | $cancelled_fired = false; |
| 384 | add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) { |
| 385 | $cancelled_fired = true; |
| 386 | } ); |
| 387 | // Paused action should fire and batches remain intact. |
| 388 | $paused_fired = false; |
| 389 | add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) { |
| 390 | $paused_fired = true; |
| 391 | } ); |
| 392 | // Resumed action should fire on resume before batches handled. |
| 393 | $resumed_fired = false; |
| 394 | add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) { |
| 395 | $resumed_fired = true; |
| 396 | } ); |
| 397 | // Completed action should fire after batches handled. |
| 398 | $completed_fired = false; |
| 399 | add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) { |
| 400 | $completed_fired = true; |
| 401 | } ); |
| 402 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' ); |
| 403 | $this->wpbp->push_to_queue( 'wibble' ); |
| 404 | $this->wpbp->save(); |
| 405 | $this->assertCount( 1, $this->wpbp->get_batches() ); |
| 406 | $this->wpbp->push_to_queue( 'wobble' ); |
| 407 | $this->wpbp->save(); |
| 408 | $this->assertCount( 2, $this->wpbp->get_batches() ); |
| 409 | $this->wpbp->pause(); |
| 410 | $this->assertTrue( $this->wpbp->is_paused(), 'is_paused' ); |
| 411 | $this->assertCount( 2, $this->wpbp->get_batches() ); |
| 412 | $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' ); |
| 413 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 414 | $this->assertFalse( $resumed_fired, 'resumed action not fired yet' ); |
| 415 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 416 | $this->wpbp->maybe_handle(); |
| 417 | $this->assertCount( 2, $this->wpbp->get_batches() ); |
| 418 | $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' ); |
| 419 | $this->assertTrue( $paused_fired, 'paused action fired' ); |
| 420 | $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' ); |
| 421 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 422 | |
| 423 | // Reset for resume and ensure dispatch does nothing to that maybe_handle can be monitored. |
| 424 | $paused_fired = false; |
| 425 | add_filter( 'pre_http_request', '__return_true' ); |
| 426 | $this->wpbp->resume(); |
| 427 | remove_filter( 'pre_http_request', '__return_true' ); |
| 428 | $this->assertFalse( $this->wpbp->is_paused(), 'not is_paused after resume' ); |
| 429 | $this->assertCount( 2, $this->wpbp->get_batches() ); |
| 430 | $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' ); |
| 431 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 432 | $this->assertTrue( $resumed_fired, 'resumed action fired' ); |
| 433 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 434 | |
| 435 | // Don't expect resumed to be fired again, and batches to be handled with valid security. |
| 436 | $resumed_fired = false; |
| 437 | $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) ); |
| 438 | $this->wpbp->maybe_handle(); |
| 439 | $this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' ); |
| 440 | $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' ); |
| 441 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 442 | $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' ); |
| 443 | $this->assertTrue( $completed_fired, 'completed action fired' ); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Test maybe_handle when handling a single batch. |
| 448 | * |
| 449 | * @return void |
| 450 | */ |
| 451 | public function test_maybe_handle_single_batch() { |
| 452 | // Cancelled action should not be fired. |
| 453 | $cancelled_fired = false; |
| 454 | add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) { |
| 455 | $cancelled_fired = true; |
| 456 | } ); |
| 457 | // Paused action should not be fired. |
| 458 | $paused_fired = false; |
| 459 | add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) { |
| 460 | $paused_fired = true; |
| 461 | } ); |
| 462 | // Resumed action should not be fired. |
| 463 | $resumed_fired = false; |
| 464 | add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) { |
| 465 | $resumed_fired = true; |
| 466 | } ); |
| 467 | // Completed action should fire after batches handled. |
| 468 | $completed_fired = false; |
| 469 | add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) { |
| 470 | $completed_fired = true; |
| 471 | } ); |
| 472 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' ); |
| 473 | $this->wpbp->push_to_queue( 'wibble' ); |
| 474 | $this->wpbp->save(); |
| 475 | $this->assertCount( 1, $this->wpbp->get_batches() ); |
| 476 | $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' ); |
| 477 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 478 | $this->assertFalse( $resumed_fired, 'resumed action not fired yet' ); |
| 479 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 480 | |
| 481 | $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) ); |
| 482 | $this->wpbp->maybe_handle(); |
| 483 | $this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' ); |
| 484 | $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' ); |
| 485 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 486 | $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' ); |
| 487 | $this->assertTrue( $completed_fired, 'completed action fired' ); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Test maybe_handle when handling nothing. |
| 492 | * |
| 493 | * @return void |
| 494 | */ |
| 495 | public function test_maybe_handle_nothing() { |
| 496 | // Cancelled action should not be fired. |
| 497 | $cancelled_fired = false; |
| 498 | add_action( $this->getWPBPProperty( 'identifier' ) . '_cancelled', function () use ( &$cancelled_fired ) { |
| 499 | $cancelled_fired = true; |
| 500 | } ); |
| 501 | // Paused action should not be fired. |
| 502 | $paused_fired = false; |
| 503 | add_action( $this->getWPBPProperty( 'identifier' ) . '_paused', function () use ( &$paused_fired ) { |
| 504 | $paused_fired = true; |
| 505 | } ); |
| 506 | // Resumed action should not be fired. |
| 507 | $resumed_fired = false; |
| 508 | add_action( $this->getWPBPProperty( 'identifier' ) . '_resumed', function () use ( &$resumed_fired ) { |
| 509 | $resumed_fired = true; |
| 510 | } ); |
| 511 | // Completed action should not be fired. |
| 512 | $completed_fired = false; |
| 513 | add_action( $this->getWPBPProperty( 'identifier' ) . '_completed', function () use ( &$completed_fired ) { |
| 514 | $completed_fired = true; |
| 515 | } ); |
| 516 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' ); |
| 517 | $this->assertCount( 0, $this->wpbp->get_batches() ); |
| 518 | $this->assertFalse( $cancelled_fired, 'cancelled action not fired yet' ); |
| 519 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 520 | $this->assertFalse( $resumed_fired, 'resumed action not fired yet' ); |
| 521 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 522 | |
| 523 | $this->wpbp->maybe_handle(); |
| 524 | $this->assertCount( 0, $this->wpbp->get_batches(), 'after resume all batches processed with maybe_handle' ); |
| 525 | $this->assertFalse( $cancelled_fired, 'cancelled action still not fired yet' ); |
| 526 | $this->assertFalse( $paused_fired, 'paused action not fired yet' ); |
| 527 | $this->assertFalse( $resumed_fired, 'resumed action still not fired yet' ); |
| 528 | $this->assertFalse( $completed_fired, 'completed action not fired yet' ); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Test is_processing. |
| 533 | * |
| 534 | * @return void |
| 535 | */ |
| 536 | public function test_is_processing() { |
| 537 | $this->assertFalse( $this->wpbp->is_processing(), 'not processing yet' ); |
| 538 | $this->executeWPBPMethod( 'lock_process' ); |
| 539 | $this->assertTrue( $this->wpbp->is_processing(), 'processing' ); |
| 540 | |
| 541 | // With batches to be processed, maybe_handle does nothing as "another instance is processing". |
| 542 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' ); |
| 543 | $this->wpbp->push_to_queue( 'wibble' ); |
| 544 | $this->wpbp->save(); |
| 545 | $this->assertCount( 1, $this->wpbp->get_batches() ); |
| 546 | $this->wpbp->maybe_handle(); |
| 547 | $this->assertCount( 1, $this->wpbp->get_batches() ); |
| 548 | |
| 549 | // Unlock and maybe_handle can process the batch. |
| 550 | $this->executeWPBPMethod( 'unlock_process' ); |
| 551 | $this->assertFalse( $this->wpbp->is_processing(), 'not processing yet' ); |
| 552 | $this->assertCount( 1, $this->wpbp->get_batches() ); |
| 553 | $_REQUEST['nonce'] = wp_create_nonce( $this->getWPBPProperty( 'identifier' ) ); |
| 554 | $this->wpbp->maybe_handle(); |
| 555 | $this->assertCount( 0, $this->wpbp->get_batches() ); |
| 556 | $this->assertFalse( $this->wpbp->is_processing(), 'not left processing on complete' ); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Test is_queued. |
| 561 | * |
| 562 | * @return void |
| 563 | */ |
| 564 | public function test_is_queued() { |
| 565 | $this->assertFalse( $this->wpbp->is_queued(), 'nothing queued until save' ); |
| 566 | |
| 567 | $this->wpbp->push_to_queue( 'wibble' ); |
| 568 | $this->assertFalse( $this->wpbp->is_queued(), 'nothing queued until save' ); |
| 569 | |
| 570 | $this->wpbp->save(); |
| 571 | $this->assertTrue( $this->wpbp->is_queued(), 'queued items exist' ); |
| 572 | |
| 573 | $this->wpbp->push_to_queue( 'wobble' ); |
| 574 | $this->wpbp->save(); |
| 575 | $this->assertTrue( $this->wpbp->is_queued(), 'queued items exist' ); |
| 576 | |
| 577 | $this->wpbp->delete_all(); |
| 578 | $this->assertFalse( $this->wpbp->is_queued(), 'queue emptied' ); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Test is_active. |
| 583 | * |
| 584 | * @return void |
| 585 | */ |
| 586 | public function test_is_active() { |
| 587 | $this->assertFalse( $this->wpbp->is_active(), 'not queued, processing, paused or cancelling' ); |
| 588 | |
| 589 | // Queued. |
| 590 | $this->wpbp->push_to_queue( 'wibble' ); |
| 591 | $this->assertFalse( $this->wpbp->is_active(), 'nothing queued until save' ); |
| 592 | |
| 593 | $this->wpbp->save(); |
| 594 | $this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' ); |
| 595 | |
| 596 | $this->wpbp->delete_all(); |
| 597 | $this->assertFalse( $this->wpbp->is_active(), 'queue emptied, so no longer active' ); |
| 598 | |
| 599 | // Processing. |
| 600 | $this->executeWPBPMethod( 'lock_process' ); |
| 601 | $this->assertTrue( $this->wpbp->is_active(), 'processing, so now active' ); |
| 602 | |
| 603 | $this->executeWPBPMethod( 'unlock_process' ); |
| 604 | $this->assertFalse( $this->wpbp->is_active(), 'not processing, so no longer active' ); |
| 605 | |
| 606 | // Paused. |
| 607 | $this->wpbp->pause(); |
| 608 | $this->assertTrue( $this->wpbp->is_active(), 'paused, so now active' ); |
| 609 | |
| 610 | $this->wpbp->resume(); |
| 611 | $this->assertFalse( $this->wpbp->is_active(), 'not paused, nothing queued, so no longer active' ); |
| 612 | |
| 613 | $this->wpbp->push_to_queue( 'wibble' ); |
| 614 | $this->wpbp->save(); |
| 615 | $this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' ); |
| 616 | $this->wpbp->pause(); |
| 617 | $this->assertTrue( $this->wpbp->is_active(), 'paused, so still active' ); |
| 618 | add_filter( 'pre_http_request', '__return_true' ); |
| 619 | $this->wpbp->resume(); |
| 620 | remove_filter( 'pre_http_request', '__return_true' ); |
| 621 | $this->assertTrue( $this->wpbp->is_active(), 'resumed but with queued items, so still active' ); |
| 622 | $this->wpbp->delete_all(); |
| 623 | $this->assertFalse( $this->wpbp->is_active(), 'queue emptied, so no longer active' ); |
| 624 | |
| 625 | // Cancelled. |
| 626 | add_filter( 'pre_http_request', '__return_true' ); |
| 627 | $this->wpbp->cancel(); |
| 628 | remove_filter( 'pre_http_request', '__return_true' ); |
| 629 | $this->assertTrue( $this->wpbp->is_active(), 'cancelling, so now active' ); |
| 630 | |
| 631 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_wp_die', '__return_false' ); |
| 632 | $this->wpbp->maybe_handle(); |
| 633 | $this->assertFalse( $this->wpbp->is_active(), 'cancel handled, so no longer active' ); |
| 634 | |
| 635 | $this->wpbp->push_to_queue( 'wibble' ); |
| 636 | $this->wpbp->save(); |
| 637 | $this->assertTrue( $this->wpbp->is_active(), 'queued items exist, so now active' ); |
| 638 | add_filter( 'pre_http_request', '__return_true' ); |
| 639 | $this->wpbp->cancel(); |
| 640 | remove_filter( 'pre_http_request', '__return_true' ); |
| 641 | $this->assertTrue( $this->wpbp->is_active(), 'cancelling, so still active' ); |
| 642 | $this->wpbp->maybe_handle(); |
| 643 | $this->assertFalse( $this->wpbp->is_active(), 'cancel handled, queue emptied, so no longer active' ); |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Test get_cron_interval. |
| 648 | * |
| 649 | * @return void |
| 650 | */ |
| 651 | public function test_get_cron_interval() { |
| 652 | // Default value. |
| 653 | $this->assertEquals( 5, $this->wpbp->get_cron_interval() ); |
| 654 | |
| 655 | // Override via property (usually on subclass). |
| 656 | $this->wpbp->cron_interval = 3; |
| 657 | $this->assertEquals( 3, $this->wpbp->get_cron_interval() ); |
| 658 | |
| 659 | // Override via filter. |
| 660 | $callback = function ( $interval ) { |
| 661 | return 1; |
| 662 | }; |
| 663 | add_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback ); |
| 664 | $this->assertEquals( 1, $this->wpbp->get_cron_interval() ); |
| 665 | |
| 666 | remove_filter( $this->getWPBPProperty( 'identifier' ) . '_cron_interval', $callback ); |
| 667 | $this->assertEquals( 3, $this->wpbp->get_cron_interval() ); |
| 668 | |
| 669 | unset( $this->wpbp->cron_interval ); |
| 670 | $this->assertEquals( 5, $this->wpbp->get_cron_interval() ); |
| 671 | } |
| 672 | } |
| 673 |