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/wpmuValidateBlogSignup.php

    r51860 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 */
     7class Tests_Multisite_wpmuValidateBlogSignup extends WP_UnitTestCase {
     8
     9    protected static $super_admin_id;
     10
     11    protected static $existing_user_login = 'existinguserfoo';
     12    protected static $existing_user_id;
     13
     14    protected static $existing_blog_name = 'existingsitefoo';
     15    protected static $existing_blog_id;
     16
     17    protected $minimum_site_name_length = 4;
     18
     19    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     20        self::$super_admin_id = $factory->user->create();
     21        grant_super_admin( self::$super_admin_id );
     22
     23        self::$existing_user_id = $factory->user->create( array( 'user_login' => self::$existing_user_login ) );
     24
     25        $network = get_network();
     26
     27        if ( is_subdomain_install() ) {
     28            $domain = self::$existing_blog_name . '.' . preg_replace( '|^www\.|', '', $network->domain );
     29            $path   = $network->path;
     30        } else {
     31            $domain = $network->domain;
     32            $path   = $network->path . self::$existing_blog_name . '/';
     33        }
     34
     35        self::$existing_blog_id = $factory->blog->create(
     36            array(
     37                'domain'     => $domain,
     38                'path'       => $path,
     39                'network_id' => $network->id,
     40            )
     41        );
     42    }
     43
     44    public static function wpTearDownAfterClass() {
     45        revoke_super_admin( self::$super_admin_id );
     46        wpmu_delete_user( self::$super_admin_id );
     47
     48        wpmu_delete_user( self::$existing_user_id );
     49
     50        wp_delete_site( self::$existing_blog_id );
     51    }
    452
    553    /**
    6      * @group multisite
     54     * @dataProvider data_validate_blogname
    755     */
    8     class Tests_Multisite_wpmuValidateBlogSignup extends WP_UnitTestCase {
    9         protected static $super_admin_id;
     56    public function test_validate_blogname( $blog_name, $error_message ) {
     57        $result = wpmu_validate_blog_signup( $blog_name, 'Foo Site Title', get_userdata( self::$super_admin_id ) );
     58        $this->assertContains( 'blogname', $result['errors']->get_error_codes(), $error_message );
     59    }
    1060
    11         protected static $existing_user_login = 'existinguserfoo';
    12         protected static $existing_user_id;
     61    public function data_validate_blogname() {
     62        $data = array(
     63            array( '', 'Site names must not be empty.' ),
     64            array( 'foo-hello', 'Site names must not contain hyphens.' ),
     65            array( 'foo_hello', 'Site names must not contain underscores.' ),
     66            array( 'foo hello', 'Site names must not contain spaces.' ),
     67            array( 'FooHello', 'Site names must not contain uppercase letters.' ),
     68            array( '12345678', 'Site names must not consist of numbers only.' ),
     69            array( self::$existing_blog_name, 'Site names must not collide with an existing site name.' ),
     70            array( self::$existing_user_login, 'Site names must not collide with an existing user login.' ),
     71            array( 'foo', 'Site names must at least contain 4 characters.' ),
     72        );
    1373
    14         protected static $existing_blog_name = 'existingsitefoo';
    15         protected static $existing_blog_id;
    16 
    17         protected $minimum_site_name_length = 4;
    18 
    19         public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    20             self::$super_admin_id = $factory->user->create();
    21             grant_super_admin( self::$super_admin_id );
    22 
    23             self::$existing_user_id = $factory->user->create( array( 'user_login' => self::$existing_user_login ) );
    24 
    25             $network = get_network();
    26 
    27             if ( is_subdomain_install() ) {
    28                 $domain = self::$existing_blog_name . '.' . preg_replace( '|^www\.|', '', $network->domain );
    29                 $path   = $network->path;
    30             } else {
    31                 $domain = $network->domain;
    32                 $path   = $network->path . self::$existing_blog_name . '/';
    33             }
    34 
    35             self::$existing_blog_id = $factory->blog->create(
    36                 array(
    37                     'domain'     => $domain,
    38                     'path'       => $path,
    39                     'network_id' => $network->id,
    40                 )
    41             );
     74        $illegal_names = get_site_option( 'illegal_names' );
     75        if ( ! empty( $illegal_names ) ) {
     76            $data[] = array( array_shift( $illegal_names ), 'Illegal site names are not allowed.' );
     77        } else {
     78            $data[] = array( 'www', 'Illegal site names are not allowed.' );
    4279        }
    4380
    44         public static function wpTearDownAfterClass() {
    45             revoke_super_admin( self::$super_admin_id );
    46             wpmu_delete_user( self::$super_admin_id );
     81        return $data;
     82    }
    4783
    48             wpmu_delete_user( self::$existing_user_id );
     84    public function test_validate_empty_blog_title() {
     85        $result = wpmu_validate_blog_signup( 'uniqueblogname1234', '', get_userdata( self::$super_admin_id ) );
     86        $this->assertContains( 'blog_title', $result['errors']->get_error_codes(), 'Site titles must not be empty.' );
     87    }
    4988
    50             wp_delete_site( self::$existing_blog_id );
    51         }
     89    public function test_validate_blogname_from_same_existing_user() {
     90        $result = wpmu_validate_blog_signup( self::$existing_user_login, 'Foo Site Title', get_userdata( self::$existing_user_id ) );
     91        $this->assertEmpty( $result['errors']->get_error_codes() );
     92    }
    5293
    53         /**
    54          * @dataProvider data_validate_blogname
    55          */
    56         public function test_validate_blogname( $blog_name, $error_message ) {
    57             $result = wpmu_validate_blog_signup( $blog_name, 'Foo Site Title', get_userdata( self::$super_admin_id ) );
    58             $this->assertContains( 'blogname', $result['errors']->get_error_codes(), $error_message );
    59         }
     94    /**
     95     * @ticket 39676
     96     *
     97     * @dataProvider data_filter_minimum_site_name_length
     98     */
     99    public function test_filter_minimum_site_name_length( $site_name, $minimum_length, $expect_error ) {
     100        $this->minimum_site_name_length = $minimum_length;
     101        add_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
    60102
    61         public function data_validate_blogname() {
    62             $data = array(
    63                 array( '', 'Site names must not be empty.' ),
    64                 array( 'foo-hello', 'Site names must not contain hyphens.' ),
    65                 array( 'foo_hello', 'Site names must not contain underscores.' ),
    66                 array( 'foo hello', 'Site names must not contain spaces.' ),
    67                 array( 'FooHello', 'Site names must not contain uppercase letters.' ),
    68                 array( '12345678', 'Site names must not consist of numbers only.' ),
    69                 array( self::$existing_blog_name, 'Site names must not collide with an existing site name.' ),
    70                 array( self::$existing_user_login, 'Site names must not collide with an existing user login.' ),
    71                 array( 'foo', 'Site names must at least contain 4 characters.' ),
    72             );
     103        $result = wpmu_validate_blog_signup( $site_name, 'Site Title', get_userdata( self::$super_admin_id ) );
    73104
    74             $illegal_names = get_site_option( 'illegal_names' );
    75             if ( ! empty( $illegal_names ) ) {
    76                 $data[] = array( array_shift( $illegal_names ), 'Illegal site names are not allowed.' );
    77             } else {
    78                 $data[] = array( 'www', 'Illegal site names are not allowed.' );
    79             }
     105        remove_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
     106        $this->minimum_site_name_length = 4;
    80107
    81             return $data;
    82         }
    83 
    84         public function test_validate_empty_blog_title() {
    85             $result = wpmu_validate_blog_signup( 'uniqueblogname1234', '', get_userdata( self::$super_admin_id ) );
    86             $this->assertContains( 'blog_title', $result['errors']->get_error_codes(), 'Site titles must not be empty.' );
    87         }
    88 
    89         public function test_validate_blogname_from_same_existing_user() {
    90             $result = wpmu_validate_blog_signup( self::$existing_user_login, 'Foo Site Title', get_userdata( self::$existing_user_id ) );
     108        if ( $expect_error ) {
     109            $this->assertContains( 'blogname', $result['errors']->get_error_codes() );
     110        } else {
    91111            $this->assertEmpty( $result['errors']->get_error_codes() );
    92         }
    93 
    94         /**
    95          * @ticket 39676
    96          *
    97          * @dataProvider data_filter_minimum_site_name_length
    98          */
    99         public function test_filter_minimum_site_name_length( $site_name, $minimum_length, $expect_error ) {
    100             $this->minimum_site_name_length = $minimum_length;
    101             add_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
    102 
    103             $result = wpmu_validate_blog_signup( $site_name, 'Site Title', get_userdata( self::$super_admin_id ) );
    104 
    105             remove_filter( 'minimum_site_name_length', array( $this, 'filter_minimum_site_name_length' ) );
    106             $this->minimum_site_name_length = 4;
    107 
    108             if ( $expect_error ) {
    109                 $this->assertContains( 'blogname', $result['errors']->get_error_codes() );
    110             } else {
    111                 $this->assertEmpty( $result['errors']->get_error_codes() );
    112             }
    113         }
    114 
    115         public function data_filter_minimum_site_name_length() {
    116             return array(
    117                 array( 'fooo', 5, true ),
    118                 array( 'foooo', 5, false ),
    119                 array( 'foo', 4, true ),
    120                 array( 'fooo', 4, false ),
    121                 array( 'fo', 3, true ),
    122                 array( 'foo', 3, false ),
    123             );
    124         }
    125 
    126         public function filter_minimum_site_name_length() {
    127             return $this->minimum_site_name_length;
    128         }
    129 
    130         /**
    131          * @ticket 43667
    132          */
    133         public function test_signup_nonce_check() {
    134             $original_php_self       = $_SERVER['PHP_SELF'];
    135             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    136             $_POST['signup_form_id'] = 'blog-signup-form';
    137             $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
    138 
    139             $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
    140             $_SERVER['PHP_SELF'] = $original_php_self;
    141 
    142             $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    143         }
    144 
    145         /**
    146          * @ticket 43667
    147          */
    148         public function test_signup_nonce_check_invalid() {
    149             $original_php_self       = $_SERVER['PHP_SELF'];
    150             $_SERVER['PHP_SELF']     = '/wp-signup.php';
    151             $_POST['signup_form_id'] = 'blog-signup-form';
    152             $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
    153 
    154             $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
    155             $_SERVER['PHP_SELF'] = $original_php_self;
    156 
    157             $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
    158112        }
    159113    }
    160114
    161 endif;
     115    public function data_filter_minimum_site_name_length() {
     116        return array(
     117            array( 'fooo', 5, true ),
     118            array( 'foooo', 5, false ),
     119            array( 'foo', 4, true ),
     120            array( 'fooo', 4, false ),
     121            array( 'fo', 3, true ),
     122            array( 'foo', 3, false ),
     123        );
     124    }
     125
     126    public function filter_minimum_site_name_length() {
     127        return $this->minimum_site_name_length;
     128    }
     129
     130    /**
     131     * @ticket 43667
     132     */
     133    public function test_signup_nonce_check() {
     134        $original_php_self       = $_SERVER['PHP_SELF'];
     135        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     136        $_POST['signup_form_id'] = 'blog-signup-form';
     137        $_POST['_signup_form']   = wp_create_nonce( 'signup_form_' . $_POST['signup_form_id'] );
     138
     139        $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
     140        $_SERVER['PHP_SELF'] = $original_php_self;
     141
     142        $this->assertNotContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     143    }
     144
     145    /**
     146     * @ticket 43667
     147     */
     148    public function test_signup_nonce_check_invalid() {
     149        $original_php_self       = $_SERVER['PHP_SELF'];
     150        $_SERVER['PHP_SELF']     = '/wp-signup.php';
     151        $_POST['signup_form_id'] = 'blog-signup-form';
     152        $_POST['_signup_form']   = wp_create_nonce( 'invalid' );
     153
     154        $valid               = wpmu_validate_blog_signup( 'my-nonce-site', 'Site Title', get_userdata( self::$super_admin_id ) );
     155        $_SERVER['PHP_SELF'] = $original_php_self;
     156
     157        $this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
     158    }
     159}
Note: See TracChangeset for help on using the changeset viewer.