From 4889e7a8d2868dd326f14fec6d9783c61f5bb689 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sat, 2 May 2020 22:34:50 +0000 Subject: [PATCH] Menus: Allow empty taxonomy terms to be surfaced when searching for items. This brings the behaviour inline with that of browsing terms or using the All Items tab, which correctly shows empty terms. Props birgire, audrasjb Fixes #45298 git-svn-id: https://develop.svn.wordpress.org/trunk@47747 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/nav-menu.php | 1 + .../class-wp-customize-nav-menus.php | 1 + tests/phpunit/tests/customize/nav-menus.php | 51 ++++++++++++++++++- .../tests/menu/wpAjaxMenuQuickSearch.php | 26 ++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/includes/nav-menu.php b/src/wp-admin/includes/nav-menu.php index 64dda1db61..3cd7f53459 100644 --- a/src/wp-admin/includes/nav-menu.php +++ b/src/wp-admin/includes/nav-menu.php @@ -113,6 +113,7 @@ function _wp_ajax_menu_quick_search( $request = array() ) { 'taxonomy' => $matches[2], 'name__like' => $query, 'number' => 10, + 'hide_empty' => false, ) ); if ( empty( $terms ) || is_wp_error( $terms ) ) { diff --git a/src/wp-includes/class-wp-customize-nav-menus.php b/src/wp-includes/class-wp-customize-nav-menus.php index 1b44b1453d..8abb6a9b5d 100644 --- a/src/wp-includes/class-wp-customize-nav-menus.php +++ b/src/wp-includes/class-wp-customize-nav-menus.php @@ -369,6 +369,7 @@ final class WP_Customize_Nav_Menus { 'taxonomies' => $taxonomies, 'name__like' => $args['s'], 'number' => 20, + 'hide_empty' => false, 'offset' => 20 * ( $args['pagenum'] - 1 ), ) ); diff --git a/tests/phpunit/tests/customize/nav-menus.php b/tests/phpunit/tests/customize/nav-menus.php index 1de6047097..bb82db7e48 100644 --- a/tests/phpunit/tests/customize/nav-menus.php +++ b/tests/phpunit/tests/customize/nav-menus.php @@ -371,7 +371,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 's' => 'cat', ) ); - $this->assertEquals( 1, count( $results ) ); + $this->assertCount( 2, $results ); // Category terms Cats Drool and Uncategorized. $count = $this->filter_count_customize_nav_menu_searched_items; add_filter( 'customize_nav_menu_searched_items', array( $this, 'filter_search' ), 10, 2 ); $results = $menus->search_available_items_query( @@ -382,7 +382,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { ); $this->assertEquals( $count + 1, $this->filter_count_customize_nav_menu_searched_items ); $this->assertInternalType( 'array', $results ); - $this->assertEquals( 2, count( $results ) ); + $this->assertCount( 3, $results ); remove_filter( 'customize_nav_menu_searched_items', array( $this, 'filter_search' ), 10 ); // Test home. @@ -398,6 +398,53 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { $this->assertEquals( 'custom', $results[0]['type'] ); } + /* + * Tests that the search_available_items_query method should return term items + * not assigned to any posts. + * + * @ticket 45298 + */ + public function test_search_available_items_query_should_return_unassigned_term_items() { + $menus = new WP_Customize_Nav_Menus( $this->wp_customize ); + + register_taxonomy( + 'wptests_tax', + 'post', + array( + 'labels' => array( + 'name' => 'Tests Taxonomy', + ), + ) + ); + + $term_id = $this->factory->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foobar', + ) + ); + + // Expected menu item array. + $expected = array( + 'title' => 'foobar', + 'id' => "term-{$term_id}", + 'type' => 'taxonomy', + 'type_label' => 'Tests Taxonomy', + 'object' => 'wptests_tax', + 'object_id' => intval( $term_id ), + 'url' => get_term_link( intval( $term_id ), '' ), + ); + + $results = $menus->search_available_items_query( + array( + 'pagenum' => 1, + 's' => 'foo', + ) + ); + + $this->assertEqualSets( $expected, $results[0] ); + } + /** * Count for number of times customize_nav_menu_searched_items filtered. * diff --git a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php index 55febb9e8d..c78161d123 100644 --- a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php +++ b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php @@ -120,4 +120,30 @@ class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase { $results = explode( "\n", trim( $output ) ); $this->assertCount( 1, $results ); } + + /** + * Test that search displays terms that are not assigned to any posts. + * + * @ticket 45298 + */ + public function test_search_should_return_unassigned_term_items() { + register_taxonomy( 'wptests_tax', 'post' ); + + $this->factory->term->create( + array( + 'taxonomy' => 'wptests_tax', + 'name' => 'foobar', + ) + ); + + $request = array( + 'type' => 'quick-search-taxonomy-wptests_tax', + 'q' => 'foobar', + ); + $output = get_echo( '_wp_ajax_menu_quick_search', array( $request ) ); + + $this->assertNotEmpty( $output ); + $results = explode( "\n", trim( $output ) ); + $this->assertCount( 1, $results ); + } }