From c56aef514cf3473896b4e582110b4772d1ac7d75 Mon Sep 17 00:00:00 2001 From: Muhibul Haque Date: Fri, 30 Oct 2026 19:05:14 +0600 Subject: [PATCH 1/2] Tests: Cover has_term(). Add unit tests for has_term(): - Verify has_term() returns true when the post has matching term by ID, name, slug, or array of terms. - Verify has_term() accepts both a post ID and WP_Post object. - Verify has_term() with empty term checks if the post has any term in the taxonomy. - Verify has_term() defaults to the global post when no post is passed. - Verify has_term() returns false for non-matching terms or invalid posts. - Verify has_term() returns false for invalid or non-existent taxonomies. See #65819. Props devmuhib009. --- tests/phpunit/tests/category/hasTerm.php | 134 +++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/phpunit/tests/category/hasTerm.php diff --git a/tests/phpunit/tests/category/hasTerm.php b/tests/phpunit/tests/category/hasTerm.php new file mode 100644 index 0000000000000..968ddeb010866 --- /dev/null +++ b/tests/phpunit/tests/category/hasTerm.php @@ -0,0 +1,134 @@ +term = self::factory()->term->create_and_get( + array( + 'taxonomy' => self::TAXONOMY, + 'name' => 'Assigned Term', + 'slug' => 'assigned-term', + ) + ); + + $this->other_term = self::factory()->term->create_and_get( + array( + 'taxonomy' => self::TAXONOMY, + 'name' => 'Other Term', + 'slug' => 'other-term', + ) + ); + + $this->post = self::factory()->post->create_and_get(); + wp_set_object_terms( $this->post->ID, $this->term->term_id, self::TAXONOMY ); + } + + /** + * Cleans up test fixtures after each test method. + */ + public function tear_down() { + unregister_taxonomy( self::TAXONOMY ); + + parent::tear_down(); + } + + /** + * Tests that has_term() returns true when the post has the specified term + * by ID, name, slug, or an array of values, and accepts a post ID or WP_Post object. + * + * @ticket 65819 + */ + public function test_returns_true_when_post_has_matching_term() { + $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY, $this->post->ID ) ); + $this->assertTrue( has_term( $this->term->name, self::TAXONOMY, $this->post->ID ) ); + $this->assertTrue( has_term( $this->term->slug, self::TAXONOMY, $this->post->ID ) ); + $this->assertTrue( has_term( array( $this->other_term->slug, $this->term->slug ), self::TAXONOMY, $this->post->ID ) ); + $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY, $this->post ) ); + } + + /** + * Tests that has_term() with an empty term check returns true when the post + * has at least one term and false when it has no terms. + * + * @ticket 65819 + */ + public function test_empty_term_checks_if_post_has_any_term() { + $this->assertTrue( has_term( '', self::TAXONOMY, $this->post->ID ) ); + + $unassigned_post_id = self::factory()->post->create(); + + $this->assertFalse( has_term( '', self::TAXONOMY, $unassigned_post_id ) ); + } + + /** + * Tests that has_term() defaults to the global post when no post parameter is passed. + * + * @ticket 65819 + */ + public function test_defaults_to_global_post_when_post_is_omitted() { + $GLOBALS['post'] = $this->post; + + $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY ) ); + $this->assertFalse( has_term( $this->other_term->term_id, self::TAXONOMY ) ); + } + + /** + * Tests that has_term() returns false when the post does not have the term + * or when the post does not exist. + * + * @ticket 65819 + */ + public function test_returns_false_for_non_matching_term_or_invalid_post() { + $this->assertFalse( has_term( $this->other_term->term_id, self::TAXONOMY, $this->post->ID ) ); + $this->assertFalse( has_term( $this->other_term->name, self::TAXONOMY, $this->post->ID ) ); + $this->assertFalse( has_term( $this->other_term->slug, self::TAXONOMY, $this->post->ID ) ); + $this->assertFalse( has_term( $this->term->term_id, self::TAXONOMY, 99999999 ) ); + } + + /** + * Tests that has_term() returns false when an invalid or non-existent taxonomy is passed. + * + * @ticket 65819 + */ + public function test_returns_false_for_invalid_taxonomy() { + $this->assertFalse( has_term( $this->term->term_id, 'non_existent_tax', $this->post->ID ) ); + $this->assertFalse( has_term( $this->term->term_id, '', $this->post->ID ) ); + } +} From 3bc709ae1a46575aa3e97de6891ce76aee70090b Mon Sep 17 00:00:00 2001 From: Muhibul Haque Date: Sun, 1 Nov 2026 15:25:24 +0600 Subject: [PATCH 2/2] Tests: Focus hasTerm.php on global post, missing post, and invalid taxonomy. See #65819. Props devmuhib009. --- tests/phpunit/tests/category/hasTerm.php | 67 ++---------------------- 1 file changed, 5 insertions(+), 62 deletions(-) diff --git a/tests/phpunit/tests/category/hasTerm.php b/tests/phpunit/tests/category/hasTerm.php index 968ddeb010866..5aa08ec4de03d 100644 --- a/tests/phpunit/tests/category/hasTerm.php +++ b/tests/phpunit/tests/category/hasTerm.php @@ -18,13 +18,6 @@ class Tests_Category_HasTerm extends WP_UnitTestCase { */ private $term; - /** - * Unassigned term object. - * - * @var WP_Term - */ - private $other_term; - /** * Post object assigned to $term. * @@ -48,78 +41,29 @@ public function set_up() { ) ); - $this->other_term = self::factory()->term->create_and_get( - array( - 'taxonomy' => self::TAXONOMY, - 'name' => 'Other Term', - 'slug' => 'other-term', - ) - ); - $this->post = self::factory()->post->create_and_get(); wp_set_object_terms( $this->post->ID, $this->term->term_id, self::TAXONOMY ); } - /** - * Cleans up test fixtures after each test method. - */ - public function tear_down() { - unregister_taxonomy( self::TAXONOMY ); - - parent::tear_down(); - } - - /** - * Tests that has_term() returns true when the post has the specified term - * by ID, name, slug, or an array of values, and accepts a post ID or WP_Post object. - * - * @ticket 65819 - */ - public function test_returns_true_when_post_has_matching_term() { - $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY, $this->post->ID ) ); - $this->assertTrue( has_term( $this->term->name, self::TAXONOMY, $this->post->ID ) ); - $this->assertTrue( has_term( $this->term->slug, self::TAXONOMY, $this->post->ID ) ); - $this->assertTrue( has_term( array( $this->other_term->slug, $this->term->slug ), self::TAXONOMY, $this->post->ID ) ); - $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY, $this->post ) ); - } - - /** - * Tests that has_term() with an empty term check returns true when the post - * has at least one term and false when it has no terms. - * - * @ticket 65819 - */ - public function test_empty_term_checks_if_post_has_any_term() { - $this->assertTrue( has_term( '', self::TAXONOMY, $this->post->ID ) ); - - $unassigned_post_id = self::factory()->post->create(); - - $this->assertFalse( has_term( '', self::TAXONOMY, $unassigned_post_id ) ); - } - /** * Tests that has_term() defaults to the global post when no post parameter is passed. * * @ticket 65819 */ - public function test_defaults_to_global_post_when_post_is_omitted() { + public function test_has_term_falls_back_to_global_post() { $GLOBALS['post'] = $this->post; $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY ) ); - $this->assertFalse( has_term( $this->other_term->term_id, self::TAXONOMY ) ); } /** - * Tests that has_term() returns false when the post does not have the term - * or when the post does not exist. + * Tests that has_term() returns false when the post does not exist or no post is in scope. * * @ticket 65819 */ - public function test_returns_false_for_non_matching_term_or_invalid_post() { - $this->assertFalse( has_term( $this->other_term->term_id, self::TAXONOMY, $this->post->ID ) ); - $this->assertFalse( has_term( $this->other_term->name, self::TAXONOMY, $this->post->ID ) ); - $this->assertFalse( has_term( $this->other_term->slug, self::TAXONOMY, $this->post->ID ) ); + public function test_has_term_returns_false_for_missing_post() { $this->assertFalse( has_term( $this->term->term_id, self::TAXONOMY, 99999999 ) ); + $this->assertFalse( has_term( $this->term->term_id, self::TAXONOMY ) ); } /** @@ -127,8 +71,7 @@ public function test_returns_false_for_non_matching_term_or_invalid_post() { * * @ticket 65819 */ - public function test_returns_false_for_invalid_taxonomy() { + public function test_has_term_returns_false_for_invalid_taxonomy() { $this->assertFalse( has_term( $this->term->term_id, 'non_existent_tax', $this->post->ID ) ); - $this->assertFalse( has_term( $this->term->term_id, '', $this->post->ID ) ); } }