Changeset 53804
- Timestamp:
- 07/31/2022 03:03:46 PM (2 years ago)
- Location:
- trunk/tests/phpunit/tests
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/actions.php
r52389 r53804 12 12 */ 13 13 public function test_simple_action() { 14 $a = new MockAction();15 $ tag= __FUNCTION__;16 17 add_action( $ tag, array( &$a, 'action' ) );18 do_action( $ tag);14 $a = new MockAction(); 15 $hook_name = __FUNCTION__; 16 17 add_action( $hook_name, array( &$a, 'action' ) ); 18 do_action( $hook_name ); 19 19 20 20 // Only one event occurred for the hook, with empty args. 21 21 $this->assertSame( 1, $a->get_call_count() ); 22 22 // Only our hook was called. 23 $this->assertSame( array( $ tag), $a->get_tags() );23 $this->assertSame( array( $hook_name ), $a->get_tags() ); 24 24 25 25 $argsvar = $a->get_args(); … … 32 32 */ 33 33 public function test_remove_action() { 34 $a = new MockAction();35 $ tag= __FUNCTION__;36 37 add_action( $ tag, array( &$a, 'action' ) );38 do_action( $ tag);34 $a = new MockAction(); 35 $hook_name = __FUNCTION__; 36 37 add_action( $hook_name, array( &$a, 'action' ) ); 38 do_action( $hook_name ); 39 39 40 40 // Make sure our hook was called correctly. 41 41 $this->assertSame( 1, $a->get_call_count() ); 42 $this->assertSame( array( $ tag), $a->get_tags() );42 $this->assertSame( array( $hook_name ), $a->get_tags() ); 43 43 44 44 // Now remove the action, do it again, and make sure it's not called this time. 45 remove_action( $ tag, array( &$a, 'action' ) );46 do_action( $ tag);45 remove_action( $hook_name, array( &$a, 'action' ) ); 46 do_action( $hook_name ); 47 47 $this->assertSame( 1, $a->get_call_count() ); 48 $this->assertSame( array( $ tag), $a->get_tags() );48 $this->assertSame( array( $hook_name ), $a->get_tags() ); 49 49 50 50 } … … 54 54 */ 55 55 public function test_has_action() { 56 $tag = __FUNCTION__; 57 $func = __FUNCTION__ . '_func'; 58 59 $this->assertFalse( has_action( $tag, $func ) ); 60 $this->assertFalse( has_action( $tag ) ); 61 add_action( $tag, $func ); 62 $this->assertSame( 10, has_action( $tag, $func ) ); 63 $this->assertTrue( has_action( $tag ) ); 64 remove_action( $tag, $func ); 65 $this->assertFalse( has_action( $tag, $func ) ); 66 $this->assertFalse( has_action( $tag ) ); 56 $hook_name = __FUNCTION__; 57 $callback = __FUNCTION__ . '_func'; 58 59 $this->assertFalse( has_action( $hook_name, $callback ) ); 60 $this->assertFalse( has_action( $hook_name ) ); 61 62 add_action( $hook_name, $callback ); 63 $this->assertSame( 10, has_action( $hook_name, $callback ) ); 64 $this->assertTrue( has_action( $hook_name ) ); 65 66 remove_action( $hook_name, $callback ); 67 $this->assertFalse( has_action( $hook_name, $callback ) ); 68 $this->assertFalse( has_action( $hook_name ) ); 67 69 } 68 70 … … 73 75 */ 74 76 public function test_multiple_actions() { 75 $a1 = new MockAction();76 $a2 = new MockAction();77 $ tag= __FUNCTION__;77 $a1 = new MockAction(); 78 $a2 = new MockAction(); 79 $hook_name = __FUNCTION__; 78 80 79 81 // Add both actions to the hook. 80 add_action( $ tag, array( &$a1, 'action' ) );81 add_action( $ tag, array( &$a2, 'action' ) );82 83 do_action( $ tag);82 add_action( $hook_name, array( &$a1, 'action' ) ); 83 add_action( $hook_name, array( &$a2, 'action' ) ); 84 85 do_action( $hook_name ); 84 86 85 87 // Both actions called once each. … … 94 96 */ 95 97 public function test_action_args_1() { 96 $a = new MockAction();97 $ tag= __FUNCTION__;98 $val = __FUNCTION__ . '_val';99 100 add_action( $ tag, array( &$a, 'action' ) );98 $a = new MockAction(); 99 $hook_name = __FUNCTION__; 100 $val = __FUNCTION__ . '_val'; 101 102 add_action( $hook_name, array( &$a, 'action' ) ); 101 103 // Call the action with a single argument. 102 do_action( $ tag, $val );104 do_action( $hook_name, $val ); 103 105 104 106 $call_count = $a->get_call_count(); … … 114 116 */ 115 117 public function test_action_args_2() { 116 $a1 = new MockAction();117 $a2 = new MockAction();118 $ tag= __FUNCTION__;119 $val1 = __FUNCTION__ . '_val1';120 $val2 = __FUNCTION__ . '_val2';118 $a1 = new MockAction(); 119 $a2 = new MockAction(); 120 $hook_name = __FUNCTION__; 121 $val1 = __FUNCTION__ . '_val1'; 122 $val2 = __FUNCTION__ . '_val2'; 121 123 122 124 // $a1 accepts two arguments, $a2 doesn't. 123 add_action( $ tag, array( &$a1, 'action' ), 10, 2 );124 add_action( $ tag, array( &$a2, 'action' ) );125 add_action( $hook_name, array( &$a1, 'action' ), 10, 2 ); 126 add_action( $hook_name, array( &$a2, 'action' ) ); 125 127 // Call the action with two arguments. 126 do_action( $ tag, $val1, $val2 );128 do_action( $hook_name, $val1, $val2 ); 127 129 128 130 $call_count = $a1->get_call_count(); … … 148 150 */ 149 151 public function test_action_args_3() { 150 $a1 = new MockAction();151 $a2 = new MockAction();152 $a3 = new MockAction();153 $ tag= __FUNCTION__;154 $val1 = __FUNCTION__ . '_val1';155 $val2 = __FUNCTION__ . '_val2';152 $a1 = new MockAction(); 153 $a2 = new MockAction(); 154 $a3 = new MockAction(); 155 $hook_name = __FUNCTION__; 156 $val1 = __FUNCTION__ . '_val1'; 157 $val2 = __FUNCTION__ . '_val2'; 156 158 157 159 // $a1 accepts two arguments, $a2 doesn't, $a3 accepts two arguments. 158 add_action( $ tag, array( &$a1, 'action' ), 10, 2 );159 add_action( $ tag, array( &$a2, 'action' ) );160 add_action( $ tag, array( &$a3, 'action' ), 10, 2 );160 add_action( $hook_name, array( &$a1, 'action' ), 10, 2 ); 161 add_action( $hook_name, array( &$a2, 'action' ) ); 162 add_action( $hook_name, array( &$a3, 'action' ), 10, 2 ); 161 163 // Call the action with two arguments. 162 do_action( $ tag, $val1, $val2 );164 do_action( $hook_name, $val1, $val2 ); 163 165 164 166 $call_count = $a1->get_call_count(); … … 187 189 */ 188 190 public function test_action_args_with_php4_syntax() { 189 $a = new MockAction();190 $ tag= __FUNCTION__;191 $val = new stdClass();192 193 add_action( $ tag, array( &$a, 'action' ) );191 $a = new MockAction(); 192 $hook_name = __FUNCTION__; 193 $val = new stdClass(); 194 195 add_action( $hook_name, array( &$a, 'action' ) ); 194 196 // Call the action with PHP 4 notation for passing object by reference. 195 do_action( $ tag, array( &$val ) );197 do_action( $hook_name, array( &$val ) ); 196 198 197 199 $call_count = $a->get_call_count(); … … 201 203 202 204 public function test_action_priority() { 203 $a = new MockAction();204 $ tag= __FUNCTION__;205 206 add_action( $ tag, array( &$a, 'action' ), 10 );207 add_action( $ tag, array( &$a, 'action2' ), 9 );208 do_action( $ tag);205 $a = new MockAction(); 206 $hook_name = __FUNCTION__; 207 208 add_action( $hook_name, array( &$a, 'action' ), 10 ); 209 add_action( $hook_name, array( &$a, 'action2' ), 9 ); 210 do_action( $hook_name ); 209 211 210 212 // Two events, one per action. … … 215 217 array( 216 218 'action' => 'action2', 217 'tag' => $ tag,219 'tag' => $hook_name, 218 220 'args' => array( '' ), 219 221 ), … … 221 223 array( 222 224 'action' => 'action', 223 'tag' => $ tag,225 'tag' => $hook_name, 224 226 'args' => array( '' ), 225 227 ), … … 233 235 */ 234 236 public function test_did_action() { 235 $ tag1 = 'action1';236 $ tag2 = 'action2';237 238 // Do action $ tag1 but not $tag2.239 do_action( $ tag1 );240 $this->assertSame( 1, did_action( $ tag1 ) );241 $this->assertSame( 0, did_action( $ tag2 ) );242 243 // Do action $ tag2 10 times.237 $hook_name1 = 'action1'; 238 $hook_name2 = 'action2'; 239 240 // Do action $hook_name1 but not $hook_name2. 241 do_action( $hook_name1 ); 242 $this->assertSame( 1, did_action( $hook_name1 ) ); 243 $this->assertSame( 0, did_action( $hook_name2 ) ); 244 245 // Do action $hook_name2 10 times. 244 246 $count = 10; 245 247 for ( $i = 0; $i < $count; $i++ ) { 246 do_action( $ tag2 );248 do_action( $hook_name2 ); 247 249 } 248 250 249 // $ tag1's count hasn't changed, $tag2 should be correct.250 $this->assertSame( 1, did_action( $ tag1 ) );251 $this->assertSame( $count, did_action( $ tag2 ) );251 // $hook_name1's count hasn't changed, $hook_name2 should be correct. 252 $this->assertSame( 1, did_action( $hook_name1 ) ); 253 $this->assertSame( $count, did_action( $hook_name2 ) ); 252 254 253 255 } … … 257 259 */ 258 260 public function test_all_action() { 259 $a = new MockAction();260 $ tag1 = __FUNCTION__ . '_1';261 $ tag2 = __FUNCTION__ . '_2';261 $a = new MockAction(); 262 $hook_name1 = __FUNCTION__ . '_1'; 263 $hook_name2 = __FUNCTION__ . '_2'; 262 264 263 265 // Add an 'all' action. … … 265 267 $this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) ); 266 268 // Do some actions. 267 do_action( $ tag1 );268 do_action( $ tag2 );269 do_action( $ tag1 );270 do_action( $ tag1 );269 do_action( $hook_name1 ); 270 do_action( $hook_name2 ); 271 do_action( $hook_name1 ); 272 do_action( $hook_name1 ); 271 273 272 274 // Our action should have been called once for each tag. 273 275 $this->assertSame( 4, $a->get_call_count() ); 274 276 // Only our hook was called. 275 $this->assertSame( array( $ tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );277 $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_tags() ); 276 278 277 279 remove_action( 'all', array( &$a, 'action' ) ); … … 284 286 */ 285 287 public function test_remove_all_action() { 286 $a = new MockAction();287 $ tag= __FUNCTION__;288 $a = new MockAction(); 289 $hook_name = __FUNCTION__; 288 290 289 291 add_action( 'all', array( &$a, 'action' ) ); 290 292 $this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) ); 291 do_action( $ tag);293 do_action( $hook_name ); 292 294 293 295 // Make sure our hook was called correctly. 294 296 $this->assertSame( 1, $a->get_call_count() ); 295 $this->assertSame( array( $ tag), $a->get_tags() );297 $this->assertSame( array( $hook_name ), $a->get_tags() ); 296 298 297 299 // Now remove the action, do it again, and make sure it's not called this time. 298 300 remove_action( 'all', array( &$a, 'action' ) ); 299 301 $this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) ); 300 do_action( $ tag);302 do_action( $hook_name ); 301 303 $this->assertSame( 1, $a->get_call_count() ); 302 $this->assertSame( array( $ tag), $a->get_tags() );304 $this->assertSame( array( $hook_name ), $a->get_tags() ); 303 305 } 304 306 … … 307 309 */ 308 310 public function test_action_ref_array() { 309 $obj = new stdClass();310 $a = new MockAction();311 $ tag= __FUNCTION__;312 313 add_action( $ tag, array( &$a, 'action' ) );314 315 do_action_ref_array( $ tag, array( &$obj ) );311 $obj = new stdClass(); 312 $a = new MockAction(); 313 $hook_name = __FUNCTION__; 314 315 add_action( $hook_name, array( &$a, 'action' ) ); 316 317 do_action_ref_array( $hook_name, array( &$obj ) ); 316 318 317 319 $args = $a->get_args(); … … 330 332 $a = new MockAction(); 331 333 332 $ tag= __FUNCTION__;333 334 add_action( $ tag, array( &$a, 'action' ) );334 $hook_name = __FUNCTION__; 335 336 add_action( $hook_name, array( &$a, 'action' ) ); 335 337 336 338 $context = array( 'key1' => 'val1' ); 337 do_action( $ tag, $context );339 do_action( $hook_name, $context ); 338 340 339 341 $args = $a->get_args(); … … 344 346 'key3' => 'val3', 345 347 ); 346 do_action( $ tag, $context2 );348 do_action( $hook_name, $context2 ); 347 349 348 350 $args = $a->get_args(); … … 370 372 */ 371 373 public function test_action_recursion() { 372 $ tag= __FUNCTION__;373 $a = new MockAction();374 $b = new MockAction();375 376 add_action( $ tag, array( $a, 'action' ), 11, 1 );377 add_action( $ tag, array( $b, 'action' ), 13, 1 );378 add_action( $ tag, array( $this, 'action_that_causes_recursion' ), 12, 1 );379 do_action( $ tag, $tag);374 $hook_name = __FUNCTION__; 375 $a = new MockAction(); 376 $b = new MockAction(); 377 378 add_action( $hook_name, array( $a, 'action' ), 11, 1 ); 379 add_action( $hook_name, array( $b, 'action' ), 13, 1 ); 380 add_action( $hook_name, array( $this, 'action_that_causes_recursion' ), 12, 1 ); 381 do_action( $hook_name, $hook_name ); 380 382 381 383 $this->assertSame( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' ); … … 386 388 * @covers ::do_action 387 389 */ 388 public function action_that_causes_recursion( $ tag) {390 public function action_that_causes_recursion( $hook_name ) { 389 391 static $recursing = false; 390 392 if ( ! $recursing ) { 391 393 $recursing = true; 392 do_action( $ tag, $tag);394 do_action( $hook_name, $hook_name ); 393 395 } 394 396 $recursing = false; … … 403 405 */ 404 406 public function test_action_callback_manipulation_while_running() { 405 $ tag= __FUNCTION__;406 $a = new MockAction();407 $b = new MockAction();408 $c = new MockAction();409 $d = new MockAction();410 $e = new MockAction();411 412 add_action( $ tag, array( $a, 'action' ), 11, 2 );413 add_action( $ tag, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 );414 add_action( $ tag, array( $b, 'action' ), 12, 2 );415 416 do_action( $ tag, $tag, array( $a, $b, $c, $d, $e ) );417 do_action( $ tag, $tag, array( $a, $b, $c, $d, $e ) );407 $hook_name = __FUNCTION__; 408 $a = new MockAction(); 409 $b = new MockAction(); 410 $c = new MockAction(); 411 $d = new MockAction(); 412 $e = new MockAction(); 413 414 add_action( $hook_name, array( $a, 'action' ), 11, 2 ); 415 add_action( $hook_name, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 ); 416 add_action( $hook_name, array( $b, 'action' ), 12, 2 ); 417 418 do_action( $hook_name, $hook_name, array( $a, $b, $c, $d, $e ) ); 419 do_action( $hook_name, $hook_name, array( $a, $b, $c, $d, $e ) ); 418 420 419 421 $this->assertSame( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' ); … … 424 426 } 425 427 426 public function action_that_manipulates_a_running_hook( $ tag, $mocks ) {427 remove_action( $ tag, array( $mocks[1], 'action' ), 12, 2 );428 add_action( $ tag, array( $mocks[2], 'action' ), 12, 2 );429 add_action( $ tag, array( $mocks[3], 'action' ), 13, 2 );430 add_action( $ tag, array( $mocks[4], 'action' ), 10, 2 );428 public function action_that_manipulates_a_running_hook( $hook_name, $mocks ) { 429 remove_action( $hook_name, array( $mocks[1], 'action' ), 12, 2 ); 430 add_action( $hook_name, array( $mocks[2], 'action' ), 12, 2 ); 431 add_action( $hook_name, array( $mocks[3], 'action' ), 13, 2 ); 432 add_action( $hook_name, array( $mocks[4], 'action' ), 10, 2 ); 431 433 } 432 434 … … 440 442 */ 441 443 public function test_remove_anonymous_callback() { 442 $ tag= __FUNCTION__;443 $a = new MockAction();444 add_action( $ tag, array( $a, 'action' ), 12, 1 );445 $this->assertTrue( has_action( $ tag) );446 447 $hook = $GLOBALS['wp_filter'][ $ tag];444 $hook_name = __FUNCTION__; 445 $a = new MockAction(); 446 add_action( $hook_name, array( $a, 'action' ), 12, 1 ); 447 $this->assertTrue( has_action( $hook_name ) ); 448 449 $hook = $GLOBALS['wp_filter'][ $hook_name ]; 448 450 449 451 // From http://wordpress.stackexchange.com/a/57088/6445 … … 455 457 ) { 456 458 remove_filter( 457 $ tag,459 $hook_name, 458 460 array( $function['function'][0], 'action' ), 459 461 $priority … … 463 465 } 464 466 465 $this->assertFalse( has_action( $ tag) );467 $this->assertFalse( has_action( $hook_name ) ); 466 468 } 467 469 … … 478 480 public function test_array_access_of_wp_filter_global() { 479 481 global $wp_filter; 480 $tag = __FUNCTION__; 481 482 add_action( $tag, '__return_null', 11, 1 ); 483 484 $this->assertArrayHasKey( 11, $wp_filter[ $tag ] ); 485 $this->assertArrayHasKey( '__return_null', $wp_filter[ $tag ][11] ); 486 487 unset( $wp_filter[ $tag ][11] ); 488 $this->assertFalse( has_action( $tag, '__return_null' ) ); 489 490 $wp_filter[ $tag ][11] = array( 482 483 $hook_name = __FUNCTION__; 484 485 add_action( $hook_name, '__return_null', 11, 1 ); 486 487 $this->assertArrayHasKey( 11, $wp_filter[ $hook_name ] ); 488 $this->assertArrayHasKey( '__return_null', $wp_filter[ $hook_name ][11] ); 489 490 unset( $wp_filter[ $hook_name ][11] ); 491 $this->assertFalse( has_action( $hook_name, '__return_null' ) ); 492 493 $wp_filter[ $hook_name ][11] = array( 491 494 '__return_null' => array( 492 495 'function' => '__return_null', … … 494 497 ), 495 498 ); 496 $this->assertSame( 11, has_action( $ tag, '__return_null' ) );499 $this->assertSame( 11, has_action( $hook_name, '__return_null' ) ); 497 500 } 498 501 … … 506 509 public function test_current_action() { 507 510 global $wp_current_filter; 511 508 512 $wp_current_filter[] = 'first'; 509 513 $wp_current_filter[] = 'second'; // Let's say a second action was invoked. … … 519 523 public function test_doing_filter() { 520 524 global $wp_current_filter; 525 521 526 $wp_current_filter = array(); // Set to an empty array first. 522 527 … … 540 545 public function test_doing_action() { 541 546 global $wp_current_filter; 547 542 548 $wp_current_filter = array(); // Set to an empty array first. 543 549 -
trunk/tests/phpunit/tests/actions/callbacks.php
r52010 r53804 12 12 */ 13 13 public function test_callback_representations() { 14 $ tag= __FUNCTION__;14 $hook_name = __FUNCTION__; 15 15 16 $this->assertFalse( has_action( $ tag) );16 $this->assertFalse( has_action( $hook_name ) ); 17 17 18 add_action( $ tag, array( 'Class', 'method' ) );18 add_action( $hook_name, array( 'Class', 'method' ) ); 19 19 20 $this->assertSame( 10, has_action( $ tag, array( 'Class', 'method' ) ) );20 $this->assertSame( 10, has_action( $hook_name, array( 'Class', 'method' ) ) ); 21 21 22 $this->assertSame( 10, has_action( $ tag, 'Class::method' ) );22 $this->assertSame( 10, has_action( $hook_name, 'Class::method' ) ); 23 23 } 24 24 } -
trunk/tests/phpunit/tests/actions/closures.php
r52010 r53804 16 16 */ 17 17 public function test_action_closure() { 18 $ tag= 'test_action_closure';19 $closure = static function( $a, $b ) {18 $hook_name = 'test_action_closure'; 19 $closure = static function( $a, $b ) { 20 20 $GLOBALS[ $a ] = $b; 21 21 }; 22 add_action( $ tag, $closure, 10, 2 );22 add_action( $hook_name, $closure, 10, 2 ); 23 23 24 $this->assertSame( 10, has_action( $ tag, $closure ) );24 $this->assertSame( 10, has_action( $hook_name, $closure ) ); 25 25 26 26 $context = array( 'val1', 'val2' ); 27 do_action( $ tag, $context[0], $context[1] );27 do_action( $hook_name, $context[0], $context[1] ); 28 28 29 29 $this->assertSame( $GLOBALS[ $context[0] ], $context[1] ); 30 30 31 $ tag2= 'test_action_closure_2';32 $closure2 = static function() {31 $hook_name2 = 'test_action_closure_2'; 32 $closure2 = static function() { 33 33 $GLOBALS['closure_no_args'] = true; 34 34 }; 35 add_action( $ tag2, $closure2 );35 add_action( $hook_name2, $closure2 ); 36 36 37 $this->assertSame( 10, has_action( $ tag2, $closure2 ) );37 $this->assertSame( 10, has_action( $hook_name2, $closure2 ) ); 38 38 39 do_action( $ tag2 );39 do_action( $hook_name2 ); 40 40 41 41 $this->assertTrue( $GLOBALS['closure_no_args'] ); 42 42 43 remove_action( $ tag, $closure );44 remove_action( $ tag2, $closure2 );43 remove_action( $hook_name, $closure ); 44 remove_action( $hook_name2, $closure2 ); 45 45 } 46 46 } -
trunk/tests/phpunit/tests/filters.php
r53803 r53804 9 9 10 10 public function test_simple_filter() { 11 $a = new MockAction();12 $ tag= __FUNCTION__;13 $val = __FUNCTION__ . '_val';14 15 add_filter( $ tag, array( $a, 'filter' ) );16 $this->assertSame( $val, apply_filters( $ tag, $val ) );11 $a = new MockAction(); 12 $hook_name = __FUNCTION__; 13 $val = __FUNCTION__ . '_val'; 14 15 add_filter( $hook_name, array( $a, 'filter' ) ); 16 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 17 17 18 18 // Only one event occurred for the hook, with empty args. 19 19 $this->assertSame( 1, $a->get_call_count() ); 20 20 // Only our hook was called. 21 $this->assertSame( array( $ tag), $a->get_tags() );21 $this->assertSame( array( $hook_name ), $a->get_tags() ); 22 22 23 23 $argsvar = $a->get_args(); … … 27 27 28 28 public function test_remove_filter() { 29 $a = new MockAction();30 $ tag= __FUNCTION__;31 $val = __FUNCTION__ . '_val';32 33 add_filter( $ tag, array( $a, 'filter' ) );34 $this->assertSame( $val, apply_filters( $ tag, $val ) );29 $a = new MockAction(); 30 $hook_name = __FUNCTION__; 31 $val = __FUNCTION__ . '_val'; 32 33 add_filter( $hook_name, array( $a, 'filter' ) ); 34 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 35 35 36 36 // Make sure our hook was called correctly. 37 37 $this->assertSame( 1, $a->get_call_count() ); 38 $this->assertSame( array( $ tag), $a->get_tags() );38 $this->assertSame( array( $hook_name ), $a->get_tags() ); 39 39 40 40 // Now remove the filter, do it again, and make sure it's not called this time. 41 remove_filter( $ tag, array( $a, 'filter' ) );42 $this->assertSame( $val, apply_filters( $ tag, $val ) );43 $this->assertSame( 1, $a->get_call_count() ); 44 $this->assertSame( array( $ tag), $a->get_tags() );41 remove_filter( $hook_name, array( $a, 'filter' ) ); 42 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 43 $this->assertSame( 1, $a->get_call_count() ); 44 $this->assertSame( array( $hook_name ), $a->get_tags() ); 45 45 46 46 } 47 47 48 48 public function test_has_filter() { 49 $ tag= __FUNCTION__;50 $ func= __FUNCTION__ . '_func';51 52 $this->assertFalse( has_filter( $ tag, $func) );53 $this->assertFalse( has_filter( $ tag) );54 55 add_filter( $ tag, $func);56 $this->assertSame( 10, has_filter( $ tag, $func) );57 $this->assertTrue( has_filter( $ tag) );58 59 remove_filter( $ tag, $func);60 $this->assertFalse( has_filter( $ tag, $func) );61 $this->assertFalse( has_filter( $ tag) );49 $hook_name = __FUNCTION__; 50 $callback = __FUNCTION__ . '_func'; 51 52 $this->assertFalse( has_filter( $hook_name, $callback ) ); 53 $this->assertFalse( has_filter( $hook_name ) ); 54 55 add_filter( $hook_name, $callback ); 56 $this->assertSame( 10, has_filter( $hook_name, $callback ) ); 57 $this->assertTrue( has_filter( $hook_name ) ); 58 59 remove_filter( $hook_name, $callback ); 60 $this->assertFalse( has_filter( $hook_name, $callback ) ); 61 $this->assertFalse( has_filter( $hook_name ) ); 62 62 } 63 63 64 64 // One tag with multiple filters. 65 65 public function test_multiple_filters() { 66 $a1 = new MockAction();67 $a2 = new MockAction();68 $ tag= __FUNCTION__;69 $val = __FUNCTION__ . '_val';66 $a1 = new MockAction(); 67 $a2 = new MockAction(); 68 $hook_name = __FUNCTION__; 69 $val = __FUNCTION__ . '_val'; 70 70 71 71 // Add both filters to the hook. 72 add_filter( $ tag, array( $a1, 'filter' ) );73 add_filter( $ tag, array( $a2, 'filter' ) );74 75 $this->assertSame( $val, apply_filters( $ tag, $val ) );72 add_filter( $hook_name, array( $a1, 'filter' ) ); 73 add_filter( $hook_name, array( $a2, 'filter' ) ); 74 75 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 76 76 77 77 // Both filters called once each. … … 81 81 82 82 public function test_filter_args_1() { 83 $a = new MockAction();84 $ tag= __FUNCTION__;85 $val = __FUNCTION__ . '_val';86 $arg1 = __FUNCTION__ . '_arg1';87 88 add_filter( $ tag, array( $a, 'filter' ), 10, 2 );83 $a = new MockAction(); 84 $hook_name = __FUNCTION__; 85 $val = __FUNCTION__ . '_val'; 86 $arg1 = __FUNCTION__ . '_arg1'; 87 88 add_filter( $hook_name, array( $a, 'filter' ), 10, 2 ); 89 89 // Call the filter with a single argument. 90 $this->assertSame( $val, apply_filters( $ tag, $val, $arg1 ) );90 $this->assertSame( $val, apply_filters( $hook_name, $val, $arg1 ) ); 91 91 92 92 $this->assertSame( 1, $a->get_call_count() ); … … 96 96 97 97 public function test_filter_args_2() { 98 $a1 = new MockAction();99 $a2 = new MockAction();100 $ tag= __FUNCTION__;101 $val = __FUNCTION__ . '_val';102 $arg1 = __FUNCTION__ . '_arg1';103 $arg2 = __FUNCTION__ . '_arg2';98 $a1 = new MockAction(); 99 $a2 = new MockAction(); 100 $hook_name = __FUNCTION__; 101 $val = __FUNCTION__ . '_val'; 102 $arg1 = __FUNCTION__ . '_arg1'; 103 $arg2 = __FUNCTION__ . '_arg2'; 104 104 105 105 // $a1 accepts two arguments, $a2 doesn't. 106 add_filter( $ tag, array( $a1, 'filter' ), 10, 3 );107 add_filter( $ tag, array( $a2, 'filter' ) );106 add_filter( $hook_name, array( $a1, 'filter' ), 10, 3 ); 107 add_filter( $hook_name, array( $a2, 'filter' ) ); 108 108 // Call the filter with two arguments. 109 $this->assertSame( $val, apply_filters( $ tag, $val, $arg1, $arg2 ) );109 $this->assertSame( $val, apply_filters( $hook_name, $val, $arg1, $arg2 ) ); 110 110 111 111 // $a1 should be called with both args. … … 121 121 122 122 public function test_filter_priority() { 123 $a = new MockAction();124 $ tag= __FUNCTION__;125 $val = __FUNCTION__ . '_val';123 $a = new MockAction(); 124 $hook_name = __FUNCTION__; 125 $val = __FUNCTION__ . '_val'; 126 126 127 127 // Make two filters with different priorities. 128 add_filter( $ tag, array( $a, 'filter' ), 10 );129 add_filter( $ tag, array( $a, 'filter2' ), 9 );130 $this->assertSame( $val, apply_filters( $ tag, $val ) );128 add_filter( $hook_name, array( $a, 'filter' ), 10 ); 129 add_filter( $hook_name, array( $a, 'filter2' ), 9 ); 130 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 131 131 132 132 // There should be two events, one per filter. … … 137 137 array( 138 138 'filter' => 'filter2', 139 'tag' => $ tag,139 'tag' => $hook_name, 140 140 'args' => array( $val ), 141 141 ), … … 143 143 array( 144 144 'filter' => 'filter', 145 'tag' => $ tag,145 'tag' => $hook_name, 146 146 'args' => array( $val ), 147 147 ), … … 155 155 */ 156 156 public function test_did_filter() { 157 $ tag1 = 'filter1';158 $ tag2 = 'filter2';159 $val = __FUNCTION__ . '_val';160 161 // Apply filter $ tag1 but not $tag2.162 apply_filters( $ tag1, $val );163 $this->assertSame( 1, did_filter( $ tag1 ) );164 $this->assertSame( 0, did_filter( $ tag2 ) );165 166 // Apply filter $ tag2 10 times.157 $hook_name1 = 'filter1'; 158 $hook_name2 = 'filter2'; 159 $val = __FUNCTION__ . '_val'; 160 161 // Apply filter $hook_name1 but not $hook_name2. 162 apply_filters( $hook_name1, $val ); 163 $this->assertSame( 1, did_filter( $hook_name1 ) ); 164 $this->assertSame( 0, did_filter( $hook_name2 ) ); 165 166 // Apply filter $hook_name2 10 times. 167 167 $count = 10; 168 168 for ( $i = 0; $i < $count; $i++ ) { 169 apply_filters( $ tag2, $val );169 apply_filters( $hook_name2, $val ); 170 170 } 171 171 172 // $ tag1's count hasn't changed, $tag2 should be correct.173 $this->assertSame( 1, did_filter( $ tag1 ) );174 $this->assertSame( $count, did_filter( $ tag2 ) );172 // $hook_name1's count hasn't changed, $hook_name2 should be correct. 173 $this->assertSame( 1, did_filter( $hook_name1 ) ); 174 $this->assertSame( $count, did_filter( $hook_name2 ) ); 175 175 176 176 } 177 177 178 178 public function test_all_filter() { 179 $a = new MockAction();180 $ tag1 = __FUNCTION__ . '_1';181 $ tag2 = __FUNCTION__ . '_2';182 $val = __FUNCTION__ . '_val';179 $a = new MockAction(); 180 $hook_name1 = __FUNCTION__ . '_1'; 181 $hook_name2 = __FUNCTION__ . '_2'; 182 $val = __FUNCTION__ . '_val'; 183 183 184 184 // Add an 'all' filter. 185 185 add_filter( 'all', array( $a, 'filterall' ) ); 186 186 // Apply some filters. 187 $this->assertSame( $val, apply_filters( $ tag1, $val ) );188 $this->assertSame( $val, apply_filters( $ tag2, $val ) );189 $this->assertSame( $val, apply_filters( $ tag1, $val ) );190 $this->assertSame( $val, apply_filters( $ tag1, $val ) );187 $this->assertSame( $val, apply_filters( $hook_name1, $val ) ); 188 $this->assertSame( $val, apply_filters( $hook_name2, $val ) ); 189 $this->assertSame( $val, apply_filters( $hook_name1, $val ) ); 190 $this->assertSame( $val, apply_filters( $hook_name1, $val ) ); 191 191 192 192 // Our filter should have been called once for each apply_filters call. 193 193 $this->assertSame( 4, $a->get_call_count() ); 194 194 // The right hooks should have been called in order. 195 $this->assertSame( array( $ tag1, $tag2, $tag1, $tag1 ), $a->get_tags() );195 $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_tags() ); 196 196 197 197 remove_filter( 'all', array( $a, 'filterall' ) ); … … 201 201 202 202 public function test_remove_all_filter() { 203 $a = new MockAction();204 $ tag= __FUNCTION__;205 $val = __FUNCTION__ . '_val';203 $a = new MockAction(); 204 $hook_name = __FUNCTION__; 205 $val = __FUNCTION__ . '_val'; 206 206 207 207 add_filter( 'all', array( $a, 'filterall' ) ); 208 208 $this->assertTrue( has_filter( 'all' ) ); 209 209 $this->assertSame( 10, has_filter( 'all', array( $a, 'filterall' ) ) ); 210 $this->assertSame( $val, apply_filters( $ tag, $val ) );210 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 211 211 212 212 // Make sure our hook was called correctly. 213 213 $this->assertSame( 1, $a->get_call_count() ); 214 $this->assertSame( array( $ tag), $a->get_tags() );214 $this->assertSame( array( $hook_name ), $a->get_tags() ); 215 215 216 216 // Now remove the filter, do it again, and make sure it's not called this time. … … 218 218 $this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) ); 219 219 $this->assertFalse( has_filter( 'all' ) ); 220 $this->assertSame( $val, apply_filters( $ tag, $val ) );220 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 221 221 // Call cound should remain at 1. 222 222 $this->assertSame( 1, $a->get_call_count() ); 223 $this->assertSame( array( $ tag), $a->get_tags() );223 $this->assertSame( array( $hook_name ), $a->get_tags() ); 224 224 } 225 225 … … 228 228 */ 229 229 public function test_remove_all_filters_should_respect_the_priority_argument() { 230 $a = new MockAction();231 $ tag= __FUNCTION__;232 233 add_filter( $ tag, array( $a, 'filter' ), 12 );234 $this->assertTrue( has_filter( $ tag) );230 $a = new MockAction(); 231 $hook_name = __FUNCTION__; 232 233 add_filter( $hook_name, array( $a, 'filter' ), 12 ); 234 $this->assertTrue( has_filter( $hook_name ) ); 235 235 236 236 // Should not be removed. 237 remove_all_filters( $ tag, 11 );238 $this->assertTrue( has_filter( $ tag) );239 240 remove_all_filters( $ tag, 12 );241 $this->assertFalse( has_filter( $ tag) );237 remove_all_filters( $hook_name, 11 ); 238 $this->assertTrue( has_filter( $hook_name ) ); 239 240 remove_all_filters( $hook_name, 12 ); 241 $this->assertFalse( has_filter( $hook_name ) ); 242 242 } 243 243 … … 246 246 */ 247 247 public function test_filter_with_ref_value() { 248 $obj = new stdClass();249 $ref = &$obj;250 $a = new MockAction();251 $ tag= __FUNCTION__;252 253 add_action( $ tag, array( $a, 'filter' ) );254 255 $filtered = apply_filters( $ tag, $ref );248 $obj = new stdClass(); 249 $ref = &$obj; 250 $a = new MockAction(); 251 $hook_name = __FUNCTION__; 252 253 add_action( $hook_name, array( $a, 'filter' ) ); 254 255 $filtered = apply_filters( $hook_name, $ref ); 256 256 257 257 $args = $a->get_args(); … … 268 268 */ 269 269 public function test_filter_with_ref_argument() { 270 $obj = new stdClass();271 $ref = &$obj;272 $a = new MockAction();273 $ tag= __FUNCTION__;274 $val = 'Hello';275 276 add_action( $ tag, array( $a, 'filter' ), 10, 2 );277 278 apply_filters( $ tag, $val, $ref );270 $obj = new stdClass(); 271 $ref = &$obj; 272 $a = new MockAction(); 273 $hook_name = __FUNCTION__; 274 $val = 'Hello'; 275 276 add_action( $hook_name, array( $a, 'filter' ), 10, 2 ); 277 278 apply_filters( $hook_name, $val, $ref ); 279 279 280 280 $args = $a->get_args(); … … 289 289 */ 290 290 public function test_filter_ref_array() { 291 $obj = new stdClass();292 $a = new MockAction();293 $ tag= __FUNCTION__;294 295 add_action( $ tag, array( $a, 'filter' ) );296 297 apply_filters_ref_array( $ tag, array( &$obj ) );291 $obj = new stdClass(); 292 $a = new MockAction(); 293 $hook_name = __FUNCTION__; 294 295 add_action( $hook_name, array( $a, 'filter' ) ); 296 297 apply_filters_ref_array( $hook_name, array( &$obj ) ); 298 298 299 299 $args = $a->get_args(); … … 308 308 */ 309 309 public function test_filter_ref_array_result() { 310 $obj = new stdClass();311 $a = new MockAction();312 $b = new MockAction();313 $ tag= __FUNCTION__;314 315 add_action( $ tag, array( $a, 'filter_append' ), 10, 2 );316 add_action( $ tag, array( $b, 'filter_append' ), 10, 2 );317 318 $result = apply_filters_ref_array( $ tag, array( 'string', &$obj ) );310 $obj = new stdClass(); 311 $a = new MockAction(); 312 $b = new MockAction(); 313 $hook_name = __FUNCTION__; 314 315 add_action( $hook_name, array( $a, 'filter_append' ), 10, 2 ); 316 add_action( $hook_name, array( $b, 'filter_append' ), 10, 2 ); 317 318 $result = apply_filters_ref_array( $hook_name, array( 'string', &$obj ) ); 319 319 320 320 $this->assertSame( $result, 'string_append_append' ); … … 338 338 */ 339 339 public function test_has_filter_after_remove_all_filters() { 340 $a = new MockAction();341 $ tag= __FUNCTION__;340 $a = new MockAction(); 341 $hook_name = __FUNCTION__; 342 342 343 343 // No priority. 344 add_filter( $ tag, array( $a, 'filter' ), 11 );345 add_filter( $ tag, array( $a, 'filter' ), 12 );346 $this->assertTrue( has_filter( $ tag) );347 348 remove_all_filters( $ tag);349 $this->assertFalse( has_filter( $ tag) );344 add_filter( $hook_name, array( $a, 'filter' ), 11 ); 345 add_filter( $hook_name, array( $a, 'filter' ), 12 ); 346 $this->assertTrue( has_filter( $hook_name ) ); 347 348 remove_all_filters( $hook_name ); 349 $this->assertFalse( has_filter( $hook_name ) ); 350 350 351 351 // Remove priorities one at a time. 352 add_filter( $ tag, array( $a, 'filter' ), 11 );353 add_filter( $ tag, array( $a, 'filter' ), 12 );354 $this->assertTrue( has_filter( $ tag) );355 356 remove_all_filters( $ tag, 11 );357 remove_all_filters( $ tag, 12 );358 $this->assertFalse( has_filter( $ tag) );352 add_filter( $hook_name, array( $a, 'filter' ), 11 ); 353 add_filter( $hook_name, array( $a, 'filter' ), 12 ); 354 $this->assertTrue( has_filter( $hook_name ) ); 355 356 remove_all_filters( $hook_name, 11 ); 357 remove_all_filters( $hook_name, 12 ); 358 $this->assertFalse( has_filter( $hook_name ) ); 359 359 } 360 360 … … 413 413 414 414 private $current_priority; 415 415 416 /** 416 417 * @ticket 39007 … … 426 427 public function current_priority_action() { 427 428 global $wp_filter; 429 428 430 $this->current_priority = $wp_filter[ current_filter() ]->current_priority(); 429 431 } -
trunk/tests/phpunit/tests/hooks/addFilter.php
r52389 r53804 15 15 $callback = '__return_null'; 16 16 $hook = new WP_Hook(); 17 $ tag= __FUNCTION__;18 $priority = 1; 19 $accepted_args = 2; 20 21 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );22 23 $function_index = _wp_filter_build_unique_id( $ tag, $callback, $priority );17 $hook_name = __FUNCTION__; 18 $priority = 1; 19 $accepted_args = 2; 20 21 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 22 23 $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 24 24 $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] ); 25 25 $this->assertSame( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] ); … … 30 30 $callback = array( $a, 'action' ); 31 31 $hook = new WP_Hook(); 32 $ tag= __FUNCTION__;33 $priority = 1; 34 $accepted_args = 2; 35 36 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );37 38 $function_index = _wp_filter_build_unique_id( $ tag, $callback, $priority );32 $hook_name = __FUNCTION__; 33 $priority = 1; 34 $accepted_args = 2; 35 36 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 37 38 $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 39 39 $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] ); 40 40 $this->assertSame( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] ); … … 44 44 $callback = array( 'MockAction', 'action' ); 45 45 $hook = new WP_Hook(); 46 $ tag= __FUNCTION__;47 $priority = 1; 48 $accepted_args = 2; 49 50 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );51 52 $function_index = _wp_filter_build_unique_id( $ tag, $callback, $priority );46 $hook_name = __FUNCTION__; 47 $priority = 1; 48 $accepted_args = 2; 49 50 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 51 52 $function_index = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 53 53 $this->assertSame( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] ); 54 54 $this->assertSame( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] ); … … 59 59 $callback_two = '__return_false'; 60 60 $hook = new WP_Hook(); 61 $ tag= __FUNCTION__;62 $priority = 1; 63 $accepted_args = 2; 64 65 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );66 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 67 68 $hook->add_filter( $ tag, $callback_two, $priority, $accepted_args );61 $hook_name = __FUNCTION__; 62 $priority = 1; 63 $accepted_args = 2; 64 65 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 66 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 67 68 $hook->add_filter( $hook_name, $callback_two, $priority, $accepted_args ); 69 69 $this->assertCount( 2, $hook->callbacks[ $priority ] ); 70 70 } … … 74 74 $callback_two = '__return_false'; 75 75 $hook = new WP_Hook(); 76 $ tag= __FUNCTION__;77 $priority = 1; 78 $accepted_args = 2; 79 80 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );81 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 82 83 $hook->add_filter( $ tag, $callback_two, $priority + 1, $accepted_args );76 $hook_name = __FUNCTION__; 77 $priority = 1; 78 $accepted_args = 2; 79 80 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 81 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 82 83 $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args ); 84 84 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 85 85 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); … … 89 89 $callback = '__return_null'; 90 90 $hook = new WP_Hook(); 91 $ tag= __FUNCTION__;92 $priority = 1; 93 $accepted_args = 2; 94 95 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );96 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 97 98 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );91 $hook_name = __FUNCTION__; 92 $priority = 1; 93 $accepted_args = 2; 94 95 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 96 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 97 98 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 99 99 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 100 100 } … … 103 103 $callback = '__return_null'; 104 104 $hook = new WP_Hook(); 105 $ tag= __FUNCTION__;106 $priority = 1; 107 $accepted_args = 2; 108 109 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );110 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 111 112 $hook->add_filter( $ tag, $callback, $priority + 1, $accepted_args );105 $hook_name = __FUNCTION__; 106 $priority = 1; 107 $accepted_args = 2; 108 109 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 110 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 111 112 $hook->add_filter( $hook_name, $callback, $priority + 1, $accepted_args ); 113 113 $this->assertCount( 1, $hook->callbacks[ $priority ] ); 114 114 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] ); … … 116 116 117 117 public function test_sort_after_add_filter() { 118 $a = new MockAction();119 $b = new MockAction();120 $c = new MockAction();121 $hook = new WP_Hook();122 $ tag= __FUNCTION__;123 124 $hook->add_filter( $ tag, array( $a, 'action' ), 10, 1 );125 $hook->add_filter( $ tag, array( $b, 'action' ), 5, 1 );126 $hook->add_filter( $ tag, array( $c, 'action' ), 8, 1 );118 $a = new MockAction(); 119 $b = new MockAction(); 120 $c = new MockAction(); 121 $hook = new WP_Hook(); 122 $hook_name = __FUNCTION__; 123 124 $hook->add_filter( $hook_name, array( $a, 'action' ), 10, 1 ); 125 $hook->add_filter( $hook_name, array( $b, 'action' ), 5, 1 ); 126 $hook->add_filter( $hook_name, array( $c, 'action' ), 8, 1 ); 127 127 128 128 $this->assertSame( array( 5, 8, 10 ), array_keys( $hook->callbacks ) ); … … 130 130 131 131 public function test_remove_and_add() { 132 $this->hook = new W p_Hook();132 $this->hook = new WP_Hook(); 133 133 134 134 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 ); … … 144 144 145 145 public function test_remove_and_add_last_filter() { 146 $this->hook = new W p_Hook();146 $this->hook = new WP_Hook(); 147 147 148 148 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 ); … … 158 158 159 159 public function test_remove_and_recurse_and_add() { 160 $this->hook = new W p_Hook();160 $this->hook = new WP_Hook(); 161 161 162 162 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 ); … … 203 203 204 204 public function test_remove_and_add_action() { 205 $this->hook = new W p_Hook();205 $this->hook = new WP_Hook(); 206 206 $this->action_output = ''; 207 207 … … 218 218 219 219 public function test_remove_and_add_last_action() { 220 $this->hook = new W p_Hook();220 $this->hook = new WP_Hook(); 221 221 $this->action_output = ''; 222 222 … … 233 233 234 234 public function test_remove_and_recurse_and_add_action() { 235 $this->hook = new W p_Hook();235 $this->hook = new WP_Hook(); 236 236 $this->action_output = ''; 237 237 -
trunk/tests/phpunit/tests/hooks/applyFilters.php
r52389 r53804 13 13 $callback = array( $a, 'filter' ); 14 14 $hook = new WP_Hook(); 15 $ tag= __FUNCTION__;15 $hook_name = __FUNCTION__; 16 16 $priority = 1; 17 17 $accepted_args = 2; 18 18 $arg = __FUNCTION__ . '_arg'; 19 19 20 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );20 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 21 21 22 22 $returned = $hook->apply_filters( $arg, array( $arg ) ); … … 30 30 $callback = array( $a, 'filter' ); 31 31 $hook = new WP_Hook(); 32 $ tag= __FUNCTION__;32 $hook_name = __FUNCTION__; 33 33 $priority = 1; 34 34 $accepted_args = 2; 35 35 $arg = __FUNCTION__ . '_arg'; 36 36 37 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );37 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 38 38 39 39 $returned_one = $hook->apply_filters( $arg, array( $arg ) ); -
trunk/tests/phpunit/tests/hooks/doAction.php
r52389 r53804 21 21 $callback = array( $a, 'action' ); 22 22 $hook = new WP_Hook(); 23 $ tag= __FUNCTION__;23 $hook_name = __FUNCTION__; 24 24 $priority = 1; 25 25 $accepted_args = 2; 26 26 $arg = __FUNCTION__ . '_arg'; 27 27 28 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );28 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 29 29 $hook->do_action( array( $arg ) ); 30 30 … … 36 36 $callback = array( $a, 'filter' ); 37 37 $hook = new WP_Hook(); 38 $ tag= __FUNCTION__;38 $hook_name = __FUNCTION__; 39 39 $priority = 1; 40 40 $accepted_args = 2; 41 41 $arg = __FUNCTION__ . '_arg'; 42 42 43 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );43 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 44 44 $hook->do_action( array( $arg ) ); 45 45 $hook->do_action( array( $arg ) ); … … 54 54 $callback_two = array( $b, 'filter' ); 55 55 $hook = new WP_Hook(); 56 $ tag= __FUNCTION__;56 $hook_name = __FUNCTION__; 57 57 $priority = 1; 58 58 $accepted_args = 2; 59 59 $arg = __FUNCTION__ . '_arg'; 60 60 61 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );62 $hook->add_filter( $ tag, $callback_two, $priority, $accepted_args );61 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 62 $hook->add_filter( $hook_name, $callback_two, $priority, $accepted_args ); 63 63 $hook->do_action( array( $arg ) ); 64 64 … … 73 73 $callback_two = array( $b, 'filter' ); 74 74 $hook = new WP_Hook(); 75 $ tag= __FUNCTION__;75 $hook_name = __FUNCTION__; 76 76 $priority = 1; 77 77 $accepted_args = 2; 78 78 $arg = __FUNCTION__ . '_arg'; 79 79 80 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );81 $hook->add_filter( $ tag, $callback_two, $priority + 1, $accepted_args );80 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 81 $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args ); 82 82 $hook->do_action( array( $arg ) ); 83 83 … … 89 89 $callback = array( $this, '_action_callback' ); 90 90 $hook = new WP_Hook(); 91 $ tag= __FUNCTION__;91 $hook_name = __FUNCTION__; 92 92 $priority = 1; 93 93 $accepted_args = 0; 94 94 $arg = __FUNCTION__ . '_arg'; 95 95 96 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );96 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 97 97 $hook->do_action( array( $arg ) ); 98 98 … … 103 103 $callback = array( $this, '_action_callback' ); 104 104 $hook = new WP_Hook(); 105 $ tag= __FUNCTION__;105 $hook_name = __FUNCTION__; 106 106 $priority = 1; 107 107 $accepted_args = 1; 108 108 $arg = __FUNCTION__ . '_arg'; 109 109 110 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );110 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 111 111 $hook->do_action( array( $arg ) ); 112 112 … … 117 117 $callback = array( $this, '_action_callback' ); 118 118 $hook = new WP_Hook(); 119 $ tag= __FUNCTION__;119 $hook_name = __FUNCTION__; 120 120 $priority = 100; 121 121 $accepted_args = 1000; 122 122 $arg = __FUNCTION__ . '_arg'; 123 123 124 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );124 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 125 125 $hook->do_action( array( $arg ) ); 126 126 -
trunk/tests/phpunit/tests/hooks/doAllHook.php
r52389 r53804 13 13 $callback = array( $a, 'action' ); 14 14 $hook = new WP_Hook(); 15 $ tag= 'all';15 $hook_name = 'all'; 16 16 $priority = 1; 17 17 $accepted_args = 2; 18 18 $arg = 'all_arg'; 19 19 20 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );20 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 21 21 $args = array( $arg ); 22 22 $hook->do_all_hook( $args ); -
trunk/tests/phpunit/tests/hooks/hasFilter.php
r52389 r53804 12 12 $callback = '__return_null'; 13 13 $hook = new WP_Hook(); 14 $ tag= __FUNCTION__;14 $hook_name = __FUNCTION__; 15 15 $priority = 1; 16 16 $accepted_args = 2; 17 17 18 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );18 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 19 19 20 $this->assertSame( $priority, $hook->has_filter( $ tag, $callback ) );20 $this->assertSame( $priority, $hook->has_filter( $hook_name, $callback ) ); 21 21 } 22 22 … … 25 25 $callback = array( $a, 'action' ); 26 26 $hook = new WP_Hook(); 27 $ tag= __FUNCTION__;27 $hook_name = __FUNCTION__; 28 28 $priority = 1; 29 29 $accepted_args = 2; 30 30 31 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );31 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 32 32 33 $this->assertSame( $priority, $hook->has_filter( $ tag, $callback ) );33 $this->assertSame( $priority, $hook->has_filter( $hook_name, $callback ) ); 34 34 } 35 35 … … 37 37 $callback = array( 'MockAction', 'action' ); 38 38 $hook = new WP_Hook(); 39 $ tag= __FUNCTION__;39 $hook_name = __FUNCTION__; 40 40 $priority = 1; 41 41 $accepted_args = 2; 42 42 43 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );43 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 44 44 45 $this->assertSame( $priority, $hook->has_filter( $ tag, $callback ) );45 $this->assertSame( $priority, $hook->has_filter( $hook_name, $callback ) ); 46 46 } 47 47 … … 49 49 $callback = '__return_null'; 50 50 $hook = new WP_Hook(); 51 $ tag= __FUNCTION__;51 $hook_name = __FUNCTION__; 52 52 $priority = 1; 53 53 $accepted_args = 2; 54 54 55 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );55 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 56 56 57 57 $this->assertTrue( $hook->has_filter() ); … … 64 64 65 65 public function test_not_has_filter_with_callback() { 66 $callback = '__return_null';67 $hook = new WP_Hook();68 $ tag= __FUNCTION__;66 $callback = '__return_null'; 67 $hook = new WP_Hook(); 68 $hook_name = __FUNCTION__; 69 69 70 $this->assertFalse( $hook->has_filter( $ tag, $callback ) );70 $this->assertFalse( $hook->has_filter( $hook_name, $callback ) ); 71 71 } 72 72 … … 74 74 $callback = '__return_null'; 75 75 $hook = new WP_Hook(); 76 $ tag= __FUNCTION__;76 $hook_name = __FUNCTION__; 77 77 $priority = 1; 78 78 $accepted_args = 2; 79 79 80 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );80 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 81 81 82 $this->assertFalse( $hook->has_filter( $ tag, '__return_false' ) );82 $this->assertFalse( $hook->has_filter( $hook_name, '__return_false' ) ); 83 83 } 84 84 } -
trunk/tests/phpunit/tests/hooks/hasFilters.php
r52389 r53804 12 12 $callback = '__return_null'; 13 13 $hook = new WP_Hook(); 14 $ tag= __FUNCTION__;14 $hook_name = __FUNCTION__; 15 15 $priority = 1; 16 16 $accepted_args = 2; 17 17 18 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );18 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 19 19 20 20 $this->assertTrue( $hook->has_filters() ); … … 29 29 $callback = '__return_null'; 30 30 $hook = new WP_Hook(); 31 $ tag= __FUNCTION__;31 $hook_name = __FUNCTION__; 32 32 $priority = 1; 33 33 $accepted_args = 2; 34 34 35 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );36 $hook->remove_filter( $ tag, $callback, $priority );35 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 36 $hook->remove_filter( $hook_name, $callback, $priority ); 37 37 $this->assertFalse( $hook->has_filters() ); 38 38 } … … 41 41 $callback = '__return_null'; 42 42 $hook = new WP_Hook(); 43 $ tag= __FUNCTION__;43 $hook_name = __FUNCTION__; 44 44 $priority = 1; 45 45 $accepted_args = 2; 46 46 47 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );48 $function_key = _wp_filter_build_unique_id( $ tag, $callback, $priority );47 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 48 $function_key = _wp_filter_build_unique_id( $hook_name, $callback, $priority ); 49 49 unset( $hook->callbacks[ $priority ][ $function_key ] ); 50 50 -
trunk/tests/phpunit/tests/hooks/iterator.php
r52389 r53804 13 13 $callback_two = '__return_false'; 14 14 $hook = new WP_Hook(); 15 $ tag= __FUNCTION__;15 $hook_name = __FUNCTION__; 16 16 $priority = 1; 17 17 $accepted_args = 2; 18 18 19 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );20 $hook->add_filter( $ tag, $callback_two, $priority + 1, $accepted_args );19 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 20 $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args ); 21 21 22 22 $functions = array(); -
trunk/tests/phpunit/tests/hooks/preinitHooks.php
r52389 r53804 10 10 11 11 public function test_array_to_hooks() { 12 $ tag1= __FUNCTION__ . '_1';13 $priority1 = 1;14 $ tag2= __FUNCTION__ . '_2';15 $priority2 = 2;16 $filters = array(17 $ tag1 => array(12 $hook_name1 = __FUNCTION__ . '_1'; 13 $priority1 = 1; 14 $hook_name2 = __FUNCTION__ . '_2'; 15 $priority2 = 2; 16 $filters = array( 17 $hook_name1 => array( 18 18 $priority1 => array( 19 19 'test1' => array( … … 23 23 ), 24 24 ), 25 $ tag2 => array(25 $hook_name2 => array( 26 26 $priority2 => array( 27 27 'test1' => array( … … 35 35 $hooks = WP_Hook::build_preinitialized_hooks( $filters ); 36 36 37 $this->assertSame( $priority1, $hooks[ $ tag1 ]->has_filter( $tag1, '__return_false' ) );38 $this->assertSame( $priority2, $hooks[ $ tag2 ]->has_filter( $tag2, '__return_null' ) );37 $this->assertSame( $priority1, $hooks[ $hook_name1 ]->has_filter( $hook_name1, '__return_false' ) ); 38 $this->assertSame( $priority2, $hooks[ $hook_name2 ]->has_filter( $hook_name2, '__return_null' ) ); 39 39 } 40 40 } -
trunk/tests/phpunit/tests/hooks/removeAllFilters.php
r52389 r53804 12 12 $callback = '__return_null'; 13 13 $hook = new WP_Hook(); 14 $ tag= __FUNCTION__;14 $hook_name = __FUNCTION__; 15 15 $priority = 1; 16 16 $accepted_args = 2; 17 17 18 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );18 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 19 19 20 20 $hook->remove_all_filters(); … … 27 27 $callback_two = '__return_false'; 28 28 $hook = new WP_Hook(); 29 $ tag= __FUNCTION__;29 $hook_name = __FUNCTION__; 30 30 $priority = 1; 31 31 $accepted_args = 2; 32 32 33 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );34 $hook->add_filter( $ tag, $callback_two, $priority + 1, $accepted_args );33 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 34 $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args ); 35 35 36 36 $hook->remove_all_filters( $priority ); 37 37 38 $this->assertFalse( $hook->has_filter( $ tag, $callback_one ) );38 $this->assertFalse( $hook->has_filter( $hook_name, $callback_one ) ); 39 39 $this->assertTrue( $hook->has_filters() ); 40 $this->assertSame( $priority + 1, $hook->has_filter( $ tag, $callback_two ) );40 $this->assertSame( $priority + 1, $hook->has_filter( $hook_name, $callback_two ) ); 41 41 } 42 42 } -
trunk/tests/phpunit/tests/hooks/removeFilter.php
r52389 r53804 12 12 $callback = '__return_null'; 13 13 $hook = new WP_Hook(); 14 $ tag= __FUNCTION__;14 $hook_name = __FUNCTION__; 15 15 $priority = 1; 16 16 $accepted_args = 2; 17 17 18 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );19 $hook->remove_filter( $ tag, $callback, $priority );18 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 19 $hook->remove_filter( $hook_name, $callback, $priority ); 20 20 21 21 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); … … 26 26 $callback = array( $a, 'action' ); 27 27 $hook = new WP_Hook(); 28 $ tag= __FUNCTION__;28 $hook_name = __FUNCTION__; 29 29 $priority = 1; 30 30 $accepted_args = 2; 31 31 32 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );33 $hook->remove_filter( $ tag, $callback, $priority );32 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 33 $hook->remove_filter( $hook_name, $callback, $priority ); 34 34 35 35 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); … … 39 39 $callback = array( 'MockAction', 'action' ); 40 40 $hook = new WP_Hook(); 41 $ tag= __FUNCTION__;41 $hook_name = __FUNCTION__; 42 42 $priority = 1; 43 43 $accepted_args = 2; 44 44 45 $hook->add_filter( $ tag, $callback, $priority, $accepted_args );46 $hook->remove_filter( $ tag, $callback, $priority );45 $hook->add_filter( $hook_name, $callback, $priority, $accepted_args ); 46 $hook->remove_filter( $hook_name, $callback, $priority ); 47 47 48 48 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); … … 53 53 $callback_two = '__return_false'; 54 54 $hook = new WP_Hook(); 55 $ tag= __FUNCTION__;55 $hook_name = __FUNCTION__; 56 56 $priority = 1; 57 57 $accepted_args = 2; 58 58 59 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );60 $hook->add_filter( $ tag, $callback_two, $priority, $accepted_args );59 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 60 $hook->add_filter( $hook_name, $callback_two, $priority, $accepted_args ); 61 61 62 $hook->remove_filter( $ tag, $callback_one, $priority );62 $hook->remove_filter( $hook_name, $callback_one, $priority ); 63 63 64 64 $this->assertCount( 1, $hook->callbacks[ $priority ] ); … … 69 69 $callback_two = '__return_false'; 70 70 $hook = new WP_Hook(); 71 $ tag= __FUNCTION__;71 $hook_name = __FUNCTION__; 72 72 $priority = 1; 73 73 $accepted_args = 2; 74 74 75 $hook->add_filter( $ tag, $callback_one, $priority, $accepted_args );76 $hook->add_filter( $ tag, $callback_two, $priority + 1, $accepted_args );75 $hook->add_filter( $hook_name, $callback_one, $priority, $accepted_args ); 76 $hook->add_filter( $hook_name, $callback_two, $priority + 1, $accepted_args ); 77 77 78 $hook->remove_filter( $ tag, $callback_one, $priority );78 $hook->remove_filter( $hook_name, $callback_one, $priority ); 79 79 $this->assertArrayNotHasKey( $priority, $hook->callbacks ); 80 80 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );
Note: See TracChangeset
for help on using the changeset viewer.