Make WordPress Core

Changeset 42350


Ignore:
Timestamp:
12/03/2017 06:10:05 PM (7 years ago)
Author:
rachelbaker
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r42343 r42350  
    420420                $existing_term = get_term( $term_id, $this->taxonomy );
    421421                $term->add_data( $existing_term->term_id, 'term_exists' );
    422                 $term->add_data( array( 'status' => 409 ) );
     422                $term->add_data( array( 'status' => 409, 'term_id' => $term_id ) );
    423423            }
    424424
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r42343 r42350  
    697697    public function test_create_item_term_already_exists() {
    698698        wp_set_current_user( self::$administrator );
     699        $existing_id = $this->factory->category->create( array( 'name' => 'Existing' ) );
     700
    699701        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    700         $request->set_param( 'name', 'test' );
    701         $response = $this->server->dispatch( $request );
    702         $this->assertEquals( 201, $response->get_status() );
     702        $request->set_param( 'name', 'Existing' );
     703
    703704        $response = $this->server->dispatch( $request );
    704705        $this->assertEquals( 409, $response->get_status() );
    705706        $data = $response->get_data();
    706707        $this->assertEquals( 'term_exists', $data['code'] );
     708        $this->assertEquals( $existing_id, (int) $data['data']['term_id'] );
     709
     710        wp_delete_term( $existing_id, 'category' );
    707711    }
    708712
Note: See TracChangeset for help on using the changeset viewer.