Skip to content

Commit 7117d32

Browse files
committed
Tests: Cover category dropdown walker output.
Add direct coverage for `Walker_CategoryDropdown` option markup, selection, value fields, escaping, formatted counts, and the `list_cats` filter. Verify output appending and indentation of nested categories using in-memory fixtures. Developed in: #14178 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64379 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4c61f15 commit 7117d32

1 file changed

Lines changed: 286 additions & 0 deletions

File tree

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
<?php
2+
3+
/**
4+
* Tests for the Walker_CategoryDropdown class.
5+
*
6+
* @group taxonomy
7+
* @group category
8+
* @group walker
9+
*
10+
* @coversDefaultClass Walker_CategoryDropdown
11+
*/
12+
class Tests_Category_Walker_CategoryDropdown extends WP_UnitTestCase {
13+
14+
/**
15+
* Creates a category object without touching the database.
16+
*
17+
* @param array $fields Fields to override.
18+
* @return stdClass Category object.
19+
*/
20+
private function get_category( array $fields = array() ) {
21+
return (object) array_merge(
22+
array(
23+
'term_id' => 5,
24+
'name' => 'News',
25+
'slug' => 'news',
26+
'parent' => 0,
27+
'count' => 1234,
28+
),
29+
$fields
30+
);
31+
}
32+
33+
/**
34+
* Tests the option element generated for a category.
35+
*
36+
* @ticket 65819
37+
*
38+
* @covers ::start_el
39+
*
40+
* @dataProvider data_start_el
41+
*
42+
* @param array $args Arguments passed to the walker.
43+
* @param int $depth Depth of the category.
44+
* @param string $expected Expected option element, without the leading tab and trailing newline.
45+
*/
46+
public function test_start_el( $args, $depth, $expected ) {
47+
$args = array_merge(
48+
array(
49+
'selected' => 0,
50+
'show_count' => 0,
51+
),
52+
$args
53+
);
54+
55+
$walker = new Walker_CategoryDropdown();
56+
$output = '';
57+
58+
$walker->start_el( $output, $this->get_category(), $depth, $args );
59+
60+
$this->assertSame( "\t" . $expected . "\n", $output );
61+
}
62+
63+
/**
64+
* Data provider.
65+
*
66+
* @return array[]
67+
*/
68+
public static function data_start_el() {
69+
return array(
70+
'default arguments' => array(
71+
array(),
72+
0,
73+
'<option class="level-0" value="5">News</option>',
74+
),
75+
'first level of depth' => array(
76+
array(),
77+
1,
78+
'<option class="level-1" value="5">&nbsp;&nbsp;&nbsp;News</option>',
79+
),
80+
'second level of depth' => array(
81+
array(),
82+
2,
83+
'<option class="level-2" value="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;News</option>',
84+
),
85+
'selected by integer ID' => array(
86+
array( 'selected' => 5 ),
87+
0,
88+
'<option class="level-0" value="5" selected="selected">News</option>',
89+
),
90+
'selected by string ID' => array(
91+
array( 'selected' => '5' ),
92+
0,
93+
'<option class="level-0" value="5" selected="selected">News</option>',
94+
),
95+
'another category is selected' => array(
96+
array( 'selected' => 50 ),
97+
0,
98+
'<option class="level-0" value="5">News</option>',
99+
),
100+
'selected value is an empty string' => array(
101+
array( 'selected' => '' ),
102+
0,
103+
'<option class="level-0" value="5">News</option>',
104+
),
105+
'selected value is false' => array(
106+
array( 'selected' => false ),
107+
0,
108+
'<option class="level-0" value="5">News</option>',
109+
),
110+
'slug as the value field' => array(
111+
array( 'value_field' => 'slug' ),
112+
0,
113+
'<option class="level-0" value="news">News</option>',
114+
),
115+
'selected by slug' => array(
116+
array(
117+
'value_field' => 'slug',
118+
'selected' => 'news',
119+
),
120+
0,
121+
'<option class="level-0" value="news" selected="selected">News</option>',
122+
),
123+
'ID does not select with a slug value' => array(
124+
array(
125+
'value_field' => 'slug',
126+
'selected' => 5,
127+
),
128+
0,
129+
'<option class="level-0" value="news">News</option>',
130+
),
131+
'unknown value field falls back to ID' => array(
132+
array(
133+
'value_field' => 'does_not_exist',
134+
'selected' => 5,
135+
),
136+
0,
137+
'<option class="level-0" value="5" selected="selected">News</option>',
138+
),
139+
'with the post count' => array(
140+
array( 'show_count' => 1 ),
141+
0,
142+
'<option class="level-0" value="5">News&nbsp;&nbsp;(1,234)</option>',
143+
),
144+
'with depth, selection and post count' => array(
145+
array(
146+
'selected' => 5,
147+
'show_count' => true,
148+
),
149+
1,
150+
'<option class="level-1" value="5" selected="selected">&nbsp;&nbsp;&nbsp;News&nbsp;&nbsp;(1,234)</option>',
151+
),
152+
);
153+
}
154+
155+
/**
156+
* Tests that the option value is escaped.
157+
*
158+
* @ticket 65819
159+
*
160+
* @covers ::start_el
161+
*/
162+
public function test_start_el_escapes_value() {
163+
$walker = new Walker_CategoryDropdown();
164+
$output = '';
165+
166+
$walker->start_el(
167+
$output,
168+
$this->get_category( array( 'slug' => 'news"><script>' ) ),
169+
0,
170+
array(
171+
'value_field' => 'slug',
172+
'selected' => 0,
173+
'show_count' => 0,
174+
)
175+
);
176+
177+
$this->assertSame( "\t" . '<option class="level-0" value="news&quot;&gt;&lt;script&gt;">News</option>' . "\n", $output );
178+
}
179+
180+
/**
181+
* Tests that the option is appended to the existing output.
182+
*
183+
* @ticket 65819
184+
*
185+
* @covers ::start_el
186+
*/
187+
public function test_start_el_appends_to_output() {
188+
$walker = new Walker_CategoryDropdown();
189+
$output = '<select>';
190+
$args = array(
191+
'selected' => 0,
192+
'show_count' => 0,
193+
);
194+
195+
$walker->start_el( $output, $this->get_category(), 0, $args );
196+
197+
$this->assertSame( '<select>' . "\t" . '<option class="level-0" value="5">News</option>' . "\n", $output );
198+
}
199+
200+
/**
201+
* Tests that the category name is filtered.
202+
*
203+
* @ticket 65819
204+
*
205+
* @covers ::start_el
206+
*/
207+
public function test_start_el_applies_list_cats_filter() {
208+
$category = $this->get_category();
209+
210+
$filter = new MockAction();
211+
add_filter( 'list_cats', array( $filter, 'filter' ), 10, 2 );
212+
add_filter(
213+
'list_cats',
214+
static function () {
215+
return 'Filtered name';
216+
},
217+
20
218+
);
219+
220+
$walker = new Walker_CategoryDropdown();
221+
$output = '';
222+
$args = array(
223+
'selected' => 0,
224+
'show_count' => 0,
225+
);
226+
227+
$walker->start_el( $output, $category, 0, $args );
228+
229+
$this->assertSame( "\t" . '<option class="level-0" value="5">Filtered name</option>' . "\n", $output, 'The filtered name should be displayed.' );
230+
$this->assertSame( array( array( 'News', $category ) ), $filter->get_args(), 'The filter should receive the name and the category.' );
231+
}
232+
233+
/**
234+
* Tests that child categories are nested under their parents.
235+
*
236+
* @ticket 65819
237+
*
238+
* @covers ::start_el
239+
*/
240+
public function test_walk_indents_child_categories() {
241+
$categories = array(
242+
$this->get_category(),
243+
$this->get_category(
244+
array(
245+
'term_id' => 6,
246+
'name' => 'Sports',
247+
'slug' => 'sports',
248+
'parent' => 5,
249+
)
250+
),
251+
$this->get_category(
252+
array(
253+
'term_id' => 7,
254+
'name' => 'Football',
255+
'slug' => 'football',
256+
'parent' => 6,
257+
)
258+
),
259+
$this->get_category(
260+
array(
261+
'term_id' => 8,
262+
'name' => 'Opinion',
263+
'slug' => 'opinion',
264+
)
265+
),
266+
);
267+
268+
$walker = new Walker_CategoryDropdown();
269+
$output = $walker->walk(
270+
$categories,
271+
0,
272+
array(
273+
'selected' => 7,
274+
'show_count' => 0,
275+
)
276+
);
277+
278+
$this->assertSame(
279+
"\t" . '<option class="level-0" value="5">News</option>' . "\n" .
280+
"\t" . '<option class="level-1" value="6">&nbsp;&nbsp;&nbsp;Sports</option>' . "\n" .
281+
"\t" . '<option class="level-2" value="7" selected="selected">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Football</option>' . "\n" .
282+
"\t" . '<option class="level-0" value="8">Opinion</option>' . "\n",
283+
$output
284+
);
285+
}
286+
}

0 commit comments

Comments
 (0)