From 3db628dded648f034f1b9b2d169a2703f2cf9e3c Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Nov 2026 09:29:31 +0530 Subject: [PATCH 1/4] Tests: Cover the admin branch of `wp_heartbeat_settings()`. `wp_heartbeat_settings()` adds `ajaxurl` only outside the admin, because admin screens define it themselves. The tests added in [65768] all run outside the admin, so they pass even when the `is_admin()` check is removed. This adds a test for admin requests, logged in and logged out. It checks that `ajaxurl` is not added, and that a valid nonce is added only for a logged-in user. Follow-up to [65768]. Co-Authored-By: Claude Opus 5.5 --- .../tests/general/wpHeartbeatSettings.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/phpunit/tests/general/wpHeartbeatSettings.php b/tests/phpunit/tests/general/wpHeartbeatSettings.php index 94169da433499..0f1da637d6770 100644 --- a/tests/phpunit/tests/general/wpHeartbeatSettings.php +++ b/tests/phpunit/tests/general/wpHeartbeatSettings.php @@ -58,4 +58,40 @@ public function test_wp_heartbeat_settings_preserves_custom_settings() { $this->assertSame( 15, $settings['interval'] ); $this->assertSame( 'customValue', $settings['customKey'] ); } + + /** + * Tests that admin requests do not get an `ajaxurl`, and get a nonce only when logged in. + * + * Admin screens define `ajaxurl` themselves. + * + * @dataProvider data_wp_heartbeat_settings_admin + * + * @param bool $logged_in Whether a user is logged in. + */ + public function test_wp_heartbeat_settings_admin( $logged_in ) { + wp_set_current_user( $logged_in ? self::factory()->user->create() : 0 ); + set_current_screen( 'dashboard' ); + + $settings = wp_heartbeat_settings( array() ); + + $this->assertArrayNotHasKey( 'ajaxurl', $settings, 'Admin requests should not get an ajaxurl.' ); + + if ( $logged_in ) { + $this->assertNotFalse( wp_verify_nonce( $settings['nonce'] ?? '', 'heartbeat-nonce' ), 'Logged-in admin requests should get a valid nonce.' ); + } else { + $this->assertArrayNotHasKey( 'nonce', $settings, 'Logged-out requests should not get a nonce.' ); + } + } + + /** + * Data provider. + * + * @return array + */ + public static function data_wp_heartbeat_settings_admin() { + return array( + 'logged in' => array( true ), + 'logged out' => array( false ), + ); + } } From 7439da958066821de9744455bfc109cfd55d41b7 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Nov 2026 09:30:51 +0530 Subject: [PATCH 2/4] Tests: Add the ticket reference to the admin `wp_heartbeat_settings()` test. See #66757. Co-Authored-By: Claude Opus 5.5 --- tests/phpunit/tests/general/wpHeartbeatSettings.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/general/wpHeartbeatSettings.php b/tests/phpunit/tests/general/wpHeartbeatSettings.php index 0f1da637d6770..cca829b689315 100644 --- a/tests/phpunit/tests/general/wpHeartbeatSettings.php +++ b/tests/phpunit/tests/general/wpHeartbeatSettings.php @@ -64,6 +64,8 @@ public function test_wp_heartbeat_settings_preserves_custom_settings() { * * Admin screens define `ajaxurl` themselves. * + * @ticket 66757 + * * @dataProvider data_wp_heartbeat_settings_admin * * @param bool $logged_in Whether a user is logged in. From 574ce94a7847b7d5f5251524b9d767f872ac7c39 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Nov 2026 21:06:48 +0530 Subject: [PATCH 3/4] Tests: Keep one logged-in case in the admin `wp_heartbeat_settings()` test. The logged-out case runs the same `is_admin()` branch, and the existing front-end tests already cover both login states. Drop the data provider and the conditional assertion. See #66757. Co-Authored-By: Claude Opus 5.5 --- .../tests/general/wpHeartbeatSettings.php | 29 +++---------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/tests/phpunit/tests/general/wpHeartbeatSettings.php b/tests/phpunit/tests/general/wpHeartbeatSettings.php index cca829b689315..f80f8ee8b48c9 100644 --- a/tests/phpunit/tests/general/wpHeartbeatSettings.php +++ b/tests/phpunit/tests/general/wpHeartbeatSettings.php @@ -60,40 +60,19 @@ public function test_wp_heartbeat_settings_preserves_custom_settings() { } /** - * Tests that admin requests do not get an `ajaxurl`, and get a nonce only when logged in. + * Tests that admin requests do not get an `ajaxurl`, but logged-in users still get a nonce. * * Admin screens define `ajaxurl` themselves. * * @ticket 66757 - * - * @dataProvider data_wp_heartbeat_settings_admin - * - * @param bool $logged_in Whether a user is logged in. */ - public function test_wp_heartbeat_settings_admin( $logged_in ) { - wp_set_current_user( $logged_in ? self::factory()->user->create() : 0 ); + public function test_wp_heartbeat_settings_admin() { + wp_set_current_user( self::factory()->user->create() ); set_current_screen( 'dashboard' ); $settings = wp_heartbeat_settings( array() ); $this->assertArrayNotHasKey( 'ajaxurl', $settings, 'Admin requests should not get an ajaxurl.' ); - - if ( $logged_in ) { - $this->assertNotFalse( wp_verify_nonce( $settings['nonce'] ?? '', 'heartbeat-nonce' ), 'Logged-in admin requests should get a valid nonce.' ); - } else { - $this->assertArrayNotHasKey( 'nonce', $settings, 'Logged-out requests should not get a nonce.' ); - } - } - - /** - * Data provider. - * - * @return array - */ - public static function data_wp_heartbeat_settings_admin() { - return array( - 'logged in' => array( true ), - 'logged out' => array( false ), - ); + $this->assertNotFalse( wp_verify_nonce( $settings['nonce'] ?? '', 'heartbeat-nonce' ), 'Logged-in admin requests should get a valid nonce.' ); } } From 2c7996f8d8e4fcc471163acddecd2e4f05015bbb Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 26 Nov 2026 18:18:05 +0530 Subject: [PATCH 4/4] Tests: Link the admin `wp_heartbeat_settings()` test to #66820. See #66820. Co-Authored-By: Claude Opus 5.5 --- tests/phpunit/tests/general/wpHeartbeatSettings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/general/wpHeartbeatSettings.php b/tests/phpunit/tests/general/wpHeartbeatSettings.php index f80f8ee8b48c9..85a4b980f14ff 100644 --- a/tests/phpunit/tests/general/wpHeartbeatSettings.php +++ b/tests/phpunit/tests/general/wpHeartbeatSettings.php @@ -64,7 +64,7 @@ public function test_wp_heartbeat_settings_preserves_custom_settings() { * * Admin screens define `ajaxurl` themselves. * - * @ticket 66757 + * @ticket 66820 */ public function test_wp_heartbeat_settings_admin() { wp_set_current_user( self::factory()->user->create() );