Make WordPress Core

Ticket #40556: 40556.patch

File 40556.patch, 3.9 KB (added by TimothyBlynJacobs, 6 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 63fb4e9..adb7db5 100644
    a b class WP_REST_Users_Controller extends WP_REST_Controller { 
    471471                }
    472472
    473473                $user = $this->prepare_item_for_database( $request );
     474               
     475                if ( ! isset( $user->user_pass ) ) {
     476                        $user->user_pass = wp_generate_password( 24 );
     477                }
    474478
    475479                if ( is_multisite() ) {
    476480                        $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    12501254                                        'description' => __( 'Password for the user (never included).' ),
    12511255                                        'type'        => 'string',
    12521256                                        'context'     => array(), // Password is never displayed.
    1253                                         'required'    => true,
    12541257                                        'arg_options' => array(
    12551258                                                'sanitize_callback' => array( $this, 'check_user_password' ),
    12561259                                        ),
  • 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 062a90e..a41c475 100644
    a b class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 
    909909                $this->assertEquals( array( 'editor' ), $data['roles'] );
    910910                $this->check_add_edit_user_response( $response );
    911911        }
     912       
     913        public function test_create_item_generates_password_if_omitted() {
     914               
     915                $this->allow_user_to_manage_multisite();
     916                wp_set_current_user( self::$user );
     917
     918                $params = array(
     919                        'username'    => 'testuser',
     920                        'email'       => 'test@example.com',
     921                        'name'        => 'Test User',
     922                        'nickname'    => 'testuser',
     923                        'slug'        => 'test-user',
     924                );
     925
     926                $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     927                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     928                $request->set_body_params( $params );
     929
     930                $response = $this->server->dispatch( $request );
     931                $data = $response->get_data();
     932               
     933                $user = get_userdata( $data['id'] );
     934                $this->assertNotEmpty( $user );
     935                $this->assertNotEmpty( $user->user_pass );
     936                $this->check_add_edit_user_response( $response );
     937        }
    912938
     939        public function test_create_item_empty_password() {
     940               
     941                $this->allow_user_to_manage_multisite();
     942                wp_set_current_user( self::$user );
     943
     944                $params = array(
     945                        'username'    => 'testuser',
     946                        'password'        => '',
     947                        'email'       => 'test@example.com',
     948                        'name'        => 'Test User',
     949                        'nickname'    => 'testuser',
     950                        'slug'        => 'test-user',
     951                );
     952
     953                $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
     954                $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
     955                $request->set_body_params( $params );
     956
     957                $response = $this->server->dispatch( $request );
     958                $this->assertErrorResponse( 'rest_invalid_param', $response );
     959                $invalid = $response->data['data']['params'];
     960
     961                $this->assertArrayHasKey( 'password', $invalid );
     962                $this->assertEquals( 'Passwords cannot be empty.', $invalid['password'] );
     963        }
     964       
    913965        public function test_create_item_invalid_username() {
    914966                $this->allow_user_to_manage_multisite();
    915967                wp_set_current_user( self::$user );
  • tests/qunit/fixtures/wp-api-generated.js

    diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js
    index 0df5b9e..6ba138b 100644
    a b mockedApiResponse.Schema = { 
    25312531                            }
    25322532                        },
    25332533                        "password": {
    2534                             "required": true,
     2534                            "required": false,
    25352535                            "description": "Password for the user (never included).",
    25362536                            "type": "string"
    25372537                        },