From 5d71ebb1178578d4b5e433beda345443258a9825 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 12:57:29 -0400 Subject: [PATCH 1/3] Tests: Add unit tests for wp_ajax_wp_link_ajax() Co-authored-by: Junie --- .../includes/ajax-actions/wpLinkAjax.php | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php new file mode 100644 index 0000000000000..42f3b31661970 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php @@ -0,0 +1,156 @@ +post->create_and_get( + array( + 'post_title' => 'Test Link Search', + 'post_status' => 'publish', + ) + ); + + $_POST = array( + 'action' => 'wp-link-ajax', + '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ), + 'search' => 'Test Link Search', + ); + + try { + $this->_handleAjax( 'wp-link-ajax' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect it to die after echoing JSON. + } + + $response = json_decode( trim( $this->_last_response ), true ); + + $this->assertIsArray( $response ); + $this->assertCount( 1, $response ); + $this->assertSame( $post->ID, $response[0]['ID'] ); + $this->assertSame( $post->post_title, $response[0]['title'] ); + } + + /** + * Tests successful search using the 'term' parameter. + * + * @ticket 65252 + */ + public function test_wp_link_ajax_term_success(): void { + $post = self::factory()->post->create_and_get( + array( + 'post_title' => 'Another Test Link', + 'post_status' => 'publish', + ) + ); + + $_POST = array( + 'action' => 'wp-link-ajax', + '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ), + 'term' => 'Another Test Link', + ); + + try { + $this->_handleAjax( 'wp-link-ajax' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect it to die after echoing JSON. + } + + $response = json_decode( trim( $this->_last_response ), true ); + + $this->assertIsArray( $response ); + $this->assertCount( 1, $response ); + $this->assertSame( $post->ID, $response[0]['ID'] ); + } + + /** + * Tests successful search with pagination. + * + * @ticket 65252 + */ + public function test_wp_link_ajax_pagination(): void { + // Create 21 posts to ensure at least 2 pages (default is 20 per page). + self::factory()->post->create_many( + 21, + array( + 'post_title' => 'Paginated Post', + 'post_status' => 'publish', + ) + ); + + $_POST = array( + 'action' => 'wp-link-ajax', + '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ), + 'search' => 'Paginated Post', + 'page' => 2, + ); + + try { + $this->_handleAjax( 'wp-link-ajax' ); + } catch ( WPAjaxDieStopException $e ) { + // We expect it to die after echoing JSON. + } + + $response = json_decode( trim( $this->_last_response ), true ); + + $this->assertIsArray( $response ); + // On page 2, we should have 1 post. + $this->assertCount( 1, $response ); + } + + /** + * Tests search failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_wp_link_ajax_invalid_nonce(): void { + $_POST = array( + 'action' => 'wp-link-ajax', + '_ajax_linking_nonce' => 'invalid-nonce', + 'search' => 'Test', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'wp-link-ajax' ); + } + + /** + * Tests behavior when no results are found. + * + * @ticket 65252 + */ + public function test_wp_link_ajax_no_results(): void { + $_POST = array( + 'action' => 'wp-link-ajax', + '_ajax_linking_nonce' => wp_create_nonce( 'internal-linking' ), + 'search' => 'NonExistentPostTitle', + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '0' ); + + $this->_handleAjax( 'wp-link-ajax' ); + } +} From 57c7f645eab391bca49e3b3d396f0519ab241a33 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 13:39:26 -0400 Subject: [PATCH 2/3] Tests: Fix exception handling in wpLinkAjax tests --- .../tests/admin/includes/ajax-actions/wpLinkAjax.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php index 42f3b31661970..bd9ae6d9b200f 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php @@ -39,7 +39,7 @@ public function test_wp_link_ajax_search_success(): void { try { $this->_handleAjax( 'wp-link-ajax' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // We expect it to die after echoing JSON. } @@ -72,7 +72,7 @@ public function test_wp_link_ajax_term_success(): void { try { $this->_handleAjax( 'wp-link-ajax' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // We expect it to die after echoing JSON. } @@ -107,7 +107,7 @@ public function test_wp_link_ajax_pagination(): void { try { $this->_handleAjax( 'wp-link-ajax' ); - } catch ( WPAjaxDieStopException $e ) { + } catch ( WPAjaxDieContinueException $e ) { // We expect it to die after echoing JSON. } @@ -148,7 +148,7 @@ public function test_wp_link_ajax_no_results(): void { 'search' => 'NonExistentPostTitle', ); - $this->expectException( WPAjaxDieStopException::class ); + $this->expectException( WPAjaxDieContinueException::class ); $this->expectExceptionMessage( '0' ); $this->_handleAjax( 'wp-link-ajax' ); From 5e856fe88c490285a2340a52f428865b0adad21e Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 16:37:52 -0400 Subject: [PATCH 3/3] Tests: Fix no-results assertion in wpLinkAjax tests --- .../tests/admin/includes/ajax-actions/wpLinkAjax.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php index bd9ae6d9b200f..a081618ad0f6d 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpLinkAjax.php @@ -148,9 +148,13 @@ public function test_wp_link_ajax_no_results(): void { 'search' => 'NonExistentPostTitle', ); - $this->expectException( WPAjaxDieContinueException::class ); - $this->expectExceptionMessage( '0' ); + try { + $this->_handleAjax( 'wp-link-ajax' ); + } catch ( WPAjaxDieContinueException $e ) { + $this->assertSame( '0', $e->getMessage() ); + return; + } - $this->_handleAjax( 'wp-link-ajax' ); + $this->fail( 'Expected WPAjaxDieContinueException was not thrown.' ); } }