Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.