Make WordPress Core


Ignore:
Timestamp:
04/09/2025 01:29:39 PM (6 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Use the ms-required group where appropriate.

This replaces the if ( is_multisite() ) conditional wrapping entire test classes with the ms-required group for more consistency across the test suite.

Follow-up to [40520].

See #63167.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/wpmuValidateUserSignup.php

    r59901 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
    4 
    5     /**
    6      * @group multisite
    7      */
    8     class Tests_Multisite_wpmuValidateUserSignup extends WP_UnitTestCase {
    9         /**
    10          * @dataProvider data_user_name
    11          */
    12         public function test_user_name( $user_name, $error_message ) {
    13             $v = wpmu_validate_user_signup( $user_name, 'foo@example.com' );
    14             $this->assertContains( 'user_name', $v['errors']->get_error_codes(), $error_message );
    15         }
    16 
    17         public function data_user_name() {
    18             return array(
    19                 array( 'contains spaces', 'User names with spaces are not allowed.' ),
    20                 array( 'ContainsCaps', 'User names with capital letters are not allowed.' ),
    21                 array( 'contains_underscores', 'User names with underscores are not allowed.' ),
    22                 array( 'contains%^*()junk', 'User names with non-alphanumeric characters are not allowed.' ),
    23                 array( '', 'Empty user names are not allowed.' ),
    24                 array( 'foo', 'User names of 3 characters are not allowed.' ),
    25                 array( 'fo', 'User names of 2 characters are not allowed.' ),
    26                 array( 'f', 'User names of 1 characters are not allowed.' ),
    27                 array( 'f', 'User names of 1 characters are not allowed.' ),
    28                 array( '12345', 'User names consisting only of numbers are not allowed.' ),
    29                 array( 'thisusernamecontainsenoughcharacterstobelongerthan60characters', 'User names longer than 60 characters are not allowed.' ),
    30             );
    31         }
    32 
    33         public function test_should_fail_for_illegal_names() {
    34             $illegal = array( 'foo123', 'bar123' );
    35             update_site_option( 'illegal_names', $illegal );
    36 
    37             foreach ( $illegal as $i ) {
    38                 $v = wpmu_validate_user_signup( $i, 'foo@example.com' );
    39                 $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
    40             }
    41         }
    42 
    43         public function test_should_fail_for_unsafe_email_address() {
    44             add_filter( 'is_email_address_unsafe', '__return_true' );
    45             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    46             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    47             remove_filter( 'is_email_address_unsafe', '__return_true' );
    48         }
    49 
    50         public function test_should_fail_for_invalid_email_address() {
    51             add_filter( 'is_email', '__return_false' );
    52             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    53             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    54             remove_filter( 'is_email', '__return_false' );
    55         }
    56 
    57         public function test_should_fail_for_emails_from_disallowed_domains() {
    58             $domains = array( 'foo.com', 'bar.org' );
    59             update_site_option( 'limited_email_domains', $domains );
    60 
    61             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    62             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    63         }
    64 
    65         public function test_should_not_fail_for_emails_from_allowed_domains_with_mixed_case() {
    66             $domains = array( 'foo.com', 'bar.org' );
    67             update_site_option( 'limited_email_domains', $domains );
    68 
    69             $v = wpmu_validate_user_signup( 'foo123', 'foo@BAR.org' );
    70             $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
    71         }
    72 
    73         public function test_should_fail_for_existing_user_name() {
    74             $u = self::factory()->user->create( array( 'user_login' => 'foo123' ) );
    75             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 */
     7class Tests_Multisite_wpmuValidateUserSignup extends WP_UnitTestCase {
     8
     9    /**
     10     * @dataProvider data_user_name
     11     */
     12    public function test_user_name( $user_name, $error_message ) {
     13        $v = wpmu_validate_user_signup( $user_name, 'foo@example.com' );
     14        $this->assertContains( 'user_name', $v['errors']->get_error_codes(), $error_message );
     15    }
     16
     17    public function data_user_name() {
     18        return array(
     19            array( 'contains spaces', 'User names with spaces are not allowed.' ),
     20            array( 'ContainsCaps', 'User names with capital letters are not allowed.' ),
     21            array( 'contains_underscores', 'User names with underscores are not allowed.' ),
     22            array( 'contains%^*()junk', 'User names with non-alphanumeric characters are not allowed.' ),
     23            array( '', 'Empty user names are not allowed.' ),
     24            array( 'foo', 'User names of 3 characters are not allowed.' ),
     25            array( 'fo', 'User names of 2 characters are not allowed.' ),
     26            array( 'f', 'User names of 1 characters are not allowed.' ),
     27            array( 'f', 'User names of 1 characters are not allowed.' ),
     28            array( '12345', 'User names consisting only of numbers are not allowed.' ),
     29            array( 'thisusernamecontainsenoughcharacterstobelongerthan60characters', 'User names longer than 60 characters are not allowed.' ),
     30        );
     31    }
     32
     33    public function test_should_fail_for_illegal_names() {
     34        $illegal = array( 'foo123', 'bar123' );
     35        update_site_option( 'illegal_names', $illegal );
     36
     37        foreach ( $illegal as $i ) {
     38            $v = wpmu_validate_user_signup( $i, 'foo@example.com' );
    7639            $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
    7740        }
    78 
    79         public function test_should_fail_for_existing_user_email() {
    80             $u = self::factory()->user->create( array( 'user_email' => 'foo@example.com' ) );
    81             $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
    82             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    83         }
    84 
    85         public function test_should_fail_for_existing_signup_with_same_username() {
    86             // Don't send notifications.
    87             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    88             wpmu_signup_user( 'foo123', 'foo@example.com' );
    89             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    90 
    91             $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
    92             $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
    93         }
    94 
    95         public function test_should_not_fail_for_existing_signup_with_same_username_if_signup_is_old() {
    96             // Don't send notifications.
    97             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    98             wpmu_signup_user( 'foo123', 'foo@example.com' );
    99             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    100 
    101             global $wpdb;
    102             $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
    103             $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
    104 
    105             $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
    106             $this->assertNotContains( 'user_name', $v['errors']->get_error_codes() );
    107         }
    108 
    109         public function test_should_fail_for_existing_signup_with_same_email() {
    110             // Don't send notifications.
    111             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    112             wpmu_signup_user( 'foo123', 'foo@example.com' );
    113             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    114 
    115             $v = wpmu_validate_user_signup( 'foo2', 'foo@example.com' );
    116             $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
    117         }
    118 
    119         public function test_should_not_fail_for_existing_signup_with_same_email_if_signup_is_old() {
    120             // Don't send notifications.
    121             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    122             wpmu_signup_user( 'foo123', 'foo@example.com' );
    123             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    124 
    125             global $wpdb;
    126             $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
    127             $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
    128 
    129             $v = wpmu_validate_user_signup( 'foo2', 'foo2@example.com' );
    130             $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
    131         }
    132 
    133         /**
    134          * @ticket 43232
    135          */
    136         public function test_should_not_fail_for_data_used_by_a_deleted_user() {
    137             global $wpdb;
    138 
    139             // Don't send notifications.
    140             add_filter( 'wpmu_signup_user_notification', '__return_false' );
    141             add_filter( 'wpmu_welcome_user_notification', '__return_false' );
    142 
    143             // Signup, activate and delete new user.
    144             wpmu_signup_user( 'foo123', 'foo@example.com' );
    145             $key  = $wpdb->get_var( "SELECT activation_key FROM $wpdb->signups WHERE user_login = 'foo123'" );
    146             $user = wpmu_activate_signup( $key );
    147             wpmu_delete_user( $user['user_id'] );
    148 
    149             $valid = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
    150 
    151             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    152             remove_filter( 'wpmu_signup_user_notification', '__return_false' );
    153 
    154             $this->assertNotContains( 'user_name', $valid['errors']->get_error_codes() );
    155             $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
    156         }
    157 
    158         public function test_invalid_email_address_with_no_banned_domains_results_in_error() {
    159             $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
    160 
    161             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    162         }
    163 
    164         public function test_invalid_email_address_with_banned_domains_results_in_error() {
    165             update_site_option( 'banned_email_domains', 'bar.com' );
    166             $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
    167             delete_site_option( 'banned_email_domains' );
    168 
    169             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    170         }
    171 
    172         public function test_incomplete_email_address_with_no_banned_domains_results_in_error() {
    173             $valid = wpmu_validate_user_signup( 'validusername', 'incomplete@email' );
    174 
    175             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    176         }
    177 
    178         public function test_valid_email_address_matching_banned_domain_results_in_error() {
    179             update_site_option( 'banned_email_domains', 'bar.com' );
    180             $valid = wpmu_validate_user_signup( 'validusername', 'email@bar.com' );
    181             delete_site_option( 'banned_email_domains' );
    182 
    183             $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
    184         }
    185 
    186         public function test_valid_email_address_not_matching_banned_domain_returns_in_success() {
    187             update_site_option( 'banned_email_domains', 'bar.com' );
    188             $valid = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
    189             delete_site_option( 'banned_email_domains' );
    190 
    191             $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
    192         }
    193 
    194         /**
    195          * @ticket 43667
    196          */
    197         public function test_signup_nonce_check() {
    198             $original_php_self       = $_SERVER['PHP_SELF'];
    199             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    200             $_POST['signup_form_id'] = 'user-signup-form';
    201             $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
    202 
    203             $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
    204             $_SERVER['PHP_SELF'] = $original_php_self;
    205 
    206             $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    207         }
    208 
    209         /**
    210          * @ticket 43667
    211          */
    212         public function test_signup_nonce_check_invalid() {
    213             $original_php_self       = $_SERVER['PHP_SELF'];
    214             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    215             $_POST['signup_form_id'] = 'user-signup-form';
    216             $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
    217 
    218             $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
    219             $_SERVER['PHP_SELF'] = $original_php_self;
    220 
    221             $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    222         }
    223 
    224         /**
    225          * Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
    226          *
    227          * @ticket 43251
    228          *
    229          * @covers ::wp_ensure_editable_role
    230          */
    231         public function test_wp_ensure_editable_role_allows_editable_roles() {
    232             $role = get_role( 'editor' );
    233             $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
    234             $this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
    235         }
    236 
    237         /**
    238          * Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
    239          *
    240          * @ticket 43251
    241          *
    242          * @covers ::wp_ensure_editable_role
    243          */
    244         public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
    245             $this->expectException( 'WPDieException' );
    246             $role = get_role( 'non-existent-role' );
    247             $this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
    248             wp_ensure_editable_role( 'non-existent-role' );
    249         }
    250 
    251         /**
    252          * Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
    253          *
    254          * @ticket 43251
    255          *
    256          * @covers ::wp_ensure_editable_role
    257          */
    258         public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
    259             add_filter(
    260                 'editable_roles',
    261                 function ( $roles ) {
    262                     unset( $roles['editor'] );
    263                     return $roles;
    264                 }
    265             );
    266             $this->expectException( 'WPDieException' );
    267             $role = get_role( 'editor' );
    268             $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
    269             wp_ensure_editable_role( 'editor' );
    270         }
    271     }
    272 
    273 endif;
     41    }
     42
     43    public function test_should_fail_for_unsafe_email_address() {
     44        add_filter( 'is_email_address_unsafe', '__return_true' );
     45        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     46        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     47        remove_filter( 'is_email_address_unsafe', '__return_true' );
     48    }
     49
     50    public function test_should_fail_for_invalid_email_address() {
     51        add_filter( 'is_email', '__return_false' );
     52        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     53        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     54        remove_filter( 'is_email', '__return_false' );
     55    }
     56
     57    public function test_should_fail_for_emails_from_disallowed_domains() {
     58        $domains = array( 'foo.com', 'bar.org' );
     59        update_site_option( 'limited_email_domains', $domains );
     60
     61        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     62        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     63    }
     64
     65    public function test_should_not_fail_for_emails_from_allowed_domains_with_mixed_case() {
     66        $domains = array( 'foo.com', 'bar.org' );
     67        update_site_option( 'limited_email_domains', $domains );
     68
     69        $v = wpmu_validate_user_signup( 'foo123', 'foo@BAR.org' );
     70        $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
     71    }
     72
     73    public function test_should_fail_for_existing_user_name() {
     74        $u = self::factory()->user->create( array( 'user_login' => 'foo123' ) );
     75        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     76        $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
     77    }
     78
     79    public function test_should_fail_for_existing_user_email() {
     80        $u = self::factory()->user->create( array( 'user_email' => 'foo@example.com' ) );
     81        $v = wpmu_validate_user_signup( 'foo123', 'foo@example.com' );
     82        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     83    }
     84
     85    public function test_should_fail_for_existing_signup_with_same_username() {
     86        // Don't send notifications.
     87        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     88        wpmu_signup_user( 'foo123', 'foo@example.com' );
     89        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     90
     91        $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
     92        $this->assertContains( 'user_name', $v['errors']->get_error_codes() );
     93    }
     94
     95    public function test_should_not_fail_for_existing_signup_with_same_username_if_signup_is_old() {
     96        // Don't send notifications.
     97        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     98        wpmu_signup_user( 'foo123', 'foo@example.com' );
     99        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     100
     101        global $wpdb;
     102        $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
     103        $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
     104
     105        $v = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
     106        $this->assertNotContains( 'user_name', $v['errors']->get_error_codes() );
     107    }
     108
     109    public function test_should_fail_for_existing_signup_with_same_email() {
     110        // Don't send notifications.
     111        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     112        wpmu_signup_user( 'foo123', 'foo@example.com' );
     113        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     114
     115        $v = wpmu_validate_user_signup( 'foo2', 'foo@example.com' );
     116        $this->assertContains( 'user_email', $v['errors']->get_error_codes() );
     117    }
     118
     119    public function test_should_not_fail_for_existing_signup_with_same_email_if_signup_is_old() {
     120        // Don't send notifications.
     121        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     122        wpmu_signup_user( 'foo123', 'foo@example.com' );
     123        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     124
     125        global $wpdb;
     126        $date = gmdate( 'Y-m-d H:i:s', time() - ( 2 * DAY_IN_SECONDS ) - 60 );
     127        $wpdb->update( $wpdb->signups, array( 'registered' => $date ), array( 'user_login' => 'foo123' ) );
     128
     129        $v = wpmu_validate_user_signup( 'foo2', 'foo2@example.com' );
     130        $this->assertNotContains( 'user_email', $v['errors']->get_error_codes() );
     131    }
     132
     133    /**
     134     * @ticket 43232
     135     */
     136    public function test_should_not_fail_for_data_used_by_a_deleted_user() {
     137        global $wpdb;
     138
     139        // Don't send notifications.
     140        add_filter( 'wpmu_signup_user_notification', '__return_false' );
     141        add_filter( 'wpmu_welcome_user_notification', '__return_false' );
     142
     143        // Signup, activate and delete new user.
     144        wpmu_signup_user( 'foo123', 'foo@example.com' );
     145        $key  = $wpdb->get_var( "SELECT activation_key FROM $wpdb->signups WHERE user_login = 'foo123'" );
     146        $user = wpmu_activate_signup( $key );
     147        wpmu_delete_user( $user['user_id'] );
     148
     149        $valid = wpmu_validate_user_signup( 'foo123', 'foo2@example.com' );
     150
     151        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     152        remove_filter( 'wpmu_signup_user_notification', '__return_false' );
     153
     154        $this->assertNotContains( 'user_name', $valid['errors']->get_error_codes() );
     155        $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
     156    }
     157
     158    public function test_invalid_email_address_with_no_banned_domains_results_in_error() {
     159        $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
     160
     161        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     162    }
     163
     164    public function test_invalid_email_address_with_banned_domains_results_in_error() {
     165        update_site_option( 'banned_email_domains', 'bar.com' );
     166        $valid = wpmu_validate_user_signup( 'validusername', 'invalid-email' );
     167        delete_site_option( 'banned_email_domains' );
     168
     169        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     170    }
     171
     172    public function test_incomplete_email_address_with_no_banned_domains_results_in_error() {
     173        $valid = wpmu_validate_user_signup( 'validusername', 'incomplete@email' );
     174
     175        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     176    }
     177
     178    public function test_valid_email_address_matching_banned_domain_results_in_error() {
     179        update_site_option( 'banned_email_domains', 'bar.com' );
     180        $valid = wpmu_validate_user_signup( 'validusername', 'email@bar.com' );
     181        delete_site_option( 'banned_email_domains' );
     182
     183        $this->assertContains( 'user_email', $valid['errors']->get_error_codes() );
     184    }
     185
     186    public function test_valid_email_address_not_matching_banned_domain_returns_in_success() {
     187        update_site_option( 'banned_email_domains', 'bar.com' );
     188        $valid = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
     189        delete_site_option( 'banned_email_domains' );
     190
     191        $this->assertNotContains( 'user_email', $valid['errors']->get_error_codes() );
     192    }
     193
     194    /**
     195     * @ticket 43667
     196     */
     197    public function test_signup_nonce_check() {
     198        $original_php_self       = $_SERVER['PHP_SELF'];
     199        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     200        $_POST['signup_form_id'] = 'user-signup-form';
     201        $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
     202
     203        $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
     204        $_SERVER['PHP_SELF'] = $original_php_self;
     205
     206        $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     207    }
     208
     209    /**
     210     * @ticket 43667
     211     */
     212    public function test_signup_nonce_check_invalid() {
     213        $original_php_self       = $_SERVER['PHP_SELF'];
     214        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     215        $_POST['signup_form_id'] = 'user-signup-form';
     216        $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
     217
     218        $valid               = wpmu_validate_user_signup( 'validusername', 'email@example.com' );
     219        $_SERVER['PHP_SELF'] = $original_php_self;
     220
     221        $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     222    }
     223
     224    /**
     225     * Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
     226     *
     227     * @ticket 43251
     228     *
     229     * @covers ::wp_ensure_editable_role
     230     */
     231    public function test_wp_ensure_editable_role_allows_editable_roles() {
     232        $role = get_role( 'editor' );
     233        $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
     234        $this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
     235    }
     236
     237    /**
     238     * Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
     239     *
     240     * @ticket 43251
     241     *
     242     * @covers ::wp_ensure_editable_role
     243     */
     244    public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
     245        $this->expectException( 'WPDieException' );
     246        $role = get_role( 'non-existent-role' );
     247        $this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
     248        wp_ensure_editable_role( 'non-existent-role' );
     249    }
     250
     251    /**
     252     * Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
     253     *
     254     * @ticket 43251
     255     *
     256     * @covers ::wp_ensure_editable_role
     257     */
     258    public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
     259        add_filter(
     260            'editable_roles',
     261            function ( $roles ) {
     262                unset( $roles['editor'] );
     263                return $roles;
     264            }
     265        );
     266        $this->expectException( 'WPDieException' );
     267        $role = get_role( 'editor' );
     268        $this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
     269        wp_ensure_editable_role( 'editor' );
     270    }
     271}
Note: See TracChangeset for help on using the changeset viewer.