Make WordPress Core


Ignore:
Timestamp:
11/21/2016 05:40:46 AM (10 years ago)
Author:
rmccue
Message:

REST API: Correctly map meta keys to field names.

This accidentally assumed $name was the same as $meta_key, which ruined the whole point of $name.

Props tharsheblows, joehoyle.
Fixes #38786.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r39222 r39328  
    8080                ));
    8181
     82                register_meta( 'post', 'test_custom_name', array(
     83                        'single' => true,
     84                        'type' => 'string',
     85                        'show_in_rest' => array(
     86                                'name'  => 'new_name',
     87                        ),
     88                ));
     89
     90                register_meta( 'post', 'test_custom_name_multi', array(
     91                        'single' => false,
     92                        'type' => 'string',
     93                        'show_in_rest' => array(
     94                                'name'  => 'new_name_multi',
     95                        ),
     96                ));
     97
    8298                /** @var WP_REST_Server $wp_rest_server */
    8399                global $wp_rest_server;
    … …  
    226242                $this->assertInternalType( 'boolean', $meta['test_bool'] );
    227243                $this->assertSame( true, $meta['test_bool'] );
     244        }
     245
     246        public function test_get_value_custom_name() {
     247                add_post_meta( self::$post_id, 'test_custom_name', 'janet' );
     248
     249                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     250                $response = $this->server->dispatch( $request );
     251
     252                $this->assertEquals( 200, $response->get_status() );
     253
     254                $data = $response->get_data();
     255                $this->assertArrayHasKey( 'meta', $data );
     256
     257                $meta = (array) $data['meta'];
     258                $this->assertArrayHasKey( 'new_name', $meta );
     259                $this->assertEquals( 'janet', $meta['new_name'] );
    228260        }
    229261
    … …  
    682714        }
    683715
     716        /**
     717         * @depends test_get_value_custom_name
     718         */
     719        public function test_set_value_custom_name() {
     720                // Ensure no data exists currently.
     721                $values = get_post_meta( self::$post_id, 'test_custom_name', false );
     722                $this->assertEmpty( $values );
     723
     724                $this->grant_write_permission();
     725
     726                $data = array(
     727                        'meta' => array(
     728                                'new_name' => 'janet',
     729                        ),
     730                );
     731                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     732                $request->set_body_params( $data );
     733
     734                $response = $this->server->dispatch( $request );
     735                $this->assertEquals( 200, $response->get_status() );
     736
     737                $meta = get_post_meta( self::$post_id, 'test_custom_name', false );
     738                $this->assertNotEmpty( $meta );
     739                $this->assertCount( 1, $meta );
     740                $this->assertEquals( 'janet', $meta[0] );
     741
     742                $data = $response->get_data();
     743                $meta = (array) $data['meta'];
     744                $this->assertArrayHasKey( 'new_name', $meta );
     745                $this->assertEquals( 'janet', $meta['new_name'] );
     746        }
     747
     748        public function test_set_value_custom_name_multiple() {
     749                // Ensure no data exists currently.
     750                $values = get_post_meta( self::$post_id, 'test_custom_name_multi', false );
     751                $this->assertEmpty( $values );
     752
     753                $this->grant_write_permission();
     754
     755                $data = array(
     756                        'meta' => array(
     757                                'new_name_multi' => array( 'janet' ),
     758                        ),
     759                );
     760                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     761                $request->set_body_params( $data );
     762
     763                $response = $this->server->dispatch( $request );
     764                $this->assertEquals( 200, $response->get_status() );
     765
     766                $meta = get_post_meta( self::$post_id, 'test_custom_name_multi', false );
     767                $this->assertNotEmpty( $meta );
     768                $this->assertCount( 1, $meta );
     769                $this->assertEquals( 'janet', $meta[0] );
     770
     771                // Add another value.
     772                $data = array(
     773                        'meta' => array(
     774                                'new_name_multi' => array( 'janet', 'graeme' ),
     775                        ),
     776                );
     777                $request->set_body_params( $data );
     778
     779                $response = $this->server->dispatch( $request );
     780                $this->assertEquals( 200, $response->get_status() );
     781
     782                $meta = get_post_meta( self::$post_id, 'test_custom_name_multi', false );
     783                $this->assertNotEmpty( $meta );
     784                $this->assertCount( 2, $meta );
     785                $this->assertContains( 'janet', $meta );
     786                $this->assertContains( 'graeme', $meta );
     787        }
     788
    684789        public function test_remove_multi_value_db_error() {
    685790                add_post_meta( self::$post_id, 'test_multi', 'val1' );
    … …  
    792897        }
    793898
     899        public function test_delete_value_custom_name() {
     900                add_post_meta( self::$post_id, 'test_custom_name', 'janet' );
     901                $current = get_post_meta( self::$post_id, 'test_custom_name', true );
     902                $this->assertEquals( 'janet', $current );
     903
     904                $this->grant_write_permission();
     905
     906                $data = array(
     907                        'meta' => array(
     908                                'new_name' => null,
     909                        ),
     910                );
     911                $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     912                $request->set_body_params( $data );
     913
     914                $response = $this->server->dispatch( $request );
     915                $this->assertEquals( 200, $response->get_status() );
     916
     917                $meta = get_post_meta( self::$post_id, 'test_custom_name', false );
     918                $this->assertEmpty( $meta );
     919        }
     920
    794921        public function test_get_schema() {
    795922                $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
Note: See TracChangeset for help on using the changeset viewer.