Make WordPress Core

Ticket #32687: 32687.7.diff

File 32687.7.diff, 24.8 KB (added by westonruter, 11 years ago)

Additional change: https://github.com/xwp/wordpress-develop/commit/820ff44dbd8378c688a9ce7ef5beac7d028a33f7

  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index b01cf73..fefacfe 100644
    final class WP_Customize_Nav_Menus { 
    8080                }
    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' );
     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 ) );
    8591                }
     92        }
    8693
    87                 $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
    88                 $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
     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(
    99117                                        'id'         => 'home',
    final class WP_Customize_Nav_Menus { 
    110128                                'offset'      => 10 * $page,
    111129                                'orderby'     => 'date',
    112130                                'order'       => 'DESC',
    113                                 'post_type'   => $taxonomy_or_post_type,
     131                                'post_type'   => $obj_name,
    114132                        ) );
    115133                        foreach ( $posts as $post ) {
    116134                                $post_title = $post->post_title;
    final class WP_Customize_Nav_Menus { 
    129147                                );
    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'      => '',
    135153                                'hide_empty'   => false,
    final class WP_Customize_Nav_Menus { 
    142160                                'pad_counts'   => false,
    143161                        ) );
    144162                        if ( is_wp_error( $terms ) ) {
    145                                 wp_send_json_error( $terms->get_error_code() );
     163                                return $terms;
    146164                        }
    147165
    148166                        foreach ( $terms as $term ) {
    final class WP_Customize_Nav_Menus { 
    158176                        }
    159177                }
    160178
    161                 wp_send_json_success( array( 'items' => $items ) );
     179                return $items;
    162180        }
    163181
    164182        /**
    final class WP_Customize_Nav_Menus { 
    184202                }
    185203
    186204                $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    187                 $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     205                $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
    188206
    189                 if ( empty( $results ) ) {
    190                         wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
     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        }
    195213
    final class WP_Customize_Nav_Menus { 
    202220         * @access public
    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' );
    211229                $query = array(
    final class WP_Customize_Nav_Menus { 
    235253                                        /* translators: %d: ID of a post */
    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' ) ),
    241259                                        'type'       => 'post_type',
    final class WP_Customize_Nav_Menus { 
    258276                // Check if any taxonomies were found.
    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' ) ),
    264282                                        'type'       => 'taxonomy',
    final class WP_Customize_Nav_Menus { 
    270288                        }
    271289                }
    272290
    273                 return $results;
     291                return $items;
    274292        }
    275293
    276294        /**
  • new file tests/phpunit/tests/ajax/CustomizeMenus.php

    diff --git tests/phpunit/tests/ajax/CustomizeMenus.php tests/phpunit/tests/ajax/CustomizeMenus.php
    new file mode 100644
    index 0000000..202585e
    - +  
     1<?php
     2/**
     3 * Testing ajax customize menus functionality
     4 *
     5 * @package    WordPress
     6 * @subpackage UnitTests
     7 * @since      4.3.0
     8 * @group      ajax
     9 * @runTestsInSeparateProcesses
     10 */
     11class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
     12
     13        /**
     14         * Instance of WP_Customize_Manager which is reset for each test.
     15         *
     16         * @var WP_Customize_Manager
     17         */
     18        public $wp_customize;
     19
     20        /**
     21         * Set up the test fixture.
     22         */
     23        public function setUp() {
     24                parent::setUp();
     25                require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
     26                wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     27                global $wp_customize;
     28                $this->wp_customize = new WP_Customize_Manager();
     29                $wp_customize = $this->wp_customize;
     30        }
     31
     32        /**
     33         * Tear down the test fixture.
     34         */
     35        public function tearDown() {
     36                wp_set_current_user( 0 );
     37                parent::tearDown();
     38        }
     39
     40        /**
     41         * Helper to keep it DRY
     42         *
     43         * @param string $action Action.
     44         */
     45        protected function make_ajax_call( $action ) {
     46                // Make the request.
     47                try {
     48                        $this->_handleAjax( $action );
     49                } catch ( WPAjaxDieContinueException $e ) {
     50                        unset( $e );
     51                }
     52        }
     53
     54        /**
     55         * Testing capabilities check for ajax_load_available_items method
     56         *
     57         * @dataProvider data_ajax_load_available_items_cap_check
     58         *
     59         * @param string $role              The role we're checking caps against.
     60         * @param array  $expected_results  Expected results.
     61         */
     62        function test_ajax_load_available_items_cap_check( $role, $expected_results ) {
     63
     64                if ( 'administrator' != $role ) {
     65                        // If we're not an admin, we should get a wp_die(-1).
     66                        $this->setExpectedException( 'WPAjaxDieStopException' );
     67                }
     68
     69                wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) );
     70
     71                $_POST = array(
     72                        'action'                => 'load-available-menu-items-customizer',
     73                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     74                );
     75
     76                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     77
     78                // If we are an admin, we should get a proper response.
     79                if ( 'administrator' === $role ) {
     80                        // Get the results.
     81                        $response = json_decode( $this->_last_response, true );
     82
     83                        $this->assertSame( $expected_results, $response );
     84                }
     85
     86        }
     87
     88        /**
     89         * Data provider for test_ajax_load_available_items_cap_check().
     90         *
     91         * Provides various post_args to induce error messages in the that can be
     92         * compared to the expected_results.
     93         *
     94         * @since 4.3.0
     95         *
     96         * @return array {
     97         *     @type array {
     98         *         @string string $role             The role that will test caps for.
     99         *         @array  array  $expected_results The expected results from the ajax call.
     100         *     }
     101         * }
     102         */
     103        function data_ajax_load_available_items_cap_check() {
     104                return array(
     105                        array(
     106                                'subscriber',
     107                                array(),
     108                        ),
     109                        array(
     110                                'contributor',
     111                                array(),
     112                        ),
     113                        array(
     114                                'author',
     115                                array(),
     116                        ),
     117                        array(
     118                                'editor',
     119                                array(),
     120                        ),
     121                        array(
     122                                'administrator',
     123                                array(
     124                                        'success' => false,
     125                                        'data'    => 'nav_menus_missing_obj_type_or_type_parameter',
     126                                ),
     127                        ),
     128                );
     129        }
     130
     131        /**
     132         * Testing the error messaging for ajax_load_available_items
     133         *
     134         * @dataProvider data_ajax_load_available_items_error_messages
     135         *
     136         * @param array $post_args POST args.
     137         * @param mixed $expected_results Expected results.
     138         */
     139        function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) {
     140
     141                $_POST = array_merge( array(
     142                        'action'                => 'load-available-menu-items-customizer',
     143                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     144                ), $post_args );
     145
     146                // Make the request.
     147                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     148
     149                // Get the results.
     150                $response = json_decode( $this->_last_response, true );
     151
     152                $this->assertSame( $expected_results, $response );
     153        }
     154
     155        /**
     156         * Data provider for test_ajax_load_available_items_error_message().
     157         *
     158         * Provides various post_args to induce error messages in the that can be
     159         * compared to the expected_results.
     160         *
     161         * @since 4.3.0
     162         *
     163         * @return array {
     164         *     @type array {
     165         *         @array array $post_args        The arguments that will merged with the $_POST array.
     166         *         @array array $expected_results The expected results from the ajax call.
     167         *     }
     168         * }
     169         */
     170        function data_ajax_load_available_items_error_messages() {
     171                return array(
     172                        // Testing empty obj_type and type.
     173                        array(
     174                                array(
     175                                        'obj_type' => '',
     176                                        'type'     => '',
     177                                ),
     178                                array(
     179                                        'success'  => false,
     180                                        'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
     181                                ),
     182                        ),
     183                        // Testing empty obj_type.
     184                        array(
     185                                array(
     186                                        'obj_type' => '',
     187                                        'type'     => 'post',
     188                                ),
     189                                array(
     190                                        'success'  => false,
     191                                        'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
     192                                ),
     193                        ),
     194                        // Testing empty type.
     195                        array(
     196                                array(
     197                                        'obj_type' => '',
     198                                        'type'     => 'post',
     199                                ),
     200                                array(
     201                                        'success'  => false,
     202                                        'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
     203                                ),
     204                        ),
     205                        // Testing incorrect obj_type option.
     206                        array(
     207                                array(
     208                                        'obj_type' => 'invalid',
     209                                        'type'     => 'post',
     210                                ),
     211                                array(
     212                                        'success'  => false,
     213                                        'data'     => 'nav_menus_invalid_obj_type',
     214                                ),
     215                        ),
     216                        // Testing incorrect type option.
     217                        array(
     218                                array(
     219                                        'obj_type' => 'post_type',
     220                                        'type'     => 'invalid',
     221                                ),
     222                                array(
     223                                        'success'  => false,
     224                                        'data'     => 'nav_menus_invalid_post_type',
     225                                ),
     226                        ),
     227                );
     228        }
     229
     230        /**
     231         * Testing the success status.
     232         *
     233         * @dataProvider data_ajax_load_available_items_success_status
     234         *
     235         * @param array $post_args       POST args.
     236         * @param array $success_status  Success status.
     237         */
     238        function test_ajax_load_available_items_success_status( $post_args, $success_status ) {
     239
     240                $_POST = array_merge( array(
     241                        'action'                => 'load-available-menu-items-customizer',
     242                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     243                ), $post_args );
     244
     245                // Make the request.
     246                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     247
     248                // Get the results.
     249                $response = json_decode( $this->_last_response, true );
     250                $this->assertSame( $success_status, $response['success'] );
     251
     252        }
     253
     254        /**
     255         * Data provider for test_ajax_load_available_items_success_status().
     256         *
     257         * Provides various post_args to retrieve results and compare against
     258         * the success status.
     259         *
     260         * @since 4.3.0
     261         *
     262         * @return array {
     263         *     @type array {
     264         *         @type array $post_args      The arguments that will merged with the $_POST array.
     265         *         @type bool  $success_status The expected success status.
     266         *     }
     267         * }
     268         */
     269        function data_ajax_load_available_items_success_status() {
     270                return array(
     271                        array(
     272                                array(
     273                                        'obj_type' => 'post_type',
     274                                        'type'     => 'post',
     275                                ),
     276                                true,
     277                        ),
     278                        array(
     279                                array(
     280                                        'obj_type' => 'post_type',
     281                                        'type'     => 'page',
     282                                ),
     283                                true,
     284                        ),
     285                        array(
     286                                array(
     287                                        'obj_type' => 'post_type',
     288                                        'type'     => 'custom',
     289                                ),
     290                                false,
     291                        ),
     292                        array(
     293                                array(
     294                                        'obj_type' => 'taxonomy',
     295                                        'type'     => 'post_tag',
     296                                ),
     297                                true,
     298                        ),
     299                );
     300        }
     301
     302        /**
     303         * Testing the array structure for a single item
     304         *
     305         * @dataProvider data_ajax_load_available_items_structure
     306         *
     307         * @param array $post_args POST args.
     308         */
     309        function test2_ajax_load_available_items_structure( $post_args ) {
     310
     311                $expected_keys = array(
     312                        'id',
     313                        'title',
     314                        'type',
     315                        'type_label',
     316                        'object',
     317                        'object_id',
     318                        'url',
     319                );
     320
     321                // Create some terms and pages.
     322                $this->factory->term->create_many( 5 );
     323                $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) );
     324
     325                $_POST = array_merge( array(
     326                        'action'                => 'load-available-menu-items-customizer',
     327                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     328                ), $post_args );
     329
     330                // Make the request.
     331                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     332
     333                // Get the results.
     334                $response = json_decode( $this->_last_response, true );
     335
     336                $this->assertNotEmpty( $response['data']['items'] );
     337
     338                // Get the second index to avoid the home page edge case.
     339                $test_item = $response['data']['items'][1];
     340
     341                foreach ( $expected_keys as $key ) {
     342                        $this->assertArrayHasKey( $key, $test_item );
     343                        $this->assertNotEmpty( $test_item[ $key ] );
     344                }
     345
     346                // Special test for the home page.
     347                if ( 'page' === $test_item['object'] ) {
     348                        $home = $response['data']['items'][0];
     349                        foreach ( $expected_keys as $key ) {
     350                                if ( 'object_id' !== $key ) {
     351                                        $this->assertArrayHasKey( $key, $home );
     352                                        if ( 'object' !== $key ) {
     353                                                $this->assertNotEmpty( $home[ $key ] );
     354                                        }
     355                                }
     356                        }
     357                }
     358        }
     359
     360        /**
     361         * Data provider for test_ajax_load_available_items_structure().
     362         *
     363         * Provides various post_args to return a list of items to test the array structure of.
     364         *
     365         * @since 4.3.0
     366         *
     367         * @return array {
     368         *     @type array {
     369         *         @type array $post_args The arguments that will merged with the $_POST array.
     370         *     }
     371         * }
     372         */
     373        function data_ajax_load_available_items_structure() {
     374                return array(
     375                        array(
     376                                array(
     377                                        'obj_type' => 'post_type',
     378                                        'type'     => 'post',
     379                                ),
     380                        ),
     381                        array(
     382                                array(
     383                                        'obj_type' => 'post_type',
     384                                        'type'     => 'page',
     385                                ),
     386                        ),
     387                        array(
     388                                array(
     389                                        'obj_type' => 'taxonomy',
     390                                        'type'     => 'post_tag',
     391                                ),
     392                        ),
     393                );
     394        }
     395
     396        /**
     397         * Testing the error messages for ajax_search_available_items
     398         *
     399         * @dataProvider data_ajax_search_available_items_caps_check
     400         *
     401         * @param string $role             Role.
     402         * @param array  $expected_results Expected results.
     403         */
     404        function test_ajax_search_available_items_caps_check( $role, $expected_results ) {
     405
     406                if ( 'administrator' != $role ) {
     407                        // If we're not an admin, we should get a wp_die(-1).
     408                        $this->setExpectedException( 'WPAjaxDieStopException' );
     409                }
     410
     411                wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) );
     412
     413                $_POST = array(
     414                        'action'                => 'search-available-menu-items-customizer',
     415                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     416                );
     417
     418                $this->make_ajax_call( 'search-available-menu-items-customizer' );
     419
     420                // If we are an admin, we should get a proper response.
     421                if ( 'administrator' === $role ) {
     422                        // Get the results.
     423                        $response = json_decode( $this->_last_response, true );
     424
     425                        $this->assertSame( $expected_results, $response );
     426                }
     427        }
     428
     429        /**
     430         *
     431         * Data provider.
     432         *
     433         * Provides various post_args to induce error messages in the that can be
     434         * compared to the expected_results.
     435         *
     436         * @since 4.3.0
     437         *
     438         * @todo Make this more DRY
     439         *
     440         * @return array {
     441         *     @type array {
     442         *         @string string $role             The role that will test caps for.
     443         *         @array  array  $expected_results The expected results from the ajax call.
     444         *     }
     445         * }
     446         */
     447        function data_ajax_search_available_items_caps_check() {
     448                return array(
     449                        array(
     450                                'subscriber',
     451                                array(),
     452                        ),
     453                        array(
     454                                'contributor',
     455                                array(),
     456                        ),
     457                        array(
     458                                'author',
     459                                array(),
     460                        ),
     461                        array(
     462                                'editor',
     463                                array(),
     464                        ),
     465                        array(
     466                                'administrator',
     467                                array(
     468                                        'success' => false,
     469                                        'data'    => 'nav_menus_missing_search_parameter',
     470                                ),
     471                        ),
     472                );
     473        }
     474
     475        /**
     476         * Testing the results of various searches
     477         *
     478         * @dataProvider data_ajax_search_available_items_results
     479         *
     480         * @param array $post_args        POST args.
     481         * @param array $expected_results Expected results.
     482         */
     483        function test_ajax_search_available_items_results( $post_args, $expected_results ) {
     484
     485                $this->factory->post->create_many( 5, array( 'post_title' => 'Test Post' ) );
     486
     487                $_POST = array_merge( array(
     488                        'action'                => 'search-available-menu-items-customizer',
     489                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     490                ), $post_args );
     491
     492                $this->make_ajax_call( 'search-available-menu-items-customizer' );
     493
     494                $response = json_decode( $this->_last_response, true );
     495
     496                if ( isset( $post_args['search'] ) && 'test' === $post_args['search'] ) {
     497                        $this->assertsame( true, $response['success'] );
     498                        $this->assertSame( 5, count( $response['data']['items'] ) );
     499                } else {
     500                        $this->assertSame( $expected_results, $response );
     501                }
     502
     503        }
     504
     505        /**
     506         * Data provider.
     507         *
     508         * Provides various post_args to test the results.
     509         *
     510         * @since 4.3.0
     511         *
     512         * @return array {
     513         *     @type array {
     514         *         @string string $post_args        The args that will be passed to ajax.
     515         *         @array  array  $expected_results The expected results from the ajax call.
     516         *     }
     517         * }
     518         */
     519        function data_ajax_search_available_items_results() {
     520                return array(
     521                        array(
     522                                array(),
     523                                array(
     524                                        'success' => false,
     525                                        'data'    => 'nav_menus_missing_search_parameter',
     526                                ),
     527                        ),
     528                        array(
     529                                array(
     530                                        'search'  => 'all_the_things',
     531                                ),
     532                                array(
     533                                        'success' => false,
     534                                        'data'    => array(
     535                                                'message' => 'No menu items found.',
     536                                        ),
     537                                ),
     538                        ),
     539                        array(
     540                                array(
     541                                        'search'  => 'test',
     542                                ),
     543                                array(
     544                                        'success' => true,
     545                                        array(),
     546                                ),
     547                        ),
     548                );
     549        }
     550}
  • tests/phpunit/tests/customize/nav-menus.php

    diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
    index 7fa88e7..773bf88 100644
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    4949        }
    5050
    5151        /**
    52          * Test the test_load_available_items_ajax method.
     52         * Test that the load_available_items_query method returns a WP_Error object.
    5353         *
    54          * @see WP_Customize_Nav_Menus::load_available_items_ajax()
     54         * @see WP_Customize_Nav_Menus::load_available_items_query()
    5555         */
    56         function test_load_available_items_ajax() {
     56        function test_load_available_items_query_returns_wp_error() {
     57                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    5758
    58                 $this->markTestIncomplete( 'This test has not been implemented.' );
     59                // Invalid post type $obj_name.
     60                $items = $menus->load_available_items_query( 'post_type', 'invalid' );
     61                $this->assertInstanceOf( 'WP_Error', $items );
    5962
     63                // Invalid taxonomy $obj_name.
     64                $items = $menus->load_available_items_query( 'taxonomy', 'invalid' );
     65                $this->assertInstanceOf( 'WP_Error', $items );
    6066        }
    6167
    6268        /**
    63          * Test the search_available_items_ajax method.
     69         * Test the load_available_items_query method maybe returns the home page item.
    6470         *
    65          * @see WP_Customize_Nav_Menus::search_available_items_ajax()
     71         * @see WP_Customize_Nav_Menus::load_available_items_query()
    6672         */
    67         function test_search_available_items_ajax() {
     73        function test_load_available_items_query_maybe_returns_home() {
     74                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     75
     76                // Expected menu item array.
     77                $expected = array(
     78                        'id'         => 'home',
     79                        'title'      => _x( 'Home', 'nav menu home label' ),
     80                        'type'       => 'custom',
     81                        'type_label' => __( 'Custom Link' ),
     82                        'object'     => '',
     83                        'url'        => home_url(),
     84                );
     85
     86                // Create pages.
     87                $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) );
     88
     89                // Home is included in menu items when page is zero.
     90                $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     91                $this->assertContains( $expected, $items );
     92
     93                // Home is not included in menu items when page is larger than zero.
     94                $items = $menus->load_available_items_query( 'post_type', 'page', 1 );
     95                $this->assertNotEmpty( $items );
     96                $this->assertNotContains( $expected, $items );
     97        }
     98
     99        /**
     100         * Test the load_available_items_query method returns post item.
     101         *
     102         * @see WP_Customize_Nav_Menus::load_available_items_query()
     103         */
     104        function test_load_available_items_query_returns_post_item_with_page_number() {
     105                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     106
     107                // Create page.
     108                $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
    68109
    69                 $this->markTestIncomplete( 'This test has not been implemented.' );
     110                // Create pages.
     111                $this->factory->post->create_many( 10 );
     112
     113                // Expected menu item array.
     114                $expected = array(
     115                        'id'         => "post-{$post_id}",
     116                        'title'      => 'Post Title',
     117                        'type'       => 'post_type',
     118                        'type_label' => 'Post',
     119                        'object'     => 'post',
     120                        'object_id'  => intval( $post_id ),
     121                        'url'        => get_permalink( intval( $post_id ) ),
     122                );
     123
     124                // Offset the query and get the second page of menu items.
     125                $items = $menus->load_available_items_query( 'post_type', 'post', 1 );
     126                $this->assertContains( $expected, $items );
     127        }
     128
     129        /**
     130         * Test the load_available_items_query method returns page item.
     131         *
     132         * @see WP_Customize_Nav_Menus::load_available_items_query()
     133         */
     134        function test_load_available_items_query_returns_page_item() {
     135                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     136
     137                // Create page.
     138                $page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
     139
     140                // Expected menu item array.
     141                $expected = array(
     142                        'id'         => "post-{$page_id}",
     143                        'title'      => 'Page Title',
     144                        'type'       => 'post_type',
     145                        'type_label' => 'Page',
     146                        'object'     => 'page',
     147                        'object_id'  => intval( $page_id ),
     148                        'url'        => get_permalink( intval( $page_id ) ),
     149                );
    70150
     151                $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     152                $this->assertContains( $expected, $items );
     153        }
     154
     155        /**
     156         * Test the load_available_items_query method returns post item.
     157         *
     158         * @see WP_Customize_Nav_Menus::load_available_items_query()
     159         */
     160        function test_load_available_items_query_returns_post_item() {
     161                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     162
     163                // Create post.
     164                $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
     165
     166                // Expected menu item array.
     167                $expected = array(
     168                        'id'         => "post-{$post_id}",
     169                        'title'      => 'Post Title',
     170                        'type'       => 'post_type',
     171                        'type_label' => 'Post',
     172                        'object'     => 'post',
     173                        'object_id'  => intval( $post_id ),
     174                        'url'        => get_permalink( intval( $post_id ) ),
     175                );
     176
     177                $items = $menus->load_available_items_query( 'post_type', 'post', 0 );
     178                $this->assertContains( $expected, $items );
     179        }
     180
     181        /**
     182         * Test the load_available_items_query method returns term item.
     183         *
     184         * @see WP_Customize_Nav_Menus::load_available_items_query()
     185         */
     186        function test_load_available_items_query_returns_term_item() {
     187                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     188
     189                // Create term.
     190                $term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
     191
     192                // Expected menu item array.
     193                $expected = array(
     194                        'id'         => "term-{$term_id}",
     195                        'title'      => 'Term Title',
     196                        'type'       => 'taxonomy',
     197                        'type_label' => 'Category',
     198                        'object'     => 'category',
     199                        'object_id'  => intval( $term_id ),
     200                        'url'        => get_term_link( intval( $term_id ), 'category' ),
     201                );
     202
     203                $items = $menus->load_available_items_query( 'taxonomy', 'category', 0 );
     204                $this->assertContains( $expected, $items );
    71205        }
    72206
    73207        /**
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    454588
    455589        }
    456590
    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 
    467591}