From f72302e4453617d80efbd92443601696f7f5ac04 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 13:13:57 -0400 Subject: [PATCH 1/2] Tests: Add unit tests for wp_ajax_menu_quick_search() Co-authored-by: Junie --- .../includes/ajax-actions/menuQuickSearch.php | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php b/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php new file mode 100644 index 0000000000000..ae4b3a466b9e0 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php @@ -0,0 +1,195 @@ +user->create( array( 'role' => 'administrator' ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + } + + /** + * Tests successful menu quick search for a post (JSON format). + * + * @ticket 65252 + */ + public function test_menu_quick_search_post_json(): void { + wp_set_current_user( self::$admin_id ); + + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Quick Search Post', + ) + ); + + $_POST = array( + 'action' => 'menu-quick-search', + 'type' => 'get-post-item', + 'object_type' => 'post', + 'ID' => $post_id, + 'response-format' => 'json', + ); + + try { + $this->_handleAjax( 'menu-quick-search' ); + } catch ( WPAjaxDieStopException $e ) { + // Expect success. + } + + $response = json_decode( $this->_last_response, true ); + $this->assertSame( $post_id, $response['ID'] ); + $this->assertSame( 'Quick Search Post', $response['post_title'] ); + $this->assertSame( 'post', $response['post_type'] ); + } + + /** + * Tests successful menu quick search for a post (markup format). + * + * @ticket 65252 + */ + public function test_menu_quick_search_post_markup(): void { + wp_set_current_user( self::$admin_id ); + + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Quick Search Post Markup', + ) + ); + + $_POST = array( + 'action' => 'menu-quick-search', + 'type' => 'get-post-item', + 'object_type' => 'post', + 'ID' => $post_id, + 'response-format' => 'markup', + ); + + try { + $this->_handleAjax( 'menu-quick-search' ); + } catch ( WPAjaxDieStopException $e ) { + // Expect success. + } + + $this->assertStringContainsString( 'Quick Search Post Markup', $this->_last_response ); + $this->assertStringContainsString( 'menu-item-title', $this->_last_response ); + } + + /** + * Tests successful menu quick search for a taxonomy term (JSON format). + * + * @ticket 65252 + */ + public function test_menu_quick_search_taxonomy_json(): void { + wp_set_current_user( self::$admin_id ); + + $term_id = self::factory()->term->create( + array( + 'name' => 'Quick Search Term', + 'taxonomy' => 'category', + ) + ); + + $_POST = array( + 'action' => 'menu-quick-search', + 'type' => 'get-post-item', + 'object_type' => 'category', + 'ID' => $term_id, + 'response-format' => 'json', + ); + + try { + $this->_handleAjax( 'menu-quick-search' ); + } catch ( WPAjaxDieStopException $e ) { + // Expect success. + } + + $response = json_decode( $this->_last_response, true ); + $this->assertSame( $term_id, $response['ID'] ); + $this->assertSame( 'Quick Search Term', $response['post_title'] ); + $this->assertSame( 'category', $response['post_type'] ); + } + + /** + * Tests successful quick search by query (JSON format). + * + * @ticket 65252 + */ + public function test_menu_quick_search_query_json(): void { + wp_set_current_user( self::$admin_id ); + + $post_id = self::factory()->post->create( + array( + 'post_title' => 'UniqueQueryPost', + ) + ); + + $_POST = array( + 'action' => 'menu-quick-search', + 'type' => 'quick-search-posttype-post', + 'q' => 'UniqueQueryPost', + 'response-format' => 'json', + ); + + try { + $this->_handleAjax( 'menu-quick-search' ); + } catch ( WPAjaxDieStopException $e ) { + // Expect success. + } + + $response = json_decode( $this->_last_response, true ); + $this->assertSame( $post_id, $response['ID'] ); + $this->assertSame( 'UniqueQueryPost', $response['post_title'] ); + } + + /** + * Tests failure due to insufficient permissions. + * + * @ticket 65252 + */ + public function test_menu_quick_search_insufficient_permissions(): void { + wp_set_current_user( self::$subscriber_id ); + + $_POST = array( + 'action' => 'menu-quick-search', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'menu-quick-search' ); + } +} From 130cd2379063252e3cc7cf592152bdcafa69d852 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 13:45:32 -0400 Subject: [PATCH 2/2] Tests: Fix exception handling in menuQuickSearch tests --- .../tests/admin/includes/ajax-actions/menuQuickSearch.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php b/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php index ae4b3a466b9e0..a3ccd4a74d12a 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuQuickSearch.php @@ -66,7 +66,7 @@ public function test_menu_quick_search_post_json(): void { try { $this->_handleAjax( 'menu-quick-search' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // Expect success. } @@ -100,7 +100,7 @@ public function test_menu_quick_search_post_markup(): void { try { $this->_handleAjax( 'menu-quick-search' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // Expect success. } @@ -133,7 +133,7 @@ public function test_menu_quick_search_taxonomy_json(): void { try { $this->_handleAjax( 'menu-quick-search' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // Expect success. } @@ -166,7 +166,7 @@ public function test_menu_quick_search_query_json(): void { try { $this->_handleAjax( 'menu-quick-search' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // Expect success. }