<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39104] trunk: REST API: Support querying for multiple post statuses.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39104">39104</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39104","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joehoyle</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-03 01:45:48 +0000 (Thu, 03 Nov 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Support querying for multiple post statuses.
Multiple post statuses can be specified by the usual CSV or array-propper format.
Props jnylen0, kadamwhite, websupporter.
Fixes <a href="https://core.trac.wordpress.org/ticket/38420">#38420</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiclasswprestserverphp">trunk/src/wp-includes/rest-api/class-wp-rest-server.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestattachmentscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestattachmentscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasanitizationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemavalidationphp">trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiclasswprestserverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/class-wp-rest-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/src/wp-includes/rest-api/class-wp-rest-server.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1183,6 +1183,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $opts['description'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $arg_data['description'] = $opts['description'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $opts['type'] ) ) {
+ $arg_data['type'] = $opts['type'];
+ }
+ if ( isset( $opts['items'] ) ) {
+ $arg_data['items'] = $opts['items'];
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> $endpoint_data['args'][ $key ] = $arg_data;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestattachmentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -30,7 +30,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected function prepare_items_query( $prepared_args = array(), $request = null ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $query_args = parent::prepare_items_query( $prepared_args, $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $query_args['post_status'] ) || ! in_array( $query_args['post_status'], array( 'inherit', 'private', 'trash' ), true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $query_args['post_status'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $query_args['post_status'] = 'inherit';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -586,7 +586,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function get_collection_params() {
</span><span class="cx" style="display: block; padding: 0 10px"> $params = parent::get_collection_params();
</span><span class="cx" style="display: block; padding: 0 10px"> $params['status']['default'] = 'inherit';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $params['status']['enum'] = array( 'inherit', 'private', 'trash' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $params['status']['items']['enum'] = array( 'inherit', 'private', 'trash' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $media_types = $this->get_media_types();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $params['media_type'] = array(
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2120,11 +2120,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $params['status'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'default' => 'publish',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'description' => __( 'Limit result set to posts assigned a specific status; can be comma-delimited list of status types.' ),
- 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ),
- 'sanitize_callback' => 'sanitize_key',
- 'type' => 'string',
- 'validate_callback' => array( $this, 'validate_user_can_query_private_statuses' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'description' => __( 'Limit result set to posts assigned one or more statuses.' ),
+ 'type' => 'array',
+ 'items' => array(
+ 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ),
+ 'type' => 'string',
+ ),
+ 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ),
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2152,27 +2154,41 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Validates whether the user can query private statuses.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Sanitizes and validates the list of post statuses, including whether the
+ * user can query private statuses.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @access public
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param mixed $value Post status.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string|array $statuses One or more post statuses.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $parameter Additional parameter to pass to validation.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return bool|WP_Error Whether the request can query private statuses, otherwise WP_Error object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function validate_user_can_query_private_statuses( $value, $request, $parameter ) {
- if ( 'publish' === $value ) {
- return rest_validate_request_arg( $value, $request, $parameter );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function sanitize_post_statuses( $statuses, $request, $parameter ) {
+ $statuses = wp_parse_slug_list( $statuses );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $post_type_obj = get_post_type_object( $this->post_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // The default status is different in WP_REST_Attachments_Controller
+ $attributes = $request->get_attributes();
+ $default_status = $attributes['args']['status']['default'];
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
- return rest_validate_request_arg( $value, $request, $parameter );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $statuses as $status ) {
+ if ( $status === $default_status ) {
+ continue;
+ }
+
+ $post_type_obj = get_post_type_object( $this->post_type );
+
+ if ( current_user_can( $post_type_obj->cap->edit_posts ) ) {
+ $result = rest_validate_request_arg( $status, $request, $parameter );
+ if ( is_wp_error( $result ) ) {
+ return $result;
+ }
+ } else {
+ return new WP_Error( 'rest_forbidden_status', __( 'Status is forbidden.' ), array( 'status' => rest_authorization_required_code() ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return new WP_Error( 'rest_forbidden_status', __( 'Status is forbidden.' ), array( 'status' => rest_authorization_required_code() ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $statuses;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestattachmentscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -327,6 +327,37 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $attachment_id1, $data[0]['id'] );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_items_multiple_statuses() {
+ // Logged out users can't make the request
+ wp_set_current_user( 0 );
+ $attachment_id1 = $this->factory->attachment->create_object( $this->test_file, 0, array(
+ 'post_mime_type' => 'image/jpeg',
+ 'post_excerpt' => 'A sample caption',
+ 'post_status' => 'private',
+ ) );
+ $attachment_id2 = $this->factory->attachment->create_object( $this->test_file, 0, array(
+ 'post_mime_type' => 'image/jpeg',
+ 'post_excerpt' => 'A sample caption',
+ 'post_status' => 'trash',
+ ) );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
+ $request->set_param( 'status', array( 'private', 'trash' ) );
+ $response = $this->server->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ // Properly authorized users can make the request
+ wp_set_current_user( self::$editor_id );
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $data = $response->get_data();
+ $this->assertEquals( 2, count( $data ) );
+ $ids = array(
+ $data[0]['id'],
+ $data[1]['id'],
+ );
+ sort( $ids );
+ $this->assertEquals( array( $attachment_id1, $attachment_id2 ), $ids );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_get_items_invalid_date() {
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'after', rand_str() );
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -310,6 +310,60 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, count( $response->get_data() ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_items_multiple_statuses_string_query() {
+ wp_set_current_user( self::$editor_id );
+
+ $this->factory->post->create( array( 'post_status' => 'draft' ) );
+ $this->factory->post->create( array( 'post_status' => 'private' ) );
+ $this->factory->post->create( array( 'post_status' => 'publish' ) );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
+ $request->set_param( 'context', 'edit' );
+ $request->set_param( 'status', 'draft,private' );
+
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $data = $response->get_data();
+ $this->assertEquals( 2, count( $data ) );
+ $statuses = array(
+ $data[0]['status'],
+ $data[1]['status'],
+ );
+ sort( $statuses );
+ $this->assertEquals( array( 'draft', 'private' ), $statuses );
+ }
+
+ public function test_get_items_multiple_statuses_array_query() {
+ wp_set_current_user( self::$editor_id );
+
+ $this->factory->post->create( array( 'post_status' => 'draft' ) );
+ $this->factory->post->create( array( 'post_status' => 'pending' ) );
+ $this->factory->post->create( array( 'post_status' => 'publish' ) );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
+ $request->set_param( 'context', 'edit' );
+ $request->set_param( 'status', array( 'draft', 'pending' ) );
+
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $data = $response->get_data();
+ $this->assertEquals( 2, count( $data ) );
+ $statuses = array(
+ $data[0]['status'],
+ $data[1]['status'],
+ );
+ sort( $statuses );
+ $this->assertEquals( array( 'draft', 'pending' ), $statuses );
+ }
+
+ public function test_get_items_multiple_statuses_one_invalid_query() {
+ $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
+ $request->set_param( 'context', 'edit' );
+ $request->set_param( 'status', array( 'draft', 'nonsense' ) );
+ $response = $this->server->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_get_items_invalid_status_query() {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( 0 );
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1963,6 +2017,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'categories_exclude', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_status_array_enum_args() {
+ $request = new WP_REST_Request( 'GET', '/wp/v2' );
+ $response = $this->server->dispatch( $request );
+ $data = $response->get_data();
+ $list_posts_args = $data['routes']['/wp/v2/posts']['endpoints'][0]['args'];
+ $status_arg = $list_posts_args['status'];
+ $this->assertEquals( 'array', $status_arg['type'] );
+ $this->assertEquals( array(
+ 'type' => 'string',
+ 'enum' => array( 'publish', 'future', 'draft', 'pending', 'private', 'trash', 'auto-draft', 'inherit', 'any' ),
+ ), $status_arg['items'] );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_get_additional_field_registration() {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $schema = array(
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemasanitizationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -86,4 +86,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 1, 2 ), rest_sanitize_value_from_schema( '1,2', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 1, 2, 0 ), rest_sanitize_value_from_schema( '1,2,a', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ public function test_type_array_with_enum() {
+ $schema = array(
+ 'type' => 'array',
+ 'items' => array(
+ 'enum' => array( 'chicken', 'ribs', 'brisket' ),
+ 'type' => 'string',
+ ),
+ );
+ $this->assertEquals( array( 'ribs', 'brisket' ), rest_sanitize_value_from_schema( array( 'ribs', 'brisket' ), $schema ) );
+ $this->assertEquals( array( 'coleslaw' ), rest_sanitize_value_from_schema( array( 'coleslaw' ), $schema ) );
+ }
+
+ public function test_type_array_with_enum_as_csv() {
+ $schema = array(
+ 'type' => 'array',
+ 'items' => array(
+ 'enum' => array( 'chicken', 'ribs', 'brisket' ),
+ 'type' => 'string',
+ ),
+ );
+ $this->assertEquals( array( 'ribs', 'chicken' ), rest_sanitize_value_from_schema( 'ribs,chicken', $schema ) );
+ $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw', $schema ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemavalidationphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php 2016-11-03 01:25:48 UTC (rev 39103)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php 2016-11-03 01:45:48 UTC (rev 39104)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -115,4 +115,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( rest_validate_value_from_schema( '1,2,3', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertWPError( rest_validate_value_from_schema( 'lol', $schema ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ public function test_type_array_with_enum() {
+ $schema = array(
+ 'type' => 'array',
+ 'items' => array(
+ 'enum' => array( 'chicken', 'ribs', 'brisket' ),
+ 'type' => 'string',
+ ),
+ );
+ $this->assertTrue( rest_validate_value_from_schema( array( 'ribs', 'brisket' ), $schema ) );
+ $this->assertWPError( rest_validate_value_from_schema( array( 'coleslaw' ), $schema ) );
+ }
+
+ public function test_type_array_with_enum_as_csv() {
+ $schema = array(
+ 'type' => 'array',
+ 'items' => array(
+ 'enum' => array( 'chicken', 'ribs', 'brisket' ),
+ 'type' => 'string',
+ ),
+ );
+ $this->assertTrue( rest_validate_value_from_schema( 'ribs,chicken', $schema ) );
+ $this->assertWPError( rest_validate_value_from_schema( 'chicken,coleslaw', $schema ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>