Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Sep 24, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 lib/endpoints/class-wp-rest-users-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,16 @@ public function get_current_item( $request ) {
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
global $wp_roles;

if ( ! empty( $request['id'] ) ) {
return new WP_Error( 'rest_user_exists', __( 'Cannot create existing user.' ), array( 'status' => 400 ) );
}

if ( ! empty( $request['role'] ) && ! isset( $wp_roles->role_objects[ $request['role'] ] ) ) {
return new WP_Error( 'rest_user_invalid_role', __( 'Role is invalid.' ), array( 'status' => 400 ) );
}

$user = $this->prepare_item_for_database( $request );

if ( is_multisite() ) {
Expand Down
21 changes: 20 additions & 1 deletion 21 tests/test-rest-users-controller.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* Unit tests covering WP_REST_Users_Controller functionality.
*
* @package WordPress
* @subpackage JSON API
*/

class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
/**
* This function is run before each method
Expand Down Expand Up @@ -356,6 +356,25 @@ public function test_create_user_invalid_id() {
$this->assertErrorResponse( 'rest_user_exists', $response, 400 );
}

public function test_create_user_invalid_role() {
$this->allow_user_to_manage_multisite();
wp_set_current_user( $this->user );

$params = array(
'username' => 'maggiesimpson',
'password' => 'i_shot_mrburns',
'email' => 'packingheat@example',
'role' => 'baby',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍

);

$request = new WP_REST_Request( 'POST', '/wp/v2/users' );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
$request->set_body_params( $params );
$response = $this->server->dispatch( $request );

$this->assertErrorResponse( 'rest_user_invalid_role', $response, 400 );
}

public function test_update_item() {
$user_id = $this->factory->user->create( array(
'user_email' => 'test@example.com',
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.