Changeset 62 in tests
- Timestamp:
- 10/19/2007 09:03:25 AM (17 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
wp-testcase/test_filters.php
r61 r62 3 3 // test do_action and related functions 4 4 5 class WPTest Actions extends WPTestCase {5 class WPTestFilters extends WPTestCase { 6 6 var $_old_filters; 7 7 8 8 function setUp() { 9 9 $this->_old_filters = $GLOBALS['wp_filter']; 10 unset($GLOBALS['merged_filters']); 11 // this screws up filter priorities 12 unset($GLOBALS['wp_filter']['all']); 10 13 } 11 14 … … 14 17 } 15 18 16 function test_simple_ action() {19 function test_simple_filter() { 17 20 $a = new MockAction(); 18 21 $tag = rand_str(); 22 $val = rand_str(); 19 23 20 add_ action($tag, array(&$a, 'action'));21 do_action($tag);24 add_filter($tag, array(&$a, 'filter')); 25 $this->assertEquals($val, apply_filters($tag, $val)); 22 26 23 27 // only one event occurred for the hook, with empty args … … 27 31 28 32 $args = array_pop($a->get_args()); 29 $this->assertEquals(array( ''), $args);33 $this->assertEquals(array($val), $args); 30 34 } 31 35 32 function test_remove_ action() {36 function test_remove_filter() { 33 37 $a = new MockAction(); 34 38 $tag = rand_str(); 39 $val = rand_str(); 35 40 36 add_ action($tag, array(&$a, 'action'));37 do_action($tag);41 add_filter($tag, array(&$a, 'filter')); 42 $this->assertEquals($val, apply_filters($tag, $val)); 38 43 39 44 // make sure our hook was called correctly … … 41 46 $this->assertEquals(array($tag), $a->get_tags()); 42 47 43 // now remove the action, do it again, and make sure it's not called this time44 remove_ action($tag, array(&$a, 'action'));45 do_action($tag);48 // now remove the filter, do it again, and make sure it's not called this time 49 remove_filter($tag, array(&$a, 'filter')); 50 $this->assertEquals($val, apply_filters($tag, $val)); 46 51 $this->assertEquals(1, $a->get_call_count()); 47 52 $this->assertEquals(array($tag), $a->get_tags()); … … 49 54 } 50 55 51 // one tag with multiple actions52 function test_multiple_ actions() {56 // one tag with multiple filters 57 function test_multiple_filters() { 53 58 $a1 = new MockAction(); 54 59 $a2 = new MockAction(); 55 60 $tag = rand_str(); 61 $val = rand_str(); 56 62 57 // add both actions to the hook58 add_ action($tag, array(&$a1, 'action'));59 add_ action($tag, array(&$a2, 'action'));63 // add both filters to the hook 64 add_filter($tag, array(&$a1, 'filter')); 65 add_filter($tag, array(&$a2, 'filter')); 60 66 61 do_action($tag);67 $this->assertEquals($val, apply_filters($tag, $val)); 62 68 63 // both actions called once each69 // both filters called once each 64 70 $this->assertEquals(1, $a1->get_call_count()); 65 71 $this->assertEquals(1, $a2->get_call_count()); 66 72 } 67 73 68 function test_ action_args_1() {74 function test_filter_args_1() { 69 75 $a = new MockAction(); 70 76 $tag = rand_str(); 71 77 $val = rand_str(); 78 $arg1 = rand_str(); 72 79 73 add_ action($tag, array(&$a, 'action'));74 // call the actionwith a single argument75 do_action($tag, $val);80 add_filter($tag, array(&$a, 'filter'), 10, 2); 81 // call the filter with a single argument 82 $this->assertEquals($val, apply_filters($tag, $val, $arg1)); 76 83 77 84 $this->assertEquals(1, $a->get_call_count()); 78 $this->assertEquals(array($val ), array_pop($a->get_args()));85 $this->assertEquals(array($val, $arg1), array_pop($a->get_args())); 79 86 } 80 87 81 function test_ action_args_2() {88 function test_filter_args_2() { 82 89 $a1 = new MockAction(); 83 90 $a2 = new MockAction(); 84 91 $tag = rand_str(); 85 $val1 = rand_str(); 86 $val2 = rand_str(); 92 $val = rand_str(); 93 $arg1 = rand_str(); 94 $arg2 = rand_str(); 87 95 88 96 // a1 accepts two arguments, a2 doesn't 89 add_ action($tag, array(&$a1, 'action'), 10, 2);90 add_ action($tag, array(&$a2, 'action'));91 // call the actionwith two arguments92 do_action($tag, $val1, $val2);97 add_filter($tag, array(&$a1, 'filter'), 10, 3); 98 add_filter($tag, array(&$a2, 'filter')); 99 // call the filter with two arguments 100 $this->assertEquals($val, apply_filters($tag, $val, $arg1, $arg2)); 93 101 94 102 // a1 should be called with both args 95 103 $this->assertEquals(1, $a1->get_call_count()); 96 $this->assertEquals(array($val 1, $val2), array_pop($a1->get_args()));104 $this->assertEquals(array($val, $arg1, $arg2), array_pop($a1->get_args())); 97 105 98 106 // a2 should be called with one only 99 107 $this->assertEquals(1, $a2->get_call_count()); 100 $this->assertEquals(array($val 1), array_pop($a2->get_args()));108 $this->assertEquals(array($val), array_pop($a2->get_args())); 101 109 } 102 110 103 function test_ action_priority() {104 $a = new MockAction( );111 function test_filter_priority() { 112 $a = new MockAction(1); 105 113 $tag = rand_str(); 106 107 add_action($tag, array(&$a, 'action'), 10); 108 add_action($tag, array(&$a, 'action2'), 9); 109 do_action($tag); 114 $val = rand_str(); 110 115 111 // two events, one per action 116 // make two filters with different priorities 117 add_filter($tag, array(&$a, 'filter'), 10); 118 add_filter($tag, array(&$a, 'filter2'), 9); 119 $this->assertEquals($val, apply_filters($tag, $val)); 120 dmp('after',$GLOBALS['wp_filter'][$tag]); 121 122 // there should be two events, one per filter 112 123 $this->assertEquals(2, $a->get_call_count()); 113 124 114 125 $expected = array ( 115 // action2 is called first because it has priority 9126 // filter2 is called first because it has priority 9 116 127 array ( 117 ' action' => 'action2',128 'filter' => 'filter2', 118 129 'tag' => $tag, 119 'args' => array( '')130 'args' => array($val) 120 131 ), 121 // action1 is called second132 // filter 1 is called second 122 133 array ( 123 ' action' => 'action',134 'filter' => 'filter', 124 135 'tag' => $tag, 125 'args' => array( '')136 'args' => array($val) 126 137 ), 127 138 ); 128 139 129 140 $this->assertEquals($expected, $a->get_events()); 130 }131 132 function test_did_action() {133 $tag1 = rand_str();134 $tag2 = rand_str();135 136 // do action tag1 but not tag2137 do_action($tag1);138 $this->assertEquals(1, did_action($tag1));139 $this->assertEquals(0, did_action($tag2));140 141 // do action tag2 a random number of times142 $count = rand(0, 10);143 for ($i=0; $i<$count; $i++)144 do_action($tag2);145 146 // tag1's count hasn't changed, tag2 should be correct147 $this->assertEquals(1, did_action($tag1));148 $this->assertEquals($count, did_action($tag2));149 150 }151 152 function test_all_action() {153 $a = new MockAction();154 $tag1 = rand_str();155 $tag2 = rand_str();156 157 // add an 'all' action158 add_action('all', array(&$a, 'action'));159 // do some actions160 do_action($tag1);161 do_action($tag2);162 do_action($tag1);163 do_action($tag1);164 165 // our action should have been called once for each tag166 $this->assertEquals(4, $a->get_call_count());167 // only our hook was called168 $this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags());169 170 }171 172 function test_remove_all_action() {173 $a = new MockAction();174 $tag = rand_str();175 176 add_action('all', array(&$a, 'action'));177 do_action($tag);178 179 // make sure our hook was called correctly180 $this->assertEquals(1, $a->get_call_count());181 $this->assertEquals(array($tag), $a->get_tags());182 183 // now remove the action, do it again, and make sure it's not called this time184 remove_action('all', array(&$a, 'action'));185 do_action($tag);186 $this->assertEquals(1, $a->get_call_count());187 $this->assertEquals(array($tag), $a->get_tags());188 141 } 189 142 … … 193 146 $tag2 = rand_str(); 194 147 195 // add an 'all' action148 // add an 'all' filter 196 149 add_filter('all', array(&$a, 'filter')); 197 // do some actions198 apply_filters($tag1, 'foo');199 apply_filters($tag2, 'foo');200 apply_filters($tag1, 'foo');201 apply_filters($tag1, 'foo');150 // do some filters 151 $this->assertEquals($val, apply_filters($tag1, $val)); 152 $this->assertEquals($val, apply_filters($tag2, $val)); 153 $this->assertEquals($val, apply_filters($tag1, $val)); 154 $this->assertEquals($val, apply_filters($tag1, $val)); 202 155 203 // our action should have been called once for each tag156 // our filter should have been called once for each apply_filters call 204 157 $this->assertEquals(4, $a->get_call_count()); 205 // only our hook was called158 // the right hooks should have been called in order 206 159 $this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags()); 207 160 208 161 } 209 162 163 function test_remove_all_filter() { 164 $a = new MockAction(); 165 $tag = rand_str(); 166 $val = rand_str(); 167 168 add_filter('all', array(&$a, 'filter')); 169 $this->assertEquals($val, apply_filters($tag1, $val)); 170 171 // make sure our hook was called correctly 172 $this->assertEquals(1, $a->get_call_count()); 173 $this->assertEquals(array($tag), $a->get_tags()); 174 175 // now remove the filter, do it again, and make sure it's not called this time 176 remove_filter('all', array(&$a, 'filter')); 177 $this->assertEquals($val, apply_filters($tag1, $val)); 178 // call cound should remain at 1 179 $this->assertEquals(1, $a->get_call_count()); 180 $this->assertEquals(array($tag), $a->get_tags()); 181 } 182 183 210 184 } 211 185
Note: See TracChangeset
for help on using the changeset viewer.