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..a3ccd4a74d12a --- /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 ( WPAjaxDieContinueException $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 ( WPAjaxDieContinueException $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 ( WPAjaxDieContinueException $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 ( WPAjaxDieContinueException $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' ); + } +}