Changeset 47122 for trunk/tests/phpunit/tests/actions.php
- Timestamp:
- 01/29/2020 12:43:23 AM (6 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/actions.php (modified) (20 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/actions.php
r46707 r47122 15 15 do_action( $tag ); 16 16 17 // only one event occurred for the hook, with empty args17 // Only one event occurred for the hook, with empty args. 18 18 $this->assertEquals( 1, $a->get_call_count() ); 19 // only our hook was called19 // Only our hook was called. 20 20 $this->assertEquals( array( $tag ), $a->get_tags() ); 21 21 … … 32 32 do_action( $tag ); 33 33 34 // make sure our hook was called correctly34 // Make sure our hook was called correctly. 35 35 $this->assertEquals( 1, $a->get_call_count() ); 36 36 $this->assertEquals( array( $tag ), $a->get_tags() ); 37 37 38 // now remove the action, do it again, and make sure it's not called this time38 // Now remove the action, do it again, and make sure it's not called this time. 39 39 remove_action( $tag, array( &$a, 'action' ) ); 40 40 do_action( $tag ); … … 58 58 } 59 59 60 // one tag with multiple actions60 // One tag with multiple actions. 61 61 function test_multiple_actions() { 62 62 $a1 = new MockAction(); … … 64 64 $tag = __FUNCTION__; 65 65 66 // add both actions to the hook66 // Add both actions to the hook. 67 67 add_action( $tag, array( &$a1, 'action' ) ); 68 68 add_action( $tag, array( &$a2, 'action' ) ); … … 70 70 do_action( $tag ); 71 71 72 // both actions called once each72 // Both actions called once each. 73 73 $this->assertEquals( 1, $a1->get_call_count() ); 74 74 $this->assertEquals( 1, $a2->get_call_count() ); … … 81 81 82 82 add_action( $tag, array( &$a, 'action' ) ); 83 // call the action with a single argument83 // Call the action with a single argument. 84 84 do_action( $tag, $val ); 85 85 … … 97 97 $val2 = __FUNCTION__ . '_val2'; 98 98 99 // a1 accepts two arguments, a2 doesn't99 // $a1 accepts two arguments, $a2 doesn't. 100 100 add_action( $tag, array( &$a1, 'action' ), 10, 2 ); 101 101 add_action( $tag, array( &$a2, 'action' ) ); 102 // call the action with two arguments102 // Call the action with two arguments. 103 103 do_action( $tag, $val1, $val2 ); 104 104 105 105 $call_count = $a1->get_call_count(); 106 // a1 should be called with both args106 // $a1 should be called with both args. 107 107 $this->assertEquals( 1, $call_count ); 108 108 $argsvar1 = $a1->get_args(); 109 109 $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) ); 110 110 111 // a2 should be called with one only111 // $a2 should be called with one only. 112 112 $this->assertEquals( 1, $a2->get_call_count() ); 113 113 $argsvar2 = $a2->get_args(); … … 130 130 $val2 = __FUNCTION__ . '_val2'; 131 131 132 // a1 accepts two arguments, a2 doesn't, a3 accepts two arguments132 // $a1 accepts two arguments, $a2 doesn't, $a3 accepts two arguments. 133 133 add_action( $tag, array( &$a1, 'action' ), 10, 2 ); 134 134 add_action( $tag, array( &$a2, 'action' ) ); 135 135 add_action( $tag, array( &$a3, 'action' ), 10, 2 ); 136 // call the action with two arguments136 // Call the action with two arguments. 137 137 do_action( $tag, $val1, $val2 ); 138 138 139 139 $call_count = $a1->get_call_count(); 140 // a1 should be called with both args140 // $a1 should be called with both args. 141 141 $this->assertEquals( 1, $call_count ); 142 142 $argsvar1 = $a1->get_args(); 143 143 $this->assertEquals( array( $val1, $val2 ), array_pop( $argsvar1 ) ); 144 144 145 // a2 should be called with one only145 // $a2 should be called with one only. 146 146 $this->assertEquals( 1, $a2->get_call_count() ); 147 147 $argsvar2 = $a2->get_args(); 148 148 $this->assertEquals( array( $val1 ), array_pop( $argsvar2 ) ); 149 149 150 // a3 should be called with both args150 // $a3 should be called with both args. 151 151 $this->assertEquals( 1, $a3->get_call_count() ); 152 152 $argsvar3 = $a3->get_args(); … … 181 181 do_action( $tag ); 182 182 183 // two events, one per action183 // Two events, one per action. 184 184 $this->assertEquals( 2, $a->get_call_count() ); 185 185 186 186 $expected = array( 187 // action2 is called first because it has priority 9187 // 'action2' is called first because it has priority 9. 188 188 array( 189 189 'action' => 'action2', … … 191 191 'args' => array( '' ), 192 192 ), 193 // action 1 is called second193 // 'action' is called second. 194 194 array( 195 195 'action' => 'action', … … 206 206 $tag2 = 'action2'; 207 207 208 // do action tag1 but not tag2208 // Do action $tag1 but not $tag2. 209 209 do_action( $tag1 ); 210 210 $this->assertEquals( 1, did_action( $tag1 ) ); 211 211 $this->assertEquals( 0, did_action( $tag2 ) ); 212 212 213 // do action tag2 a random number of times213 // Do action $tag2 a random number of times. 214 214 $count = rand( 0, 10 ); 215 215 for ( $i = 0; $i < $count; $i++ ) { … … 217 217 } 218 218 219 // tag1's count hasn't changed, tag2 should be correct219 // $tag1's count hasn't changed, $tag2 should be correct. 220 220 $this->assertEquals( 1, did_action( $tag1 ) ); 221 221 $this->assertEquals( $count, did_action( $tag2 ) ); … … 228 228 $tag2 = __FUNCTION__ . '_2'; 229 229 230 // add an 'all' action230 // Add an 'all' action. 231 231 add_action( 'all', array( &$a, 'action' ) ); 232 232 $this->assertEquals( 10, has_filter( 'all', array( &$a, 'action' ) ) ); 233 // do some actions233 // Do some actions. 234 234 do_action( $tag1 ); 235 235 do_action( $tag2 ); … … 237 237 do_action( $tag1 ); 238 238 239 // our action should have been called once for each tag239 // Our action should have been called once for each tag. 240 240 $this->assertEquals( 4, $a->get_call_count() ); 241 // only our hook was called241 // Only our hook was called. 242 242 $this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() ); 243 243 … … 255 255 do_action( $tag ); 256 256 257 // make sure our hook was called correctly257 // Make sure our hook was called correctly. 258 258 $this->assertEquals( 1, $a->get_call_count() ); 259 259 $this->assertEquals( array( $tag ), $a->get_tags() ); 260 260 261 // now remove the action, do it again, and make sure it's not called this time261 // Now remove the action, do it again, and make sure it's not called this time. 262 262 remove_action( 'all', array( &$a, 'action' ) ); 263 263 $this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) ); … … 278 278 $args = $a->get_args(); 279 279 $this->assertSame( $args[0][0], $obj ); 280 // just in case we don't trust assertSame280 // Just in case we don't trust assertSame(). 281 281 $obj->foo = true; 282 282 $this->assertFalse( empty( $args[0][0]->foo ) ); … … 457 457 function test_doing_filter() { 458 458 global $wp_current_filter; 459 $wp_current_filter = array(); // Set to an empty array first 460 461 $this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed462 $this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed 459 $wp_current_filter = array(); // Set to an empty array first. 460 461 $this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed. 462 $this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed. 463 463 464 464 $wp_current_filter[] = 'testing'; 465 465 466 $this->assertTrue( doing_filter() ); // No action is passed in, and a filter is being processed467 $this->assertTrue( doing_filter( 'testing' ) ); // Filter is passed in and is being processed468 $this->assertFalse( doing_filter( 'something_else' ) ); // Filter is passed in but not being processed 466 $this->assertTrue( doing_filter() ); // No action is passed in, and a filter is being processed. 467 $this->assertTrue( doing_filter( 'testing' ) ); // Filter is passed in and is being processed. 468 $this->assertFalse( doing_filter( 'something_else' ) ); // Filter is passed in but not being processed. 469 469 470 470 $wp_current_filter = array(); … … 476 476 function test_doing_action() { 477 477 global $wp_current_filter; 478 $wp_current_filter = array(); // Set to an empty array first 479 480 $this->assertFalse( doing_action() ); // No action is passed in, and no filter is being processed481 $this->assertFalse( doing_action( 'testing' ) ); // Action is passed in but not being processed 478 $wp_current_filter = array(); // Set to an empty array first. 479 480 $this->assertFalse( doing_action() ); // No action is passed in, and no filter is being processed. 481 $this->assertFalse( doing_action( 'testing' ) ); // Action is passed in but not being processed. 482 482 483 483 $wp_current_filter[] = 'testing'; 484 484 485 $this->assertTrue( doing_action() ); // No action is passed in, and a filter is being processed486 $this->assertTrue( doing_action( 'testing' ) ); // Action is passed in and is being processed487 $this->assertFalse( doing_action( 'something_else' ) ); // Action is passed in but not being processed 485 $this->assertTrue( doing_action() ); // No action is passed in, and a filter is being processed. 486 $this->assertTrue( doing_action( 'testing' ) ); // Action is passed in and is being processed. 487 $this->assertFalse( doing_action( 'something_else' ) ); // Action is passed in but not being processed. 488 488 489 489 $wp_current_filter = array(); … … 494 494 */ 495 495 function test_doing_filter_real() { 496 $this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed497 $this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed 496 $this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed. 497 $this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed. 498 498 499 499 add_filter( 'testing', array( $this, 'apply_testing_filter' ) ); … … 506 506 $this->assertTrue( $this->apply_testing_filter ); 507 507 508 $this->assertFalse( doing_filter() ); // No longer doing any filters509 $this->assertFalse( doing_filter( 'testing' ) ); // No longer doing this filter 508 $this->assertFalse( doing_filter() ); // No longer doing any filters. 509 $this->assertFalse( doing_filter( 'testing' ) ); // No longer doing this filter. 510 510 } 511 511
Note: See TracChangeset
for help on using the changeset viewer.