Make WordPress Core

Changeset 62 in tests for wp-testcase/test_filters.php


Ignore:
Timestamp:
10/19/2007 09:03:25 AM (19 years ago)
Author:
tellyworth
Message:

add test_filters

File:
1 copied

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_filters.php

    r61 r62  
    33// test do_action and related functions
    44
    5 class WPTestActions extends WPTestCase {
     5class WPTestFilters extends WPTestCase {
    66        var $_old_filters;
    77
    88        function setUp() {
    99                $this->_old_filters = $GLOBALS['wp_filter'];
     10                unset($GLOBALS['merged_filters']);
     11                // this screws up filter priorities
     12                unset($GLOBALS['wp_filter']['all']);
    1013        }
    1114
     
    1417        }
    1518
    16         function test_simple_action() {
     19        function test_simple_filter() {
    1720                $a = new MockAction();
    1821                $tag = rand_str();
     22                $val = rand_str();
    1923
    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));
    2226
    2327                // only one event occurred for the hook, with empty args
     
    2731
    2832                $args = array_pop($a->get_args());
    29                 $this->assertEquals(array(''), $args);
     33                $this->assertEquals(array($val), $args);
    3034        }
    3135
    32         function test_remove_action() {
     36        function test_remove_filter() {
    3337                $a = new MockAction();
    3438                $tag = rand_str();
     39                $val = rand_str();
    3540
    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));
    3843
    3944                // make sure our hook was called correctly
     
    4146                $this->assertEquals(array($tag), $a->get_tags());
    4247
    43                 // now remove the action, do it again, and make sure it's not called this time
    44                 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));
    4651                $this->assertEquals(1, $a->get_call_count());
    4752                $this->assertEquals(array($tag), $a->get_tags());
     
    4954        }
    5055
    51         // one tag with multiple actions
    52         function test_multiple_actions() {
     56        // one tag with multiple filters
     57        function test_multiple_filters() {
    5358                $a1 = new MockAction();
    5459                $a2 = new MockAction();
    5560                $tag = rand_str();
     61                $val = rand_str();
    5662
    57                 // add both actions to the hook
    58                 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'));
    6066
    61                 do_action($tag);
     67                $this->assertEquals($val, apply_filters($tag, $val));
    6268
    63                 // both actions called once each
     69                // both filters called once each
    6470                $this->assertEquals(1, $a1->get_call_count());
    6571                $this->assertEquals(1, $a2->get_call_count());
    6672        }
    6773
    68         function test_action_args_1() {
     74        function test_filter_args_1() {
    6975                $a = new MockAction();
    7076                $tag = rand_str();
    7177                $val = rand_str();
     78                $arg1 = rand_str();
    7279
    73                 add_action($tag, array(&$a, 'action'));
    74                 // call the action with a single argument
    75                 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));
    7683
    7784                $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()));
    7986        }
    8087
    81         function test_action_args_2() {
     88        function test_filter_args_2() {
    8289                $a1 = new MockAction();
    8390                $a2 = new MockAction();
    8491                $tag = rand_str();
    85                 $val1 = rand_str();
    86                 $val2 = rand_str();
     92                $val = rand_str();
     93                $arg1 = rand_str();
     94                $arg2 = rand_str();
    8795
    8896                // 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 action with two arguments
    92                 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));
    93101
    94102                // a1 should be called with both args
    95103                $this->assertEquals(1, $a1->get_call_count());
    96                 $this->assertEquals(array($val1, $val2), array_pop($a1->get_args()));
     104                $this->assertEquals(array($val, $arg1, $arg2), array_pop($a1->get_args()));
    97105
    98106                // a2 should be called with one only
    99107                $this->assertEquals(1, $a2->get_call_count());
    100                 $this->assertEquals(array($val1), array_pop($a2->get_args()));
     108                $this->assertEquals(array($val), array_pop($a2->get_args()));
    101109        }
    102110
    103         function test_action_priority() {
    104                 $a = new MockAction();
     111        function test_filter_priority() {
     112                $a = new MockAction(1);
    105113                $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();
    110115
    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
    112123                $this->assertEquals(2, $a->get_call_count());
    113124
    114125                $expected = array (
    115                         // action2 is called first because it has priority 9
     126                        // filter2 is called first because it has priority 9
    116127                        array (
    117                                 'action' => 'action2',
     128                                'filter' => 'filter2',
    118129                                'tag' => $tag,
    119                                 'args' => array('')
     130                                'args' => array($val)
    120131                        ),
    121                         // action 1 is called second
     132                        // filter 1 is called second
    122133                        array (
    123                                 'action' => 'action',
     134                                'filter' => 'filter',
    124135                                'tag' => $tag,
    125                                 'args' => array('')
     136                                'args' => array($val)
    126137                        ),
    127138                );
    128139
    129140                $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 tag2
    137                 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 times
    142                 $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 correct
    147                 $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' action
    158                 add_action('all', array(&$a, 'action'));
    159                 // do some actions
    160                 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 tag
    166                 $this->assertEquals(4, $a->get_call_count());
    167                 // only our hook was called
    168                 $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 correctly
    180                 $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 time
    184                 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());
    188141        }
    189142
     
    193146                $tag2 = rand_str();
    194147
    195                 // add an 'all' action
     148                // add an 'all' filter
    196149                add_filter('all', array(&$a, 'filter'));
    197                 // do some actions
    198                 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));
    202155
    203                 // our action should have been called once for each tag
     156                // our filter should have been called once for each apply_filters call
    204157                $this->assertEquals(4, $a->get_call_count());
    205                 // only our hook was called
     158                // the right hooks should have been called in order
    206159                $this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags());
    207160
    208161        }
    209162
     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
    210184}
    211185
Note: See TracChangeset for help on using the changeset viewer.