Skip to content

Tests: Cover has_term() - #15228

Closed
devmuhib009 wants to merge 2 commits into
WordPress:trunkfrom
devmuhib009:tests/65819-cover-has-term
Closed

devmuhib009 wants to merge 2 commits into
WordPress:trunkfrom
devmuhib009:tests/65819-cover-has-term

Conversation

@devmuhib009

Copy link
Copy Markdown
Member

This PR adds comprehensive unit test coverage for has_term():

  • Tests that has_term() returns true when the post has the matching term specified by ID, name, slug, or an array of values.
  • Tests that has_term() works when passing either a post ID or a WP_Post object.
  • Tests that passing an empty string as $term checks if the post has any term in the given taxonomy.
  • Tests that has_term() defaults to the global $post when the post parameter is omitted.
  • Tests that has_term() returns false for non-matching terms, invalid post IDs, or invalid/non-existent taxonomies.

Trac ticket: https://core.trac.wordpress.org/ticket/65819
Props devmuhib009.

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.
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

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:

Props devmuhib, huzaifaalmesbah, lancewillett.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@huzaifaalmesbah huzaifaalmesbah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_Post objects.
  • 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 lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new has_term() test class lacks coverage for matching names, slugs and arrays, explicit WP_Post inputs, empty-term outcomes, and non-matching terms.

1 open issue

Review effort: Balanced

$GLOBALS['post'] = $this->post;

$this->assertTrue( has_term( $this->term->term_id, self::TAXONOMY ) );
}

@lancewillett lancewillett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

wporg-sync pushed a commit that referenced this pull request Nov 1, 2026
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
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 1, 2026
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
@lancewillett

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants