Make WordPress Core

Changeset 33322


Ignore:
Timestamp:
07/18/2015 11:19:33 PM (10 years ago)
Author:
westonruter
Message:

Customizer: Finish unit tests for nav menus.

Removes object_type restriction to allow for future extensibility. Refactors some methods to improve testability. Includes new tests for Ajax requests.

Fixes #32687.
Props valendesigns, welcher, westonruter.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r33256 r33322  
    8181
    8282        $obj_type = sanitize_key( $_POST['obj_type'] );
    83         if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
    84             wp_send_json_error( 'nav_menus_invalid_obj_type' );
    85         }
    86 
    87         $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
    88         $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
     83        $obj_name = sanitize_key( $_POST['type'] );
     84        $page = empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] );
     85        $items = $this->load_available_items_query( $obj_type, $obj_name, $page );
     86
     87        if ( is_wp_error( $items ) ) {
     88            wp_send_json_error( $items->get_error_code() );
     89        } else {
     90            wp_send_json_success( array( 'items' => $items ) );
     91        }
     92    }
     93
     94    /**
     95     * Performs the post_type and taxonomy queries for loading available menu items.
     96     *
     97     * @since 4.3.0
     98     * @access public
     99     *
     100     * @param string $obj_type Optional. Accepts any custom object type and has built-in support for
     101     *                         'post_type' and 'taxonomy'. Default is 'post_type'.
     102     * @param string $obj_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
     103     * @param int    $page     Optional. The page number used to generate the query offset. Default is '0'.
     104     * @return WP_Error|array Returns either a WP_Error object or an array of menu items.
     105     */
     106    public function load_available_items_query( $obj_type = 'post_type', $obj_name = 'page', $page = 0 ) {
    89107        $items = array();
    90108
    91109        if ( 'post_type' === $obj_type ) {
    92             if ( ! get_post_type_object( $taxonomy_or_post_type ) ) {
    93                 wp_send_json_error( 'nav_menus_invalid_post_type' );
     110            if ( ! get_post_type_object( $obj_name ) ) {
     111                return new WP_Error( 'nav_menus_invalid_post_type' );
    94112            }
    95113
    96             if ( 0 === $page && 'page' === $taxonomy_or_post_type ) {
     114            if ( 0 === $page && 'page' === $obj_name ) {
    97115                // Add "Home" link. Treat as a page, but switch to custom on add.
    98116                $items[] = array(
     
    111129                'orderby'     => 'date',
    112130                'order'       => 'DESC',
    113                 'post_type'   => $taxonomy_or_post_type,
     131                'post_type'   => $obj_name,
    114132            ) );
    115133            foreach ( $posts as $post ) {
     
    130148            }
    131149        } elseif ( 'taxonomy' === $obj_type ) {
    132             $terms = get_terms( $taxonomy_or_post_type, array(
     150            $terms = get_terms( $obj_name, array(
    133151                'child_of'     => 0,
    134152                'exclude'      => '',
     
    143161            ) );
    144162            if ( is_wp_error( $terms ) ) {
    145                 wp_send_json_error( $terms->get_error_code() );
     163                return $terms;
    146164            }
    147165
     
    159177        }
    160178
    161         wp_send_json_success( array( 'items' => $items ) );
     179        return $items;
    162180    }
    163181
     
    185203
    186204        $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    187         $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
    188 
    189         if ( empty( $results ) ) {
    190             wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
     205        $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     206
     207        if ( empty( $items ) ) {
     208            wp_send_json_error( array( 'message' => __( 'No menu items found.' ) ) );
    191209        } else {
    192             wp_send_json_success( array( 'items' => $results ) );
     210            wp_send_json_success( array( 'items' => $items ) );
    193211        }
    194212    }
     
    203221     *
    204222     * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
    205      * @return array Results.
     223     * @return array Menu items.
    206224     */
    207225    public function search_available_items_query( $args = array() ) {
    208         $results = array();
     226        $items = array();
    209227
    210228        $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
     
    236254                    $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
    237255                }
    238                 $results[] = array(
     256                $items[] = array(
    239257                    'id'         => 'post-' . $post->ID,
    240258                    'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     
    259277        if ( ! empty( $terms ) ) {
    260278            foreach ( $terms as $term ) {
    261                 $results[] = array(
     279                $items[] = array(
    262280                    'id'         => 'term-' . $term->term_id,
    263281                    'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     
    271289        }
    272290
    273         return $results;
     291        return $items;
    274292    }
    275293
  • trunk/tests/phpunit/tests/customize/nav-menus.php

    r33138 r33322  
    5050
    5151    /**
    52      * Test the test_load_available_items_ajax method.
    53      *
    54      * @see WP_Customize_Nav_Menus::load_available_items_ajax()
    55      */
    56     function test_load_available_items_ajax() {
    57 
    58         $this->markTestIncomplete( 'This test has not been implemented.' );
    59 
    60     }
    61 
    62     /**
    63      * Test the search_available_items_ajax method.
    64      *
    65      * @see WP_Customize_Nav_Menus::search_available_items_ajax()
    66      */
    67     function test_search_available_items_ajax() {
    68 
    69         $this->markTestIncomplete( 'This test has not been implemented.' );
    70 
     52     * Test that the load_available_items_query method returns a WP_Error object.
     53     *
     54     * @see WP_Customize_Nav_Menus::load_available_items_query()
     55     */
     56    function test_load_available_items_query_returns_wp_error() {
     57        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     58
     59        // Invalid post type $obj_name.
     60        $items = $menus->load_available_items_query( 'post_type', 'invalid' );
     61        $this->assertInstanceOf( 'WP_Error', $items );
     62        $this->assertEquals( 'nav_menus_invalid_post_type', $items->get_error_code() );
     63
     64        // Invalid taxonomy $obj_name.
     65        $items = $menus->load_available_items_query( 'taxonomy', 'invalid' );
     66        $this->assertInstanceOf( 'WP_Error', $items );
     67        $this->assertEquals( 'invalid_taxonomy', $items->get_error_code() );
     68    }
     69
     70    /**
     71     * Test the load_available_items_query method maybe returns the home page item.
     72     *
     73     * @see WP_Customize_Nav_Menus::load_available_items_query()
     74     */
     75    function test_load_available_items_query_maybe_returns_home() {
     76        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     77
     78        // Expected menu item array.
     79        $expected = array(
     80            'id'         => 'home',
     81            'title'      => _x( 'Home', 'nav menu home label' ),
     82            'type'       => 'custom',
     83            'type_label' => __( 'Custom Link' ),
     84            'object'     => '',
     85            'url'        => home_url(),
     86        );
     87
     88        // Create pages.
     89        $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) );
     90
     91        // Home is included in menu items when page is zero.
     92        $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     93        $this->assertContains( $expected, $items );
     94
     95        // Home is not included in menu items when page is larger than zero.
     96        $items = $menus->load_available_items_query( 'post_type', 'page', 1 );
     97        $this->assertNotEmpty( $items );
     98        $this->assertNotContains( $expected, $items );
     99    }
     100
     101    /**
     102     * Test the load_available_items_query method returns post item.
     103     *
     104     * @see WP_Customize_Nav_Menus::load_available_items_query()
     105     */
     106    function test_load_available_items_query_returns_post_item_with_page_number() {
     107        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     108
     109        // Create page.
     110        $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
     111
     112        // Create pages.
     113        $this->factory->post->create_many( 10 );
     114
     115        // Expected menu item array.
     116        $expected = array(
     117            'id'         => "post-{$post_id}",
     118            'title'      => 'Post Title',
     119            'type'       => 'post_type',
     120            'type_label' => 'Post',
     121            'object'     => 'post',
     122            'object_id'  => intval( $post_id ),
     123            'url'        => get_permalink( intval( $post_id ) ),
     124        );
     125
     126        // Offset the query and get the second page of menu items.
     127        $items = $menus->load_available_items_query( 'post_type', 'post', 1 );
     128        $this->assertContains( $expected, $items );
     129    }
     130
     131    /**
     132     * Test the load_available_items_query method returns page item.
     133     *
     134     * @see WP_Customize_Nav_Menus::load_available_items_query()
     135     */
     136    function test_load_available_items_query_returns_page_item() {
     137        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     138
     139        // Create page.
     140        $page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
     141
     142        // Expected menu item array.
     143        $expected = array(
     144            'id'         => "post-{$page_id}",
     145            'title'      => 'Page Title',
     146            'type'       => 'post_type',
     147            'type_label' => 'Page',
     148            'object'     => 'page',
     149            'object_id'  => intval( $page_id ),
     150            'url'        => get_permalink( intval( $page_id ) ),
     151        );
     152
     153        $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     154        $this->assertContains( $expected, $items );
     155    }
     156
     157    /**
     158     * Test the load_available_items_query method returns post item.
     159     *
     160     * @see WP_Customize_Nav_Menus::load_available_items_query()
     161     */
     162    function test_load_available_items_query_returns_post_item() {
     163        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     164
     165        // Create post.
     166        $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
     167
     168        // Expected menu item array.
     169        $expected = array(
     170            'id'         => "post-{$post_id}",
     171            'title'      => 'Post Title',
     172            'type'       => 'post_type',
     173            'type_label' => 'Post',
     174            'object'     => 'post',
     175            'object_id'  => intval( $post_id ),
     176            'url'        => get_permalink( intval( $post_id ) ),
     177        );
     178
     179        $items = $menus->load_available_items_query( 'post_type', 'post', 0 );
     180        $this->assertContains( $expected, $items );
     181    }
     182
     183    /**
     184     * Test the load_available_items_query method returns term item.
     185     *
     186     * @see WP_Customize_Nav_Menus::load_available_items_query()
     187     */
     188    function test_load_available_items_query_returns_term_item() {
     189        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     190
     191        // Create term.
     192        $term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
     193
     194        // Expected menu item array.
     195        $expected = array(
     196            'id'         => "term-{$term_id}",
     197            'title'      => 'Term Title',
     198            'type'       => 'taxonomy',
     199            'type_label' => 'Category',
     200            'object'     => 'category',
     201            'object_id'  => intval( $term_id ),
     202            'url'        => get_term_link( intval( $term_id ), 'category' ),
     203        );
     204
     205        $items = $menus->load_available_items_query( 'taxonomy', 'category', 0 );
     206        $this->assertContains( $expected, $items );
    71207    }
    72208
     
    455591    }
    456592
    457     /**
    458      * Test the render_menu method.
    459      *
    460      * @see WP_Customize_Nav_Menus::render_menu()
    461      */
    462     function test_render_menu() {
    463 
    464         $this->markTestIncomplete( 'This test has not been implemented.' );
    465     }
    466 
    467593}
Note: See TracChangeset for help on using the changeset viewer.