From 84ee354268169b6066673c09ce229bab6310b1ce Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 16:50:22 -0400 Subject: [PATCH 1/2] Tests: Add unit tests for wp_ajax_get_permalink() Co-authored-by: Junie --- .../includes/ajax-actions/getPermalink.php | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php b/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php new file mode 100644 index 0000000000000..136ff1253e375 --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php @@ -0,0 +1,108 @@ +user->create( array( 'role' => 'administrator' ) ); + } + + /** + * Tests successful retrieval of a post permalink. + * + * @ticket 65252 + */ + public function test_get_permalink_success(): void { + wp_set_current_user( self::$admin_id ); + + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Test Post', + ) + ); + + $_POST = array( + 'action' => 'get-permalink', + 'post_id' => $post_id, + 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ), + ); + + try { + $this->_handleAjax( 'get-permalink' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expect success. + } + + $this->assertStringContainsString( 'p=' . $post_id, $this->_last_response ); + $this->assertStringContainsString( 'preview=true', $this->_last_response ); + } + + /** + * Tests failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_get_permalink_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'get-permalink', + 'post_id' => 123, + 'getpermalinknonce' => 'invalid-nonce', + ); + + $this->expectException( WPAjaxDieContinueException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'get-permalink' ); + } + + /** + * Tests behavior with missing post ID. + * + * @ticket 65252 + */ + public function test_get_permalink_missing_post_id(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'get-permalink', + 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ), + ); + + try { + $this->_handleAjax( 'get-permalink' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expect success (it will return a link for post ID 0, which is usually home or a generic preview link). + } + + $this->assertNotEmpty( $this->_last_response ); + } +} From 7284fbb49f07d31bed25c5e24f1544d1c5e5a009 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 20 May 2026 16:55:00 -0400 Subject: [PATCH 2/2] Update getPermalink.php --- .../admin/includes/ajax-actions/getPermalink.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php b/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php index 136ff1253e375..74da47db7dedb 100644 --- a/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php +++ b/tests/phpunit/tests/admin/includes/ajax-actions/getPermalink.php @@ -49,9 +49,9 @@ public function test_get_permalink_success(): void { ); $_POST = array( - 'action' => 'get-permalink', - 'post_id' => $post_id, - 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ), + 'action' => 'get-permalink', + 'post_id' => $post_id, + 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ), ); try { @@ -73,9 +73,9 @@ public function test_get_permalink_invalid_nonce(): void { wp_set_current_user( self::$admin_id ); $_POST = array( - 'action' => 'get-permalink', - 'post_id' => 123, - 'getpermalinknonce' => 'invalid-nonce', + 'action' => 'get-permalink', + 'post_id' => 123, + 'getpermalinknonce' => 'invalid-nonce', ); $this->expectException( WPAjaxDieContinueException::class ); @@ -93,8 +93,8 @@ public function test_get_permalink_missing_post_id(): void { wp_set_current_user( self::$admin_id ); $_POST = array( - 'action' => 'get-permalink', - 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ), + 'action' => 'get-permalink', + 'getpermalinknonce' => wp_create_nonce( 'getpermalink' ), ); try {