Repository navigation
Tests: Cover has_term() - #15228
Tests: Cover has_term()#15228devmuhib009 wants to merge 2 commits into
Conversation
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.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
huzaifaalmesbah
left a comment
There was a problem hiding this comment.
LGTM! Comprehensive unit test coverage for has_term(). Tests matching by ID, name, slug, array of values, global $post fallback, empty term check, and invalid post/taxonomy edge cases cleanly. All CI checks pass.
AI Review: Gemini 3.8 Flash
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the test-only additions covering has_term() behavior.
No open issues 🎉
What changed in this PR
Adds unit tests for WordPress’s has_term() function, covering term matching, post selection, and invalid inputs without changing production code.
Changes:
- Covers matching terms by ID, name, slug, and array, using post IDs and
WP_Postobjects. - Verifies empty-term checks and fallback to the global post.
- Checks false results for non-matching terms, nonexistent posts, and invalid taxonomies.
| File | Description |
|---|---|
tests/phpunit/tests/category/hasTerm.php |
Adds taxonomy fixtures and five tests covering has_term() behavior. |
Review effort: Balanced
lancewillett
left a comment
There was a problem hiding this comment.
Please trim the ID/name/slug/array matching matrix: the existing term-assignment tests already call has_term() for those outcomes. Focus this PR on the wrapper’s missing-post result, conversion of the invalid-taxonomy error to false, and one global-post lookup. Drop unregister_taxonomy() teardown: Core resets these taxonomies before each test, and the same randomized suite passes without this override.
AI review · gpt-6
…xonomy. See #65819. Props devmuhib009.
| $GLOBALS['post'] = $this->post; | ||
|
|
||
| $this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY ) ); | ||
| } |
lancewillett
left a comment
There was a problem hiding this comment.
The revised patch addresses the earlier review requests.
Validated with the related coverage patches on current trunk: 156 tests / 741 assertions in each of single-site and multisite, randomized seed 1101. No failures, errors, warnings, or skips.
Current-head CI and Copilot review checked.
AI review · gpt-6
Extend existing `is_page_template()` assertions for default and custom templates. Cover the global-post fallback and failure results in `has_term()`. Developed in: #15222, #15228 Props devmuhib, huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65169 602fd350-edb4-49c9-b593-d223f7449a82
Extend existing `is_page_template()` assertions for default and custom templates. Cover the global-post fallback and failure results in `has_term()`. Developed in: WordPress/wordpress-develop#15222, WordPress/wordpress-develop#15228 Props devmuhib, huzaifaalmesbah. See #65819. Built from https://develop.svn.wordpress.org/trunk@65169 git-svn-id: http://core.svn.wordpress.org/trunk@64318 1a063a9b-81f0-0310-95a4-ce76da25c4cd
|
PR #15228 landed in https://core.trac.wordpress.org/changeset/65169 |

This PR adds comprehensive unit test coverage for
has_term():has_term()returnstruewhen the post has the matching term specified by ID, name, slug, or an array of values.has_term()works when passing either a post ID or aWP_Postobject.$termchecks if the post has any term in the given taxonomy.has_term()defaults to the global$postwhen the post parameter is omitted.has_term()returnsfalsefor non-matching terms, invalid post IDs, or invalid/non-existent taxonomies.Trac ticket: https://core.trac.wordpress.org/ticket/65819
Props devmuhib009.