|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the get_search_form() function. |
| 4 | + * |
| 5 | + * @since 7.1.0 |
| 6 | + * |
| 7 | + * @group general |
| 8 | + * @group template |
| 9 | + * |
| 10 | + * @covers ::get_search_form |
| 11 | + */ |
| 12 | +class Tests_General_GetSearchForm extends WP_UnitTestCase { |
| 13 | + |
| 14 | + /** |
| 15 | + * Removes any theme support added during a test. |
| 16 | + */ |
| 17 | + public function tear_down() { |
| 18 | + remove_theme_support( 'html5' ); |
| 19 | + remove_theme_support( 'search-element' ); |
| 20 | + parent::tear_down(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Enables the html5 'search-form' theme support so the html5 markup is used. |
| 25 | + */ |
| 26 | + private function enable_html5_search_form() { |
| 27 | + add_theme_support( 'html5', array( 'search-form' ) ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The html5 form should keep role="search" on the <form> by default. |
| 32 | + * |
| 33 | + * @ticket 65288 |
| 34 | + */ |
| 35 | + public function test_html5_default_uses_form_role_search() { |
| 36 | + $this->enable_html5_search_form(); |
| 37 | + |
| 38 | + $form = get_search_form( array( 'echo' => false ) ); |
| 39 | + |
| 40 | + $this->assertStringContainsString( '<form role="search"', $form, 'The default html5 form should keep role="search" on the form.' ); |
| 41 | + $this->assertStringNotContainsString( '<search', $form, 'The default html5 form should not be wrapped in a <search> element.' ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Opting in should wrap the html5 form in a <search> element and drop role="search". |
| 46 | + * |
| 47 | + * @ticket 65288 |
| 48 | + */ |
| 49 | + public function test_wrap_in_search_wraps_form_and_drops_role() { |
| 50 | + $this->enable_html5_search_form(); |
| 51 | + |
| 52 | + $form = get_search_form( |
| 53 | + array( |
| 54 | + 'echo' => false, |
| 55 | + 'wrap_in_search' => true, |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | + $this->assertStringContainsString( '<search>', $form, 'The opted-in form should open a <search> element.' ); |
| 60 | + $this->assertStringContainsString( '</search>', $form, 'The opted-in form should close the <search> element.' ); |
| 61 | + $this->assertStringContainsString( '<form method="get" class="search-form"', $form, 'The inner form markup should be preserved.' ); |
| 62 | + $this->assertStringNotContainsString( 'role="search"', $form, 'role="search" should be dropped to avoid a nested duplicate landmark.' ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * A custom aria_label should name the <search> landmark, not the inner form. |
| 67 | + * |
| 68 | + * @ticket 65288 |
| 69 | + */ |
| 70 | + public function test_wrap_in_search_applies_aria_label_to_search_element() { |
| 71 | + $this->enable_html5_search_form(); |
| 72 | + |
| 73 | + $form = get_search_form( |
| 74 | + array( |
| 75 | + 'echo' => false, |
| 76 | + 'wrap_in_search' => true, |
| 77 | + 'aria_label' => 'Search products', |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + $this->assertStringContainsString( '<search aria-label="Search products">', $form, 'The aria-label should be applied to the <search> element.' ); |
| 82 | + $this->assertStringContainsString( '<form method="get"', $form, 'The inner form should not carry the aria-label.' ); |
| 83 | + $this->assertStringNotContainsString( '<form method="get" aria-label', $form, 'The aria-label should not appear on the inner form.' ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Without a custom aria_label, the <search> element should have no attributes. |
| 88 | + * |
| 89 | + * @ticket 65288 |
| 90 | + */ |
| 91 | + public function test_wrap_in_search_without_aria_label_has_no_attributes() { |
| 92 | + $this->enable_html5_search_form(); |
| 93 | + |
| 94 | + $form = get_search_form( |
| 95 | + array( |
| 96 | + 'echo' => false, |
| 97 | + 'wrap_in_search' => true, |
| 98 | + ) |
| 99 | + ); |
| 100 | + |
| 101 | + $this->assertStringContainsString( '<search>', $form, 'The <search> element should have no attributes when no aria-label is set.' ); |
| 102 | + $this->assertStringNotContainsString( '<search >', $form, 'The <search> element should not contain a stray space.' ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * The default html5 form should still apply a custom aria_label to the form. |
| 107 | + * |
| 108 | + * @ticket 65288 |
| 109 | + */ |
| 110 | + public function test_html5_default_applies_aria_label_to_form() { |
| 111 | + $this->enable_html5_search_form(); |
| 112 | + |
| 113 | + $form = get_search_form( |
| 114 | + array( |
| 115 | + 'echo' => false, |
| 116 | + 'aria_label' => 'Search the site', |
| 117 | + ) |
| 118 | + ); |
| 119 | + |
| 120 | + $this->assertStringContainsString( '<form role="search" aria-label="Search the site"', $form, 'The aria-label should be applied to the form by default.' ); |
| 121 | + $this->assertStringNotContainsString( '<search', $form, 'The default form should not be wrapped in a <search> element.' ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * The wrap_in_search argument should not affect the xhtml format. |
| 126 | + * |
| 127 | + * The <search> element does not exist in XHTML 1.x, so themes without html5 |
| 128 | + * 'search-form' support should continue to receive the unchanged xhtml markup. |
| 129 | + * |
| 130 | + * @ticket 65288 |
| 131 | + */ |
| 132 | + public function test_wrap_in_search_is_ignored_for_xhtml_format() { |
| 133 | + // No html5 theme support added: the format defaults to xhtml. |
| 134 | + $form = get_search_form( |
| 135 | + array( |
| 136 | + 'echo' => false, |
| 137 | + 'wrap_in_search' => true, |
| 138 | + ) |
| 139 | + ); |
| 140 | + |
| 141 | + $this->assertStringNotContainsString( '<search', $form, 'The xhtml format should not use the <search> element.' ); |
| 142 | + $this->assertStringContainsString( '<form role="search"', $form, 'The xhtml format should keep role="search" on the form.' ); |
| 143 | + $this->assertStringContainsString( 'id="searchform"', $form, 'The xhtml markup should be unchanged.' ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * The wrapping should be enableable globally via the search_form_args filter. |
| 148 | + * |
| 149 | + * @ticket 65288 |
| 150 | + */ |
| 151 | + public function test_wrap_in_search_can_be_enabled_via_filter() { |
| 152 | + $this->enable_html5_search_form(); |
| 153 | + |
| 154 | + add_filter( |
| 155 | + 'search_form_args', |
| 156 | + static function ( $args ) { |
| 157 | + $args['wrap_in_search'] = true; |
| 158 | + return $args; |
| 159 | + } |
| 160 | + ); |
| 161 | + |
| 162 | + $form = get_search_form( array( 'echo' => false ) ); |
| 163 | + |
| 164 | + $this->assertStringContainsString( '<search>', $form, 'The search_form_args filter should be able to enable wrapping.' ); |
| 165 | + $this->assertStringNotContainsString( 'role="search"', $form, 'role="search" should be dropped when wrapping is enabled via filter.' ); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Declaring 'search-element' theme support should wrap the form by default. |
| 170 | + * |
| 171 | + * @ticket 65288 |
| 172 | + */ |
| 173 | + public function test_search_element_theme_support_enables_wrap_by_default() { |
| 174 | + $this->enable_html5_search_form(); |
| 175 | + add_theme_support( 'search-element' ); |
| 176 | + |
| 177 | + $form = get_search_form( array( 'echo' => false ) ); |
| 178 | + |
| 179 | + $this->assertStringContainsString( '<search>', $form, 'Declaring search-element support should wrap the form by default.' ); |
| 180 | + $this->assertStringNotContainsString( 'role="search"', $form, 'role="search" should be dropped when search-element support is declared.' ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * An explicit wrap_in_search => false should override 'search-element' theme support. |
| 185 | + * |
| 186 | + * @ticket 65288 |
| 187 | + */ |
| 188 | + public function test_wrap_in_search_false_overrides_theme_support() { |
| 189 | + $this->enable_html5_search_form(); |
| 190 | + add_theme_support( 'search-element' ); |
| 191 | + |
| 192 | + $form = get_search_form( |
| 193 | + array( |
| 194 | + 'echo' => false, |
| 195 | + 'wrap_in_search' => false, |
| 196 | + ) |
| 197 | + ); |
| 198 | + |
| 199 | + $this->assertStringNotContainsString( '<search', $form, 'An explicit false should override search-element theme support.' ); |
| 200 | + $this->assertStringContainsString( '<form role="search"', $form, 'The form should keep role="search" when wrapping is explicitly disabled.' ); |
| 201 | + } |
| 202 | +} |
0 commit comments