<!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>[43340] trunk/tests/phpunit/tests/rest-api: REST API: Improve test coverage by providing tests for term meta.</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 { white-space: pre-line; 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/43340">43340</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/43340","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-06-11 17:54:19 +0000 (Mon, 11 Jun 2018)</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: Improve test coverage by providing tests for term meta.
See <a href="https://core.trac.wordpress.org/ticket/38323">#38323</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsrestapirestpostmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestuserscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-users-controller.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsrestapiresttermmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsrestapirestpostmetafieldsphp"></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-post-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php 2018-06-11 16:32:32 UTC (rev 43339)
+++ trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php 2018-06-11 17:54:19 UTC (rev 43340)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,7 +14,7 @@
</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><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( $factory ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$wp_meta_keys_saved = $GLOBALS['wp_meta_keys'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
</ins><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><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiresttermmetafieldsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php 2018-06-11 17:54:19 UTC (rev 43340)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,1071 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_REST_Terms meta functionality.
+ *
+ * @package WordPress
+ * @subpackage REST API
+ */
+
+/**
+ * @group restapi
+ */
+class WP_Test_REST_Term_Meta_Fields extends WP_Test_REST_TestCase {
+ protected static $wp_meta_keys_saved;
+ protected static $category_id;
+
+ public static function wpSetUpBeforeClass( $factory ) {
+ self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array();
+ self::$category_id = $factory->category->create();
+ }
+
+ public static function wpTearDownAfterClass() {
+ $GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved;
+ wp_delete_term( self::$category_id, 'category' );
+ }
+
+ public function setUp() {
+ parent::setUp();
+
+ register_meta(
+ 'term', 'test_single', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'type' => 'string',
+ )
+ );
+ register_meta(
+ 'term', 'test_multi', array(
+ 'show_in_rest' => true,
+ 'single' => false,
+ 'type' => 'string',
+ )
+ );
+ register_meta(
+ 'term', 'test_bad_auth', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'auth_callback' => '__return_false',
+ 'type' => 'string',
+ )
+ );
+ register_meta(
+ 'term', 'test_bad_auth_multi', array(
+ 'show_in_rest' => true,
+ 'single' => false,
+ 'auth_callback' => '__return_false',
+ 'type' => 'string',
+ )
+ );
+ register_meta( 'term', 'test_no_rest', array() );
+ register_meta(
+ 'term', 'test_rest_disabled', array(
+ 'show_in_rest' => false,
+ 'type' => 'string',
+ )
+ );
+ register_meta(
+ 'term', 'test_custom_schema', array(
+ 'single' => true,
+ 'type' => 'integer',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'number',
+ ),
+ ),
+ )
+ );
+ register_meta(
+ 'term', 'test_custom_schema_multi', array(
+ 'single' => false,
+ 'type' => 'integer',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'number',
+ ),
+ ),
+ )
+ );
+ register_meta(
+ 'term', 'test_invalid_type', array(
+ 'single' => true,
+ 'type' => 'lalala',
+ 'show_in_rest' => true,
+ )
+ );
+ register_meta(
+ 'term', 'test_no_type', array(
+ 'single' => true,
+ 'type' => null,
+ 'show_in_rest' => true,
+ )
+ );
+
+ register_meta(
+ 'term', 'test_custom_name', array(
+ 'single' => true,
+ 'type' => 'string',
+ 'show_in_rest' => array(
+ 'name' => 'new_name',
+ ),
+ )
+ );
+
+ register_meta(
+ 'term', 'test_custom_name_multi', array(
+ 'single' => false,
+ 'type' => 'string',
+ 'show_in_rest' => array(
+ 'name' => 'new_name_multi',
+ ),
+ )
+ );
+
+ /** @var WP_REST_Server $wp_rest_server */
+ global $wp_rest_server;
+ $wp_rest_server = new Spy_REST_Server;
+ do_action( 'rest_api_init', $wp_rest_server );
+ }
+
+ protected function grant_write_permission() {
+ // Ensure we have write permission.
+ $user = $this->factory->user->create(
+ array(
+ 'role' => 'editor',
+ )
+ );
+ wp_set_current_user( $user );
+ }
+
+ public function test_get_value() {
+ add_term_meta( self::$category_id, 'test_single', 'testvalue' );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertArrayHasKey( 'meta', $data );
+
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'test_single', $meta );
+ $this->assertEquals( 'testvalue', $meta['test_single'] );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_get_multi_value() {
+ add_term_meta( self::$category_id, 'test_multi', 'value1' );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'test_multi', $meta );
+ $this->assertInternalType( 'array', $meta['test_multi'] );
+ $this->assertContains( 'value1', $meta['test_multi'] );
+
+ // Check after an update.
+ add_term_meta( self::$category_id, 'test_multi', 'value2' );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertContains( 'value1', $meta['test_multi'] );
+ $this->assertContains( 'value2', $meta['test_multi'] );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_get_unregistered() {
+ add_term_meta( self::$category_id, 'test_unregistered', 'value1' );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayNotHasKey( 'test_unregistered', $meta );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_get_registered_no_api_access() {
+ add_term_meta( self::$category_id, 'test_no_rest', 'for_the_wicked' );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayNotHasKey( 'test_no_rest', $meta );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_get_registered_api_disabled() {
+ add_term_meta( self::$category_id, 'test_rest_disabled', 'sleepless_nights' );
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayNotHasKey( 'test_rest_disabled', $meta );
+ }
+
+ public function test_get_value_types() {
+ register_meta(
+ 'term', 'test_string', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'type' => 'string',
+ )
+ );
+ register_meta(
+ 'term', 'test_number', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'type' => 'number',
+ )
+ );
+ register_meta(
+ 'term', 'test_bool', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'type' => 'boolean',
+ )
+ );
+
+ /** @var WP_REST_Server $wp_rest_server */
+ global $wp_rest_server;
+ $wp_rest_server = new Spy_REST_Server;
+ do_action( 'rest_api_init', $wp_rest_server );
+
+ add_term_meta( self::$category_id, 'test_string', 42 );
+ add_term_meta( self::$category_id, 'test_number', '42' );
+ add_term_meta( self::$category_id, 'test_bool', 1 );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+
+ $this->assertArrayHasKey( 'test_string', $meta );
+ $this->assertInternalType( 'string', $meta['test_string'] );
+ $this->assertSame( '42', $meta['test_string'] );
+
+ $this->assertArrayHasKey( 'test_number', $meta );
+ $this->assertInternalType( 'float', $meta['test_number'] );
+ $this->assertSame( 42.0, $meta['test_number'] );
+
+ $this->assertArrayHasKey( 'test_bool', $meta );
+ $this->assertInternalType( 'boolean', $meta['test_bool'] );
+ $this->assertSame( true, $meta['test_bool'] );
+ }
+
+ public function test_get_value_custom_name() {
+ add_term_meta( self::$category_id, 'test_custom_name', 'janet' );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertArrayHasKey( 'meta', $data );
+
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'new_name', $meta );
+ $this->assertEquals( 'janet', $meta['new_name'] );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_set_value() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_single', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_single' => 'test_value',
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_single', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 1, $meta );
+ $this->assertEquals( 'test_value', $meta[0] );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'test_single', $meta );
+ $this->assertEquals( 'test_value', $meta['test_single'] );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_set_duplicate_single_value() {
+ // Start with an existing metakey and value.
+ $values = update_term_meta( self::$category_id, 'test_single', 'test_value' );
+ $this->assertEquals( 'test_value', get_term_meta( self::$category_id, 'test_single', true ) );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_single' => 'test_value',
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_single', true );
+ $this->assertNotEmpty( $meta );
+ $this->assertEquals( 'test_value', $meta );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'test_single', $meta );
+ $this->assertEquals( 'test_value', $meta['test_single'] );
+ }
+
+ /**
+ * @depends test_set_value
+ */
+ public function test_set_value_unauthenticated() {
+ $data = array(
+ 'meta' => array(
+ 'test_single' => 'test_value',
+ ),
+ );
+
+ wp_set_current_user( 0 );
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_update', $response, 401 );
+
+ // Check that the value wasn't actually updated.
+ $this->assertEmpty( get_term_meta( self::$category_id, 'test_single', false ) );
+ }
+
+ /**
+ * @depends test_set_value
+ */
+ public function test_set_value_blocked() {
+ $data = array(
+ 'meta' => array(
+ 'test_bad_auth' => 'test_value',
+ ),
+ );
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
+ $this->assertEmpty( get_term_meta( self::$category_id, 'test_bad_auth', false ) );
+ }
+
+ /**
+ * @depends test_set_value
+ */
+ public function test_set_value_db_error() {
+ $data = array(
+ 'meta' => array(
+ 'test_single' => 'test_value',
+ ),
+ );
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ /**
+ * Disable showing error as the below is going to intentionally
+ * trigger a DB error.
+ */
+ global $wpdb;
+ $wpdb->suppress_errors = true;
+ add_filter( 'query', array( $this, 'error_insert_query' ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ remove_filter( 'query', array( $this, 'error_insert_query' ) );
+ $wpdb->show_errors = true;
+
+ $this->assertErrorResponse( 'rest_meta_database_error', $response, 500 );
+ }
+
+ public function test_set_value_invalid_type() {
+ $values = get_term_meta( self::$category_id, 'test_invalid_type', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_invalid_type' => 'test_value',
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEmpty( get_term_meta( self::$category_id, 'test_invalid_type', false ) );
+ }
+
+ public function test_set_value_multiple() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_multi' => array( 'val1' ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 1, $meta );
+ $this->assertEquals( 'val1', $meta[0] );
+
+ // Add another value.
+ $data = array(
+ 'meta' => array(
+ 'test_multi' => array( 'val1', 'val2' ),
+ ),
+ );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 2, $meta );
+ $this->assertContains( 'val1', $meta );
+ $this->assertContains( 'val2', $meta );
+ }
+
+ /**
+ * Test removing only one item with duplicate items.
+ */
+ public function test_set_value_remove_one() {
+ add_term_meta( self::$category_id, 'test_multi', 'c' );
+ add_term_meta( self::$category_id, 'test_multi', 'n' );
+ add_term_meta( self::$category_id, 'test_multi', 'n' );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_multi' => array( 'c', 'n' ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 2, $meta );
+ $this->assertContains( 'c', $meta );
+ $this->assertContains( 'n', $meta );
+ }
+
+ /**
+ * @depends test_set_value_multiple
+ */
+ public function test_set_value_multiple_unauthenticated() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertEmpty( $values );
+
+ wp_set_current_user( 0 );
+
+ $data = array(
+ 'meta' => array(
+ 'test_multi' => array( 'val1' ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_update', $response, 401 );
+
+ $meta = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertEmpty( $meta );
+ }
+
+ public function test_set_value_invalid_value() {
+ register_meta(
+ 'term', 'my_meta_key', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'type' => 'string',
+ )
+ );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'my_meta_key' => array( 'c', 'n' ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
+ public function test_set_value_invalid_value_multiple() {
+ register_meta(
+ 'term', 'my_meta_key', array(
+ 'show_in_rest' => true,
+ 'single' => false,
+ 'type' => 'string',
+ )
+ );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'my_meta_key' => array( array( 'a' ) ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+ }
+
+ public function test_set_value_sanitized() {
+ register_meta(
+ 'term', 'my_meta_key', array(
+ 'show_in_rest' => true,
+ 'single' => true,
+ 'type' => 'integer',
+ )
+ );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'my_meta_key' => '1', // Set to a string.
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $this->assertEquals( 1, $data['meta']['my_meta_key'] );
+ }
+
+ public function test_set_value_csv() {
+ register_meta(
+ 'term', 'my_meta_key', array(
+ 'show_in_rest' => true,
+ 'single' => false,
+ 'type' => 'integer',
+ )
+ );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'my_meta_key' => '1,2,3', // Set to a string.
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $this->assertEquals( array( 1, 2, 3 ), $data['meta']['my_meta_key'] );
+ }
+
+ /**
+ * @depends test_set_value_multiple
+ */
+ public function test_set_value_multiple_blocked() {
+ $data = array(
+ 'meta' => array(
+ 'test_bad_auth_multi' => array( 'test_value' ),
+ ),
+ );
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
+ $this->assertEmpty( get_term_meta( self::$category_id, 'test_bad_auth_multi', false ) );
+ }
+
+ public function test_add_multi_value_db_error() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_multi' => array( 'val1' ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ /**
+ * Disable showing error as the below is going to intentionally
+ * trigger a DB error.
+ */
+ global $wpdb;
+ $wpdb->suppress_errors = true;
+ add_filter( 'query', array( $this, 'error_insert_query' ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ remove_filter( 'query', array( $this, 'error_insert_query' ) );
+ $wpdb->show_errors = true;
+
+ $this->assertErrorResponse( 'rest_meta_database_error', $response, 500 );
+ }
+
+ /**
+ * @depends test_get_value
+ */
+ public function test_set_value_single_custom_schema() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_custom_schema', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_custom_schema' => 3,
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_schema', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 1, $meta );
+ $this->assertEquals( 3, $meta[0] );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'test_custom_schema', $meta );
+ $this->assertEquals( 3, $meta['test_custom_schema'] );
+ }
+
+ public function test_set_value_multiple_custom_schema() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_custom_schema_multi', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_custom_schema_multi' => array( 2 ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_schema_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 1, $meta );
+ $this->assertEquals( 2, $meta[0] );
+
+ // Add another value.
+ $data = array(
+ 'meta' => array(
+ 'test_custom_schema_multi' => array( 2, 8 ),
+ ),
+ );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_schema_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 2, $meta );
+ $this->assertContains( 2, $meta );
+ $this->assertContains( 8, $meta );
+ }
+
+ /**
+ * @depends test_get_value_custom_name
+ */
+ public function test_set_value_custom_name() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_custom_name', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'new_name' => 'janet',
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_name', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 1, $meta );
+ $this->assertEquals( 'janet', $meta[0] );
+
+ $data = $response->get_data();
+ $meta = (array) $data['meta'];
+ $this->assertArrayHasKey( 'new_name', $meta );
+ $this->assertEquals( 'janet', $meta['new_name'] );
+ }
+
+ public function test_set_value_custom_name_multiple() {
+ // Ensure no data exists currently.
+ $values = get_term_meta( self::$category_id, 'test_custom_name_multi', false );
+ $this->assertEmpty( $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'new_name_multi' => array( 'janet' ),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_name_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 1, $meta );
+ $this->assertEquals( 'janet', $meta[0] );
+
+ // Add another value.
+ $data = array(
+ 'meta' => array(
+ 'new_name_multi' => array( 'janet', 'graeme' ),
+ ),
+ );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_name_multi', false );
+ $this->assertNotEmpty( $meta );
+ $this->assertCount( 2, $meta );
+ $this->assertContains( 'janet', $meta );
+ $this->assertContains( 'graeme', $meta );
+ }
+
+ /**
+ * @ticket 38989
+ */
+ public function test_set_value_invalid_meta_string_request_type() {
+ update_term_meta( self::$category_id, 'test_single', 'So I tied an onion to my belt, which was the style at the time.' );
+ $term_original = get_term( self::$category_id );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'name' => 'Ignore this name',
+ 'meta' => 'Not an array.',
+ );
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
+ // The meta value should not have changed.
+ $current_value = get_term_meta( self::$category_id, 'test_single', true );
+ $this->assertEquals( 'So I tied an onion to my belt, which was the style at the time.', $current_value );
+
+ // Ensure the term name update was not processed.
+ $term_updated = get_term( self::$category_id );
+ $this->assertEquals( $term_original->name, $term_updated->name );
+ }
+
+ /**
+ * @ticket 38989
+ */
+ public function test_set_value_invalid_meta_float_request_type() {
+ update_term_meta( self::$category_id, 'test_single', 'Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on them.' );
+ $term_original = get_term( self::$category_id );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'name' => 'Ignore this name',
+ 'meta' => 1.234,
+ );
+
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
+ // The meta value should not have changed.
+ $current_value = get_term_meta( self::$category_id, 'test_single', true );
+ $this->assertEquals( 'Now, to take the ferry cost a nickel, and in those days, nickels had pictures of bumblebees on them.', $current_value );
+
+ // Ensure the term name update was not processed.
+ $term_updated = get_term( self::$category_id );
+ $this->assertEquals( $term_original->name, $term_updated->name );
+ }
+
+ public function test_remove_multi_value_db_error() {
+ add_term_meta( self::$category_id, 'test_multi', 'val1' );
+ $values = get_term_meta( self::$category_id, 'test_multi', false );
+ $this->assertEquals( array( 'val1' ), $values );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_multi' => array(),
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ /**
+ * Disable showing error as the below is going to intentionally
+ * trigger a DB error.
+ */
+ global $wpdb;
+ $wpdb->suppress_errors = true;
+ add_filter( 'query', array( $this, 'error_delete_query' ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ remove_filter( 'query', array( $this, 'error_delete_query' ) );
+ $wpdb->show_errors = true;
+
+ $this->assertErrorResponse( 'rest_meta_database_error', $response, 500 );
+ }
+
+
+ public function test_delete_value() {
+ add_term_meta( self::$category_id, 'test_single', 'val1' );
+ $current = get_term_meta( self::$category_id, 'test_single', true );
+ $this->assertEquals( 'val1', $current );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_single' => null,
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_single', false );
+ $this->assertEmpty( $meta );
+ }
+
+ /**
+ * @depends test_delete_value
+ */
+ public function test_delete_value_blocked() {
+ add_term_meta( self::$category_id, 'test_bad_auth', 'val1' );
+ $current = get_term_meta( self::$category_id, 'test_bad_auth', true );
+ $this->assertEquals( 'val1', $current );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_bad_auth' => null,
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
+
+ $meta = get_term_meta( self::$category_id, 'test_bad_auth', true );
+ $this->assertEquals( 'val1', $meta );
+ }
+
+ /**
+ * @depends test_delete_value
+ */
+ public function test_delete_value_db_error() {
+ add_term_meta( self::$category_id, 'test_single', 'val1' );
+ $current = get_term_meta( self::$category_id, 'test_single', true );
+ $this->assertEquals( 'val1', $current );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'test_single' => null,
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+ /**
+ * Disable showing error as the below is going to intentionally
+ * trigger a DB error.
+ */
+ global $wpdb;
+ $wpdb->suppress_errors = true;
+ add_filter( 'query', array( $this, 'error_delete_query' ) );
+
+ $response = rest_get_server()->dispatch( $request );
+ remove_filter( 'query', array( $this, 'error_delete_query' ) );
+ $wpdb->show_errors = true;
+
+ $this->assertErrorResponse( 'rest_meta_database_error', $response, 500 );
+ }
+
+ public function test_delete_value_custom_name() {
+ add_term_meta( self::$category_id, 'test_custom_name', 'janet' );
+ $current = get_term_meta( self::$category_id, 'test_custom_name', true );
+ $this->assertEquals( 'janet', $current );
+
+ $this->grant_write_permission();
+
+ $data = array(
+ 'meta' => array(
+ 'new_name' => null,
+ ),
+ );
+ $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $request->set_body_params( $data );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $meta = get_term_meta( self::$category_id, 'test_custom_name', false );
+ $this->assertEmpty( $meta );
+ }
+
+ public function test_get_schema() {
+ $request = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/categories/%d', self::$category_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $data = $response->get_data();
+ $schema = $data['schema'];
+
+ $this->assertArrayHasKey( 'meta', $schema['properties'] );
+ $meta_schema = $schema['properties']['meta']['properties'];
+
+ $this->assertArrayHasKey( 'test_single', $meta_schema );
+ $this->assertEquals( 'string', $meta_schema['test_single']['type'] );
+
+ $this->assertArrayHasKey( 'test_multi', $meta_schema );
+ $this->assertEquals( 'array', $meta_schema['test_multi']['type'] );
+ $this->assertArrayHasKey( 'items', $meta_schema['test_multi'] );
+ $this->assertEquals( 'string', $meta_schema['test_multi']['items']['type'] );
+
+ $this->assertArrayHasKey( 'test_custom_schema', $meta_schema );
+ $this->assertEquals( 'number', $meta_schema['test_custom_schema']['type'] );
+
+ $this->assertArrayNotHasKey( 'test_no_rest', $meta_schema );
+ $this->assertArrayNotHasKey( 'test_rest_disabled', $meta_schema );
+ $this->assertArrayNotHasKey( 'test_invalid_type', $meta_schema );
+ $this->assertArrayNotHasKey( 'test_no_type', $meta_schema );
+ }
+
+ /**
+ * Internal function used to disable an insert query which
+ * will trigger a wpdb error for testing purposes.
+ */
+ public function error_insert_query( $query ) {
+ if ( strpos( $query, 'INSERT' ) === 0 ) {
+ $query = '],';
+ }
+ return $query;
+ }
+
+ /**
+ * Internal function used to disable an insert query which
+ * will trigger a wpdb error for testing purposes.
+ */
+ public function error_delete_query( $query ) {
+ if ( strpos( $query, 'DELETE' ) === 0 ) {
+ $query = '],';
+ }
+ return $query;
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsrestapirestuserscontrollerphp"></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-users-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-users-controller.php 2018-06-11 16:32:32 UTC (rev 43339)
+++ trunk/tests/phpunit/tests/rest-api/rest-users-controller.php 2018-06-11 17:54:19 UTC (rev 43340)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -884,7 +884,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_user_invalid_id() {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$user );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'GET', '/wp/v2/users/100' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'GET', '/wp/v2/users/7777' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2248,7 +2248,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->allow_user_to_manage_multisite();
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$user );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/100' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request = new WP_REST_Request( 'DELETE', '/wp/v2/users/7777' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $request['force'] = true;
</span><span class="cx" style="display: block; padding: 0 10px"> $request->set_param( 'reassign', false );
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2300,7 +2300,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/users/%d', $user_id ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $request['force'] = true;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $request->set_param( 'reassign', 100 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $request->set_param( 'reassign', 7777 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Not implemented in multisite.
</span></span></pre>
</div>
</div>
</body>
</html>