From 2b079e098a1a1411eaaa1c9032f5d0a3d0125b30 Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Sun, 3 Dec 2017 18:10:05 +0000 Subject: [PATCH] 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 --- .../endpoints/class-wp-rest-terms-controller.php | 2 +- .../tests/rest-api/rest-categories-controller.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php index 459ef3e948..7c62c34fe8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php @@ -419,7 +419,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller { if ( $term_id = $term->get_error_data( 'term_exists' ) ) { $existing_term = get_term( $term_id, $this->taxonomy ); $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; diff --git a/tests/phpunit/tests/rest-api/rest-categories-controller.php b/tests/phpunit/tests/rest-api/rest-categories-controller.php index 27f6edceac..4c1152133e 100644 --- a/tests/phpunit/tests/rest-api/rest-categories-controller.php +++ b/tests/phpunit/tests/rest-api/rest-categories-controller.php @@ -696,14 +696,18 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas */ public function test_create_item_term_already_exists() { 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->set_param( 'name', 'test' ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 201, $response->get_status() ); + $request->set_param( 'name', 'Existing' ); + $response = $this->server->dispatch( $request ); $this->assertEquals( 409, $response->get_status() ); $data = $response->get_data(); $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() {