From c4cccefcc8daa89fd872a88de0eee5a248d21225 Mon Sep 17 00:00:00 2001 From: rvpatel Date: Thu, 22 Oct 2026 00:21:31 +0530 Subject: [PATCH] Site Health: Use a passing label for the insecure registration test. The insecure registration test set the label for its critical result as the default, so a site with safe registration settings listed "Open Registration with privileged default role" under passed tests. The passing result now reads "New users are not given a privileged role", and the critical result sets its own label in sentence case. Its description is wrapped in a paragraph, like the passing description and other Site Health results. Adds the first tests for this check. See #66359. --- .../includes/class-wp-site-health.php | 5 +- ...pSiteHealthGetTestInsecureRegistration.php | 109 ++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/admin/wpSiteHealthGetTestInsecureRegistration.php diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index de0d7cd8da332..51562a02fe112 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -1905,7 +1905,7 @@ public function get_test_insecure_registration() { $default_role = get_option( 'default_role' ); $result = array( - 'label' => __( 'Open Registration with privileged default role' ), + 'label' => __( 'New users are not given a privileged role' ), 'status' => 'good', 'badge' => array( 'label' => __( 'Security' ), @@ -1917,7 +1917,8 @@ public function get_test_insecure_registration() { ); if ( $users_can_register && in_array( $default_role, array( 'editor', 'administrator' ), true ) ) { - $result['description'] = __( 'Registration is open to anyone, and the default role is set to a privileged role.' ); + $result['label'] = __( 'Open registration with a privileged default role' ); + $result['description'] = '

' . __( 'Registration is open to anyone, and the default role is set to a privileged role.' ) . '

'; $result['status'] = 'critical'; $result['actions'] = sprintf( '

%s

', diff --git a/tests/phpunit/tests/admin/wpSiteHealthGetTestInsecureRegistration.php b/tests/phpunit/tests/admin/wpSiteHealthGetTestInsecureRegistration.php new file mode 100644 index 0000000000000..402dff0a4161f --- /dev/null +++ b/tests/phpunit/tests/admin/wpSiteHealthGetTestInsecureRegistration.php @@ -0,0 +1,109 @@ +set_registration( $users_can_register ); + update_option( 'default_role', $default_role ); + + $result = ( new WP_Site_Health() )->get_test_insecure_registration(); + + $this->assertSame( $expected_status, $result['status'] ); + $this->assertSame( 'insecure_registration', $result['test'] ); + } + + /** + * Data provider. + * + * @return array + */ + public static function data_registration_settings() { + return array( + 'registration closed, administrator' => array( 0, 'administrator', 'good' ), + 'registration closed, editor' => array( 0, 'editor', 'good' ), + 'registration open, subscriber' => array( 1, 'subscriber', 'good' ), + 'registration open, contributor' => array( 1, 'contributor', 'good' ), + 'registration open, author' => array( 1, 'author', 'good' ), + 'registration open, editor' => array( 1, 'editor', 'critical' ), + 'registration open, administrator' => array( 1, 'administrator', 'critical' ), + ); + } + + /** + * Tests that a passing result does not use the label describing the problem. + * + * @ticket 66359 + */ + public function test_passing_result_has_a_label_describing_the_safe_state() { + $this->set_registration( 1 ); + update_option( 'default_role', 'subscriber' ); + + $result = ( new WP_Site_Health() )->get_test_insecure_registration(); + + $this->assertSame( 'good', $result['status'] ); + $this->assertSame( 'New users are not given a privileged role', $result['label'] ); + $this->assertSame( '', $result['actions'] ); + } + + /** + * Tests the label, description markup and action of a critical result. + * + * @ticket 66359 + */ + public function test_critical_result_describes_the_problem() { + $this->set_registration( 1 ); + update_option( 'default_role', 'administrator' ); + + $result = ( new WP_Site_Health() )->get_test_insecure_registration(); + + $this->assertSame( 'critical', $result['status'] ); + $this->assertSame( 'Open registration with a privileged default role', $result['label'] ); + $this->assertStringStartsWith( '

', $result['description'], 'The description should be wrapped in a paragraph like other Site Health results.' ); + $this->assertStringEndsWith( '

', $result['description'] ); + $this->assertStringContainsString( admin_url( 'options-general.php' ), $result['actions'] ); + } + + /** + * Opens or closes registration. + * + * On multisite, the `users_can_register` option is filtered to follow the + * network's `registration` setting, see users_can_register_signup_filter(). + * + * @param int $users_can_register Whether anyone can register. + */ + private function set_registration( $users_can_register ) { + if ( is_multisite() ) { + update_site_option( 'registration', $users_can_register ? 'user' : 'none' ); + } else { + update_option( 'users_can_register', $users_can_register ); + } + } +}