Skip to content

Tests: Cover get_the_generator() type inference from the current filter - #16232

Open
shail-mehta wants to merge 3 commits into
WordPress:trunkfrom
shail-mehta:tests/66757-get-the-generator-filter
Open

shail-mehta wants to merge 3 commits into
WordPress:trunkfrom
shail-mehta:tests/66757-get-the-generator-filter

Conversation

@shail-mehta

@shail-mehta shail-mehta commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Trac ticket: #66832

What

When $type is empty, get_the_generator() infers the generator type from current_filter() (rss2_head → rss2, atom_head → atom, and so on).

Why

Existing tests cover an empty type with no active filter (returns null) and explicit types. They do not cover filter-based inference. Removing the switch ( $current_filter ) mapping left those tests green.

How

Adds test_get_the_generator_infers_type_from_current_filter() with a data provider mapping all eight hooks to their expected type:

Filter Inferred type
rss2_head rss2
commentsrss2_head rss2
rss_head comment
opml_head comment
rdf_header rdf
atom_head atom
comments_atom_head atom
app_head atom

Each case compares get_the_generator() against get_the_generator( $type ), reusing the existing explicit-type markup coverage. WP_UnitTestCase restores $wp_current_filter automatically between tests.

Use of AI Tools

  • Cursor

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 were identified for the test-only change adding regression coverage for filter-based generator type inference.

No open issues 🎉

What changed in this PR

Adds regression coverage for get_the_generator() inferring its output type from the current filter when no type is supplied, addressing Trac #66757. No production code changes.

Changes:

  • Covers all eight recognized filter names through a data provider.
  • Checks expected markup using the current WordPress version.
  • Restores $wp_current_filter after each case, including assertion failures.
File Description
tests/​phpunit/​tests/​general/​getTheGenerator.php Adds data-driven tests for filter-based generator type inference.

Review effort: Balanced

@shail-mehta
shail-mehta marked this pull request as ready for review November 25, 2026 14:37
@github-actions

github-actions Bot commented Nov 25, 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 shailu25, mukesh27, 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 or WordPress.org 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.

Comment on lines +104 to +117
global $wp_current_filter;

$original_filter = $wp_current_filter;
$wp_current_filter = array( $filter );

try {
$version = ( 'atom' === $type || 'rss2' === $type || 'rdf' === $type )
? get_bloginfo_rss( 'version' )
: get_bloginfo( 'version' );

$this->assertSame( sprintf( $expected, $version ), get_the_generator() );
} finally {
$wp_current_filter = $original_filter;
}

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 base test case backs up and restores $wp_current_filter around each test (see backup_hooks() and restore_hooks() in abstract-testcase.php), so the manual restore and try/finally aren't needed:

Suggested change
global $wp_current_filter;
$original_filter = $wp_current_filter;
$wp_current_filter = array( $filter );
try {
$version = ( 'atom' === $type || 'rss2' === $type || 'rdf' === $type )
? get_bloginfo_rss( 'version' )
: get_bloginfo( 'version' );
$this->assertSame( sprintf( $expected, $version ), get_the_generator() );
} finally {
$wp_current_filter = $original_filter;
}
global $wp_current_filter;
$wp_current_filter = array( $filter );
$version = ( 'atom' === $type || 'rss2' === $type || 'rdf' === $type )
? get_bloginfo_rss( 'version' )
: get_bloginfo( 'version' );
$this->assertSame( sprintf( $expected, $version ), get_the_generator() );

@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 keep the hook-to-type mapping and reuse the existing coverage for the markup. The provider can hold just the hook and expected type, then compare the inferred result with the explicit result for that type. The existing test independently checks the explicit markup. Removing just the commentsrss2_head mapping broke its new row locally. Mukesh is also right that the base test restores $wp_current_filter.

Before landing, please link a ticket in the component that owns the code under test, per the 7.3 ticket guidance. Related coverage can share a component ticket.

AI review · gpt-6.2-astra

@shail-mehta

Copy link
Copy Markdown
Member Author

Thanks for the reviews!

I've simplified the data provider to hold just the hook and expected type, compared get_the_generator() against get_the_generator( $type ), removed the manual $wp_current_filter restore, and linked component ticket #66832 in the @ticket annotation and PR description.

@shail-mehta shail-mehta self-assigned this Nov 26, 2026

@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 provider now tests the mapping without rebuilding the markup. Removing the rss2_head mapping failed that row. The base test case restores the hook stack. The revised class passed on both single-site and multisite.

AI review · gpt-6.2-astra

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.

4 participants