Skip to content

Commit 2f45dcf

Browse files
committed
Tests: Cover zero-slug category dropdown selection.
Verify that `Walker_CategoryDropdown::start_el()` leaves a category with slug `'0'` unselected when `selected` is `false`, and selects it when `selected` is `'0'`. Guard against loose comparisons selecting the wrong option. Follow-up to [64379]. Developed in: #14313 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64396 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c8842fe commit 2f45dcf

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

‎tests/phpunit/tests/category/walkerCategoryDropdown.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,55 @@ public static function data_start_el() {
152152
);
153153
}
154154

155+
/**
156+
* Tests that the selected value is compared as a string with a category slug of '0'.
157+
*
158+
* A loose comparison would treat the slug '0' as equal to false.
159+
*
160+
* @ticket 65819
161+
*
162+
* @covers ::start_el
163+
*
164+
* @dataProvider data_start_el_with_zero_slug
165+
*
166+
* @param mixed $selected Selected value.
167+
* @param string $expected Expected option element, without the leading tab and trailing newline.
168+
*/
169+
public function test_start_el_compares_zero_slug_as_string( $selected, $expected ) {
170+
$walker = new Walker_CategoryDropdown();
171+
$output = '';
172+
173+
$walker->start_el(
174+
$output,
175+
$this->get_category(
176+
array(
177+
'name' => 'Zero',
178+
'slug' => '0',
179+
)
180+
),
181+
0,
182+
array(
183+
'value_field' => 'slug',
184+
'selected' => $selected,
185+
'show_count' => 0,
186+
)
187+
);
188+
189+
$this->assertSame( "\t" . $expected . "\n", $output );
190+
}
191+
192+
/**
193+
* Data provider.
194+
*
195+
* @return array[]
196+
*/
197+
public static function data_start_el_with_zero_slug() {
198+
return array(
199+
'selected value is false' => array( false, '<option class="level-0" value="0">Zero</option>' ),
200+
'selected value is \'0\'' => array( '0', '<option class="level-0" value="0" selected="selected">Zero</option>' ),
201+
);
202+
}
203+
155204
/**
156205
* Tests that the option value is escaped.
157206
*

0 commit comments

Comments
 (0)