Make WordPress Core

Ticket #32687: 32687.8.diff

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

Additional changes: https://github.com/xwp/wordpress-develop/compare/820ff44...a5d6a8d

  • 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..c39e9b5
    - +  
     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 type option.
     206                        array(
     207                                array(
     208                                        'obj_type' => 'post_type',
     209                                        'type'     => 'invalid',
     210                                ),
     211                                array(
     212                                        'success'  => false,
     213                                        'data'     => 'nav_menus_invalid_post_type',
     214                                ),
     215                        ),
     216                );
     217        }
     218
     219        /**
     220         * Testing the success status.
     221         *
     222         * @dataProvider data_ajax_load_available_items_success_status
     223         *
     224         * @param array $post_args       POST args.
     225         * @param array $success_status  Success status.
     226         */
     227        function test_ajax_load_available_items_success_status( $post_args, $success_status ) {
     228
     229                $_POST = array_merge( array(
     230                        'action'                => 'load-available-menu-items-customizer',
     231                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     232                ), $post_args );
     233
     234                // Make the request.
     235                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     236
     237                // Get the results.
     238                $response = json_decode( $this->_last_response, true );
     239                $this->assertSame( $success_status, $response['success'] );
     240
     241        }
     242
     243        /**
     244         * Data provider for test_ajax_load_available_items_success_status().
     245         *
     246         * Provides various post_args to retrieve results and compare against
     247         * the success status.
     248         *
     249         * @since 4.3.0
     250         *
     251         * @return array {
     252         *     @type array {
     253         *         @type array $post_args      The arguments that will merged with the $_POST array.
     254         *         @type bool  $success_status The expected success status.
     255         *     }
     256         * }
     257         */
     258        function data_ajax_load_available_items_success_status() {
     259                return array(
     260                        array(
     261                                array(
     262                                        'obj_type' => 'post_type',
     263                                        'type'     => 'post',
     264                                ),
     265                                true,
     266                        ),
     267                        array(
     268                                array(
     269                                        'obj_type' => 'post_type',
     270                                        'type'     => 'page',
     271                                ),
     272                                true,
     273                        ),
     274                        array(
     275                                array(
     276                                        'obj_type' => 'post_type',
     277                                        'type'     => 'custom',
     278                                ),
     279                                false,
     280                        ),
     281                        array(
     282                                array(
     283                                        'obj_type' => 'taxonomy',
     284                                        'type'     => 'post_tag',
     285                                ),
     286                                true,
     287                        ),
     288                );
     289        }
     290
     291        /**
     292         * Testing the array structure for a single item
     293         *
     294         * @dataProvider data_ajax_load_available_items_structure
     295         *
     296         * @param array $post_args POST args.
     297         */
     298        function test2_ajax_load_available_items_structure( $post_args ) {
     299
     300                $expected_keys = array(
     301                        'id',
     302                        'title',
     303                        'type',
     304                        'type_label',
     305                        'object',
     306                        'object_id',
     307                        'url',
     308                );
     309
     310                // Create some terms and pages.
     311                $this->factory->term->create_many( 5 );
     312                $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) );
     313
     314                $_POST = array_merge( array(
     315                        'action'                => 'load-available-menu-items-customizer',
     316                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     317                ), $post_args );
     318
     319                // Make the request.
     320                $this->make_ajax_call( 'load-available-menu-items-customizer' );
     321
     322                // Get the results.
     323                $response = json_decode( $this->_last_response, true );
     324
     325                $this->assertNotEmpty( $response['data']['items'] );
     326
     327                // Get the second index to avoid the home page edge case.
     328                $test_item = $response['data']['items'][1];
     329
     330                foreach ( $expected_keys as $key ) {
     331                        $this->assertArrayHasKey( $key, $test_item );
     332                        $this->assertNotEmpty( $test_item[ $key ] );
     333                }
     334
     335                // Special test for the home page.
     336                if ( 'page' === $test_item['object'] ) {
     337                        $home = $response['data']['items'][0];
     338                        foreach ( $expected_keys as $key ) {
     339                                if ( 'object_id' !== $key ) {
     340                                        $this->assertArrayHasKey( $key, $home );
     341                                        if ( 'object' !== $key ) {
     342                                                $this->assertNotEmpty( $home[ $key ] );
     343                                        }
     344                                }
     345                        }
     346                }
     347        }
     348
     349        /**
     350         * Data provider for test_ajax_load_available_items_structure().
     351         *
     352         * Provides various post_args to return a list of items to test the array structure of.
     353         *
     354         * @since 4.3.0
     355         *
     356         * @return array {
     357         *     @type array {
     358         *         @type array $post_args The arguments that will merged with the $_POST array.
     359         *     }
     360         * }
     361         */
     362        function data_ajax_load_available_items_structure() {
     363                return array(
     364                        array(
     365                                array(
     366                                        'obj_type' => 'post_type',
     367                                        'type'     => 'post',
     368                                ),
     369                        ),
     370                        array(
     371                                array(
     372                                        'obj_type' => 'post_type',
     373                                        'type'     => 'page',
     374                                ),
     375                        ),
     376                        array(
     377                                array(
     378                                        'obj_type' => 'taxonomy',
     379                                        'type'     => 'post_tag',
     380                                ),
     381                        ),
     382                );
     383        }
     384
     385        /**
     386         * Testing the error messages for ajax_search_available_items
     387         *
     388         * @dataProvider data_ajax_search_available_items_caps_check
     389         *
     390         * @param string $role             Role.
     391         * @param array  $expected_results Expected results.
     392         */
     393        function test_ajax_search_available_items_caps_check( $role, $expected_results ) {
     394
     395                if ( 'administrator' != $role ) {
     396                        // If we're not an admin, we should get a wp_die(-1).
     397                        $this->setExpectedException( 'WPAjaxDieStopException' );
     398                }
     399
     400                wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) );
     401
     402                $_POST = array(
     403                        'action'                => 'search-available-menu-items-customizer',
     404                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     405                );
     406
     407                $this->make_ajax_call( 'search-available-menu-items-customizer' );
     408
     409                // If we are an admin, we should get a proper response.
     410                if ( 'administrator' === $role ) {
     411                        // Get the results.
     412                        $response = json_decode( $this->_last_response, true );
     413
     414                        $this->assertSame( $expected_results, $response );
     415                }
     416        }
     417
     418        /**
     419         * Data provider for test_ajax_search_available_items_caps_check().
     420         *
     421         * Provides various post_args to induce error messages in the that can be
     422         * compared to the expected_results.
     423         *
     424         * @since 4.3.0
     425         *
     426         * @todo Make this more DRY
     427         *
     428         * @return array {
     429         *     @type array {
     430         *         @string string $role             The role that will test caps for.
     431         *         @array  array  $expected_results The expected results from the ajax call.
     432         *     }
     433         * }
     434         */
     435        function data_ajax_search_available_items_caps_check() {
     436                return array(
     437                        array(
     438                                'subscriber',
     439                                array(),
     440                        ),
     441                        array(
     442                                'contributor',
     443                                array(),
     444                        ),
     445                        array(
     446                                'author',
     447                                array(),
     448                        ),
     449                        array(
     450                                'editor',
     451                                array(),
     452                        ),
     453                        array(
     454                                'administrator',
     455                                array(
     456                                        'success' => false,
     457                                        'data'    => 'nav_menus_missing_search_parameter',
     458                                ),
     459                        ),
     460                );
     461        }
     462
     463        /**
     464         * Testing the results of various searches
     465         *
     466         * @dataProvider data_ajax_search_available_items_results
     467         *
     468         * @param array $post_args        POST args.
     469         * @param array $expected_results Expected results.
     470         */
     471        function test_ajax_search_available_items_results( $post_args, $expected_results ) {
     472
     473                $this->factory->post->create_many( 5, array( 'post_title' => 'Test Post' ) );
     474
     475                $_POST = array_merge( array(
     476                        'action'                => 'search-available-menu-items-customizer',
     477                        'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     478                ), $post_args );
     479
     480                $this->make_ajax_call( 'search-available-menu-items-customizer' );
     481
     482                $response = json_decode( $this->_last_response, true );
     483
     484                if ( isset( $post_args['search'] ) && 'test' === $post_args['search'] ) {
     485                        $this->assertsame( true, $response['success'] );
     486                        $this->assertSame( 5, count( $response['data']['items'] ) );
     487                } else {
     488                        $this->assertSame( $expected_results, $response );
     489                }
     490
     491        }
     492
     493        /**
     494         * Data provider for test_ajax_search_available_items_results().
     495         *
     496         * Provides various post_args to test the results.
     497         *
     498         * @since 4.3.0
     499         *
     500         * @return array {
     501         *     @type array {
     502         *         @string string $post_args        The args that will be passed to ajax.
     503         *         @array  array  $expected_results The expected results from the ajax call.
     504         *     }
     505         * }
     506         */
     507        function data_ajax_search_available_items_results() {
     508                return array(
     509                        array(
     510                                array(),
     511                                array(
     512                                        'success' => false,
     513                                        'data'    => 'nav_menus_missing_search_parameter',
     514                                ),
     515                        ),
     516                        array(
     517                                array(
     518                                        'search'  => 'all_the_things',
     519                                ),
     520                                array(
     521                                        'success' => false,
     522                                        'data'    => array(
     523                                                'message' => 'No menu items found.',
     524                                        ),
     525                                ),
     526                        ),
     527                        array(
     528                                array(
     529                                        'search'  => 'test',
     530                                ),
     531                                array(
     532                                        'success' => true,
     533                                        array(),
     534                                ),
     535                        ),
     536                );
     537        }
     538}
  • 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..41991a8 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 );
     62                $this->assertEquals( 'nav_menus_invalid_post_type', $items->get_error_code() );
    5963
     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() );
    6068        }
    6169
    6270        /**
    63          * Test the search_available_items_ajax method.
     71         * Test the load_available_items_query method maybe returns the home page item.
    6472         *
    65          * @see WP_Customize_Nav_Menus::search_available_items_ajax()
     73         * @see WP_Customize_Nav_Menus::load_available_items_query()
    6674         */
    67         function test_search_available_items_ajax() {
     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' ) );
    68111
    69                 $this->markTestIncomplete( 'This test has not been implemented.' );
     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                );
    70152
     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
    73209        /**
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    454590
    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}