Skip to content

Tests: Add unit test coverage for cat_is_ancestor_of() - #14430

Open
rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/cat-is-ancestor-of
Open

rejaulalomkhan wants to merge 2 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/cat-is-ancestor-of

Conversation

@rejaulalomkhan

@rejaulalomkhan rejaulalomkhan commented Oct 18, 2026 •

Copy link
Copy Markdown

Adds unit test coverage for cat_is_ancestor_of() in src/wp-includes/category.php.

Test scenarios covered:

  • Direct parent category relationships (parent -> child, grandparent -> parent).
  • Indirect ancestor relationships (grandparent -> child).
  • Non-ancestor relationships (reverse hierarchy, unrelated categories).
  • Comparing a category to itself (cat1 === cat2 returns false).
  • Parameter combinations: category IDs, WP_Term objects, and mixed ID/object inputs via @dataProvider.
  • Non-existent category IDs.
  • Invalid or empty inputs (0, '', null, false) via @dataProvider.
  • Terms from a different taxonomy (post_tag).

Verification:

  • Single-site PHPUnit: OK (16 tests, 23 assertions)
  • Multisite PHPUnit: OK (16 tests, 23 assertions)
  • Randomized order PHPUnit: OK (16 tests, 23 assertions)
  • PHPCS (WordPress-Core): Clean (0 errors, 0 warnings)
  • Mutation testing: Verified intentional test failure when return value is inverted.

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


Use of AI Tools: Antigravity (Gemini 2.5) was used to assist in test case ideation, code scaffolding, and formatting to WordPress core standards. All assertions and test outcomes were verified locally through PHPUnit and PHPCS.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini 2.5
Used for: Writing PHPUnit unit test coverage for cat_is_ancestor_of(), running test assertions, PHPCS, and quality gate checks. All code was reviewed and verified.

Adds comprehensive PHPUnit tests covering direct parent and grandparent category ancestor checks, non-ancestor hierarchies, self-comparison, category ID and WP_Term object parameter variations, non-existent categories, invalid and empty inputs, and terms from different taxonomies.

See #65819.
@github-actions

github-actions Bot commented Oct 18, 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. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props rejaulalomkhan, lancewillett.

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

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 replacement taxonomy checks lose existing regression coverage for the category-only restriction.

1 open issue
What changed in this PR

Expands and reorganizes PHPUnit coverage for cat_is_ancestor_of() without changing production code.

Changes:

  • Splits hierarchy checks into focused tests.
  • Adds data providers for parameter types and invalid inputs.
  • Adds missing-category and cross-taxonomy cases.
File Description
tests/​phpunit/​tests/​category/​catIsAncestorOf.php Refactors fixtures and expands category ancestry tests.

Review effort: Balanced

Comment on lines +213 to +214
$this->assertFalse( cat_is_ancestor_of( self::$tag_id, self::$child_id ) );
$this->assertFalse( cat_is_ancestor_of( self::$parent_id, self::$tag_id ) );

@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 retain the existing cross-taxonomy regression test. The replacement checks weaken that protection even though they pass.

Reviewed with the unit-tests audit and the fresh Copilot review.

AI review · gpt-6

* @ticket 65819
*/
public function test_returns_false_for_term_from_different_taxonomy() {
$this->assertFalse( cat_is_ancestor_of( self::$tag_id, self::$child_id ) );

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 restore test_returns_false_for_term_ids_from_another_taxonomy() with a real parent–child pair in another hierarchical taxonomy. These new tag/category pairs are unrelated, so they still return false if taxonomy lookup is accidentally unrestricted. The deleted related-term fixture would detect that regression.

The tag/category checks use unrelated terms, so they still pass if the taxonomy lookup is accidentally unrestricted. A real parent-child pair in another hierarchical taxonomy catches that regression.

See #65819.
@rejaulalomkhan

Copy link
Copy Markdown
Author

@lancewillett Thanks for the review! Addressed in bd939b7:

  • Restored test_returns_false_for_term_ids_from_another_taxonomy() with a real parent–child pair in a hierarchical wptests_tax taxonomy, next to the tag checks.
  • Checked it: when cat_is_ancestor_of() passes an empty taxonomy to term_is_ancestor_of(), only this test fails; the tag/category checks still pass.

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

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants