Skip to content

Tests: Unregister custom taxonomies and post type in taxonomy tests teardown - #14952

Open
rejaulalomkhan wants to merge 3 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/65893-isolate-taxonomy-general
Open

rejaulalomkhan wants to merge 3 commits into
WordPress:trunkfrom
rejaulalomkhan:tests/65893-isolate-taxonomy-general

Conversation

@rejaulalomkhan

@rejaulalomkhan rejaulalomkhan commented Oct 27, 2026 •

Copy link
Copy Markdown

Description

Tests_Taxonomy registers several taxonomies and post types without unregistering them, and three tests unset taxonomy objects directly, which leaves their rewrite tags and permastructs behind.

  • Adds tear_down() that unregisters what the tests actually register:
    • taxonomies: wptests_tax, wptests_tax_1, wptests_tax_2, wptests_conflict, taxonomy53212, random, foo, tax_new, test_tax, custom-tax, test_tax1, test_tax2
    • post types: test_cpt, wptests_pt, wptests_conflict, post-custom-tax, test_post_type
  • In test_register_taxonomy(), test_register_hierarchical_taxonomy() and test_registering_taxonomies_to_object_types(), replaces unset( $GLOBALS['wp_taxonomies'][ $tax ] ) with unregister_taxonomy( $tax ). Assertions are unchanged.
  • Removes _unregister_post_type( 'wptests_pt' ) from the wptests_conflict ancestors test: that test registers wptests_conflict, which teardown now removes.

Testing

vendor/bin/phpunit --filter '/^Tests_Taxonomy::/'
vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter '/^Tests_Taxonomy::/'
vendor/bin/phpunit --filter '/^Tests_Taxonomy::/' --order-by=random --random-order-seed=12345
vendor/bin/phpunit --filter '/^Tests_Taxonomy::/' --order-by=random --random-order-seed=67890
vendor/bin/phpunit --group taxonomy
vendor/bin/phpcs tests/phpunit/tests/taxonomy.php

All pass (67 tests, 182 assertions; 1121 in the taxonomy group); PHPCS: no errors. With pretty permalinks enabled, a check after each teardown found leftover registrations or rewrite tags after 44 of the 67 tests before this change, and none after it.

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Use of AI Tools

AI assistance: Yes
Tool(s): Google Antigravity (original change), Claude Code (review follow-up)
Model(s): Gemini 2.5 Pro, Claude Opus 5.5
Used for: Finding the leaked registrations, then correcting the teardown list, replacing the manual unsets and checking the leftover state per test. I reviewed every line and take responsibility for the change.

…eardown

In Tests_Taxonomy, tests register various custom taxonomies ('wptests_tax', 'wptests_tax_1', 'wptests_tax_2', 'taxonomy53212', 'random', 'foo', 'test_tax', 'tax_default_term_1', 'tax_default_term_2') and custom post type 'cpt_default_term'.

Add tear_down() to unregister these taxonomies and post type after each test to ensure test isolation and prevent taxonomy pollution across test suites.

See #65893.
@github-actions

github-actions Bot commented Oct 27, 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 rejaulalomkhan, huzaifaalmesbah, lancewillett, mukesh27.

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.

Review Findings

In test_registering_taxonomies_to_object_types() (line 311), custom post type 'test_cpt' is registered via register_post_type( $post_type ).

It is currently missing from tear_down(), causing 'test_cpt' to leak into subsequent tests. Please add unregister_post_type( 'test_cpt' ); to tear_down() (see inline suggestion).

AI assistance: Yes (Gemini 3.8 Flash)

unregister_taxonomy( 'wptests_tax_2' );
unregister_taxonomy( 'taxonomy53212' );
unregister_taxonomy( 'random' );
unregister_taxonomy( 'foo' );

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.

Suggested change
unregister_taxonomy( 'foo' );
unregister_post_type( 'cpt_default_term' );
unregister_post_type( 'test_cpt' );

Follow-up to address review feedback on PR WordPress#14952 by unregistering test_cpt in tear_down().

See #65893.
@rejaulalomkhan

Copy link
Copy Markdown
Author

Thanks for the review, @huzaifaalmesbah! Updated ear_down() to unregister est_cpt.

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

