Make WordPress Core

Changeset 42578


Ignore:
Timestamp:
01/23/2018 08:21:46 PM (6 years ago)
Author:
jorbin
Message:

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

Merges [42350] into 4.9

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

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r41760 r42578  
    416416                $existing_term = get_term( $term_id, $this->taxonomy );
    417417                $term->add_data( $existing_term->term_id, 'term_exists' );
    418                 $term->add_data( array( 'status' => 409 ) );
     418                $term->add_data( array( 'status' => 409, 'term_id' => $term_id ) );
    419419            }
    420420
  • branches/4.9/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r41760 r42578  
    624624    public function test_create_item_term_already_exists() {
    625625        wp_set_current_user( self::$administrator );
     626        $existing_id = $this->factory->category->create( array( 'name' => 'Existing' ) );
     627
    626628        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    627         $request->set_param( 'name', 'test' );
    628         $response = $this->server->dispatch( $request );
    629         $this->assertEquals( 201, $response->get_status() );
     629        $request->set_param( 'name', 'Existing' );
     630
    630631        $response = $this->server->dispatch( $request );
    631632        $this->assertEquals( 409, $response->get_status() );
    632633        $data = $response->get_data();
    633634        $this->assertEquals( 'term_exists', $data['code'] );
     635        $this->assertEquals( $existing_id, (int) $data['data']['term_id'] );
     636
     637        wp_delete_term( $existing_id, 'category' );
    634638    }
    635639
Note: See TracChangeset for help on using the changeset viewer.