Ticket #41672: 41672.2.diff
File 41672.2.diff, 3.6 KB (added by , 4 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 58071fde6e..8f3dad462c 100644
a b class WP_REST_Users_Controller extends WP_REST_Controller { 556 556 } 557 557 return $error; 558 558 } 559 } 560 561 if ( is_multisite() ) { 562 $user_id = wpmu_create_user( $user->user_login, $user->user_pass, $user->user_email ); 559 $user_id = wpmu_create_user($user->user_login, $user->user_pass, $user->user_email); 563 560 564 if ( ! $user_id) {561 if (!$user_id) { 565 562 return new WP_Error( 566 563 'rest_user_create', 567 __( 'Error creating new user.'),568 array( 'status' => 500)564 __('Error creating new user.'), 565 array('status' => 500) 569 566 ); 570 567 } 571 568 572 569 $user->ID = $user_id; 573 $user_id = wp_update_user( wp_slash( (array) $user ) ); 570 $user_id = wp_update_user(wp_slash((array)$user)); 571 } else { 572 $user_id = wp_insert_user( wp_slash( (array) $user ) ); 573 } 574 574 575 if ( is_wp_error( $user_id ) ) { 576 return $user_id; 575 if ( is_wp_error( $user_id ) ) { 576 if ( in_array( $user_id->get_error_code(), array( 'existing_user_email', 'existing_user_login' ) ) ) { 577 $user_id->add_data( array( 'status' => 409 ) ); 577 578 } 579 return $user_id; 580 } 578 581 582 if ( is_multisite() ) { 579 583 $result = add_user_to_blog( get_site()->id, $user_id, '' ); 580 584 if ( is_wp_error( $result ) ) { 581 585 return $result; 582 586 } 583 } else {584 $user_id = wp_insert_user( wp_slash( (array) $user ) );585 586 if ( is_wp_error( $user_id ) ) {587 return $user_id;588 }589 587 } 590 588 591 589 $user = get_user_by( 'id', $user_id ); -
tests/phpunit/tests/rest-api/rest-users-controller.php
diff --git a/tests/phpunit/tests/rest-api/rest-users-controller.php b/tests/phpunit/tests/rest-api/rest-users-controller.php index 7897d10d1d..3472acbabd 100644
a b class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 1490 1490 $this->assertErrorResponse( 'rest_user_invalid_role', $response, 400 ); 1491 1491 } 1492 1492 1493 /*** 1494 * @ticket 41672 1495 */ 1496 public function test_create_user_with_existing_username_or_email() { 1497 $this->allow_user_to_manage_multisite(); 1498 wp_set_current_user( self::$user ); 1499 $params = array( 1500 'username' => 'testjsonuser', 1501 'password' => 'testjsonpassword', 1502 'email' => 'testjson@example.com', 1503 ); 1504 $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); 1505 $request->add_header( 'content-type', 'application/json' ); 1506 $request->set_body( wp_json_encode( $params ) ); 1507 $response = $this->server->dispatch( $request ); 1508 $this->check_add_edit_user_response( $response ); 1509 // Make request again, expecting existing_user_email response 1510 $response = $this->server->dispatch( $request ); 1511 $this->assertErrorResponse( 'existing_user_email', $response, 409 ); 1512 // Make request again, expecting existing_user_login response 1513 $params = array( 1514 'username' => 'testjsonuser', 1515 'password' => 'testjsonpassword', 1516 'email' => 'testjson1@example.com', 1517 ); 1518 1519 $request = new WP_REST_Request( 'POST', '/wp/v2/users' ); 1520 $request->add_header( 'content-type', 'application/json' ); 1521 $request->set_body( wp_json_encode( $params ) ); 1522 1523 $response = $this->server->dispatch( $request ); 1524 $this->assertErrorResponse( 'existing_user_login', $response, 409 ); 1525 1526 } 1527 1493 1528 public function test_update_item() { 1494 1529 $user_id = $this->factory->user->create( 1495 1530 array(