Make WordPress Core


Ignore:
Timestamp:
09/19/2019 02:35:47 PM (5 years ago)
Author:
kadamwhite
Message:

REST API: Issue warning if array meta is registered without item schema.

The purpose of meta registration is to assert that the meta key will contain a predictable value conforming to a schema, so the schema is therefore considered to be required.

Props TimothyBlynJacobs, grapplerulrich.
Fixes #43392.

File:
1 edited

Legend:

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

    r45903 r46186  
    22062206
    22072207    /**
     2208     * @ticket 43392
     2209     */
     2210    public function test_register_meta_issues_doing_it_wrong_when_show_in_rest_is_true() {
     2211        $this->setExpectedIncorrectUsage( 'register_meta' );
     2212
     2213        $registered = register_meta(
     2214            'post',
     2215            'invalid_array',
     2216            array(
     2217                'type'         => 'array',
     2218                'show_in_rest' => true,
     2219            )
     2220        );
     2221
     2222        self::assertFalse( $registered );
     2223    }
     2224
     2225    /**
     2226     * @ticket 43392
     2227     */
     2228    public function test_register_meta_issues_doing_it_wrong_when_show_in_rest_omits_schema() {
     2229        $this->setExpectedIncorrectUsage( 'register_meta' );
     2230
     2231        $registered = register_meta(
     2232            'post',
     2233            'invalid_array',
     2234            array(
     2235                'type'         => 'array',
     2236                'show_in_rest' => array(
     2237                    'prepare_callback' => 'rest_sanitize_value_from_schema',
     2238                ),
     2239            )
     2240        );
     2241
     2242        self::assertFalse( $registered );
     2243    }
     2244
     2245    /**
     2246     * @ticket 43392
     2247     */
     2248    public function test_register_meta_issues_doing_it_wrong_when_show_in_rest_omits_schema_items() {
     2249        $this->setExpectedIncorrectUsage( 'register_meta' );
     2250
     2251        $registered = register_meta(
     2252            'post',
     2253            'invalid_array',
     2254            array(
     2255                'type'         => 'array',
     2256                'show_in_rest' => array(
     2257                    'schema' => array(
     2258                        'default' => array( 'Hi!' ),
     2259                    ),
     2260                ),
     2261            )
     2262        );
     2263
     2264        self::assertFalse( $registered );
     2265    }
     2266
     2267    /**
    22082268     * Internal function used to disable an insert query which
    22092269     * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.