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..74da47db7dedb --- /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 ); + } +}