Add term meta support to XML-RPC addTerm and editTerm endpoints.

This changeset also includes the new function `has_term_meta()`, a
counterpart to `has_meta()` (for posts).

Props enrico.sorcinelli.
Fixes #35991.

git-svn-id: https://develop.svn.wordpress.org/trunk@40916 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2017-06-18 10:39:12 +00:00
parent 21e3c92742
commit 03cd8d5b70
7 changed files with 253 additions and 0 deletions
+34
View File
@@ -404,6 +404,40 @@ class Tests_Term_Meta extends WP_UnitTestCase {
$this->assertSame( '', get_term_meta( $t, 'foo1', true ) );
}
/**
* @ticket 35991
*/
public function test_has_term_meta() {
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$term_meta_id = add_term_meta( $t, 'foo', 'bar' );
$meta = has_term_meta( $t );
$this->assertSame( 1, count( $meta ) );
$expected = array(
'meta_key' => 'foo',
'meta_value' => 'bar',
'meta_id' => $term_meta_id,
'term_id' => $t,
);
$found = $meta[0];
$this->assertEquals( $expected, $found );
}
/**
* @ticket 35991
*/
public function test_has_term_meta_empty_results() {
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$meta = has_term_meta( $t );
$this->assertSame( array(), $meta );
}
public static function set_cache_results( $q ) {
$q->set( 'cache_results', true );
}
@@ -145,4 +145,74 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
$this->assertNotIXRError( $result );
$this->assertInternalType( 'boolean', $result );
}
/**
* @ticket 35991
*/
public function test_update_term_meta() {
register_taxonomy( 'wptests_tax', 'post' );
$t = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$meta_id = add_term_meta( $t, 'foo', 'bar' );
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array(
1,
'editor',
'editor',
$t,
array(
'taxonomy' => 'wptests_tax',
'custom_fields' => array(
array(
'id' => $meta_id,
'key' => 'foo',
'value' => 'baz',
),
),
),
) );
$this->assertNotIXRError( $result );
$found = get_term_meta( $t, 'foo', true );
$this->assertSame( 'baz', $found );
}
/**
* @ticket 35991
*/
public function test_delete_term_meta() {
register_taxonomy( 'wptests_tax', 'post' );
$t = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$meta_id = add_term_meta( $t, 'foo', 'bar' );
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array(
1,
'editor',
'editor',
$t,
array(
'taxonomy' => 'wptests_tax',
'custom_fields' => array(
array(
'id' => $meta_id,
),
),
),
) );
$this->assertNotIXRError( $result );
$found = get_term_meta( $t, 'foo' );
$this->assertSame( array(), $found );
}
}
+26
View File
@@ -69,6 +69,7 @@ class Tests_XMLRPC_wp_getTerm extends WP_XMLRPC_UnitTestCase {
$this->make_user_by_role( 'editor' );
$term = get_term( self::$term_id, 'category', ARRAY_A );
$term['custom_fields'] = array();
$result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', self::$term_id ) );
@@ -95,4 +96,29 @@ class Tests_XMLRPC_wp_getTerm extends WP_XMLRPC_UnitTestCase {
$this->assertEquals( 'category', $result['taxonomy'] );
$this->assertEquals( $term['description'], $result['description'] );
}
/**
* @ticket 35991
*/
public function test_get_term_meta() {
$this->make_user_by_role( 'editor' );
// Add term meta to test wp.getTerm.
add_term_meta( self::$term_id, 'foo', 'bar' );
$term = get_term( self::$term_id, 'category', ARRAY_A );
$result = $this->myxmlrpcserver->wp_getTerm( array(
1,
'editor',
'editor',
'category',
self::$term_id,
) );
$this->assertNotIXRError( $result );
$this->assertInternalType( 'array', $result['custom_fields'] );
$term_meta = get_term_meta( self::$term_id, '', true );
$this->assertEquals( $term_meta['foo'][0], $result['custom_fields'][0]['value'] );
}
}
@@ -50,6 +50,9 @@ class Tests_XMLRPC_wp_getTerms extends WP_XMLRPC_UnitTestCase {
foreach( $results as $term ) {
$this->assertInternalType( 'int', $term['count'] );
// Check custom term meta
$this->assertInternalType( 'array', $term['custom_fields'] );
// We expect all other IDs to be strings not integers so we don't return something larger than an XMLRPC integer can describe.
$this->assertStringMatchesFormat( '%d', $term['term_id'] );
$this->assertStringMatchesFormat( '%d', $term['term_group'] );
+24
View File
@@ -106,4 +106,28 @@ class Tests_XMLRPC_wp_newTerm extends WP_XMLRPC_UnitTestCase {
$this->assertNotIXRError( $result );
$this->assertStringMatchesFormat( '%d', $result );
}
/**
* @ticket 35991
*/
public function test_add_term_meta() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_newTerm( array(
1,
'editor',
'editor',
array(
'taxonomy' => 'category',
'name' => 'Test meta',
'custom_fields' => array(
array(
'key' => 'key1',
'value' => 'value1',
),
),
),
) );
$this->assertNotIXRError( $result );
$this->assertStringMatchesFormat( '%d', $result );
}
}