WordPress.org

Make WordPress Core

Changeset 25 in tests


Ignore:
Timestamp:
09/22/07 23:45:08 (7 years ago)
Author:
tellyworth
Message:

add test_actions

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_actions.php

    r24 r25  
    2020    } 
    2121 
     22    // one tag with multiple actions 
    2223    function test_multiple_actions() { 
    2324        $a1 = new MockAction(); 
     
    4849        $this->assertEquals(array($val), array_pop($a->get_args())); 
    4950    } 
     51     
     52    function test_action_args_2() { 
     53        $a1 = new MockAction(); 
     54        $a2 = new MockAction(); 
     55        $tag = rand_str(); 
     56        $val1 = rand_str(); 
     57        $val2 = rand_str(); 
     58 
     59        // a1 accepts two arguments, a2 doesn't 
     60        add_action($tag, array(&$a1, 'action'), 10, 2); 
     61        add_action($tag, array(&$a2, 'action')); 
     62        // call the action with two arguments 
     63        do_action($tag, $val1, $val2); 
     64 
     65        // a1 should be called with both args 
     66        $this->assertEquals(1, $a1->get_call_count()); 
     67        $this->assertEquals(array($val1, $val2), array_pop($a1->get_args())); 
     68 
     69        // a2 should be called with one only 
     70        $this->assertEquals(1, $a2->get_call_count()); 
     71        $this->assertEquals(array($val1), array_pop($a2->get_args())); 
     72    } 
     73 
     74    function test_action_priority() { 
     75        $a = new MockAction(); 
     76        $tag = rand_str(); 
     77         
     78        add_action($tag, array(&$a, 'action'), 10); 
     79        add_action($tag, array(&$a, 'action2'), 9); 
     80        do_action($tag); 
     81 
     82        // two events, one per action 
     83        $this->assertEquals(2, $a->get_call_count()); 
     84 
     85        $expected = array ( 
     86            // action2 is called first because it has priority 9 
     87            array ( 
     88                'action' => 'action2', 
     89                'tag' => $tag, 
     90                'args' => array('') 
     91            ), 
     92            // action 1 is called second 
     93            array ( 
     94                'action' => 'action', 
     95                'tag' => $tag, 
     96                'args' => array('') 
     97            ), 
     98        ); 
     99 
     100        $this->assertEquals($expected, $a->get_events()); 
     101    } 
     102 
    50103} 
    51104 
  • wp-testlib/utils.php

    r24 r25  
    3838        $args = func_get_args(); 
    3939        $current_action = $wp_actions[count($wp_actions)-1]; 
    40         $this->events[] = array('tag'=>$current_action, 'args'=>$args); 
     40        $this->events[] = array('action' => __FUNCTION__, 'tag'=>$current_action, 'args'=>$args); 
     41    } 
     42 
     43    function action2() { 
     44        global $wp_actions; 
     45 
     46        $args = func_get_args(); 
     47        $current_action = $wp_actions[count($wp_actions)-1]; 
     48        $this->events[] = array('action' => __FUNCTION__, 'tag'=>$current_action, 'args'=>$args); 
     49    } 
     50     
     51    // return a list of all the actions, tags and args 
     52    function get_events() { 
     53        return $this->events; 
    4154    } 
    4255 
     
    5669    function get_tags() { 
    5770        $out = array(); 
    58         foreach ($this->events as $e) 
     71        foreach ($this->events as $e) { 
    5972            $out[] = $e['tag']; 
     73        } 
    6074        return $out; 
    6175    } 
Note: See TracChangeset for help on using the changeset viewer.