Make WordPress Core

Ticket #41672: 41672-with-409-and-unit-tests.diff

File 41672-with-409-and-unit-tests.diff, 4.0 KB (added by shooper, 6 years ago)

Adds HTTP 409 response code, and unit tests

  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

     
    501501                        $user_id = wp_insert_user( wp_slash( (array) $user ) );
    502502
    503503                        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
    504509                                return $user_id;
    505510                        }
    506511                }
  • src/wp-includes/user.php

     
    14821482                return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) );
    14831483        }
    14841484
     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
    14851496        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                }
    14871502        }
    14881503
    14891504        /**
     
    15371552         */
    15381553        $user_url = apply_filters( 'pre_user_url', $raw_user_url );
    15391554
    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.3
    1546          *
    1547          * @param string $raw_user_email The user's email.
    1548          */
    1549         $user_email = apply_filters( 'pre_user_email', $raw_user_email );
    1550 
    15511555        /*
    15521556         * If there is no update, just check for `email_exists`. If there is an update,
    15531557         * 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

     
    11601160                $this->check_add_edit_user_response( $response );
    11611161        }
    11621162
     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
    11631203        public function test_create_user_without_permission() {
    11641204                wp_set_current_user( self::$editor );
    11651205