Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions tests/phpunit/tests/general/getSearchForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Tests for the `get_search_form()` function.
*
* @group general
* @group template
*
* @covers ::get_search_form
*/
class Tests_General_GetSearchForm extends WP_UnitTestCase {

/**
* Tests that echo => false returns the form markup.
*
* @ticket 66830
*/
public function test_get_search_form_returns_markup_when_echo_false() {
$form = get_search_form( array( 'echo' => false ) );

$this->assertIsString( $form );
$this->assertStringContainsString( 'role="search"', $form );
$this->assertStringContainsString( 'name="s"', $form );
$this->assertStringContainsString( 'id="searchform"', $form );
}

/**
* Tests that aria_label is escaped and HTML5 markup is used when supported.
*
* @ticket 66830
*/
public function test_get_search_form_includes_aria_label() {
add_theme_support( 'html5', array( 'search-form' ) );

$form = get_search_form(
array(
'echo' => false,
'aria_label' => 'News & reviews',
)
);

$this->assertStringContainsString( 'aria-label="News &amp; reviews"', $form, 'The aria-label attribute should be escaped.' );
$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.' );
}

/**
* Tests the boolean back-compat argument still controls returning vs echoing.
*
* @ticket 66830
*/
public function test_get_search_form_boolean_echo_back_compat() {
$form = get_search_form( false );

$this->assertIsString( $form );
$this->assertStringContainsString( 'name="s"', $form );

$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.' );
}

}
Loading