Changeset 42343 for trunk/tests/phpunit/tests/filters.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/filters.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/filters.php
r39430 r42343 9 9 10 10 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->assertEquals( $val, apply_filters($tag, $val));11 $a = new MockAction(); 12 $tag = __FUNCTION__; 13 $val = __FUNCTION__ . '_val'; 14 15 add_filter( $tag, array( $a, 'filter' ) ); 16 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 17 17 18 18 // only one event occurred for the hook, with empty args 19 $this->assertEquals( 1, $a->get_call_count());19 $this->assertEquals( 1, $a->get_call_count() ); 20 20 // only our hook was called 21 $this->assertEquals( array($tag), $a->get_tags());21 $this->assertEquals( array( $tag ), $a->get_tags() ); 22 22 23 23 $argsvar = $a->get_args(); 24 $args = array_pop( $argsvar );25 $this->assertEquals( array($val), $args);24 $args = array_pop( $argsvar ); 25 $this->assertEquals( array( $val ), $args ); 26 26 } 27 27 28 28 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->assertEquals( $val, apply_filters($tag, $val));29 $a = new MockAction(); 30 $tag = __FUNCTION__; 31 $val = __FUNCTION__ . '_val'; 32 33 add_filter( $tag, array( $a, 'filter' ) ); 34 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 35 35 36 36 // make sure our hook was called correctly 37 $this->assertEquals( 1, $a->get_call_count());38 $this->assertEquals( array($tag), $a->get_tags());37 $this->assertEquals( 1, $a->get_call_count() ); 38 $this->assertEquals( array( $tag ), $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->assertEquals( $val, apply_filters($tag, $val));43 $this->assertEquals( 1, $a->get_call_count());44 $this->assertEquals( array($tag), $a->get_tags());41 remove_filter( $tag, array( $a, 'filter' ) ); 42 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 43 $this->assertEquals( 1, $a->get_call_count() ); 44 $this->assertEquals( array( $tag ), $a->get_tags() ); 45 45 46 46 } … … 50 50 $func = __FUNCTION__ . '_func'; 51 51 52 $this->assertFalse( has_filter( $tag, $func) );53 $this->assertFalse( has_filter( $tag) );54 add_filter( $tag, $func);55 $this->assertEquals( 10, has_filter( $tag, $func) );56 $this->assertTrue( has_filter( $tag) );57 remove_filter( $tag, $func);58 $this->assertFalse( has_filter( $tag, $func) );59 $this->assertFalse( has_filter( $tag) );52 $this->assertFalse( has_filter( $tag, $func ) ); 53 $this->assertFalse( has_filter( $tag ) ); 54 add_filter( $tag, $func ); 55 $this->assertEquals( 10, has_filter( $tag, $func ) ); 56 $this->assertTrue( has_filter( $tag ) ); 57 remove_filter( $tag, $func ); 58 $this->assertFalse( has_filter( $tag, $func ) ); 59 $this->assertFalse( has_filter( $tag ) ); 60 60 } 61 61 62 62 // one tag with multiple filters 63 63 function test_multiple_filters() { 64 $a1 = new MockAction();65 $a2 = new MockAction();64 $a1 = new MockAction(); 65 $a2 = new MockAction(); 66 66 $tag = __FUNCTION__; 67 67 $val = __FUNCTION__ . '_val'; 68 68 69 69 // add both filters to the hook 70 add_filter( $tag, array($a1, 'filter'));71 add_filter( $tag, array($a2, 'filter'));72 73 $this->assertEquals( $val, apply_filters($tag, $val));70 add_filter( $tag, array( $a1, 'filter' ) ); 71 add_filter( $tag, array( $a2, 'filter' ) ); 72 73 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 74 74 75 75 // both filters called once each 76 $this->assertEquals( 1, $a1->get_call_count());77 $this->assertEquals( 1, $a2->get_call_count());76 $this->assertEquals( 1, $a1->get_call_count() ); 77 $this->assertEquals( 1, $a2->get_call_count() ); 78 78 } 79 79 80 80 function test_filter_args_1() { 81 $a = new MockAction();81 $a = new MockAction(); 82 82 $tag = __FUNCTION__; 83 83 $val = __FUNCTION__ . '_val'; 84 84 $arg1 = __FUNCTION__ . '_arg1'; 85 85 86 add_filter( $tag, array($a, 'filter'), 10, 2);86 add_filter( $tag, array( $a, 'filter' ), 10, 2 ); 87 87 // call the filter with a single argument 88 $this->assertEquals( $val, apply_filters($tag, $val, $arg1));89 90 $this->assertEquals( 1, $a->get_call_count());88 $this->assertEquals( $val, apply_filters( $tag, $val, $arg1 ) ); 89 90 $this->assertEquals( 1, $a->get_call_count() ); 91 91 $argsvar = $a->get_args(); 92 92 $this->assertEquals( array( $val, $arg1 ), array_pop( $argsvar ) ); … … 94 94 95 95 function test_filter_args_2() { 96 $a1 = new MockAction();97 $a2 = new MockAction();96 $a1 = new MockAction(); 97 $a2 = new MockAction(); 98 98 $tag = __FUNCTION__; 99 99 $val = __FUNCTION__ . '_val'; … … 102 102 103 103 // a1 accepts two arguments, a2 doesn't 104 add_filter( $tag, array($a1, 'filter'), 10, 3);105 add_filter( $tag, array($a2, 'filter'));104 add_filter( $tag, array( $a1, 'filter' ), 10, 3 ); 105 add_filter( $tag, array( $a2, 'filter' ) ); 106 106 // call the filter with two arguments 107 $this->assertEquals( $val, apply_filters($tag, $val, $arg1, $arg2));107 $this->assertEquals( $val, apply_filters( $tag, $val, $arg1, $arg2 ) ); 108 108 109 109 // a1 should be called with both args 110 $this->assertEquals( 1, $a1->get_call_count());110 $this->assertEquals( 1, $a1->get_call_count() ); 111 111 $argsvar1 = $a1->get_args(); 112 112 $this->assertEquals( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) ); 113 113 114 114 // a2 should be called with one only 115 $this->assertEquals( 1, $a2->get_call_count());115 $this->assertEquals( 1, $a2->get_call_count() ); 116 116 $argsvar2 = $a2->get_args(); 117 117 $this->assertEquals( array( $val ), array_pop( $argsvar2 ) ); … … 119 119 120 120 function test_filter_priority() { 121 $a = new MockAction();121 $a = new MockAction(); 122 122 $tag = __FUNCTION__; 123 123 $val = __FUNCTION__ . '_val'; 124 124 125 125 // make two filters with different priorities 126 add_filter( $tag, array($a, 'filter'), 10);127 add_filter( $tag, array($a, 'filter2'), 9);128 $this->assertEquals( $val, apply_filters($tag, $val));126 add_filter( $tag, array( $a, 'filter' ), 10 ); 127 add_filter( $tag, array( $a, 'filter2' ), 9 ); 128 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 129 129 130 130 // there should be two events, one per filter 131 $this->assertEquals( 2, $a->get_call_count());132 133 $expected = array (131 $this->assertEquals( 2, $a->get_call_count() ); 132 133 $expected = array( 134 134 // filter2 is called first because it has priority 9 135 array (135 array( 136 136 'filter' => 'filter2', 137 'tag' => $tag,138 'args' => array($val)137 'tag' => $tag, 138 'args' => array( $val ), 139 139 ), 140 140 // filter 1 is called second 141 array (141 array( 142 142 'filter' => 'filter', 143 'tag' => $tag,144 'args' => array($val)143 'tag' => $tag, 144 'args' => array( $val ), 145 145 ), 146 146 ); 147 147 148 $this->assertEquals( $expected, $a->get_events());148 $this->assertEquals( $expected, $a->get_events() ); 149 149 } 150 150 151 151 function test_all_filter() { 152 $a = new MockAction();152 $a = new MockAction(); 153 153 $tag1 = __FUNCTION__ . '_1'; 154 154 $tag2 = __FUNCTION__ . '_2'; … … 156 156 157 157 // add an 'all' filter 158 add_filter( 'all', array($a, 'filterall'));158 add_filter( 'all', array( $a, 'filterall' ) ); 159 159 // do some filters 160 $this->assertEquals( $val, apply_filters($tag1, $val));161 $this->assertEquals( $val, apply_filters($tag2, $val));162 $this->assertEquals( $val, apply_filters($tag1, $val));163 $this->assertEquals( $val, apply_filters($tag1, $val));160 $this->assertEquals( $val, apply_filters( $tag1, $val ) ); 161 $this->assertEquals( $val, apply_filters( $tag2, $val ) ); 162 $this->assertEquals( $val, apply_filters( $tag1, $val ) ); 163 $this->assertEquals( $val, apply_filters( $tag1, $val ) ); 164 164 165 165 // our filter should have been called once for each apply_filters call 166 $this->assertEquals( 4, $a->get_call_count());166 $this->assertEquals( 4, $a->get_call_count() ); 167 167 // the right hooks should have been called in order 168 $this->assertEquals( array($tag1, $tag2, $tag1, $tag1), $a->get_tags());169 170 remove_filter( 'all', array($a, 'filterall'));171 $this->assertFalse( has_filter( 'all', array($a, 'filterall')) );168 $this->assertEquals( array( $tag1, $tag2, $tag1, $tag1 ), $a->get_tags() ); 169 170 remove_filter( 'all', array( $a, 'filterall' ) ); 171 $this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) ); 172 172 173 173 } 174 174 175 175 function test_remove_all_filter() { 176 $a = new MockAction();177 $tag = __FUNCTION__; 178 $val = __FUNCTION__ . '_val'; 179 180 add_filter( 'all', array($a, 'filterall'));181 $this->assertTrue( has_filter( 'all') );182 $this->assertEquals( 10, has_filter( 'all', array($a, 'filterall')) );183 $this->assertEquals( $val, apply_filters($tag, $val));176 $a = new MockAction(); 177 $tag = __FUNCTION__; 178 $val = __FUNCTION__ . '_val'; 179 180 add_filter( 'all', array( $a, 'filterall' ) ); 181 $this->assertTrue( has_filter( 'all' ) ); 182 $this->assertEquals( 10, has_filter( 'all', array( $a, 'filterall' ) ) ); 183 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 184 184 185 185 // make sure our hook was called correctly 186 $this->assertEquals( 1, $a->get_call_count());187 $this->assertEquals( array($tag), $a->get_tags());186 $this->assertEquals( 1, $a->get_call_count() ); 187 $this->assertEquals( array( $tag ), $a->get_tags() ); 188 188 189 189 // now remove the filter, do it again, and make sure it's not called this time 190 remove_filter( 'all', array($a, 'filterall'));191 $this->assertFalse( has_filter( 'all', array($a, 'filterall')) );192 $this->assertFalse( has_filter( 'all') );193 $this->assertEquals( $val, apply_filters($tag, $val));190 remove_filter( 'all', array( $a, 'filterall' ) ); 191 $this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) ); 192 $this->assertFalse( has_filter( 'all' ) ); 193 $this->assertEquals( $val, apply_filters( $tag, $val ) ); 194 194 // call cound should remain at 1 195 $this->assertEquals( 1, $a->get_call_count());196 $this->assertEquals( array($tag), $a->get_tags());195 $this->assertEquals( 1, $a->get_call_count() ); 196 $this->assertEquals( array( $tag ), $a->get_tags() ); 197 197 } 198 198 … … 201 201 */ 202 202 function test_remove_all_filters_should_respect_the_priority_argument() { 203 $a = new MockAction();203 $a = new MockAction(); 204 204 $tag = __FUNCTION__; 205 205 $val = __FUNCTION__ . '_val'; … … 221 221 function test_filter_ref_array() { 222 222 $obj = new stdClass(); 223 $a = new MockAction();224 $tag = __FUNCTION__; 225 226 add_action( $tag, array($a, 'filter'));227 228 apply_filters_ref_array( $tag, array(&$obj));223 $a = new MockAction(); 224 $tag = __FUNCTION__; 225 226 add_action( $tag, array( $a, 'filter' ) ); 227 228 apply_filters_ref_array( $tag, array( &$obj ) ); 229 229 230 230 $args = $a->get_args(); 231 $this->assertSame( $args[0][0], $obj);231 $this->assertSame( $args[0][0], $obj ); 232 232 // just in case we don't trust assertSame 233 233 $obj->foo = true; 234 $this->assertFalse( empty( $args[0][0]->foo) );234 $this->assertFalse( empty( $args[0][0]->foo ) ); 235 235 } 236 236 … … 240 240 function test_filter_ref_array_result() { 241 241 $obj = new stdClass(); 242 $a = new MockAction();243 $b = new MockAction();244 $tag = __FUNCTION__; 245 246 add_action( $tag, array($a, 'filter_append'), 10, 2);247 add_action( $tag, array($b, 'filter_append'), 10, 2);248 249 $result = apply_filters_ref_array( $tag, array('string', &$obj));250 251 $this->assertEquals( $result, 'string_append_append');242 $a = new MockAction(); 243 $b = new MockAction(); 244 $tag = __FUNCTION__; 245 246 add_action( $tag, array( $a, 'filter_append' ), 10, 2 ); 247 add_action( $tag, array( $b, 'filter_append' ), 10, 2 ); 248 249 $result = apply_filters_ref_array( $tag, array( 'string', &$obj ) ); 250 251 $this->assertEquals( $result, 'string_append_append' ); 252 252 253 253 $args = $a->get_args(); 254 $this->assertSame( $args[0][1], $obj);254 $this->assertSame( $args[0][1], $obj ); 255 255 // just in case we don't trust assertSame 256 256 $obj->foo = true; 257 $this->assertFalse( empty( $args[0][1]->foo) );257 $this->assertFalse( empty( $args[0][1]->foo ) ); 258 258 259 259 $args = $b->get_args(); 260 $this->assertSame( $args[0][1], $obj);260 $this->assertSame( $args[0][1], $obj ); 261 261 // just in case we don't trust assertSame 262 262 $obj->foo = true; 263 $this->assertFalse( empty( $args[0][1]->foo) );264 265 } 266 267 function _self_removal( $tag) {268 remove_action( $tag, array( $this, '_self_removal'), 10, 1 );263 $this->assertFalse( empty( $args[0][1]->foo ) ); 264 265 } 266 267 function _self_removal( $tag ) { 268 remove_action( $tag, array( $this, '_self_removal' ), 10, 1 ); 269 269 return $tag; 270 270 } … … 274 274 */ 275 275 function test_has_filter_after_remove_all_filters() { 276 $a = new MockAction();276 $a = new MockAction(); 277 277 $tag = __FUNCTION__; 278 278 $val = __FUNCTION__ . '_val'; … … 379 379 public function _other_priority_action() { 380 380 global $wp_filter; 381 $this->current_priority = $wp_filter[ 'the_content']->current_priority();381 $this->current_priority = $wp_filter['the_content']->current_priority(); 382 382 } 383 383 }
Note: See TracChangeset
for help on using the changeset viewer.