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
+239 -163
View File
@@ -14,11 +14,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
$term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
$term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_2' ) );
$q = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_2',
'fields' => 'ids',
'hide_empty' => false,
) );
$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_2',
'fields' => 'ids',
'hide_empty' => false,
)
);
$this->assertEqualSets( array( $term_2 ), $q->terms );
}
@@ -30,11 +32,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
$term_1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
$term_2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_2' ) );
$q = new WP_Term_Query( array(
'taxonomy' => array( 'wptests_tax_2' ),
'fields' => 'ids',
'hide_empty' => false,
) );
$q = new WP_Term_Query(
array(
'taxonomy' => array( 'wptests_tax_2' ),
'fields' => 'ids',
'hide_empty' => false,
)
);
$this->assertEqualSets( array( $term_2 ), $q->terms );
}
@@ -56,11 +60,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
array( 'term_id' => $terms[0] )
);
$q = new WP_Term_Query( array(
'term_taxonomy_id' => 12345,
'fields' => 'ids',
'hide_empty' => false,
) );
$q = new WP_Term_Query(
array(
'term_taxonomy_id' => 12345,
'fields' => 'ids',
'hide_empty' => false,
)
);
$this->assertEqualSets( array( $terms[0] ), $q->terms );
}
@@ -88,11 +94,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
array( 'term_id' => $terms[2] )
);
$q = new WP_Term_Query( array(
'term_taxonomy_id' => array( 12345, 6789 ),
'fields' => 'ids',
'hide_empty' => false,
) );
$q = new WP_Term_Query(
array(
'term_taxonomy_id' => array( 12345, 6789 ),
'fields' => 'ids',
'hide_empty' => false,
)
);
$this->assertEqualSets( array( $terms[0], $terms[2] ), $q->terms );
}
@@ -109,13 +117,15 @@ class Tests_Term_Query extends WP_UnitTestCase {
add_term_meta( $terms[1], 'foo', 1 );
add_term_meta( $terms[2], 'foo', 100 );
$q = new WP_Term_Query( array(
'taxonomy' => array( 'wptests_tax' ),
'fields' => 'ids',
'hide_empty' => false,
'meta_key' => 'foo',
'orderby' => 'meta_value_num',
) );
$q = new WP_Term_Query(
array(
'taxonomy' => array( 'wptests_tax' ),
'fields' => 'ids',
'hide_empty' => false,
'meta_key' => 'foo',
'orderby' => 'meta_value_num',
)
);
$found = array_map( 'intval', $q->terms );
$this->assertSame( array( $terms[1], $terms[0], $terms[2] ), $found );
@@ -128,10 +138,12 @@ class Tests_Term_Query extends WP_UnitTestCase {
register_taxonomy( 'wptests_tax', 'post' );
add_filter( 'terms_clauses', array( $this, 'filter_terms_clauses' ) );
$q = new WP_Term_Query( array(
'taxonomy' => array( 'wptests_tax' ),
'orderby' => 'name',
) );
$q = new WP_Term_Query(
array(
'taxonomy' => array( 'wptests_tax' ),
'orderby' => 'name',
)
);
remove_filter( 'terms_clauses', array( $this, 'filter_terms_clauses' ) );
$this->assertContains( 'ORDER BY tt.term_id', $q->request );
@@ -151,12 +163,14 @@ class Tests_Term_Query extends WP_UnitTestCase {
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
sort( $terms );
$q = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax',
'orderby' => 'term_order',
'fields' => 'ids',
'hide_empty' => false,
) );
$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax',
'orderby' => 'term_order',
'fields' => 'ids',
'hide_empty' => false,
)
);
$this->assertSame( $terms, $q->get_terms() );
}
@@ -169,17 +183,21 @@ class Tests_Term_Query extends WP_UnitTestCase {
self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
$q1 = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'hide_empty' => false
) );
$q1 = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'hide_empty' => false,
)
);
$this->assertNotEmpty( $q1->terms );
$q2 = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'hide_empty' => false
) );
$q2 = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'hide_empty' => false,
)
);
$this->assertNotEmpty( $q2->terms );
}
@@ -194,11 +212,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
$t1 = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax_1' ) );
$t2 = self::factory()->term->create_and_get( array( 'taxonomy' => 'wptests_tax_1' ) );
$query = new WP_Term_Query( array(
'include' => "{$t1->term_id},{$t2->term_id}",
'orderby' => 'include',
'hide_empty' => false,
) );
$query = new WP_Term_Query(
array(
'include' => "{$t1->term_id},{$t2->term_id}",
'orderby' => 'include',
'hide_empty' => false,
)
);
$terms = $query->get_terms();
$this->assertEquals( array( $t1, $t2 ), $terms );
@@ -215,11 +235,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
)
);
$this->assertEqualSets( array( $t ), $query->terms );
}
@@ -235,11 +257,13 @@ class Tests_Term_Query extends WP_UnitTestCase {
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => array( $p ),
'fields' => 'ids',
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => array( $p ),
'fields' => 'ids',
)
);
$this->assertEqualSets( array( $t ), $query->terms );
}
@@ -250,17 +274,19 @@ class Tests_Term_Query extends WP_UnitTestCase {
public function test_duplicates_should_be_removed_for_fields_all() {
register_taxonomy( 'wptests_tax_1', 'post' );
$posts = self::factory()->post->create_many( 2 );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
foreach ( $posts as $p ) {
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
}
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $posts,
'fields' => 'all',
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $posts,
'fields' => 'all',
)
);
$this->assertSame( 1, count( $query->terms ) );
$this->assertSame( $t, reset( $query->terms )->term_id );
@@ -272,17 +298,19 @@ class Tests_Term_Query extends WP_UnitTestCase {
public function test_duplicates_should_not_be_removed_for_fields_all_with_object_id() {
register_taxonomy( 'wptests_tax_1', 'post' );
$posts = self::factory()->post->create_many( 2 );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) );
foreach ( $posts as $p ) {
wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' );
}
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $posts,
'fields' => 'all_with_object_id',
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $posts,
'fields' => 'all_with_object_id',
)
);
$this->assertSame( 2, count( $query->terms ) );
foreach ( $query->terms as $term ) {
@@ -297,27 +325,31 @@ class Tests_Term_Query extends WP_UnitTestCase {
public function test_object_ids_cache_should_be_invalidated_by_term_relationship_change() {
register_taxonomy( 'wptests_tax_1', 'post' );
$p = self::factory()->post->create();
$p = self::factory()->post->create();
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
wp_set_object_terms( $p, array( $terms[0] ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
)
);
$found = $query->get_terms();
$this->assertEqualSets( array( $terms[0] ), $found );
wp_set_object_terms( $p, array( $terms[1] ), 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'object_ids' => $p,
'fields' => 'ids',
)
);
$found = $query->get_terms();
$this->assertEqualSets( array( $terms[1] ), $found );
@@ -334,21 +366,25 @@ class Tests_Term_Query extends WP_UnitTestCase {
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
)
);
$count = $query->get_terms();
$this->assertEquals( 2, $count );
$num_queries = $wpdb->num_queries;
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
)
);
$count = $query->get_terms();
$this->assertEquals( 2, $count );
$this->assertSame( $num_queries, $wpdb->num_queries );
@@ -363,21 +399,25 @@ class Tests_Term_Query extends WP_UnitTestCase {
$terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax_1' ) );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
)
);
$count = $query->get_terms();
$this->assertEquals( 2, $count );
wp_delete_term( $terms[0], 'wptests_tax_1' );
$query = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
) );
$query = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax_1',
'fields' => 'count',
'hide_empty' => false,
)
);
$count = $query->get_terms();
$this->assertEquals( 1, $count );
}
@@ -386,16 +426,20 @@ class Tests_Term_Query extends WP_UnitTestCase {
* @ticket 40496
*/
public function test_get_the_terms_should_respect_taxonomy_orderby() {
register_taxonomy( 'wptests_tax', 'post', array(
'sort' => true,
'args' => array(
'orderby' => 'term_order',
),
) );
$term_ids = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$post_id = self::factory()->post->create();
register_taxonomy(
'wptests_tax', 'post', array(
'sort' => true,
'args' => array(
'orderby' => 'term_order',
),
)
);
$term_ids = self::factory()->term->create_many(
2, array(
'taxonomy' => 'wptests_tax',
)
);
$post_id = self::factory()->post->create();
wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' );
$terms = get_the_terms( $post_id, 'wptests_tax' );
$this->assertEquals( array( $term_ids[0], $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) );
@@ -409,16 +453,20 @@ class Tests_Term_Query extends WP_UnitTestCase {
* @ticket 40496
*/
public function test_wp_get_object_terms_should_respect_taxonomy_orderby() {
register_taxonomy( 'wptests_tax', 'post', array(
'sort' => true,
'args' => array(
'orderby' => 'term_order',
),
) );
$term_ids = self::factory()->term->create_many( 2, array(
'taxonomy' => 'wptests_tax',
) );
$post_id = self::factory()->post->create();
register_taxonomy(
'wptests_tax', 'post', array(
'sort' => true,
'args' => array(
'orderby' => 'term_order',
),
)
);
$term_ids = self::factory()->term->create_many(
2, array(
'taxonomy' => 'wptests_tax',
)
);
$post_id = self::factory()->post->create();
wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' );
$terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) );
$this->assertEquals( array( $term_ids[0], $term_ids[1], 1 ), wp_list_pluck( $terms, 'term_id' ) );
@@ -434,38 +482,58 @@ class Tests_Term_Query extends WP_UnitTestCase {
public function test_should_allow_same_args_with_the_get_terms() {
register_post_type( 'wptests_pt' );
register_taxonomy( 'wptests_tax', 'wptests_pt' );
$t1 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
'name' => 'foo',
'slug' => 'bar',
) );
$t2 = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
'name' => 'bar',
'slug' => 'foo',
) );
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'foo',
'slug' => 'bar',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'bar',
'slug' => 'foo',
)
);
$p = self::factory()->post->create( array(
'post_type' => 'wptests_pt',
) );
$p = self::factory()->post->create(
array(
'post_type' => 'wptests_pt',
)
);
wp_set_object_terms( $p, array( $t1, $t2 ), 'wptests_tax' );
$expected = wp_get_post_terms( $p, 'wptests_tax', array(
'fields' => 'ids',
) );
$expected = wp_get_post_terms(
$p, 'wptests_tax', array(
'fields' => 'ids',
)
);
$found1 = array_keys( wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'id=>parent',
) ) );
$found1 = array_keys(
wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'id=>parent',
)
)
);
$found2 = array_keys( wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'id=>slug',
) ) );
$found2 = array_keys(
wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'id=>slug',
)
)
);
$found3 = array_keys( wp_get_object_terms( $p, 'wptests_tax', array(
'fields' => 'id=>name',
) ) );
$found3 = array_keys(
wp_get_object_terms(
$p, 'wptests_tax', array(
'fields' => 'id=>name',
)
)
);
$this->assertSame( $expected, $found1 );
$this->assertSame( $expected, $found2 );
@@ -481,12 +549,14 @@ class Tests_Term_Query extends WP_UnitTestCase {
$post_id = self::factory()->post->create();
register_taxonomy( 'wptests_tax', 'post' );
$term_id = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$term_id = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
)
);
wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
$q = new WP_Term_Query();
$q = new WP_Term_Query();
$args = array(
'taxonomy' => 'wptests_tax',
'parent' => $term_id,
@@ -504,12 +574,14 @@ class Tests_Term_Query extends WP_UnitTestCase {
$post_id = self::factory()->post->create();
register_taxonomy( 'wptests_tax', 'post' );
$term_id = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$term_id = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
)
);
wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
$q = new WP_Term_Query();
$q = new WP_Term_Query();
$args = array(
'taxonomy' => 'wptests_tax',
'child_of' => $term_id,
@@ -527,15 +599,19 @@ class Tests_Term_Query extends WP_UnitTestCase {
$post_id = self::factory()->post->create();
register_taxonomy( 'wptests_tax', 'post' );
$term_id = self::factory()->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$term_id = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
)
);
wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
$q = new WP_Term_Query( array(
'taxonomy' => 'wptests_tax',
'parent' => $term_id,
) );
$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax',
'parent' => $term_id,
)
);
$this->assertSame( array(), $q->terms );
}
}