Skip to content

Tests: Cover get_search_form() return, aria_label escaping, HTML5 markup, and boolean back-compat - #16237

Open
shail-mehta wants to merge 3 commits into
WordPress:trunkfrom
shail-mehta:tests/66757-get-search-form
Open

shail-mehta wants to merge 3 commits into
WordPress:trunkfrom
shail-mehta:tests/66757-get-search-form

Conversation

@shail-mehta

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

Copy link
Copy Markdown
Member

Trac ticket: #66830

What

Adds unit tests for get_search_form() covering:

  • echo => false → returns default XHTML search form markup as a string
  • aria_label + HTML5 search-form theme support → escapes aria_label (News & reviews → aria-label="News & reviews") and renders HTML5 markup (class="search-form", type="search", without id="searchform")
  • boolean back-compat argument → false returns markup, true echoes it

Why

get_search_form() had no dedicated coverage. Removing the echo => false return path, aria_label escaping, HTML5 markup branch, or boolean $echo back-compat left existing tests green.

How

New Tests_General_GetSearchForm asserts fixed substrings (role="search", name="s", id="searchform", aria-label="News & reviews", class="search-form", type="search"), not values derived by re-calling get_search_form() for an expected baseline.

WP_UnitTestCase::set_up() resets theme support before each test, so the default tests exercise XHTML markup and the aria_label test enables add_theme_support( 'html5', array( 'search-form' ) ) without extra teardown.

Testing

  • npm run test:php -- --filter Tests_General_GetSearchForm — 3 tests pass
  • PHPCS clean

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 are reported for the added get_search_form() unit coverage.

No open issues 🎉

What changed in this PR

Adds dedicated unit coverage for WordPress’s get_search_form() to protect return behavior, accessibility labeling, and boolean argument compatibility.

Changes:

  • Checks that echo => false returns a string containing default XHTML form markup.
  • Verifies that aria_label appears in the generated markup.
  • Tests that legacy boolean arguments control returning versus echoing the form.
File Description
tests/​phpunit/​tests/​general/​getSearchForm.php Adds three tests covering form returns, accessibility labeling, and boolean backward compatibility.

Review effort: Balanced

@shail-mehta
shail-mehta marked this pull request as ready for review November 25, 2026 14:33
@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.


$this->expectOutputRegex( '/name="s"/' );
get_search_form( true );
}

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.

All three tests use the XHTML markup. The HTML5 branch, which most modern themes use, isn't covered yet. This test covers it. I ran it locally, and it fails when the format is forced to xhtml. set_up() already removes html5 support before each test, so no cleanup is needed:

Suggested change
}
}
/**
* Tests that the HTML5 markup is used when the theme supports it.
*
* @ticket 66757
*/
public function test_get_search_form_uses_html5_markup_with_theme_support() {
add_theme_support( 'html5', array( 'search-form' ) );
$form = get_search_form( array( 'echo' => false ) );
$this->assertStringContainsString( 'class="search-form"', $form, 'The HTML5 form class should be used.' );
$this->assertStringContainsString( 'type="search"', $form, 'The HTML5 search input type should be used.' );
$this->assertStringNotContainsString( 'id="searchform"', $form, 'The XHTML form ID should not be used.' );
}

@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 exercise the label with a value such as News & reviews and expect News &amp; reviews in the attribute. The current three tests still passed when I removed the escaping. Changing only that fixture caught the regression. You can make that same case use HTML5 search-form support to cover Mukesh’s point without adding a fourth test.

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 shail-mehta changed the title Tests: Cover get_search_form() return, aria_label, and boolean back-compat Tests: Cover get_search_form() return, aria_label escaping, HTML5 markup, and boolean back-compat Nov 26, 2026
@shail-mehta

Copy link
Copy Markdown
Member Author

Thanks for the review! I've addressed both points.

The aria_label test now uses News & reviews and asserts the escaped aria-label="News &amp; reviews". It also enables html5 search-form theme support, so it covers the HTML5 markup without adding a fourth test. The @ticket tags and PR description now link #66830.

@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 ampersand makes the escaping assertion meaningful: dropping esc_attr() failed this case. It also exercises the HTML5 branch. Core resets HTML5 theme support between tests, and the class passed in both site modes.

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