| | 1 | <?php |
| | 2 | |
| | 3 | if ( is_multisite() ) : |
| | 4 | |
| | 5 | /** |
| | 6 | * @group multisite |
| | 7 | */ |
| | 8 | class Tests_Multisite_WpmuValidateBlogSignup extends WP_UnitTestCase { |
| | 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 | public static function wpSetUpBeforeClass( $factory ) { |
| | 18 | self::$super_admin_id = $factory->user->create(); |
| | 19 | grant_super_admin( self::$super_admin_id ); |
| | 20 | |
| | 21 | self::$existing_user_id = $factory->user->create( array( 'user_login' => self::$existing_user_login ) ); |
| | 22 | |
| | 23 | $network = get_network(); |
| | 24 | |
| | 25 | if ( is_subdomain_install() ) { |
| | 26 | $domain = self::$existing_blog_name . '.' . preg_replace( '|^www\.|', '', $network->domain ); |
| | 27 | $path = $network->path; |
| | 28 | } else { |
| | 29 | $domain = $network->domain; |
| | 30 | $path = $network->path . self::$existing_blog_name . '/'; |
| | 31 | } |
| | 32 | |
| | 33 | self::$existing_blog_id = $factory->blog->create( array( |
| | 34 | 'domain' => $domain, |
| | 35 | 'path' => $path, |
| | 36 | 'site_id' => $network->id, |
| | 37 | ) ); |
| | 38 | } |
| | 39 | |
| | 40 | public static function wpTearDownAfterClass() { |
| | 41 | revoke_super_admin( self::$super_admin_id ); |
| | 42 | wpmu_delete_user( self::$super_admin_id ); |
| | 43 | |
| | 44 | wpmu_delete_user( self::$existing_user_id ); |
| | 45 | |
| | 46 | wpmu_delete_blog( self::$existing_blog_id, true ); |
| | 47 | } |
| | 48 | |
| | 49 | /** |
| | 50 | * @dataProvider data_validate_blogname |
| | 51 | */ |
| | 52 | public function test_validate_blogname( $blog_name, $error_message ) { |
| | 53 | $result = wpmu_validate_blog_signup( $blog_name, 'Foo Site Title', get_userdata( self::$super_admin_id ) ); |
| | 54 | $this->assertContains( 'blogname', $result['errors']->get_error_codes(), $error_message ); |
| | 55 | } |
| | 56 | |
| | 57 | public function data_validate_blogname() { |
| | 58 | $data = array( |
| | 59 | array( '', 'Site names must not be empty.' ), |
| | 60 | array( 'foo-hello', 'Site names must not contain hyphens.' ), |
| | 61 | array( 'foo_hello', 'Site names must not contain underscores.' ), |
| | 62 | array( 'foo hello', 'Site names must not contain spaces.' ), |
| | 63 | array( 'FooHello', 'Site names must not contain uppercase letters.' ), |
| | 64 | array( '12345678', 'Site names must not consist of numbers only.' ), |
| | 65 | array( self::$existing_blog_name, 'Site names must not collide with an existing site name.' ), |
| | 66 | array( self::$existing_user_login, 'Site names must not collide with an existing user login.' ), |
| | 67 | ); |
| | 68 | |
| | 69 | if ( ! is_super_admin() ) { |
| | 70 | $data[] = array( 'foo', 'Site names must at least contain 4 characters.' ); |
| | 71 | } |
| | 72 | |
| | 73 | $illegal_names = get_site_option( 'illegal_names' ); |
| | 74 | if ( ! empty( $illegal_names ) ) { |
| | 75 | $data[] = array( array_shift( $illegal_names ), 'Illegal site names are not allowed.' ); |
| | 76 | } else { |
| | 77 | $data[] = array( 'www', 'Illegal site names are not allowed.' ); |
| | 78 | } |
| | 79 | |
| | 80 | return $data; |
| | 81 | } |
| | 82 | |
| | 83 | public function test_validate_empty_blog_title() { |
| | 84 | $result = wpmu_validate_blog_signup( 'uniqueblogname1234', '', get_userdata( self::$super_admin_id ) ); |
| | 85 | $this->assertContains( 'blog_title', $result['errors']->get_error_codes(), 'Site titles must not be empty.' ); |
| | 86 | } |
| | 87 | |
| | 88 | public function test_validate_blogname_from_same_existing_user() { |
| | 89 | $result = wpmu_validate_blog_signup( self::$existing_user_login, 'Foo Site Title', get_userdata( self::$existing_user_id ) ); |
| | 90 | $this->assertEmpty( $result['errors']->get_error_codes() ); |
| | 91 | } |
| | 92 | } |
| | 93 | |
| | 94 | endif; |