<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[33322] trunk: Customizer: Finish unit tests for nav menus.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/33322">33322</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/33322","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-07-18 23:19:33 +0000 (Sat, 18 Jul 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/32687">#32687</a>.
Props valendesigns, welcher, westonruter.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcustomizenavmenusphp">trunk/src/wp-includes/class-wp-customize-nav-menus.php</a></li>
<li><a href="#trunktestsphpunittestscustomizenavmenusphp">trunk/tests/phpunit/tests/customize/nav-menus.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsajaxCustomizeMenusphp">trunk/tests/phpunit/tests/ajax/CustomizeMenus.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpcustomizenavmenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-nav-menus.php    2015-07-18 17:09:40 UTC (rev 33321)
+++ trunk/src/wp-includes/class-wp-customize-nav-menus.php      2015-07-18 23:19:33 UTC (rev 33322)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -80,20 +80,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $obj_type = sanitize_key( $_POST['obj_type'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
-                       wp_send_json_error( 'nav_menus_invalid_obj_type' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $obj_name = sanitize_key( $_POST['type'] );
+               $page = empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] );
+               $items = $this->load_available_items_query( $obj_type, $obj_name, $page );
+
+               if ( is_wp_error( $items ) ) {
+                       wp_send_json_error( $items->get_error_code() );
+               } else {
+                       wp_send_json_success( array( 'items' => $items ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
-               $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Performs the post_type and taxonomy queries for loading available menu items.
+        *
+        * @since 4.3.0
+        * @access public
+        *
+        * @param string $obj_type Optional. Accepts any custom object type and has built-in support for
+        *                         'post_type' and 'taxonomy'. Default is 'post_type'.
+        * @param string $obj_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
+        * @param int    $page     Optional. The page number used to generate the query offset. Default is '0'.
+        * @return WP_Error|array Returns either a WP_Error object or an array of menu items.
+        */
+       public function load_available_items_query( $obj_type = 'post_type', $obj_name = 'page', $page = 0 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $items = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'post_type' === $obj_type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! get_post_type_object( $taxonomy_or_post_type ) ) {
-                               wp_send_json_error( 'nav_menus_invalid_post_type' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! get_post_type_object( $obj_name ) ) {
+                               return new WP_Error( 'nav_menus_invalid_post_type' );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( 0 === $page && 'page' === $taxonomy_or_post_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( 0 === $page && 'page' === $obj_name ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 // Add "Home" link. Treat as a page, but switch to custom on add.
</span><span class="cx" style="display: block; padding: 0 10px">                                $items[] = array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'id'         => 'home',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -110,7 +128,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'offset'      => 10 * $page,
</span><span class="cx" style="display: block; padding: 0 10px">                                'orderby'     => 'date',
</span><span class="cx" style="display: block; padding: 0 10px">                                'order'       => 'DESC',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'post_type'   => $taxonomy_or_post_type,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'post_type'   => $obj_name,
</ins><span class="cx" style="display: block; padding: 0 10px">                         ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $posts as $post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $post_title = $post->post_title;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -129,7 +147,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( 'taxonomy' === $obj_type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $terms = get_terms( $taxonomy_or_post_type, array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $terms = get_terms( $obj_name, array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'child_of'     => 0,
</span><span class="cx" style="display: block; padding: 0 10px">                                'exclude'      => '',
</span><span class="cx" style="display: block; padding: 0 10px">                                'hide_empty'   => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -142,7 +160,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'pad_counts'   => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( is_wp_error( $terms ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                wp_send_json_error( $terms->get_error_code() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         return $terms;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $terms as $term ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -158,7 +176,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp_send_json_success( array( 'items' => $items ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $items;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -184,12 +202,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $results ) ) {
-                       wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $items ) ) {
+                       wp_send_json_error( array( 'message' => __( 'No menu items found.' ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        wp_send_json_success( array( 'items' => $results ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 wp_send_json_success( array( 'items' => $items ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -202,10 +220,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return array Results.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return array Menu items.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function search_available_items_query( $args = array() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $results = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $items = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
</span><span class="cx" style="display: block; padding: 0 10px">                $query = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -235,7 +253,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        /* translators: %d: ID of a post */
</span><span class="cx" style="display: block; padding: 0 10px">                                        $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $results[] = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $items[] = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'id'         => 'post-' . $post->ID,
</span><span class="cx" style="display: block; padding: 0 10px">                                        'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'       => 'post_type',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -258,7 +276,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Check if any taxonomies were found.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $terms ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $terms as $term ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $results[] = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $items[] = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'id'         => 'term-' . $term->term_id,
</span><span class="cx" style="display: block; padding: 0 10px">                                        'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'       => 'taxonomy',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -270,7 +288,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return $results;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return $items;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsajaxCustomizeMenusphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/ajax/CustomizeMenus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/ajax/CustomizeMenus.php                         (rev 0)
+++ trunk/tests/phpunit/tests/ajax/CustomizeMenus.php   2015-07-18 23:19:33 UTC (rev 33322)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,538 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Testing ajax customize menus functionality
+ *
+ * @package    WordPress
+ * @subpackage UnitTests
+ * @since      4.3.0
+ * @group      ajax
+ * @runTestsInSeparateProcesses
+ */
+class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase {
+
+       /**
+        * Instance of WP_Customize_Manager which is reset for each test.
+        *
+        * @var WP_Customize_Manager
+        */
+       public $wp_customize;
+
+       /**
+        * Set up the test fixture.
+        */
+       public function setUp() {
+               parent::setUp();
+               require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
+               wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
+               global $wp_customize;
+               $this->wp_customize = new WP_Customize_Manager();
+               $wp_customize = $this->wp_customize;
+       }
+
+       /**
+        * Tear down the test fixture.
+        */
+       public function tearDown() {
+               wp_set_current_user( 0 );
+               parent::tearDown();
+       }
+
+       /**
+        * Helper to keep it DRY
+        *
+        * @param string $action Action.
+        */
+       protected function make_ajax_call( $action ) {
+               // Make the request.
+               try {
+                       $this->_handleAjax( $action );
+               } catch ( WPAjaxDieContinueException $e ) {
+                       unset( $e );
+               }
+       }
+
+       /**
+        * Testing capabilities check for ajax_load_available_items method
+        *
+        * @dataProvider data_ajax_load_available_items_cap_check
+        *
+        * @param string $role              The role we're checking caps against.
+        * @param array  $expected_results  Expected results.
+        */
+       function test_ajax_load_available_items_cap_check( $role, $expected_results ) {
+
+               if ( 'administrator' != $role ) {
+                       // If we're not an admin, we should get a wp_die(-1).
+                       $this->setExpectedException( 'WPAjaxDieStopException' );
+               }
+
+               wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) );
+
+               $_POST = array(
+                       'action'                => 'load-available-menu-items-customizer',
+                       'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
+               );
+
+               $this->make_ajax_call( 'load-available-menu-items-customizer' );
+
+               // If we are an admin, we should get a proper response.
+               if ( 'administrator' === $role ) {
+                       // Get the results.
+                       $response = json_decode( $this->_last_response, true );
+
+                       $this->assertSame( $expected_results, $response );
+               }
+
+       }
+
+       /**
+        * Data provider for test_ajax_load_available_items_cap_check().
+        *
+        * Provides various post_args to induce error messages in the that can be
+        * compared to the expected_results.
+        *
+        * @since 4.3.0
+        *
+        * @return array {
+        *     @type array {
+        *         @string string $role             The role that will test caps for.
+        *         @array  array  $expected_results The expected results from the ajax call.
+        *     }
+        * }
+        */
+       function data_ajax_load_available_items_cap_check() {
+               return array(
+                       array(
+                               'subscriber',
+                               array(),
+                       ),
+                       array(
+                               'contributor',
+                               array(),
+                       ),
+                       array(
+                               'author',
+                               array(),
+                       ),
+                       array(
+                               'editor',
+                               array(),
+                       ),
+                       array(
+                               'administrator',
+                               array(
+                                       'success' => false,
+                                       'data'    => 'nav_menus_missing_obj_type_or_type_parameter',
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * Testing the error messaging for ajax_load_available_items
+        *
+        * @dataProvider data_ajax_load_available_items_error_messages
+        *
+        * @param array $post_args POST args.
+        * @param mixed $expected_results Expected results.
+        */
+       function test_ajax_load_available_items_error_messages( $post_args, $expected_results ) {
+
+               $_POST = array_merge( array(
+                       'action'                => 'load-available-menu-items-customizer',
+                       'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
+               ), $post_args );
+
+               // Make the request.
+               $this->make_ajax_call( 'load-available-menu-items-customizer' );
+
+               // Get the results.
+               $response = json_decode( $this->_last_response, true );
+
+               $this->assertSame( $expected_results, $response );
+       }
+
+       /**
+        * Data provider for test_ajax_load_available_items_error_message().
+        *
+        * Provides various post_args to induce error messages in the that can be
+        * compared to the expected_results.
+        *
+        * @since 4.3.0
+        *
+        * @return array {
+        *     @type array {
+        *         @array array $post_args        The arguments that will merged with the $_POST array.
+        *         @array array $expected_results The expected results from the ajax call.
+        *     }
+        * }
+        */
+       function data_ajax_load_available_items_error_messages() {
+               return array(
+                       // Testing empty obj_type and type.
+                       array(
+                               array(
+                                       'obj_type' => '',
+                                       'type'     => '',
+                               ),
+                               array(
+                                       'success'  => false,
+                                       'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
+                               ),
+                       ),
+                       // Testing empty obj_type.
+                       array(
+                               array(
+                                       'obj_type' => '',
+                                       'type'     => 'post',
+                               ),
+                               array(
+                                       'success'  => false,
+                                       'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
+                               ),
+                       ),
+                       // Testing empty type.
+                       array(
+                               array(
+                                       'obj_type' => '',
+                                       'type'     => 'post',
+                               ),
+                               array(
+                                       'success'  => false,
+                                       'data'     => 'nav_menus_missing_obj_type_or_type_parameter',
+                               ),
+                       ),
+                       // Testing incorrect type option.
+                       array(
+                               array(
+                                       'obj_type' => 'post_type',
+                                       'type'     => 'invalid',
+                               ),
+                               array(
+                                       'success'  => false,
+                                       'data'     => 'nav_menus_invalid_post_type',
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * Testing the success status.
+        *
+        * @dataProvider data_ajax_load_available_items_success_status
+        *
+        * @param array $post_args       POST args.
+        * @param array $success_status  Success status.
+        */
+       function test_ajax_load_available_items_success_status( $post_args, $success_status ) {
+
+               $_POST = array_merge( array(
+                       'action'                => 'load-available-menu-items-customizer',
+                       'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
+               ), $post_args );
+
+               // Make the request.
+               $this->make_ajax_call( 'load-available-menu-items-customizer' );
+
+               // Get the results.
+               $response = json_decode( $this->_last_response, true );
+               $this->assertSame( $success_status, $response['success'] );
+
+       }
+
+       /**
+        * Data provider for test_ajax_load_available_items_success_status().
+        *
+        * Provides various post_args to retrieve results and compare against
+        * the success status.
+        *
+        * @since 4.3.0
+        *
+        * @return array {
+        *     @type array {
+        *         @type array $post_args      The arguments that will merged with the $_POST array.
+        *         @type bool  $success_status The expected success status.
+        *     }
+        * }
+        */
+       function data_ajax_load_available_items_success_status() {
+               return array(
+                       array(
+                               array(
+                                       'obj_type' => 'post_type',
+                                       'type'     => 'post',
+                               ),
+                               true,
+                       ),
+                       array(
+                               array(
+                                       'obj_type' => 'post_type',
+                                       'type'     => 'page',
+                               ),
+                               true,
+                       ),
+                       array(
+                               array(
+                                       'obj_type' => 'post_type',
+                                       'type'     => 'custom',
+                               ),
+                               false,
+                       ),
+                       array(
+                               array(
+                                       'obj_type' => 'taxonomy',
+                                       'type'     => 'post_tag',
+                               ),
+                               true,
+                       ),
+               );
+       }
+
+       /**
+        * Testing the array structure for a single item
+        *
+        * @dataProvider data_ajax_load_available_items_structure
+        *
+        * @param array $post_args POST args.
+        */
+       function test2_ajax_load_available_items_structure( $post_args ) {
+
+               $expected_keys = array(
+                       'id',
+                       'title',
+                       'type',
+                       'type_label',
+                       'object',
+                       'object_id',
+                       'url',
+               );
+
+               // Create some terms and pages.
+               $this->factory->term->create_many( 5 );
+               $this->factory->post->create_many( 5, array( 'post_type' => 'page' ) );
+
+               $_POST = array_merge( array(
+                       'action'                => 'load-available-menu-items-customizer',
+                       'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
+               ), $post_args );
+
+               // Make the request.
+               $this->make_ajax_call( 'load-available-menu-items-customizer' );
+
+               // Get the results.
+               $response = json_decode( $this->_last_response, true );
+
+               $this->assertNotEmpty( $response['data']['items'] );
+
+               // Get the second index to avoid the home page edge case.
+               $test_item = $response['data']['items'][1];
+
+               foreach ( $expected_keys as $key ) {
+                       $this->assertArrayHasKey( $key, $test_item );
+                       $this->assertNotEmpty( $test_item[ $key ] );
+               }
+
+               // Special test for the home page.
+               if ( 'page' === $test_item['object'] ) {
+                       $home = $response['data']['items'][0];
+                       foreach ( $expected_keys as $key ) {
+                               if ( 'object_id' !== $key ) {
+                                       $this->assertArrayHasKey( $key, $home );
+                                       if ( 'object' !== $key ) {
+                                               $this->assertNotEmpty( $home[ $key ] );
+                                       }
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Data provider for test_ajax_load_available_items_structure().
+        *
+        * Provides various post_args to return a list of items to test the array structure of.
+        *
+        * @since 4.3.0
+        *
+        * @return array {
+        *     @type array {
+        *         @type array $post_args The arguments that will merged with the $_POST array.
+        *     }
+        * }
+        */
+       function data_ajax_load_available_items_structure() {
+               return array(
+                       array(
+                               array(
+                                       'obj_type' => 'post_type',
+                                       'type'     => 'post',
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'obj_type' => 'post_type',
+                                       'type'     => 'page',
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'obj_type' => 'taxonomy',
+                                       'type'     => 'post_tag',
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * Testing the error messages for ajax_search_available_items
+        *
+        * @dataProvider data_ajax_search_available_items_caps_check
+        *
+        * @param string $role             Role.
+        * @param array  $expected_results Expected results.
+        */
+       function test_ajax_search_available_items_caps_check( $role, $expected_results ) {
+
+               if ( 'administrator' != $role ) {
+                       // If we're not an admin, we should get a wp_die(-1).
+                       $this->setExpectedException( 'WPAjaxDieStopException' );
+               }
+
+               wp_set_current_user( $this->factory->user->create( array( 'role' => $role ) ) );
+
+               $_POST = array(
+                       'action'                => 'search-available-menu-items-customizer',
+                       'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
+               );
+
+               $this->make_ajax_call( 'search-available-menu-items-customizer' );
+
+               // If we are an admin, we should get a proper response.
+               if ( 'administrator' === $role ) {
+                       // Get the results.
+                       $response = json_decode( $this->_last_response, true );
+
+                       $this->assertSame( $expected_results, $response );
+               }
+       }
+
+       /**
+        * Data provider for test_ajax_search_available_items_caps_check().
+        *
+        * Provides various post_args to induce error messages in the that can be
+        * compared to the expected_results.
+        *
+        * @since 4.3.0
+        *
+        * @todo Make this more DRY
+        *
+        * @return array {
+        *     @type array {
+        *         @string string $role             The role that will test caps for.
+        *         @array  array  $expected_results The expected results from the ajax call.
+        *     }
+        * }
+        */
+       function data_ajax_search_available_items_caps_check() {
+               return array(
+                       array(
+                               'subscriber',
+                               array(),
+                       ),
+                       array(
+                               'contributor',
+                               array(),
+                       ),
+                       array(
+                               'author',
+                               array(),
+                       ),
+                       array(
+                               'editor',
+                               array(),
+                       ),
+                       array(
+                               'administrator',
+                               array(
+                                       'success' => false,
+                                       'data'    => 'nav_menus_missing_search_parameter',
+                               ),
+                       ),
+               );
+       }
+
+       /**
+        * Testing the results of various searches
+        *
+        * @dataProvider data_ajax_search_available_items_results
+        *
+        * @param array $post_args        POST args.
+        * @param array $expected_results Expected results.
+        */
+       function test_ajax_search_available_items_results( $post_args, $expected_results ) {
+
+               $this->factory->post->create_many( 5, array( 'post_title' => 'Test Post' ) );
+
+               $_POST = array_merge( array(
+                       'action'                => 'search-available-menu-items-customizer',
+                       'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
+               ), $post_args );
+
+               $this->make_ajax_call( 'search-available-menu-items-customizer' );
+
+               $response = json_decode( $this->_last_response, true );
+
+               if ( isset( $post_args['search'] ) && 'test' === $post_args['search'] ) {
+                       $this->assertsame( true, $response['success'] );
+                       $this->assertSame( 5, count( $response['data']['items'] ) );
+               } else {
+                       $this->assertSame( $expected_results, $response );
+               }
+
+       }
+
+       /**
+        * Data provider for test_ajax_search_available_items_results().
+        *
+        * Provides various post_args to test the results.
+        *
+        * @since 4.3.0
+        *
+        * @return array {
+        *     @type array {
+        *         @string string $post_args        The args that will be passed to ajax.
+        *         @array  array  $expected_results The expected results from the ajax call.
+        *     }
+        * }
+        */
+       function data_ajax_search_available_items_results() {
+               return array(
+                       array(
+                               array(),
+                               array(
+                                       'success' => false,
+                                       'data'    => 'nav_menus_missing_search_parameter',
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'search'  => 'all_the_things',
+                               ),
+                               array(
+                                       'success' => false,
+                                       'data'    => array(
+                                               'message' => 'No menu items found.',
+                                       ),
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'search'  => 'test',
+                               ),
+                               array(
+                                       'success' => true,
+                                       array(),
+                               ),
+                       ),
+               );
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestscustomizenavmenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/nav-menus.php 2015-07-18 17:09:40 UTC (rev 33321)
+++ trunk/tests/phpunit/tests/customize/nav-menus.php   2015-07-18 23:19:33 UTC (rev 33322)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,28 +49,164 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Test the test_load_available_items_ajax method.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Test that the load_available_items_query method returns a WP_Error object.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @see WP_Customize_Nav_Menus::load_available_items_ajax()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @see WP_Customize_Nav_Menus::load_available_items_query()
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_load_available_items_ajax() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_load_available_items_query_returns_wp_error() {
+               $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->markTestIncomplete( 'This test has not been implemented.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Invalid post type $obj_name.
+               $items = $menus->load_available_items_query( 'post_type', 'invalid' );
+               $this->assertInstanceOf( 'WP_Error', $items );
+               $this->assertEquals( 'nav_menus_invalid_post_type', $items->get_error_code() );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Invalid taxonomy $obj_name.
+               $items = $menus->load_available_items_query( 'taxonomy', 'invalid' );
+               $this->assertInstanceOf( 'WP_Error', $items );
+               $this->assertEquals( 'invalid_taxonomy', $items->get_error_code() );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Test the search_available_items_ajax method.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Test the load_available_items_query method maybe returns the home page item.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @see WP_Customize_Nav_Menus::search_available_items_ajax()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @see WP_Customize_Nav_Menus::load_available_items_query()
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function test_search_available_items_ajax() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_load_available_items_query_maybe_returns_home() {
+               $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->markTestIncomplete( 'This test has not been implemented.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Expected menu item array.
+               $expected = array(
+                       'id'         => 'home',
+                       'title'      => _x( 'Home', 'nav menu home label' ),
+                       'type'       => 'custom',
+                       'type_label' => __( 'Custom Link' ),
+                       'object'     => '',
+                       'url'        => home_url(),
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Create pages.
+               $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) );
+
+               // Home is included in menu items when page is zero.
+               $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
+               $this->assertContains( $expected, $items );
+
+               // Home is not included in menu items when page is larger than zero.
+               $items = $menus->load_available_items_query( 'post_type', 'page', 1 );
+               $this->assertNotEmpty( $items );
+               $this->assertNotContains( $expected, $items );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test the load_available_items_query method returns post item.
+        *
+        * @see WP_Customize_Nav_Menus::load_available_items_query()
+        */
+       function test_load_available_items_query_returns_post_item_with_page_number() {
+               $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
+
+               // Create page.
+               $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
+
+               // Create pages.
+               $this->factory->post->create_many( 10 );
+
+               // Expected menu item array.
+               $expected = array(
+                       'id'         => "post-{$post_id}",
+                       'title'      => 'Post Title',
+                       'type'       => 'post_type',
+                       'type_label' => 'Post',
+                       'object'     => 'post',
+                       'object_id'  => intval( $post_id ),
+                       'url'        => get_permalink( intval( $post_id ) ),
+               );
+
+               // Offset the query and get the second page of menu items.
+               $items = $menus->load_available_items_query( 'post_type', 'post', 1 );
+               $this->assertContains( $expected, $items );
+       }
+
+       /**
+        * Test the load_available_items_query method returns page item.
+        *
+        * @see WP_Customize_Nav_Menus::load_available_items_query()
+        */
+       function test_load_available_items_query_returns_page_item() {
+               $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
+
+               // Create page.
+               $page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
+
+               // Expected menu item array.
+               $expected = array(
+                       'id'         => "post-{$page_id}",
+                       'title'      => 'Page Title',
+                       'type'       => 'post_type',
+                       'type_label' => 'Page',
+                       'object'     => 'page',
+                       'object_id'  => intval( $page_id ),
+                       'url'        => get_permalink( intval( $page_id ) ),
+               );
+
+               $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
+               $this->assertContains( $expected, $items );
+       }
+
+       /**
+        * Test the load_available_items_query method returns post item.
+        *
+        * @see WP_Customize_Nav_Menus::load_available_items_query()
+        */
+       function test_load_available_items_query_returns_post_item() {
+               $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
+
+               // Create post.
+               $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
+
+               // Expected menu item array.
+               $expected = array(
+                       'id'         => "post-{$post_id}",
+                       'title'      => 'Post Title',
+                       'type'       => 'post_type',
+                       'type_label' => 'Post',
+                       'object'     => 'post',
+                       'object_id'  => intval( $post_id ),
+                       'url'        => get_permalink( intval( $post_id ) ),
+               );
+
+               $items = $menus->load_available_items_query( 'post_type', 'post', 0 );
+               $this->assertContains( $expected, $items );
+       }
+
+       /**
+        * Test the load_available_items_query method returns term item.
+        *
+        * @see WP_Customize_Nav_Menus::load_available_items_query()
+        */
+       function test_load_available_items_query_returns_term_item() {
+               $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
+
+               // Create term.
+               $term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
+
+               // Expected menu item array.
+               $expected = array(
+                       'id'         => "term-{$term_id}",
+                       'title'      => 'Term Title',
+                       'type'       => 'taxonomy',
+                       'type_label' => 'Category',
+                       'object'     => 'category',
+                       'object_id'  => intval( $term_id ),
+                       'url'        => get_term_link( intval( $term_id ), 'category' ),
+               );
+
+               $items = $menus->load_available_items_query( 'taxonomy', 'category', 0 );
+               $this->assertContains( $expected, $items );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Test the search_available_items_query method.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @see WP_Customize_Nav_Menus::search_available_items_query()
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -454,14 +590,4 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Test the render_menu method.
-        *
-        * @see WP_Customize_Nav_Menus::render_menu()
-        */
-       function test_render_menu() {
-
-               $this->markTestIncomplete( 'This test has not been implemented.' );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>