REST API: Add existing term_id to the error data object when attempting to create a duplicate term.

Props shooper, coleh.
Fixes #42597. See #41370.

git-svn-id: https://develop.svn.wordpress.org/trunk@42350 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Rachel Baker 2017-12-03 18:10:05 +00:00
parent 6b325e9916
commit 2b079e098a
2 changed files with 8 additions and 4 deletions

View File

@ -419,7 +419,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
if ( $term_id = $term->get_error_data( 'term_exists' ) ) { if ( $term_id = $term->get_error_data( 'term_exists' ) ) {
$existing_term = get_term( $term_id, $this->taxonomy ); $existing_term = get_term( $term_id, $this->taxonomy );
$term->add_data( $existing_term->term_id, 'term_exists' ); $term->add_data( $existing_term->term_id, 'term_exists' );
$term->add_data( array( 'status' => 409 ) ); $term->add_data( array( 'status' => 409, 'term_id' => $term_id ) );
} }
return $term; return $term;

View File

@ -696,14 +696,18 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
*/ */
public function test_create_item_term_already_exists() { public function test_create_item_term_already_exists() {
wp_set_current_user( self::$administrator ); wp_set_current_user( self::$administrator );
$existing_id = $this->factory->category->create( array( 'name' => 'Existing' ) );
$request = new WP_REST_Request( 'POST', '/wp/v2/categories' ); $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
$request->set_param( 'name', 'test' ); $request->set_param( 'name', 'Existing' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 201, $response->get_status() );
$response = $this->server->dispatch( $request ); $response = $this->server->dispatch( $request );
$this->assertEquals( 409, $response->get_status() ); $this->assertEquals( 409, $response->get_status() );
$data = $response->get_data(); $data = $response->get_data();
$this->assertEquals( 'term_exists', $data['code'] ); $this->assertEquals( 'term_exists', $data['code'] );
$this->assertEquals( $existing_id, (int) $data['data']['term_id'] );
wp_delete_term( $existing_id, 'category' );
} }
public function test_create_item_invalid_taxonomy() { public function test_create_item_invalid_taxonomy() {