From 559dc212023805f998e7e3f9f31c57a01ec98f2f Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Sat, 26 Oct 2013 02:53:30 +0000 Subject: [PATCH] 3.7 regression from [25119]: Have in_category() return false when the first argument is empty. Adds unit tests. props ericlewis. fixes #25706 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@25923 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/category-template.php | 3 +++ tests/phpunit/tests/taxonomy.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 27b8bf221f..57f7cb9ada 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -232,6 +232,9 @@ function get_the_category_list( $separator = '', $parents='', $post_id = false ) * @return bool True if the current post is in any of the given categories. */ function in_category( $category, $post = null ) { + if ( empty( $category ) ) + return false; + return has_category( $category, $post ); } diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index c9c6ee6fcc..e94ad4c4f3 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -152,4 +152,20 @@ class Tests_Taxonomy extends WP_UnitTestCase { _unregister_post_type( $post_type ); } + /** + * @ticket 25706 + */ + function test_in_category() { + $post = $this->factory->post->create_and_get(); + + // in_category() returns false when first parameter is empty() + $this->assertFalse( in_category( '', $post ) ); + $this->assertFalse( in_category( false, $post ) ); + $this->assertFalse( in_category( null, $post ) ); + + // Test expected behavior of in_category() + $term = wp_insert_term( 'Test', 'category' ); + wp_set_object_terms( $post->ID, $term['term_id'], 'category' ); + $this->assertTrue( in_category( $term['term_id'], $post ) ); + } }