Ticket #41672: 41672-with-409-and-unit-tests.diff
File 41672-with-409-and-unit-tests.diff, 4.0 KB (added by , 6 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
501 501 $user_id = wp_insert_user( wp_slash( (array) $user ) ); 502 502 503 503 if ( is_wp_error( $user_id ) ) { 504 505 if ( in_array( $user_id->get_error_code(), array( 'existing_user_email', 'existing_user_login' ) ) ) { 506 $user_id->add_data( array( 'status' => 409 ) ); 507 } 508 504 509 return $user_id; 505 510 } 506 511 } -
src/wp-includes/user.php
1482 1482 return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) ); 1483 1483 } 1484 1484 1485 $raw_user_email = empty( $userdata['user_email'] ) ? '' : $userdata['user_email']; 1486 1487 /** 1488 * Filters a user's email before the user is created or updated. 1489 * 1490 * @since 2.0.3 1491 * 1492 * @param string $raw_user_email The user's email. 1493 */ 1494 $user_email = apply_filters( 'pre_user_email', $raw_user_email ); 1495 1485 1496 if ( ! $update && username_exists( $user_login ) ) { 1486 return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) ); 1497 if ( email_exists( $user_email ) ) { 1498 return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); 1499 } else { 1500 return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) ); 1501 } 1487 1502 } 1488 1503 1489 1504 /** … … 1537 1552 */ 1538 1553 $user_url = apply_filters( 'pre_user_url', $raw_user_url ); 1539 1554 1540 $raw_user_email = empty( $userdata['user_email'] ) ? '' : $userdata['user_email'];1541 1542 /**1543 * Filters a user's email before the user is created or updated.1544 *1545 * @since 2.0.31546 *1547 * @param string $raw_user_email The user's email.1548 */1549 $user_email = apply_filters( 'pre_user_email', $raw_user_email );1550 1551 1555 /* 1552 1556 * If there is no update, just check for `email_exists`. If there is an update, 1553 1557 * check if current email and new email are the same, or not, and check `email_exists` -
tests/phpunit/tests/rest-api/rest-users-controller.php
1160 1160 $this->check_add_edit_user_response( $response ); 1161 1161 } 1162 1162 1163 /*** 1164 * @ticket 41672 1165 */ 1166 public function test_json_create_user_with_existing_username_or_email() { 1167 $this->allow_user_to_manage_multisite(); 1168 wp_set_current_user( self::$user ); 1169 1170 $params = array( 1171 'username' => 'testjsonuser', 1172 'password' => 'testjsonpassword', 1173 'email' => 'testjson@example.com', 1174 ); 1175 1176 $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); 1177 $request->add_header( 'content-type', 'application/json' ); 1178 $request->set_body( wp_json_encode( $params ) ); 1179 1180 $response = $this->server->dispatch( $request ); 1181 $this->check_add_edit_user_response( $response ); 1182 1183 // Make request again, expecting existing_user_email response 1184 $response = $this->server->dispatch( $request ); 1185 $this->assertErrorResponse( 'existing_user_email', $response, 409 ); 1186 1187 // Make request again, expecting existing_user_login response 1188 $params = array( 1189 'username' => 'testjsonuser', 1190 'password' => 'testjsonpassword', 1191 'email' => 'testjson1@example.com', 1192 ); 1193 1194 $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); 1195 $request->add_header( 'content-type', 'application/json' ); 1196 $request->set_body( wp_json_encode( $params ) ); 1197 1198 $response = $this->server->dispatch( $request ); 1199 $this->assertErrorResponse( 'existing_user_login', $response, 409 ); 1200 1201 } 1202 1163 1203 public function test_create_user_without_permission() { 1164 1204 wp_set_current_user( self::$editor ); 1165 1205