diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/findPosts.php b/tests/phpunit/tests/admin/includes/ajax-actions/findPosts.php new file mode 100644 index 0000000000000..2ca9b411edd7c --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/findPosts.php @@ -0,0 +1,112 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + /** + * Tests successful post search. + * + * @ticket 65252 + */ + public function test_find_posts_success(): void { + wp_set_current_user( self::$admin_id ); + + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Searchable Post Title', + ) + ); + + $_POST = array( + 'action' => 'find-posts', + 'ps' => 'Searchable', + '_ajax_nonce' => wp_create_nonce( 'find-posts' ), + ); + + try { + $this->_handleAjax( 'find-posts' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expect success. + } + + $this->assertStringContainsString( 'Searchable Post Title', $this->_last_response ); + $this->assertStringContainsString( 'class="widefat"', $this->_last_response ); + } + + /** + * Tests failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_find_posts_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'find-posts', + 'ps' => 'Searchable', + '_ajax_nonce' => 'invalid-nonce', + ); + + $this->expectException( WPAjaxDieContinueException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'find-posts' ); + } + + /** + * Tests failure when no results are found. + * + * @ticket 65252 + */ + public function test_find_posts_no_results(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'find-posts', + 'ps' => 'NonExistentTitle', + '_ajax_nonce' => wp_create_nonce( 'find-posts' ), + ); + + try { + $this->_handleAjax( 'find-posts' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expect success. + } + + $response = json_decode( $this->_last_response, true ); + $this->assertIsArray( $response ); + $this->assertFalse( $response['success'] ); + $this->assertSame( 'No items found.', $response['data'] ); + } +}