Make WordPress Core

Changeset 60 in tests for wp-testcase/test_actions.php


Ignore:
Timestamp:
10/19/2007 05:48:50 AM (19 years ago)
Author:
tellyworth
Message:

test 'all' action

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_actions.php

    r26 r60  
    44
    55class WPTestActions extends WPTestCase {
     6        var $_old_filters;
     7
     8        function setUp() {
     9                $this->_old_filters = $GLOBALS['wp_filter'];
     10        }
     11
     12        function tearDown() {
     13                $GLOBALS['wp_filter'] = $this->_old_filters;
     14        }
     15
    616        function test_simple_action() {
    717                $a = new MockAction();
    818                $tag = rand_str();
    9                
     19
    1020                add_action($tag, array(&$a, 'action'));
    1121                do_action($tag);
     
    1828                $args = array_pop($a->get_args());
    1929                $this->assertEquals(array(''), $args);
     30        }
     31
     32        function test_remove_action() {
     33                $a = new MockAction();
     34                $tag = rand_str();
     35
     36                add_action($tag, array(&$a, 'action'));
     37                do_action($tag);
     38
     39                // make sure our hook was called correctly
     40                $this->assertEquals(1, $a->get_call_count());
     41                $this->assertEquals(array($tag), $a->get_tags());
     42
     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);
     46                $this->assertEquals(1, $a->get_call_count());
     47                $this->assertEquals(array($tag), $a->get_tags());
     48
    2049        }
    2150
     
    4978                $this->assertEquals(array($val), array_pop($a->get_args()));
    5079        }
    51        
     80
    5281        function test_action_args_2() {
    5382                $a1 = new MockAction();
     
    104133                $tag1 = rand_str();
    105134                $tag2 = rand_str();
    106                
     135
    107136                // do action tag1 but not tag2
    108137                do_action($tag1);
     
    120149
    121150        }
     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                var_dump('added', $GLOBALS['wp_filter']);
     185                remove_action('all', array(&$a, 'action'));
     186                var_dump('removed', $GLOBALS['wp_filter']);
     187                do_action($tag);
     188                $this->assertEquals(1, $a->get_call_count());
     189                $this->assertEquals(array($tag), $a->get_tags());
     190        }
     191
     192        function test_all_filter() {
     193                $a = new MockAction();
     194                $tag1 = rand_str();
     195                $tag2 = rand_str();
     196
     197                // add an 'all' action
     198                add_filter('all', array(&$a, 'filter'));
     199                // do some actions
     200                apply_filters($tag1, 'foo');
     201                apply_filters($tag2, 'foo');
     202                apply_filters($tag1, 'foo');
     203                apply_filters($tag1, 'foo');
     204
     205                // our action should have been called once for each tag
     206                $this->assertEquals(4, $a->get_call_count());
     207                // only our hook was called
     208                $this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags());
     209
     210        }
     211
    122212}
    123213
Note: See TracChangeset for help on using the changeset viewer.