WordPress.org

Make WordPress Core

Changeset 38400


Ignore:
Timestamp:
08/27/2016 11:27:17 AM (4 years ago)
Author:
peterwilsoncc
Message:

Unit tests: Enforce $args object in wp_nav_menu() & Walker_Nav_Menu.

WordPress always* passes $args to filters as an array of arugments. A exception is made in wp_nav_menu() and the associated walker where these are passed to filters as an object, this has been the case for seven years (since [13368]).

These new tests enforce the use of an object in these filters to ensure backward compatibility is maintained.

See #24587.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r36888 r38400  
    276276        $this->assertEmpty( $post_type_archive_item->description );
    277277    }
     278
     279    /**
     280     * Confirm `wp_nav_menu()` and `Walker_Nav_Menu` passes an $args object to filters.
     281     *
     282     * `wp_nav_menu()` is unique in that it uses an $args object rather than an array.
     283     * This has been the case for some time and should be maintained for reasons of
     284     * backward compatibility.
     285     *
     286     * @ticket 24587
     287     */
     288    function test_wp_nav_menu_filters_are_passed_args_object() {
     289        $tag_id = self::factory()->tag->create();
     290
     291        $tag_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     292            'menu-item-type' => 'taxonomy',
     293            'menu-item-object' => 'post_tag',
     294            'menu-item-object-id' => $tag_id,
     295            'menu-item-status' => 'publish',
     296        ) );
     297
     298        /*
     299         * The tests take place in a range of filters to ensure the passed
     300         * arguments are an object.
     301         */
     302        // In function.
     303        add_filter( 'pre_wp_nav_menu',          array( $this, '_confirm_second_param_args_object' ), 10, 2 );
     304        add_filter( 'wp_nav_menu_objects',      array( $this, '_confirm_second_param_args_object' ), 10, 2 );
     305        add_filter( 'wp_nav_menu_items',        array( $this, '_confirm_second_param_args_object' ), 10, 2 );
     306
     307        // In walker.
     308        add_filter( 'nav_menu_item_args',       array( $this, '_confirm_nav_menu_item_args_object' ) );
     309
     310        add_filter( 'nav_menu_css_class',       array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     311        add_filter( 'nav_menu_item_id',         array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     312        add_filter( 'nav_menu_link_attributes', array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     313        add_filter( 'nav_menu_item_title',      array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     314
     315        add_filter( 'walker_nav_menu_start_el', array( $this, '_confirm_forth_param_args_object' ), 10, 4 );
     316
     317        wp_nav_menu( array(
     318            'echo' => false,
     319            'menu' => $this->menu_id,
     320        ) );
     321        wp_delete_term( $tag_id, 'post_tag' );
     322
     323        /*
     324         * Remove test filters.
     325         */
     326        // In function.
     327        remove_filter( 'pre_wp_nav_menu',          array( $this, '_confirm_second_param_args_object' ), 10, 2 );
     328        remove_filter( 'wp_nav_menu_objects',      array( $this, '_confirm_second_param_args_object' ), 10, 2 );
     329        remove_filter( 'wp_nav_menu_items',        array( $this, '_confirm_second_param_args_object' ), 10, 2 );
     330
     331        // In walker.
     332        remove_filter( 'nav_menu_item_args',       array( $this, '_confirm_nav_menu_item_args_object' ) );
     333
     334        remove_filter( 'nav_menu_css_class',       array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     335        remove_filter( 'nav_menu_item_id',         array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     336        remove_filter( 'nav_menu_link_attributes', array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     337        remove_filter( 'nav_menu_item_title',      array( $this, '_confirm_third_param_args_object' ), 10, 3 );
     338
     339        remove_filter( 'walker_nav_menu_start_el', array( $this, '_confirm_forth_param_args_object' ), 10, 4 );
     340
     341    }
     342
     343    /**
     344     * Run tests required to confrim Walker_Nav_Menu receives an $args object.
     345     */
     346    function _confirm_nav_menu_item_args_object( $args ) {
     347        $this->assertTrue( is_object( $args ) );
     348        return $args;
     349    }
     350
     351    function _confirm_second_param_args_object( $ignored_1, $args ) {
     352        $this->assertTrue( is_object( $args ) );
     353        return $ignored_1;
     354    }
     355
     356    function _confirm_third_param_args_object( $ignored_1, $ignored_2, $args ) {
     357        $this->assertTrue( is_object( $args ) );
     358        return $ignored_1;
     359    }
     360
     361    function _confirm_forth_param_args_object( $ignored_1, $ignored_2, $ignored_3, $args ) {
     362        $this->assertTrue( is_object( $args ) );
     363        return $ignored_1;
     364    }
    278365}
Note: See TracChangeset for help on using the changeset viewer.