WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/12/2018 11:55:47 PM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.