From baf04d5b3a861be76b8f2495293d8384511cbf61 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 7 May 2026 13:20:08 -0400 Subject: [PATCH 1/2] Add unit tests for heartbeat_autosave() in wp-admin/includes/misc.php --- .../admin/includes/misc/heartbeatAutosave.php | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php diff --git a/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php b/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php new file mode 100644 index 0000000000000..8d6f5d272a5bd --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php @@ -0,0 +1,106 @@ + 'data' ); + $data = array(); + + $result = heartbeat_autosave( $response, $data ); + + $this->assertSame( $response, $result, 'The response should remain unchanged when no autosave data is provided.' ); + } + + /** + * Tests that heartbeat_autosave() correctly handles successful autosave. + * + * @ticket 65201 + */ + public function test_heartbeat_autosave_success() { + $post_id = self::factory()->post->create(); + $user_id = self::factory()->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $user_id ); + + $response = array(); + $data = array( + 'wp_autosave' => array( + 'post_id' => $post_id, + 'post_type' => 'post', + '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ), + 'post_content' => 'Autosaved content', + ), + ); + + $result = heartbeat_autosave( $response, $data ); + + $this->assertArrayHasKey( 'wp_autosave', $result ); + $this->assertTrue( $result['wp_autosave']['success'] ); + $this->assertStringContainsString( 'Draft saved at', $result['wp_autosave']['message'] ); + } + + /** + * Tests that heartbeat_autosave() correctly handles autosave errors. + * + * @ticket 65201 + */ + public function test_heartbeat_autosave_error() { + $post_id = self::factory()->post->create(); + $user_id = self::factory()->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $user_id ); + + $response = array(); + $data = array( + 'wp_autosave' => array( + 'post_id' => $post_id, + '_wpnonce' => 'invalid-nonce', + ), + ); + + $result = heartbeat_autosave( $response, $data ); + + $this->assertArrayHasKey( 'wp_autosave', $result ); + $this->assertFalse( $result['wp_autosave']['success'] ); + $this->assertSame( 'Error while saving.', $result['wp_autosave']['message'] ); + } + + /** + * Tests that heartbeat_autosave() handles empty return from wp_autosave(). + * + * @ticket 65201 + */ + public function test_heartbeat_autosave_empty_result() { + $post_id = self::factory()->post->create(); + $user_id = self::factory()->user->create( array( 'role' => 'editor' ) ); + wp_set_current_user( $user_id ); + + $response = array(); + $data = array( + 'wp_autosave' => array( + 'post_id' => $post_id, + 'post_type' => 'post', + '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ), + ), + ); + + // Trigger an empty return by making edit_post return 0. + add_filter( 'wp_insert_post_empty_content', '__return_true' ); + + $result = heartbeat_autosave( $response, $data ); + + $this->assertArrayHasKey( 'wp_autosave', $result ); + $this->assertFalse( $result['wp_autosave']['success'] ); + $this->assertSame( 'Content, title, and excerpt are empty.', $result['wp_autosave']['message'] ); + } +} From 8e5b3e0cc5cf1708c7ec6ff5762307d3807238da Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 7 May 2026 13:45:57 -0400 Subject: [PATCH 2/2] Fix formatting in heartbeatAutosave.php --- .../phpunit/tests/admin/includes/misc/heartbeatAutosave.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php b/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php index 8d6f5d272a5bd..08ec76af470b8 100644 --- a/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php +++ b/tests/phpunit/tests/admin/includes/misc/heartbeatAutosave.php @@ -88,9 +88,9 @@ public function test_heartbeat_autosave_empty_result() { $response = array(); $data = array( 'wp_autosave' => array( - 'post_id' => $post_id, - 'post_type' => 'post', - '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ), + 'post_id' => $post_id, + 'post_type' => 'post', + '_wpnonce' => wp_create_nonce( 'update-post_' . $post_id ), ), );