|
| 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