| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group user |
| | 5 | */ |
| | 6 | class Tests_User_WpValidateUserLogin extends WP_UnitTestCase { |
| | 7 | |
| | 8 | /** |
| | 9 | * A function to filter add an illegal user login. |
| | 10 | * |
| | 11 | * @param array $illegal_logins |
| | 12 | * @return array |
| | 13 | */ |
| | 14 | public function illegal_user_logins( $illegal_logins ) { |
| | 15 | $illegal_logins[] = 'adm1n'; |
| | 16 | return $illegal_logins; |
| | 17 | } |
| | 18 | |
| | 19 | /** |
| | 20 | * A function to filter and invalidate a login. |
| | 21 | * |
| | 22 | * @param array $illegal_logins |
| | 23 | * @return array |
| | 24 | */ |
| | 25 | public function invalidate_login( $error, $user_login ) { |
| | 26 | $error->add( 'invalid_username', 'Invalid username.' ); |
| | 27 | } |
| | 28 | |
| | 29 | public function test_user_login_is_valid() { |
| | 30 | $this->assertTrue( wp_validate_user_login( 'jonsnow' ) ); |
| | 31 | } |
| | 32 | |
| | 33 | public function test_user_login_can_be_less_than_three_characters() { |
| | 34 | $this->assertTrue( wp_validate_user_login( 'tom' ) ); |
| | 35 | } |
| | 36 | |
| | 37 | public function test_user_login_cannot_be_more_than_sixty_characters() { |
| | 38 | $valid = wp_validate_user_login( 'john-jacob-jingleheimer-david-marcus-castro-manuel-andrew-schmidt' ); |
| | 39 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 40 | } |
| | 41 | |
| | 42 | public function allowed_usernames_with_special_characters() { |
| | 43 | return array( |
| | 44 | array( 'arya.stark' ), |
| | 45 | array( 'arya_stark' ), |
| | 46 | array( 'arya-stark' ), |
| | 47 | array( 'arya.stark@hotmail.com' ), |
| | 48 | ); |
| | 49 | } |
| | 50 | |
| | 51 | /** |
| | 52 | * @dataProvider allowed_usernames_with_special_characters |
| | 53 | */ |
| | 54 | public function test_user_login_can_contain_some_special_characters( $user_login ) { |
| | 55 | $this->assertTrue( wp_validate_user_login( $user_login ) ); |
| | 56 | } |
| | 57 | |
| | 58 | public function test_user_login_cannot_contain_illegal_characters() { |
| | 59 | $valid = wp_validate_user_login( 'cer$ei' ); |
| | 60 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 61 | } |
| | 62 | |
| | 63 | public function illegal_logins_in_multisite() { |
| | 64 | return array( |
| | 65 | array( 'www' ), |
| | 66 | array( 'web' ), |
| | 67 | array( 'root' ), |
| | 68 | // array( 'admin' ), |
| | 69 | array( 'main' ), |
| | 70 | array( 'invite' ), |
| | 71 | array( 'administrator' ), |
| | 72 | ); |
| | 73 | } |
| | 74 | |
| | 75 | /** |
| | 76 | * @dataProvider illegal_logins_in_multisite |
| | 77 | */ |
| | 78 | public function test_user_login_cannot_contain_illegal_login_in_multisite( $user_login ) { |
| | 79 | $valid = wp_validate_user_login( $user_login ); |
| | 80 | if ( is_multisite() ) { |
| | 81 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 82 | } else { |
| | 83 | $this->assertTrue( $valid ); |
| | 84 | } |
| | 85 | } |
| | 86 | |
| | 87 | public function test_user_login_must_contain_letter_in_multisite() { |
| | 88 | $valid = wp_validate_user_login( '.' ); |
| | 89 | if ( is_multisite() ) { |
| | 90 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 91 | } else { |
| | 92 | $this->assertTrue( $valid ); |
| | 93 | } |
| | 94 | } |
| | 95 | |
| | 96 | public function test_illegal_login_filter_should_invalidate_user_logins() { |
| | 97 | add_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) ); |
| | 98 | $valid = wp_validate_user_login( 'adm1n' ); |
| | 99 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 100 | remove_filter( 'illegal_user_logins', array( $this, 'illegal_user_logins' ) ); |
| | 101 | } |
| | 102 | |
| | 103 | public function test_validate_username_filter_can_invalidate_user_login() { |
| | 104 | add_filter( 'validate_username', '__return_false' ); |
| | 105 | $valid = wp_validate_user_login( 'robert' ); |
| | 106 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 107 | remove_filter( 'validate_username', '__return_false' ); |
| | 108 | } |
| | 109 | |
| | 110 | public function test_additional_validation_can_invalidate() { |
| | 111 | add_action( 'wp_validate_user_login', array( $this, 'invalidate_login' ), 10, 2 ); |
| | 112 | $valid = wp_validate_user_login( 'glerf' ); |
| | 113 | $this->assertInstanceOf( 'WP_Error', $valid ); |
| | 114 | remove_filter( 'wp_validate_user_login', array( $this, 'invalidate_login' ), 10, 2 ); |
| | 115 | } |
| | 116 | |
| | 117 | } |