Several cleanup names do not match the registered fixtures, and additional registrations remain omitted.

1 open issue
What changed in this PR

This PR adds per-test cleanup to Tests_Taxonomy to reduce global taxonomy and post-type state leaking between WordPress test suites.

Changes:

  • Adds a tear_down() method with explicit taxonomy and post-type unregistration.
  • Calls the parent teardown after custom cleanup.
File Description
tests/​phpunit/​tests/​taxonomy.php Adds cleanup for custom taxonomy and post-type registrations after each test.

Review effort: Balanced

Comment thread tests/phpunit/tests/taxonomy.php Outdated
Comment on lines +30 to +32
unregister_taxonomy( 'tax_default_term_1' );
unregister_taxonomy( 'tax_default_term_2' );
unregister_post_type( 'cpt_default_term' );

@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 suite passes, but actual-test lifecycle probes confirmed two remaining cleanup gaps: several fixture names are wrong or missing, and three tests manually unset their taxonomy objects before teardown can remove their rewrite state. Please address both before landing.

AI review · gpt-6

Comment thread tests/phpunit/tests/taxonomy.php Outdated
unregister_taxonomy( 'random' );
unregister_taxonomy( 'foo' );
unregister_taxonomy( 'test_tax' );
unregister_taxonomy( 'tax_default_term_1' );

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 align the cleanup list with the actual registrations. tax_default_term_1, tax_default_term_2, and cpt_default_term are not the fixture names used here. The actual tests leave these taxonomies: wptests_conflict, custom-tax, test_tax1, test_tax2; and these post types: wptests_pt, wptests_conflict, post-custom-tax, test_post_type. Add the missing cleanup and remove the incorrect names.

AI review · gpt-6

unregister_taxonomy( 'taxonomy53212' );
unregister_taxonomy( 'random' );
unregister_taxonomy( 'foo' );
unregister_taxonomy( 'test_tax' );

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 also replace the manual taxonomy unsets in test_register_taxonomy(), test_register_hierarchical_taxonomy(), and test_registering_taxonomies_to_object_types() with unregister_taxonomy( $tax ). Once the object is unset, teardown cannot unregister it. With pretty permalinks, the actual tests leave tax_new/test_tax rewrite tags and permastructs. The replacement preserved their assertions and cleared that residue in local probes.

AI review · gpt-6

Comment thread tests/phpunit/tests/taxonomy.php Outdated
Comment on lines +30 to +32
unregister_taxonomy( 'tax_default_term_1' );
unregister_taxonomy( 'tax_default_term_2' );
unregister_post_type( 'cpt_default_term' );

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.

tax_default_term_1, tax_default_term_2 and cpt_default_term are never registered in this file. The test at line 1113 registers test_tax1, test_tax2 and test_post_type, so those are the names that need cleaning up.

A few other registrations are also still missing from tear_down():

  • tax_new (lines 228 and 242)
  • wptests_conflict, registered as both a post type and a taxonomy (line 627)
  • post-custom-tax and custom-tax (around line 1060)

Separately, the existing _unregister_post_type( 'wptests_pt' ) at the end of the wptests_conflict test (line 666) uses the wrong name. That test registers wptests_conflict, so it never cleaned up its own post type.

Unregister the taxonomies and post types the tests actually register, and drop names that are never registered in this class. Replace the manual $wp_taxonomies unsets with unregister_taxonomy() so their rewrite tags and permastructs are removed, and drop the wptests_conflict test's cleanup of the wrong post type.

See #65893.
@rejaulalomkhan

Copy link
Copy Markdown
Author

@lancewillett @mukeshpanchal27 Updated in 370a354:

  • tear_down() now unregisters what the class actually registers: tax_new, custom-tax, test_tax1, test_tax2, wptests_conflict (taxonomy and post type), wptests_pt, post-custom-tax and test_post_type. The three names it never registers are gone.
  • The manual $wp_taxonomies unsets in the three tests are now unregister_taxonomy( $tax ).
  • Removed the wrong _unregister_post_type( 'wptests_pt' ) from the wptests_conflict test.

With pretty permalinks, no taxonomies, post types or rewrite tags are left after any test in the class now (before: 44 of 67). Tests_Taxonomy passes on single site, multisite and two random seeds.

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.

5 participants