Make WordPress Core

Changeset 53805


Ignore:
Timestamp:
07/31/2022 04:25:27 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Update the terminology used for action or filter names in MockAction class.

This replaces the "tag" wording with "hook name" where appropriate, to match the core function signatures.

Includes:

  • Introducing a ::get_hook_names() method instead of ::get_tags(), keeping the latter as an alias.
  • Adding a hook_name key to the ::$events property, keeping tag for backward compatibility for now.
  • Adding missing @since tags for class methods.

Follow-up to [24/tests], [62/tests], [70/tests], [50807], [53804].

See #55652.

Location:
trunk/tests/phpunit
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/utils.php

    r52009 r53805  
    5757 *     $ma = new MockAction();
    5858 *     add_action( 'foo', array( &$ma, 'action' ) );
     59 *
     60 * @since UT (3.7.0)
    5961 */
    6062class MockAction {
     
    6466    /**
    6567     * PHP5 constructor.
     68     *
     69     * @since UT (3.7.0)
    6670     */
    6771    public function __construct( $debug = 0 ) {
     
    7074    }
    7175
     76    /**
     77     * @since UT (3.7.0)
     78     */
    7279    public function reset() {
    7380        $this->events = array();
    7481    }
    7582
     83    /**
     84     * @since UT (3.7.0)
     85     */
    7686    public function current_filter() {
     87        global $wp_actions;
     88
    7789        if ( is_callable( 'current_filter' ) ) {
    7890            return current_filter();
    7991        }
    80         global $wp_actions;
     92
    8193        return end( $wp_actions );
    8294    }
    8395
     96    /**
     97     * @since UT (3.7.0)
     98     */
    8499    public function action( $arg ) {
     100        $current_filter = $this->current_filter();
     101
    85102        if ( $this->debug ) {
    86             dmp( __FUNCTION__, $this->current_filter() );
    87         }
    88         $args           = func_get_args();
     103            dmp( __FUNCTION__, $current_filter );
     104        }
     105
    89106        $this->events[] = array(
    90             'action' => __FUNCTION__,
    91             'tag'    => $this->current_filter(),
    92             'args'   => $args,
     107            'action'    => __FUNCTION__,
     108            'hook_name' => $current_filter,
     109            'tag'       => $current_filter, // Back compat.
     110            'args'      => func_get_args(),
    93111        );
     112
    94113        return $arg;
    95114    }
    96115
     116    /**
     117     * @since UT (3.7.0)
     118     */
    97119    public function action2( $arg ) {
     120        $current_filter = $this->current_filter();
     121
    98122        if ( $this->debug ) {
    99             dmp( __FUNCTION__, $this->current_filter() );
    100         }
    101 
    102         $args           = func_get_args();
     123            dmp( __FUNCTION__, $current_filter );
     124        }
     125
    103126        $this->events[] = array(
    104             'action' => __FUNCTION__,
    105             'tag'    => $this->current_filter(),
    106             'args'   => $args,
     127            'action'    => __FUNCTION__,
     128            'hook_name' => $current_filter,
     129            'tag'       => $current_filter, // Back compat.
     130            'args'      => func_get_args(),
    107131        );
     132
    108133        return $arg;
    109134    }
    110135
     136    /**
     137     * @since UT (3.7.0)
     138     */
    111139    public function filter( $arg ) {
     140        $current_filter = $this->current_filter();
     141
    112142        if ( $this->debug ) {
    113             dmp( __FUNCTION__, $this->current_filter() );
    114         }
    115 
    116         $args           = func_get_args();
     143            dmp( __FUNCTION__, $current_filter );
     144        }
     145
    117146        $this->events[] = array(
    118             'filter' => __FUNCTION__,
    119             'tag'    => $this->current_filter(),
    120             'args'   => $args,
     147            'filter'    => __FUNCTION__,
     148            'hook_name' => $current_filter,
     149            'tag'       => $current_filter, // Back compat.
     150            'args'      => func_get_args(),
    121151        );
     152
    122153        return $arg;
    123154    }
    124155
     156    /**
     157     * @since UT (3.7.0)
     158     */
    125159    public function filter2( $arg ) {
     160        $current_filter = $this->current_filter();
     161
    126162        if ( $this->debug ) {
    127             dmp( __FUNCTION__, $this->current_filter() );
    128         }
    129 
    130         $args           = func_get_args();
     163            dmp( __FUNCTION__, $current_filter );
     164        }
     165
    131166        $this->events[] = array(
    132             'filter' => __FUNCTION__,
    133             'tag'    => $this->current_filter(),
    134             'args'   => $args,
     167            'filter'    => __FUNCTION__,
     168            'hook_name' => $current_filter,
     169            'tag'       => $current_filter, // Back compat.
     170            'args'      => func_get_args(),
    135171        );
     172
    136173        return $arg;
    137174    }
    138175
     176    /**
     177     * @since UT (3.7.0)
     178     */
    139179    public function filter_append( $arg ) {
     180        $current_filter = $this->current_filter();
     181
    140182        if ( $this->debug ) {
    141             dmp( __FUNCTION__, $this->current_filter() );
    142         }
    143 
    144         $args           = func_get_args();
     183            dmp( __FUNCTION__, $current_filter );
     184        }
     185
    145186        $this->events[] = array(
    146             'filter' => __FUNCTION__,
    147             'tag'    => $this->current_filter(),
    148             'args'   => $args,
     187            'filter'    => __FUNCTION__,
     188            'hook_name' => $current_filter,
     189            'tag'       => $current_filter, // Back compat.
     190            'args'      => func_get_args(),
    149191        );
     192
    150193        return $arg . '_append';
    151194    }
    152195
    153     public function filterall( $tag, ...$args ) {
    154         // This one doesn't return the result, so it's safe to use with the new 'all' filter.
     196    /**
     197     * Does not return the result, so it's safe to use with the 'all' filter.
     198     *
     199     * @since UT (3.7.0)
     200     */
     201    public function filterall( $hook_name, ...$args ) {
     202        $current_filter = $this->current_filter();
     203
    155204        if ( $this->debug ) {
    156             dmp( __FUNCTION__, $this->current_filter() );
     205            dmp( __FUNCTION__, $current_filter );
    157206        }
    158207
    159208        $this->events[] = array(
    160             'filter' => __FUNCTION__,
    161             'tag'    => $tag,
    162             'args'   => $args,
     209            'filter'    => __FUNCTION__,
     210            'hook_name' => $hook_name,
     211            'tag'       => $hook_name, // Back compat.
     212            'args'      => $args,
    163213        );
    164214    }
    165215
    166     // Return a list of all the actions, tags and args.
     216    /**
     217     * Returns a list of all the actions, hook names and args.
     218     *
     219     * @since UT (3.7.0)
     220     */
    167221    public function get_events() {
    168222        return $this->events;
    169223    }
    170224
    171     // Return a count of the number of times the action was called since the last reset.
    172     public function get_call_count( $tag = '' ) {
    173         if ( $tag ) {
     225    /**
     226     * Returns a count of the number of times the action was called since the last reset.
     227     *
     228     * @since UT (3.7.0)
     229     */
     230    public function get_call_count( $hook_name = '' ) {
     231        if ( $hook_name ) {
    174232            $count = 0;
     233
    175234            foreach ( $this->events as $e ) {
    176                 if ( $e['action'] === $tag ) {
     235                if ( $e['action'] === $hook_name ) {
    177236                    ++$count;
    178237                }
    179238            }
     239
    180240            return $count;
    181241        }
     242
    182243        return count( $this->events );
    183244    }
    184245
    185     // Return an array of the tags that triggered calls to this action.
     246    /**
     247     * Returns an array of the hook names that triggered calls to this action.
     248     *
     249     * @since 6.1.0
     250     */
     251    public function get_hook_names() {
     252        $out = array();
     253
     254        foreach ( $this->events as $e ) {
     255            $out[] = $e['hook_name'];
     256        }
     257
     258        return $out;
     259    }
     260
     261    /**
     262     * Returns an array of the hook names that triggered calls to this action.
     263     *
     264     * @since UT (3.7.0)
     265     * @since 6.1.0 Turned into an alias for ::get_hook_names().
     266     */
    186267    public function get_tags() {
    187         $out = array();
    188         foreach ( $this->events as $e ) {
    189             $out[] = $e['tag'];
    190         }
    191         return $out;
    192     }
    193 
    194     // Return an array of args passed in calls to this action.
     268        return $this->get_hook_names();
     269    }
     270
     271    /**
     272     * Returns an array of args passed in calls to this action.
     273     *
     274     * @since UT (3.7.0)
     275     */
    195276    public function get_args() {
    196277        $out = array();
     278
    197279        foreach ( $this->events as $e ) {
    198280            $out[] = $e['args'];
    199281        }
     282
    200283        return $out;
    201284    }
  • trunk/tests/phpunit/tests/actions.php

    r53804 r53805  
    2121        $this->assertSame( 1, $a->get_call_count() );
    2222        // Only our hook was called.
    23         $this->assertSame( array( $hook_name ), $a->get_tags() );
     23        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    2424
    2525        $argsvar = $a->get_args();
     
    4040        // Make sure our hook was called correctly.
    4141        $this->assertSame( 1, $a->get_call_count() );
    42         $this->assertSame( array( $hook_name ), $a->get_tags() );
     42        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    4343
    4444        // Now remove the action, do it again, and make sure it's not called this time.
     
    4646        do_action( $hook_name );
    4747        $this->assertSame( 1, $a->get_call_count() );
    48         $this->assertSame( array( $hook_name ), $a->get_tags() );
     48        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    4949
    5050    }
     
    216216            // 'action2' is called first because it has priority 9.
    217217            array(
    218                 'action' => 'action2',
    219                 'tag'    => $hook_name,
    220                 'args'   => array( '' ),
     218                'action'    => 'action2',
     219                'hook_name' => $hook_name,
     220                'tag'       => $hook_name, // Back compat.
     221                'args'      => array( '' ),
    221222            ),
    222223            // 'action' is called second.
    223224            array(
    224                 'action' => 'action',
    225                 'tag'    => $hook_name,
    226                 'args'   => array( '' ),
     225                'action'    => 'action',
     226                'hook_name' => $hook_name,
     227                'tag'       => $hook_name, // Back compat.
     228                'args'      => array( '' ),
    227229            ),
    228230        );
     
    275277        $this->assertSame( 4, $a->get_call_count() );
    276278        // Only our hook was called.
    277         $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_tags() );
     279        $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() );
    278280
    279281        remove_action( 'all', array( &$a, 'action' ) );
     
    295297        // Make sure our hook was called correctly.
    296298        $this->assertSame( 1, $a->get_call_count() );
    297         $this->assertSame( array( $hook_name ), $a->get_tags() );
     299        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    298300
    299301        // Now remove the action, do it again, and make sure it's not called this time.
     
    302304        do_action( $hook_name );
    303305        $this->assertSame( 1, $a->get_call_count() );
    304         $this->assertSame( array( $hook_name ), $a->get_tags() );
     306        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    305307    }
    306308
  • trunk/tests/phpunit/tests/filters.php

    r53804 r53805  
    1919        $this->assertSame( 1, $a->get_call_count() );
    2020        // Only our hook was called.
    21         $this->assertSame( array( $hook_name ), $a->get_tags() );
     21        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    2222
    2323        $argsvar = $a->get_args();
     
    3636        // Make sure our hook was called correctly.
    3737        $this->assertSame( 1, $a->get_call_count() );
    38         $this->assertSame( array( $hook_name ), $a->get_tags() );
     38        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    3939
    4040        // Now remove the filter, do it again, and make sure it's not called this time.
     
    4242        $this->assertSame( $val, apply_filters( $hook_name, $val ) );
    4343        $this->assertSame( 1, $a->get_call_count() );
    44         $this->assertSame( array( $hook_name ), $a->get_tags() );
     44        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    4545
    4646    }
     
    136136            // 'filter2' is called first because it has priority 9.
    137137            array(
    138                 'filter' => 'filter2',
    139                 'tag'    => $hook_name,
    140                 'args'   => array( $val ),
     138                'filter'    => 'filter2',
     139                'hook_name' => $hook_name,
     140                'tag'       => $hook_name, // Back compat.
     141                'args'      => array( $val ),
    141142            ),
    142143            // 'filter' is called second.
    143144            array(
    144                 'filter' => 'filter',
    145                 'tag'    => $hook_name,
    146                 'args'   => array( $val ),
     145                'filter'    => 'filter',
     146                'hook_name' => $hook_name,
     147                'tag'       => $hook_name, // Back compat.
     148                'args'      => array( $val ),
    147149            ),
    148150        );
     
    193195        $this->assertSame( 4, $a->get_call_count() );
    194196        // The right hooks should have been called in order.
    195         $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_tags() );
     197        $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() );
    196198
    197199        remove_filter( 'all', array( $a, 'filterall' ) );
     
    212214        // Make sure our hook was called correctly.
    213215        $this->assertSame( 1, $a->get_call_count() );
    214         $this->assertSame( array( $hook_name ), $a->get_tags() );
     216        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    215217
    216218        // Now remove the filter, do it again, and make sure it's not called this time.
     
    221223        // Call cound should remain at 1.
    222224        $this->assertSame( 1, $a->get_call_count() );
    223         $this->assertSame( array( $hook_name ), $a->get_tags() );
     225        $this->assertSame( array( $hook_name ), $a->get_hook_names() );
    224226    }
    225227
  • trunk/tests/phpunit/tests/user/author.php

    r53147 r53805  
    174174
    175175        $this->assertSame( 1, $filter->get_call_count() );
    176         $this->assertSame( array( 'the_author_link' ), $filter->get_tags() );
     176        $this->assertSame( array( 'the_author_link' ), $filter->get_hook_names() );
    177177    }
    178178}
Note: See TracChangeset for help on using the changeset viewer.