Code is Poetry.

WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.



git-svn-id: https://develop.svn.wordpress.org/trunk@42343 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2017-11-30 23:09:33 +00:00
parent ec6a089f98
commit 8f95800d52
1103 changed files with 105978 additions and 78184 deletions
+406 -257
View File
@@ -15,15 +15,20 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_get_object_terms_by_slug() {
$post_id = self::factory()->post->create();
$terms_1 = array('Foo', 'Bar', 'Baz');
$terms_1_slugs = array('foo', 'bar', 'baz');
$terms_1 = array( 'Foo', 'Bar', 'Baz' );
$terms_1_slugs = array( 'foo', 'bar', 'baz' );
// set the initial terms
$tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
$this->assertEquals( 3, count($tt_1) );
$this->assertEquals( 3, count( $tt_1 ) );
// make sure they're correct
$terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 'term_id'));
$terms = wp_get_object_terms(
$post_id, $this->taxonomy, array(
'fields' => 'slugs',
'orderby' => 'term_id',
)
);
$this->assertEquals( $terms_1_slugs, $terms );
}
@@ -33,8 +38,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_should_not_filter_out_duplicate_terms_associated_with_different_objects() {
$post_id1 = self::factory()->post->create();
$post_id2 = self::factory()->post->create();
$cat_id = self::factory()->category->create();
$cat_id2 = self::factory()->category->create();
$cat_id = self::factory()->category->create();
$cat_id2 = self::factory()->category->create();
wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) );
wp_set_post_categories( $post_id2, $cat_id );
@@ -42,9 +47,11 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$this->assertCount( 2, $terms );
$this->assertEquals( array( $cat_id, $cat_id2 ), wp_list_pluck( $terms, 'term_id' ) );
$terms2 = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category', array(
'fields' => 'all_with_object_id'
) );
$terms2 = wp_get_object_terms(
array( $post_id1, $post_id2 ), 'category', array(
'fields' => 'all_with_object_id',
)
);
$this->assertCount( 3, $terms2 );
$this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) );
@@ -55,17 +62,18 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_should_return_objects_with_int_properties() {
$post_id = self::factory()->post->create();
$term = wp_insert_term( 'one', $this->taxonomy );
$term = wp_insert_term( 'one', $this->taxonomy );
wp_set_object_terms( $post_id, $term, $this->taxonomy );
$terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'all_with_object_id' ) );
$term = array_shift( $terms );
$terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'all_with_object_id' ) );
$term = array_shift( $terms );
$int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
foreach ( $int_fields as $field )
foreach ( $int_fields as $field ) {
$this->assertInternalType( 'int', $term->$field, $field );
}
$terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'ids' ) );
$term = array_shift( $terms );
$term = array_shift( $terms );
$this->assertInternalType( 'int', $term, 'term' );
}
@@ -74,7 +82,7 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
*/
public function test_references_should_be_reset_after_wp_get_object_terms_filter() {
$post_id = self::factory()->post->create();
$terms_1 = array('foo', 'bar', 'baz');
$terms_1 = array( 'foo', 'bar', 'baz' );
wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
add_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
@@ -107,25 +115,33 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_name() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
)
);
wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'name',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'name',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -133,27 +149,35 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_count() {
$posts = self::factory()->post->create_many( 3 );
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
)
);
wp_set_object_terms( $posts[0], array( $t3, $t2, $t1 ), $this->taxonomy );
wp_set_object_terms( $posts[1], array( $t3, $t1 ), $this->taxonomy );
wp_set_object_terms( $posts[2], array( $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $posts[0], $this->taxonomy, array(
'orderby' => 'count',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$posts[0], $this->taxonomy, array(
'orderby' => 'count',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t2, $t1, $t3 ), $found );
}
@@ -161,25 +185,33 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_slug() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'slug' => 'aaa',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'slug' => 'zzz',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'slug' => 'jjj',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'slug' => 'aaa',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'slug' => 'zzz',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'slug' => 'jjj',
)
);
wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'slug',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'slug',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -187,15 +219,21 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_term_group() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
// No great way to do this in the API.
global $wpdb;
@@ -205,10 +243,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'term_group',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'term_group',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -216,15 +256,21 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_term_order() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
@@ -234,14 +280,31 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$term_3 = get_term( $t3, $this->taxonomy );
global $wpdb;
$wpdb->update( $wpdb->term_relationships, array( 'term_order' => 1 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id, 'object_id' => $p ) );
$wpdb->update( $wpdb->term_relationships, array( 'term_order' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id, 'object_id' => $p ) );
$wpdb->update( $wpdb->term_relationships, array( 'term_order' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id, 'object_id' => $p ) );
$wpdb->update(
$wpdb->term_relationships, array( 'term_order' => 1 ), array(
'term_taxonomy_id' => $term_1->term_taxonomy_id,
'object_id' => $p,
)
);
$wpdb->update(
$wpdb->term_relationships, array( 'term_order' => 3 ), array(
'term_taxonomy_id' => $term_2->term_taxonomy_id,
'object_id' => $p,
)
);
$wpdb->update(
$wpdb->term_relationships, array( 'term_order' => 2 ), array(
'term_taxonomy_id' => $term_3->term_taxonomy_id,
'object_id' => $p,
)
);
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'term_order',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'term_order',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -252,15 +315,21 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_parent() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
@@ -273,10 +342,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'parent',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'parent',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -290,24 +361,32 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax_3',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax_2',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_3',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_2',
)
);
wp_set_object_terms( $p, $t1, $this->taxonomy );
wp_set_object_terms( $p, $t2, 'wptests_tax_3' );
wp_set_object_terms( $p, $t3, 'wptests_tax_2' );
$found = wp_get_object_terms( $p, array( $this->taxonomy, 'wptests_tax_2', 'wptests_tax_3' ), array(
'orderby' => 'taxonomy',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, array( $this->taxonomy, 'wptests_tax_2', 'wptests_tax_3' ), array(
'orderby' => 'taxonomy',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -318,15 +397,21 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_orderby_tt_id() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
// term_taxonomy_id will only have a different order from term_id in legacy situations.
$term_1 = get_term( $t1, $this->taxonomy );
@@ -340,10 +425,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'term_taxonomy_id',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'term_taxonomy_id',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t1, $t3, $t2 ), $found );
}
@@ -351,26 +438,34 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_order_desc() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
)
);
wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'orderby' => 'name',
'order' => 'DESC',
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'orderby' => 'name',
'order' => 'DESC',
'fields' => 'ids',
)
);
$this->assertEquals( array( $t2, $t3, $t1 ), $found );
}
@@ -379,33 +474,45 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 15675
*/
public function test_parent() {
register_taxonomy( 'wptests_tax2', 'post', array(
'hierarchical' => true,
) );
register_taxonomy(
'wptests_tax2', 'post', array(
'hierarchical' => true,
)
);
$t1 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax2',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax2',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax2',
'parent' => $t1,
) );
$t4 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax2',
'parent' => $t2,
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
'parent' => $t1,
)
);
$t4 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax2',
'parent' => $t2,
)
);
$p = self::factory()->post->create();
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), 'wptests_tax2' );
$found = wp_get_object_terms( $p, 'wptests_tax2', array(
'parent' => $t1,
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax2', array(
'parent' => $t1,
'fields' => 'ids',
)
);
$this->assertEquals( array( $t3 ), $found );
}
@@ -414,29 +521,39 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
* @ticket 15675
*/
public function test_parent_0() {
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'parent' => $t1,
) );
$t4 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'parent' => $t2,
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'parent' => $t1,
)
);
$t4 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'parent' => $t2,
)
);
$p = self::factory()->post->create();
wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
$found = wp_get_object_terms( $p, $this->taxonomy, array(
'parent' => 0,
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, $this->taxonomy, array(
'parent' => 0,
'fields' => 'ids',
)
);
$this->assertEqualSets( array( $t1, $t2 ), $found );
}
@@ -482,9 +599,11 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$p = self::factory()->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'update_term_meta_cache' => false,
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'update_term_meta_cache' => false,
)
);
$num_queries = $wpdb->num_queries;
@@ -510,10 +629,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$p = self::factory()->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'update_term_meta_cache' => true,
'fields' => 'all_with_object_id',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'update_term_meta_cache' => true,
'fields' => 'all_with_object_id',
)
);
$num_queries = $wpdb->num_queries;
@@ -539,10 +660,12 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$p = self::factory()->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'update_term_meta_cache' => true,
'fields' => 'ids',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'update_term_meta_cache' => true,
'fields' => 'ids',
)
);
$num_queries = $wpdb->num_queries;
@@ -567,14 +690,16 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$p = self::factory()->post->create();
wp_set_object_terms( $p, $terms, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
),
) );
)
);
$this->assertEqualSets( array( $terms[0], $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
}
@@ -588,9 +713,11 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'all',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'all',
)
);
$this->assertNotEmpty( $found );
foreach ( $found as $f ) {
@@ -607,9 +734,11 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'all_with_object_id',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'all_with_object_id',
)
);
$this->assertNotEmpty( $found );
foreach ( $found as $f ) {
@@ -628,12 +757,14 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'all_with_object_id',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'all_with_object_id',
)
);
$num_queries = $wpdb->num_queries;
$term = get_term( $t );
$term = get_term( $t );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
@@ -646,9 +777,11 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $p, $t, 'wptests_tax' );
$found = wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'all_with_object_id',
) );
$found = wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'all_with_object_id',
)
);
foreach ( $found as $f ) {
$this->assertSame( $p, $f->object_id );
@@ -666,24 +799,26 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
register_taxonomy( 'wptests_tax1', 'post' );
register_taxonomy( 'wptests_tax2', 'post' );
$p = self::factory()->post->create();
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
$t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
wp_set_object_terms( $p, $t1, 'wptests_tax1' );
wp_set_object_terms( $p, $t2, 'wptests_tax2' );
$found = wp_get_object_terms( $p, array(
'wptests_tax1',
'wptests_tax2',
), array(
'fields' => 'all_with_object_id',
) );
$found = wp_get_object_terms(
$p, array(
'wptests_tax1',
'wptests_tax2',
), array(
'fields' => 'all_with_object_id',
)
);
$this->assertEqualSets( array( $t1, $t2 ), wp_list_pluck( $found, 'term_id' ) );
$num_queries = $wpdb->num_queries;
$term1 = get_term( $t1 );
$term2 = get_term( $t2 );
$term1 = get_term( $t1 );
$term2 = get_term( $t2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
@@ -693,13 +828,15 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_object_id_should_be_set_on_objects_that_share_terms() {
register_taxonomy( 'wptests_tax', 'post' );
$posts = self::factory()->post->create_many( 2 );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $posts[0], $t, 'wptests_tax' );
wp_set_object_terms( $posts[1], $t, 'wptests_tax' );
$found = wp_get_object_terms( $posts, 'wptests_tax', array(
'fields' => 'all_with_object_id',
) );
$found = wp_get_object_terms(
$posts, 'wptests_tax', array(
'fields' => 'all_with_object_id',
)
);
$this->assertEqualSets( $posts, wp_list_pluck( $found, 'object_id' ) );
}
@@ -713,18 +850,24 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
public function test_verify_args_parameter_can_be_string() {
$p = self::factory()->post->create();
$t1 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
) );
$t3 = self::factory()->term->create( array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'AAA',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'ZZZ',
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => $this->taxonomy,
'name' => 'JJJ',
)
);
wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
@@ -741,8 +884,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
register_taxonomy( $taxonomy, 'post', array( 'sort' => 'true' ) );
$post_id = self::factory()->post->create();
$terms = array( 'foo', 'bar', 'baz' );
$set = wp_set_object_terms( $post_id, $terms, $taxonomy );
$terms = array( 'foo', 'bar', 'baz' );
$set = wp_set_object_terms( $post_id, $terms, $taxonomy );
// Filter for maintaining term order
add_filter( 'wp_get_object_terms_args', array( $this, 'filter_wp_get_object_terms_args' ), 10, 3 );
@@ -756,7 +899,7 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
$this->assertEquals( implode( ',', $terms ), $terms_to_edit );
}
function filter_wp_get_object_terms_args ( $args, $object_ids, $taxonomies ) {
function filter_wp_get_object_terms_args( $args, $object_ids, $taxonomies ) {
$args['orderby'] = 'term_order';
return $args;
}
@@ -776,23 +919,29 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
register_taxonomy( $taxonomy1, 'post', $taxonomy_arguments );
register_taxonomy( $taxonomy2, 'post', $taxonomy_arguments );
$post_id = self::factory()->post->create();
$term_1_id = self::factory()->term->create( array(
'taxonomy' => $taxonomy1,
) );
$term_2_id = self::factory()->term->create( array(
'taxonomy' => $taxonomy2,
) );
$post_id = self::factory()->post->create();
$term_1_id = self::factory()->term->create(
array(
'taxonomy' => $taxonomy1,
)
);
$term_2_id = self::factory()->term->create(
array(
'taxonomy' => $taxonomy2,
)
);
wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 );
wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 );
$expected = array( $term_1_id, $term_2_id );
$actual = wp_get_object_terms( $post_id, array( $taxonomy1, $taxonomy2 ), array(
'orderby' => 'term_id',
'fields' => 'ids',
) );
$actual = wp_get_object_terms(
$post_id, array( $taxonomy1, $taxonomy2 ), array(
'orderby' => 'term_id',
'fields' => 'ids',
)
);
$this->assertEqualSets( $expected, $actual );
}