Skip to content

Commit eb79078

Browse files
lancewillettarshidkv12
authored andcommitted
Site Health: Use a passing label for the insecure registration test.
The insecure registration check used its warning label as the default, so a site with safe registration settings listed "Open Registration with privileged default role" under Passed tests. Give the passing result its own label, set the warning label only on the critical result, and wrap the critical description in a paragraph like other Site Health results. Add the first tests for this check. Follow-up to [61687]. Developed in: WordPress#14669 Props ravipatel, johnbillion. Fixes #66359. git-svn-id: https://develop.svn.wordpress.org/trunk@65332 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2503671 commit eb79078

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

‎src/wp-admin/includes/class-wp-site-health.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ public function get_test_insecure_registration() {
19031903
$default_role = get_option( 'default_role' );
19041904

19051905
$result = array(
1906-
'label' => __( 'Open Registration with privileged default role' ),
1906+
'label' => __( 'New users are not given a privileged role' ),
19071907
'status' => 'good',
19081908
'badge' => array(
19091909
'label' => __( 'Security' ),
@@ -1915,7 +1915,8 @@ public function get_test_insecure_registration() {
19151915
);
19161916

19171917
if ( $users_can_register && in_array( $default_role, array( 'editor', 'administrator' ), true ) ) {
1918-
$result['description'] = __( 'Registration is open to anyone, and the default role is set to a privileged role.' );
1918+
$result['label'] = __( 'Open registration with a privileged default role' );
1919+
$result['description'] = '<p>' . __( 'Registration is open to anyone, and the default role is set to a privileged role.' ) . '</p>';
19191920
$result['status'] = 'critical';
19201921
$result['actions'] = sprintf(
19211922
'<p><a href="%s">%s</a></p>',
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/**
4+
* Tests for WP_Site_Health::get_test_insecure_registration().
5+
*
6+
* @group admin
7+
* @group site-health
8+
*
9+
* @covers WP_Site_Health::get_test_insecure_registration
10+
*/
11+
class Tests_Admin_wpSiteHealthGetTestInsecureRegistration extends WP_UnitTestCase {
12+
13+
/**
14+
* Loads the Site Health class.
15+
*/
16+
public static function set_up_before_class() {
17+
parent::set_up_before_class();
18+
19+
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
20+
}
21+
22+
/**
23+
* Tests the status for each registration setting and default role.
24+
*
25+
* @ticket 66359
26+
*
27+
* @dataProvider data_registration_settings
28+
*
29+
* @param int $users_can_register Whether anyone can register.
30+
* @param string $default_role The default role for new users.
31+
* @param string $expected_status The expected test status.
32+
*/
33+
public function test_status_depends_on_registration_and_default_role( $users_can_register, $default_role, $expected_status ) {
34+
$this->set_registration( $users_can_register );
35+
update_option( 'default_role', $default_role );
36+
37+
$result = ( new WP_Site_Health() )->get_test_insecure_registration();
38+
39+
$this->assertSame( $expected_status, $result['status'] );
40+
$this->assertSame( 'insecure_registration', $result['test'] );
41+
}
42+
43+
/**
44+
* Data provider.
45+
*
46+
* @return array<string, array{0: int, 1: string, 2: string}>
47+
*/
48+
public static function data_registration_settings() {
49+
return array(
50+
'registration closed, administrator' => array( 0, 'administrator', 'good' ),
51+
'registration closed, editor' => array( 0, 'editor', 'good' ),
52+
'registration open, subscriber' => array( 1, 'subscriber', 'good' ),
53+
'registration open, contributor' => array( 1, 'contributor', 'good' ),
54+
'registration open, author' => array( 1, 'author', 'good' ),
55+
'registration open, editor' => array( 1, 'editor', 'critical' ),
56+
'registration open, administrator' => array( 1, 'administrator', 'critical' ),
57+
);
58+
}
59+
60+
/**
61+
* Tests that a passing result does not use the label describing the problem.
62+
*
63+
* @ticket 66359
64+
*/
65+
public function test_passing_result_has_a_label_describing_the_safe_state() {
66+
$this->set_registration( 1 );
67+
update_option( 'default_role', 'subscriber' );
68+
69+
$result = ( new WP_Site_Health() )->get_test_insecure_registration();
70+
71+
$this->assertSame( 'good', $result['status'] );
72+
$this->assertSame( 'New users are not given a privileged role', $result['label'] );
73+
$this->assertSame( '', $result['actions'] );
74+
}
75+
76+
/**
77+
* Tests the label, description markup and action of a critical result.
78+
*
79+
* @ticket 66359
80+
*/
81+
public function test_critical_result_describes_the_problem() {
82+
$this->set_registration( 1 );
83+
update_option( 'default_role', 'administrator' );
84+
85+
$result = ( new WP_Site_Health() )->get_test_insecure_registration();
86+
87+
$this->assertSame( 'critical', $result['status'] );
88+
$this->assertSame( 'Open registration with a privileged default role', $result['label'] );
89+
$this->assertStringStartsWith( '<p>', $result['description'], 'The description should be wrapped in a paragraph like other Site Health results.' );
90+
$this->assertStringEndsWith( '</p>', $result['description'] );
91+
$this->assertStringContainsString( admin_url( 'options-general.php' ), $result['actions'] );
92+
}
93+
94+
/**
95+
* Opens or closes registration.
96+
*
97+
* On multisite, the `users_can_register` option is filtered to follow the
98+
* network's `registration` setting, see users_can_register_signup_filter().
99+
*
100+
* @param int $users_can_register Whether anyone can register.
101+
*/
102+
private function set_registration( $users_can_register ) {
103+
if ( is_multisite() ) {
104+
update_site_option( 'registration', $users_can_register ? 'user' : 'none' );
105+
} else {
106+
update_option( 'users_can_register', $users_can_register );
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)