<!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>[39155] trunk: REST API: Respect unfiltered_html for HTML post fields.</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/39155">39155</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/39155","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>rmccue</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-08 05:54:22 +0000 (Tue, 08 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: Respect unfiltered_html for HTML post fields.
This necessitates a change to our slashing code as well. Ah slashing, the cause of, and solution to, all of life's problems.
Props jnylen0.
Fixes <a href="https://core.trac.wordpress.org/ticket/38609">#38609</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<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>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<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-08 04:28:47 UTC (rev 39154)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php 2016-11-08 05:54:22 UTC (rev 39155)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -142,7 +142,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $attachment->post_title = preg_replace( '/\.[^.]+$/', '', basename( $file ) );
</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">- $id = wp_insert_post( $attachment, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = wp_insert_post( wp_slash( (array) $attachment ), true );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'db_update_error' === $id->get_error_code() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -250,18 +250,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Attachment caption (post_excerpt internally)
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['caption'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_string( $request['caption'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_attachment->post_excerpt = wp_filter_post_kses( $request['caption'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_attachment->post_excerpt = $request['caption'];
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( isset( $request['caption']['raw'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_attachment->post_excerpt = wp_filter_post_kses( $request['caption']['raw'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_attachment->post_excerpt = $request['caption']['raw'];
</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">
</span><span class="cx" style="display: block; padding: 0 10px"> // Attachment description (post_content internally)
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $request['description'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_string( $request['description'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_attachment->post_content = wp_filter_post_kses( $request['description'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_attachment->post_content = $request['description'];
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( isset( $request['description']['raw'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_attachment->post_content = wp_filter_post_kses( $request['description']['raw'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_attachment->post_content = $request['description']['raw'];
</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">
</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-08 04:28:47 UTC (rev 39154)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2016-11-08 05:54:22 UTC (rev 39155)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -488,7 +488,7 @@
</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"> $post->post_type = $this->post_type;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $post_id = wp_insert_post( $post, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $post_id = wp_insert_post( wp_slash( (array) $post ), true );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $post_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -628,7 +628,7 @@
</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"> // convert the post object to an array, otherwise wp_update_post will expect non-escaped input.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $post_id = wp_update_post( (array) $post, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $post_id = wp_update_post( wp_slash( (array) $post ), true );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $post_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'db_update_error' === $post_id->get_error_code() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -969,27 +969,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Post title.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_string( $request['title'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_post->post_title = wp_filter_post_kses( $request['title'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_post->post_title = $request['title'];
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( ! empty( $request['title']['raw'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_post->post_title = wp_filter_post_kses( $request['title']['raw'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_post->post_title = $request['title']['raw'];
</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">
</span><span class="cx" style="display: block; padding: 0 10px"> // Post content.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_string( $request['content'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_post->post_content = wp_filter_post_kses( $request['content'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_post->post_content = $request['content'];
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( isset( $request['content']['raw'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_post->post_content = wp_filter_post_kses( $request['content']['raw'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_post->post_content = $request['content']['raw'];
</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">
</span><span class="cx" style="display: block; padding: 0 10px"> // Post excerpt.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $schema['properties']['excerpt'] ) && isset( $request['excerpt'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_string( $request['excerpt'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_post->post_excerpt = wp_filter_post_kses( $request['excerpt'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_post->post_excerpt = $request['excerpt'];
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( isset( $request['excerpt']['raw'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_post->post_excerpt = wp_filter_post_kses( $request['excerpt']['raw'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $prepared_post->post_excerpt = $request['excerpt']['raw'];
</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">
</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-08 04:28:47 UTC (rev 39154)
+++ trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php 2016-11-08 05:54:22 UTC (rev 39155)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,12 +10,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @group restapi
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ protected static $superadmin_id;
</ins><span class="cx" style="display: block; padding: 0 10px"> protected static $editor_id;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $author_id;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $contributor_id;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $uploader_id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$superadmin_id = $factory->user->create( array(
+ 'role' => 'administrator',
+ 'user_login' => 'superadmin',
+ ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$editor_id = $factory->user->create( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'role' => 'editor',
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,6 +34,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> self::$uploader_id = $factory->user->create( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'role' => 'uploader',
</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 ( is_multisite() ) {
+ update_site_option( 'site_admins', array( 'superadmin' ) );
+ }
</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"> public static function wpTearDownAfterClass() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -53,7 +63,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $orig_file2 = DIR_TESTDATA . '/images/codeispoetry.png';
</span><span class="cx" style="display: block; padding: 0 10px"> $this->test_file2 = '/tmp/codeispoetry.png';
</span><span class="cx" style="display: block; padding: 0 10px"> copy( $orig_file2, $this->test_file2 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><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"> public function test_register_routes() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -723,6 +732,250 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
</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 verify_attachment_roundtrip( $input = array(), $expected_output = array() ) {
+ // Create the post
+ $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
+ $request->set_header( 'Content-Type', 'image/jpeg' );
+ $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
+ $request->set_body( file_get_contents( $this->test_file ) );
+
+ foreach ( $input as $name => $value ) {
+ $request->set_param( $name, $value );
+ }
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 201, $response->get_status() );
+ $actual_output = $response->get_data();
+
+ // Remove <p class="attachment"> from rendered description
+ // see https://core.trac.wordpress.org/ticket/38679
+ $content = $actual_output['description']['rendered'];
+ $content = explode( "\n", trim( $content ) );
+ if ( preg_match( '/^<p class="attachment">/', $content[0] ) ) {
+ $content = implode( "\n", array_slice( $content, 1 ) );
+ $actual_output['description']['rendered'] = $content;
+ }
+
+ // Compare expected API output to actual API output
+ $this->assertEquals( $expected_output['title']['raw'] , $actual_output['title']['raw'] );
+ $this->assertEquals( $expected_output['title']['rendered'] , trim( $actual_output['title']['rendered'] ) );
+ $this->assertEquals( $expected_output['description']['raw'] , $actual_output['description']['raw'] );
+ $this->assertEquals( $expected_output['description']['rendered'], trim( $actual_output['description']['rendered'] ) );
+ $this->assertEquals( $expected_output['caption']['raw'] , $actual_output['caption']['raw'] );
+ $this->assertEquals( $expected_output['caption']['rendered'] , trim( $actual_output['caption']['rendered'] ) );
+
+ // Compare expected API output to WP internal values
+ $post = get_post( $actual_output['id'] );
+ $this->assertEquals( $expected_output['title']['raw'], $post->post_title );
+ $this->assertEquals( $expected_output['description']['raw'], $post->post_content );
+ $this->assertEquals( $expected_output['caption']['raw'], $post->post_excerpt );
+
+ // Update the post
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/media/%d', $actual_output['id'] ) );
+ foreach ( $input as $name => $value ) {
+ $request->set_param( $name, $value );
+ }
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $actual_output = $response->get_data();
+
+ // Remove <p class="attachment"> from rendered description
+ // see https://core.trac.wordpress.org/ticket/38679
+ $content = $actual_output['description']['rendered'];
+ $content = explode( "\n", trim( $content ) );
+ if ( preg_match( '/^<p class="attachment">/', $content[0] ) ) {
+ $content = implode( "\n", array_slice( $content, 1 ) );
+ $actual_output['description']['rendered'] = $content;
+ }
+
+ // Compare expected API output to actual API output
+ $this->assertEquals( $expected_output['title']['raw'] , $actual_output['title']['raw'] );
+ $this->assertEquals( $expected_output['title']['rendered'] , trim( $actual_output['title']['rendered'] ) );
+ $this->assertEquals( $expected_output['description']['raw'] , $actual_output['description']['raw'] );
+ $this->assertEquals( $expected_output['description']['rendered'], trim( $actual_output['description']['rendered'] ) );
+ $this->assertEquals( $expected_output['caption']['raw'] , $actual_output['caption']['raw'] );
+ $this->assertEquals( $expected_output['caption']['rendered'] , trim( $actual_output['caption']['rendered'] ) );
+
+ // Compare expected API output to WP internal values
+ $post = get_post( $actual_output['id'] );
+ $this->assertEquals( $expected_output['title']['raw'] , $post->post_title );
+ $this->assertEquals( $expected_output['description']['raw'], $post->post_content );
+ $this->assertEquals( $expected_output['caption']['raw'], $post->post_excerpt );
+ }
+
+ public static function attachment_roundtrip_provider() {
+ return array(
+ array(
+ // Raw values.
+ array(
+ 'title' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'description' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'caption' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'rendered' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ ),
+ 'description' => array(
+ 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
+ ),
+ 'caption' => array(
+ 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
+ ),
+ )
+ ),
+ array(
+ // Raw values.
+ array(
+ 'title' => '\\\&\\\ & &invalid; < < &lt;',
+ 'description' => '\\\&\\\ & &invalid; < < &lt;',
+ 'caption' => '\\\&\\\ & &invalid; < < &lt;',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => '\\\&\\\ & &invalid; < < &lt;',
+ 'rendered' => '\\\&\\\ & &invalid; < < &lt;',
+ ),
+ 'description' => array(
+ 'raw' => '\\\&\\\ & &invalid; < < &lt;',
+ 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>',
+ ),
+ 'caption' => array(
+ 'raw' => '\\\&\\\ & &invalid; < < &lt;',
+ 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>',
+ ),
+ ),
+ ),
+ array(
+ // Raw values.
+ array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => 'div <strong>strong</strong> oh noes',
+ 'rendered' => 'div <strong>strong</strong> oh noes',
+ ),
+ 'description' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ 'caption' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ )
+ ),
+ array(
+ // Raw values.
+ array(
+ 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
+ 'description' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
+ 'caption' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => '<a href="#">link</a>',
+ 'rendered' => '<a href="#">link</a>',
+ ),
+ 'description' => array(
+ 'raw' => '<a href="#" target="_blank">link</a>',
+ 'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+ ),
+ 'caption' => array(
+ 'raw' => '<a href="#" target="_blank">link</a>',
+ 'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+ ),
+ )
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider attachment_roundtrip_provider
+ */
+ public function test_post_roundtrip_as_author( $raw, $expected ) {
+ wp_set_current_user( self::$author_id );
+ $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+ $this->verify_attachment_roundtrip( $raw, $expected );
+ }
+
+ public function test_attachment_roundtrip_as_editor_unfiltered_html() {
+ wp_set_current_user( self::$editor_id );
+ if ( is_multisite() ) {
+ $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+ $this->verify_attachment_roundtrip( array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ), array(
+ 'title' => array(
+ 'raw' => 'div <strong>strong</strong> oh noes',
+ 'rendered' => 'div <strong>strong</strong> oh noes',
+ ),
+ 'description' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ 'caption' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ ) );
+ } else {
+ $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+ $this->verify_attachment_roundtrip( array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ), array(
+ 'title' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ),
+ 'description' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ 'caption' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ ) );
+ }
+ }
+
+ public function test_attachment_roundtrip_as_superadmin_unfiltered_html() {
+ wp_set_current_user( self::$superadmin_id );
+ $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+ $this->verify_attachment_roundtrip( array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'caption' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ), array(
+ 'title' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ),
+ 'description' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ 'caption' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_delete_item() {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$editor_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
</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-08 04:28:47 UTC (rev 39154)
+++ trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2016-11-08 05:54:22 UTC (rev 39155)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +12,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Testcase {
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $post_id;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static $superadmin_id;
</ins><span class="cx" style="display: block; padding: 0 10px"> protected static $editor_id;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $author_id;
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $contributor_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,6 +24,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><span class="cx" style="display: block; padding: 0 10px"> self::$post_id = $factory->post->create();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$superadmin_id = $factory->user->create( array(
+ 'role' => 'administrator',
+ 'user_login' => 'superadmin',
+ ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> self::$editor_id = $factory->user->create( array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'role' => 'editor',
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -33,6 +38,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'role' => 'contributor',
</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">+ if ( is_multisite() ) {
+ update_site_option( 'site_admins', array( 'superadmin' ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Only support 'post' and 'gallery'
</span><span class="cx" style="display: block; padding: 0 10px"> self::$supported_formats = get_theme_support( 'post-formats' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_theme_support( 'post-formats', array( 'post', 'gallery' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2003,6 +2012,228 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_cannot_assign_term', $response, 403 );
</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 verify_post_roundtrip( $input = array(), $expected_output = array() ) {
+ // Create the post
+ $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
+ foreach ( $input as $name => $value ) {
+ $request->set_param( $name, $value );
+ }
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 201, $response->get_status() );
+ $actual_output = $response->get_data();
+
+ // Compare expected API output to actual API output
+ $this->assertEquals( $expected_output['title']['raw'] , $actual_output['title']['raw'] );
+ $this->assertEquals( $expected_output['title']['rendered'] , trim( $actual_output['title']['rendered'] ) );
+ $this->assertEquals( $expected_output['content']['raw'] , $actual_output['content']['raw'] );
+ $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
+ $this->assertEquals( $expected_output['excerpt']['raw'] , $actual_output['excerpt']['raw'] );
+ $this->assertEquals( $expected_output['excerpt']['rendered'], trim( $actual_output['excerpt']['rendered'] ) );
+
+ // Compare expected API output to WP internal values
+ $post = get_post( $actual_output['id'] );
+ $this->assertEquals( $expected_output['title']['raw'] , $post->post_title );
+ $this->assertEquals( $expected_output['content']['raw'], $post->post_content );
+ $this->assertEquals( $expected_output['excerpt']['raw'], $post->post_excerpt );
+
+ // Update the post
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $actual_output['id'] ) );
+ foreach ( $input as $name => $value ) {
+ $request->set_param( $name, $value );
+ }
+ $response = $this->server->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $actual_output = $response->get_data();
+
+ // Compare expected API output to actual API output
+ $this->assertEquals( $expected_output['title']['raw'] , $actual_output['title']['raw'] );
+ $this->assertEquals( $expected_output['title']['rendered'] , trim( $actual_output['title']['rendered'] ) );
+ $this->assertEquals( $expected_output['content']['raw'] , $actual_output['content']['raw'] );
+ $this->assertEquals( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
+ $this->assertEquals( $expected_output['excerpt']['raw'] , $actual_output['excerpt']['raw'] );
+ $this->assertEquals( $expected_output['excerpt']['rendered'], trim( $actual_output['excerpt']['rendered'] ) );
+
+ // Compare expected API output to WP internal values
+ $post = get_post( $actual_output['id'] );
+ $this->assertEquals( $expected_output['title']['raw'] , $post->post_title );
+ $this->assertEquals( $expected_output['content']['raw'], $post->post_content );
+ $this->assertEquals( $expected_output['excerpt']['raw'], $post->post_excerpt );
+ }
+
+ public static function post_roundtrip_provider() {
+ return array(
+ array(
+ // Raw values.
+ array(
+ 'title' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'content' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'excerpt' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'rendered' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ ),
+ 'content' => array(
+ 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
+ ),
+ 'excerpt' => array(
+ 'raw' => '\o/ ¯\_(ツ)_/¯ 🚢',
+ 'rendered' => '<p>\o/ ¯\_(ツ)_/¯ 🚢</p>',
+ ),
+ )
+ ),
+ array(
+ // Raw values.
+ array(
+ 'title' => '\\\&\\\ & &invalid; < < &lt;',
+ 'content' => '\\\&\\\ & &invalid; < < &lt;',
+ 'excerpt' => '\\\&\\\ & &invalid; < < &lt;',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => '\\\&\\\ & &invalid; < < &lt;',
+ 'rendered' => '\\\&\\\ & &invalid; < < &lt;',
+ ),
+ 'content' => array(
+ 'raw' => '\\\&\\\ & &invalid; < < &lt;',
+ 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>',
+ ),
+ 'excerpt' => array(
+ 'raw' => '\\\&\\\ & &invalid; < < &lt;',
+ 'rendered' => '<p>\\\&\\\ & &invalid; < < &lt;</p>',
+ ),
+ ),
+ ),
+ array(
+ // Raw values.
+ array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => 'div <strong>strong</strong> oh noes',
+ 'rendered' => 'div <strong>strong</strong> oh noes',
+ ),
+ 'content' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ 'excerpt' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ )
+ ),
+ array(
+ // Raw values.
+ array(
+ 'title' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
+ 'content' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
+ 'excerpt' => '<a href="#" target="_blank" data-unfiltered=true>link</a>',
+ ),
+ // Expected returned values.
+ array(
+ 'title' => array(
+ 'raw' => '<a href="#">link</a>',
+ 'rendered' => '<a href="#">link</a>',
+ ),
+ 'content' => array(
+ 'raw' => '<a href="#" target="_blank">link</a>',
+ 'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+ ),
+ 'excerpt' => array(
+ 'raw' => '<a href="#" target="_blank">link</a>',
+ 'rendered' => '<p><a href="#" target="_blank">link</a></p>',
+ ),
+ )
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider post_roundtrip_provider
+ */
+ public function test_post_roundtrip_as_author( $raw, $expected ) {
+ wp_set_current_user( self::$author_id );
+ $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+ $this->verify_post_roundtrip( $raw, $expected );
+ }
+
+ public function test_post_roundtrip_as_editor_unfiltered_html() {
+ wp_set_current_user( self::$editor_id );
+ if ( is_multisite() ) {
+ $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+ $this->verify_post_roundtrip( array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ), array(
+ 'title' => array(
+ 'raw' => 'div <strong>strong</strong> oh noes',
+ 'rendered' => 'div <strong>strong</strong> oh noes',
+ ),
+ 'content' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ 'excerpt' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> oh noes',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> oh noes</p>",
+ ),
+ ) );
+ } else {
+ $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+ $this->verify_post_roundtrip( array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ), array(
+ 'title' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ),
+ 'content' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ 'excerpt' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ ) );
+ }
+ }
+
+ public function test_post_roundtrip_as_superadmin_unfiltered_html() {
+ wp_set_current_user( self::$superadmin_id );
+ $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+ $this->verify_post_roundtrip( array(
+ 'title' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'excerpt' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ), array(
+ 'title' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ ),
+ 'content' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ 'excerpt' => array(
+ 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+ 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
+ ),
+ ) );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_delete_item() {
</span><span class="cx" style="display: block; padding: 0 10px"> $post_id = $this->factory->post->create( array( 'post_title' => 'Deleted post' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$editor_id );
</span></span></pre>
</div>
</div>
</body>
</html>