| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group user |
| 5 | * @group admin |
| 6 | */ |
| 7 | class Tests_Admin_userNew extends WP_UnitTestCase { |
| 8 | protected $reset_post; |
| 9 | protected $activation_key; |
| 10 | |
| 11 | public function setUp() { |
| 12 | parent::setUp(); |
| 13 | $this->reset_post = $_POST; |
| 14 | $_POST = array( |
| 15 | 'email' => 'foo@bar.com', |
| 16 | 'pass1' => 'foo bar pass', |
| 17 | 'pass2' => 'foo bar pass' |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | public function tearDown() { |
| 22 | parent::tearDown(); |
| 23 | $_POST = $this->reset_post; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @ticket 17904 |
| 28 | */ |
| 29 | public function test_user_new_empty_username() { |
| 30 | $_POST['user_login'] = ''; |
| 31 | |
| 32 | if ( is_multisite() ) { |
| 33 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 34 | $wp_error = $result['errors']; |
| 35 | } else { |
| 36 | $wp_error = edit_user(); |
| 37 | } |
| 38 | |
| 39 | $this->assertTrue( is_wp_error( $wp_error ) ); |
| 40 | $this->assertNotEmpty( $wp_error->errors['user_name'] ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @ticket 17904 |
| 45 | */ |
| 46 | public function test_user_new_less_than_four_chars() { |
| 47 | $_POST['user_login'] = 'foo'; |
| 48 | |
| 49 | if ( is_multisite() ) { |
| 50 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 51 | $wp_error = $result['errors']; |
| 52 | } else { |
| 53 | $wp_error = edit_user(); |
| 54 | } |
| 55 | |
| 56 | $this->assertTrue( is_wp_error( $wp_error ) ); |
| 57 | $this->assertNotEmpty( $wp_error->errors['user_name'] ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @ticket 17904 |
| 62 | */ |
| 63 | public function test_user_new_space_in_username() { |
| 64 | $_POST['user_login'] = 'foo bar'; |
| 65 | |
| 66 | if ( is_multisite() ) { |
| 67 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 68 | $wp_error = $result['errors']; |
| 69 | } else { |
| 70 | $wp_error = edit_user(); |
| 71 | } |
| 72 | |
| 73 | $this->assertTrue( is_wp_error( $wp_error ) ); |
| 74 | $this->assertNotEmpty( $wp_error->errors['user_name'] ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @ticket 17904 |
| 79 | */ |
| 80 | public function test_user_new_illegal_names() { |
| 81 | $_POST['user_login'] = 'root'; |
| 82 | |
| 83 | if ( is_multisite() ) { |
| 84 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 85 | $wp_error = $result['errors']; |
| 86 | } else { |
| 87 | $wp_error = edit_user(); |
| 88 | } |
| 89 | |
| 90 | $this->assertTrue( is_wp_error( $wp_error ) ); |
| 91 | $this->assertNotEmpty( $wp_error->errors['user_name'] ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @ticket 17904 |
| 96 | */ |
| 97 | public function test_user_new_no_letters() { |
| 98 | $_POST['user_login'] = '12345'; |
| 99 | |
| 100 | if ( is_multisite() ) { |
| 101 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 102 | $wp_error = $result['errors']; |
| 103 | } else { |
| 104 | $wp_error = edit_user(); |
| 105 | } |
| 106 | |
| 107 | $this->assertTrue( is_wp_error( $wp_error ) ); |
| 108 | $this->assertNotEmpty( $wp_error->errors['user_name'] ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @ticket 17904 |
| 113 | */ |
| 114 | public function test_user_new_username_exists() { |
| 115 | $_POST['user_login'] = 'foobar'; |
| 116 | |
| 117 | $this->factory->user->create( array( 'user_login' => $_POST['user_login'] ) ); |
| 118 | |
| 119 | if ( is_multisite() ) { |
| 120 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 121 | $wp_error = $result['errors']; |
| 122 | } else { |
| 123 | $wp_error = edit_user(); |
| 124 | } |
| 125 | |
| 126 | $this->assertTrue( is_wp_error( $wp_error ) ); |
| 127 | $this->assertNotEmpty( $wp_error->errors['user_name'] ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @ticket 17904 |
| 132 | */ |
| 133 | public function test_user_new_username_ok() { |
| 134 | $_POST['user_login'] = 'foobar'; |
| 135 | |
| 136 | if ( is_multisite() ) { |
| 137 | $result = wpmu_validate_user_signup( $_POST['user_login'], $_POST['email'] ); |
| 138 | |
| 139 | add_filter( 'wpmu_signup_user_notification', array( $this, 'set_activation_key' ), 10, 3 ); |
| 140 | wpmu_signup_user( $result['user_name'], $result['user_email'] ); |
| 141 | remove_filter( 'wpmu_signup_user_notification', array( $this, 'set_activation_key' ), 10, 3 ); |
| 142 | |
| 143 | $user_data = wpmu_activate_signup( $this->activation_key ); |
| 144 | $user_id = $user_data['user_id']; |
| 145 | } else { |
| 146 | $user_id = edit_user(); |
| 147 | } |
| 148 | |
| 149 | $this->assertFalse( is_wp_error( $user_id ) ); |
| 150 | $this->assertNotEmpty( $user_id ); |
| 151 | } |
| 152 | |
| 153 | public function set_activation_key( $user, $user_email, $key ) { |
| 154 | $this->activation_key = $key; |
| 155 | return false; |
| 156 | } |
| 157 | } |