Changeset 53804 for trunk/tests/phpunit/tests/filters.php
- Timestamp:
- 07/31/2022 03:03:46 PM (4 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/filters.php (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)