Add classes for custom taxonomy terms in get_post_class().

Props sillybean.
Fixes #16223.

git-svn-id: https://develop.svn.wordpress.org/trunk@31271 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-01-23 15:40:31 +00:00
parent 10d3b42211
commit 8ec0792557
2 changed files with 89 additions and 20 deletions

View File

@@ -0,0 +1,67 @@
<?php
/**
* @group post
* @covers ::get_post_class
*/
class Tests_Post_GetPostClass extends WP_UnitTestCase {
protected $post_id;
public function setUp() {
parent::setUp();
$this->post_id = $this->factory->post->create();
}
public function test_with_tags() {
wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'post_tag' );
$found = get_post_class( '', $this->post_id );
$this->assertContains( 'tag-foo', $found );
$this->assertContains( 'tag-bar', $found );
}
public function test_with_categories() {
$cats = $this->factory->category->create_many( 2 );
wp_set_post_terms( $this->post_id, $cats, 'category' );
$cat0 = get_term( $cats[0], 'category' );
$cat1 = get_term( $cats[1], 'category' );
$found = get_post_class( '', $this->post_id );
$this->assertContains( 'category-' . $cat0->slug, $found );
$this->assertContains( 'category-' . $cat1->slug, $found );
}
public function test_with_custom_taxonomy() {
register_taxonomy( 'wptests_tax', 'post' );
wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'wptests_tax' );
$found = get_post_class( '', $this->post_id );
$this->assertContains( 'wptests_tax-foo', $found );
$this->assertContains( 'wptests_tax-bar', $found );
}
/**
* @group cache
*/
public function test_taxonomy_classes_hit_cache() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
wp_set_post_terms( $this->post_id, array( 'foo', 'bar' ), 'wptests_tax' );
wp_set_post_terms( $this->post_id, array( 'footag', 'bartag' ), 'post_tag' );
// Prime cache, including meta cache, which is used by get_post_class().
update_object_term_cache( $this->post_id, 'post' );
update_meta_cache( 'post', $this->post_id );
$num_queries = $wpdb->num_queries;
$found = get_post_class( '', $this->post_id );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
}