Make WordPress Core

Changeset 43636


Ignore:
Timestamp:
09/12/2018 11:55:47 PM (6 years ago)
Author:
kadamwhite
Message:

REST API: Pass correct ID to meta->update_value to permit setting term meta during term creation.

Props joehoyle.
Fixes #44834.

Location:
trunk
Files:
2 edited

Legend:

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

    r43571 r43636  
    455455        $schema = $this->get_item_schema();
    456456        if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
    457             $meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
     457            $meta_update = $this->meta->update_value( $request['meta'], $term->term_id );
    458458
    459459            if ( is_wp_error( $meta_update ) ) {
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r43571 r43636  
    756756        $response = rest_get_server()->dispatch( $request );
    757757        $this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
     758    }
     759
     760    public function test_create_item_with_meta() {
     761        wp_set_current_user( self::$administrator );
     762        $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
     763        $request->set_param( 'name', 'My Awesome Term' );
     764        $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
     765        $response = rest_get_server()->dispatch( $request );
     766        $this->assertEquals( 201, $response->get_status() );
     767        $headers = $response->get_headers();
     768        $data    = $response->get_data();
     769        $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
     770        $this->assertEquals( 'My Awesome Term', $data['name'] );
     771        $this->assertEquals( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
     772    }
     773
     774    public function test_create_item_with_meta_wrong_id() {
     775        wp_set_current_user( self::$administrator );
     776        $existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) );
     777        $request         = new WP_REST_Request( 'POST', '/wp/v2/tags' );
     778        $request->set_param( 'name', 'My Awesome Term' );
     779        $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
     780        $request->set_param( 'id', $existing_tag_id );
     781        $response = rest_get_server()->dispatch( $request );
     782        $this->assertEquals( 201, $response->get_status() );
     783        $headers = $response->get_headers();
     784        $data    = $response->get_data();
     785        $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
     786        $this->assertEquals( 'My Awesome Term', $data['name'] );
     787        $this->assertEquals( '', get_term_meta( $existing_tag_id, 'test_tag_single', true ) );
     788        $this->assertEquals( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
    758789    }
    759790
Note: See TracChangeset for help on using the changeset viewer.