Skip to content

Commit cd67053

Browse files
committed
Tests: Add coverage for get_the_category_list().
Cover unordered and separated lists, parent-category display modes, permalink-dependent link attributes, and the current-post default. Verify empty results and the arguments and output of the `the_category_list` and `the_category` filters. Developed in: #14325 Props huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64424 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a07ec56 commit cd67053

1 file changed

Lines changed: 304 additions & 0 deletions

File tree

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_the_category_list().
5+
*
6+
* @group taxonomy
7+
* @group category
8+
*
9+
* @covers ::get_the_category_list
10+
*/
11+
class Tests_Category_GetTheCategoryList extends WP_UnitTestCase {
12+
13+
/**
14+
* ID of the parent category.
15+
*
16+
* @var int
17+
*/
18+
private static $parent_id;
19+
20+
/**
21+
* ID of the child category.
22+
*
23+
* @var int
24+
*/
25+
private static $child_id;
26+
27+
/**
28+
* ID of a top-level category.
29+
*
30+
* @var int
31+
*/
32+
private static $other_id;
33+
34+
/**
35+
* ID of a post in the child and top-level categories.
36+
*
37+
* @var int
38+
*/
39+
private static $post_id;
40+
41+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
42+
self::$parent_id = $factory->category->create(
43+
array(
44+
'name' => 'Parent',
45+
'slug' => 'parent',
46+
)
47+
);
48+
self::$child_id = $factory->category->create(
49+
array(
50+
'name' => 'Child',
51+
'slug' => 'child',
52+
'parent' => self::$parent_id,
53+
)
54+
);
55+
self::$other_id = $factory->category->create(
56+
array(
57+
'name' => 'Other',
58+
'slug' => 'other',
59+
)
60+
);
61+
62+
self::$post_id = $factory->post->create(
63+
array(
64+
'post_category' => array( self::$child_id, self::$other_id ),
65+
)
66+
);
67+
}
68+
69+
/**
70+
* Gets the plain permalink of a category.
71+
*
72+
* @param int $category_id Category ID.
73+
* @return string Category link.
74+
*/
75+
private function category_link( $category_id ) {
76+
return 'http://' . WP_TESTS_DOMAIN . '/?cat=' . $category_id;
77+
}
78+
79+
/**
80+
* Tests the unordered list built without a separator.
81+
*
82+
* @ticket 65819
83+
*
84+
* @dataProvider data_parents_in_list
85+
*
86+
* @param string $parents How to display the category parents.
87+
* @param string $expected Expected list, with %child%, %parent% and %other% placeholders for the links.
88+
*/
89+
public function test_builds_unordered_list_without_separator( $parents, $expected ) {
90+
$expected = str_replace(
91+
array( '%child%', '%parent%', '%other%' ),
92+
array( $this->category_link( self::$child_id ), $this->category_link( self::$parent_id ), $this->category_link( self::$other_id ) ),
93+
$expected
94+
);
95+
96+
$this->assertSame( $expected, get_the_category_list( '', $parents, self::$post_id ) );
97+
}
98+
99+
/**
100+
* Data provider.
101+
*
102+
* @return array[]
103+
*/
104+
public static function data_parents_in_list() {
105+
return array(
106+
'without parents' => array(
107+
'',
108+
'<ul class="post-categories">' .
109+
"\n\t" . '<li><a href="%child%" rel="category">Child</a></li>' .
110+
"\n\t" . '<li><a href="%other%" rel="category">Other</a></li>' .
111+
'</ul>',
112+
),
113+
'multiple parent links' => array(
114+
'multiple',
115+
'<ul class="post-categories">' .
116+
"\n\t" . '<li><a href="%parent%">Parent</a><a href="%child%" rel="category">Child</a></li>' .
117+
"\n\t" . '<li><a href="%other%" rel="category">Other</a></li>' .
118+
'</ul>',
119+
),
120+
'single link with parent' => array(
121+
'single',
122+
'<ul class="post-categories">' .
123+
"\n\t" . '<li><a href="%child%" rel="category">ParentChild</a></li>' .
124+
"\n\t" . '<li><a href="%other%" rel="category">Other</a></li>' .
125+
'</ul>',
126+
),
127+
'uppercase parents value' => array(
128+
'MULTIPLE',
129+
'<ul class="post-categories">' .
130+
"\n\t" . '<li><a href="%parent%">Parent</a><a href="%child%" rel="category">Child</a></li>' .
131+
"\n\t" . '<li><a href="%other%" rel="category">Other</a></li>' .
132+
'</ul>',
133+
),
134+
'unknown parents value' => array(
135+
'unknown',
136+
'<ul class="post-categories">' .
137+
"\n\t" . '<li><a href="%child%" rel="category">Child</a></li>' .
138+
"\n\t" . '<li><a href="%other%" rel="category">Other</a></li>' .
139+
'</ul>',
140+
),
141+
);
142+
}
143+
144+
/**
145+
* Tests the list joined with a separator.
146+
*
147+
* @ticket 65819
148+
*
149+
* @dataProvider data_parents_with_separator
150+
*
151+
* @param string $parents How to display the category parents.
152+
* @param string $expected Expected list, with %child%, %parent% and %other% placeholders for the links.
153+
*/
154+
public function test_joins_links_with_separator( $parents, $expected ) {
155+
$expected = str_replace(
156+
array( '%child%', '%parent%', '%other%' ),
157+
array( $this->category_link( self::$child_id ), $this->category_link( self::$parent_id ), $this->category_link( self::$other_id ) ),
158+
$expected
159+
);
160+
161+
$this->assertSame( $expected, get_the_category_list( ' | ', $parents, self::$post_id ) );
162+
}
163+
164+
/**
165+
* Data provider.
166+
*
167+
* @return array[]
168+
*/
169+
public static function data_parents_with_separator() {
170+
return array(
171+
'without parents' => array(
172+
'',
173+
'<a href="%child%" rel="category">Child</a> | <a href="%other%" rel="category">Other</a>',
174+
),
175+
'multiple parent links' => array(
176+
'multiple',
177+
'<a href="%parent%">Parent</a> | <a href="%child%" rel="category">Child</a> | <a href="%other%" rel="category">Other</a>',
178+
),
179+
'single link with parent' => array(
180+
'single',
181+
'<a href="%child%" rel="category">Parent | Child</a> | <a href="%other%" rel="category">Other</a>',
182+
),
183+
);
184+
}
185+
186+
/**
187+
* Tests that the rel attribute includes "tag" when pretty permalinks are used.
188+
*
189+
* @ticket 65819
190+
*/
191+
public function test_uses_category_tag_rel_with_pretty_permalinks() {
192+
$this->set_permalink_structure( '/%postname%/' );
193+
// Registers the category permastruct again for the new permalink structure.
194+
create_initial_taxonomies();
195+
196+
$this->assertSame(
197+
'<a href="http://' . WP_TESTS_DOMAIN . '/category/parent/child/" rel="category tag">Child</a>, <a href="http://' . WP_TESTS_DOMAIN . '/category/other/" rel="category tag">Other</a>',
198+
get_the_category_list( ', ', '', self::$post_id )
199+
);
200+
}
201+
202+
/**
203+
* Tests that the current post is used when no post ID is passed.
204+
*
205+
* @ticket 65819
206+
*/
207+
public function test_uses_current_post_by_default() {
208+
$this->go_to( get_permalink( self::$post_id ) );
209+
the_post();
210+
211+
$this->assertSame(
212+
'<a href="' . $this->category_link( self::$child_id ) . '" rel="category">Child</a>, <a href="' . $this->category_link( self::$other_id ) . '" rel="category">Other</a>',
213+
get_the_category_list( ', ' )
214+
);
215+
}
216+
217+
/**
218+
* Tests that a post type without categories gets an empty string.
219+
*
220+
* @ticket 65819
221+
*/
222+
public function test_returns_empty_string_for_post_type_without_categories() {
223+
$page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
224+
225+
$filter = new MockAction();
226+
add_filter( 'the_category', array( $filter, 'filter' ), 10, 3 );
227+
228+
$this->assertSame( '', get_the_category_list( ', ', 'single', $page_id ) );
229+
$this->assertSame( array( array( '', ', ', 'single' ) ), $filter->get_args(), 'The the_category filter should receive the empty list.' );
230+
}
231+
232+
/**
233+
* Tests that "Uncategorized" is returned when the post has no categories.
234+
*
235+
* @ticket 65819
236+
*/
237+
public function test_returns_uncategorized_without_categories() {
238+
add_filter( 'the_category_list', '__return_empty_array' );
239+
240+
$filter = new MockAction();
241+
add_filter( 'the_category', array( $filter, 'filter' ), 10, 3 );
242+
243+
$this->assertSame( 'Uncategorized', get_the_category_list( ', ', 'multiple', self::$post_id ) );
244+
$this->assertSame( array( array( 'Uncategorized', ', ', 'multiple' ) ), $filter->get_args(), 'The the_category filter should receive the default text.' );
245+
}
246+
247+
/**
248+
* Tests that the the_category_list filter receives the categories and post ID and controls the list.
249+
*
250+
* @ticket 65819
251+
*/
252+
public function test_the_category_list_filter() {
253+
$filter = new MockAction();
254+
add_filter( 'the_category_list', array( $filter, 'filter' ), 10, 2 );
255+
add_filter(
256+
'the_category_list',
257+
static function () {
258+
return array( get_category( self::$other_id ) );
259+
},
260+
20
261+
);
262+
263+
$this->assertSame(
264+
'<a href="' . $this->category_link( self::$other_id ) . '" rel="category">Other</a>',
265+
get_the_category_list( ', ', '', self::$post_id ),
266+
'The filtered categories should be listed.'
267+
);
268+
269+
$args = $filter->get_args();
270+
271+
$this->assertCount( 1, $args, 'The filter should run once.' );
272+
$this->assertSame( array( self::$child_id, self::$other_id ), wp_list_pluck( $args[0][0], 'term_id' ), 'The filter should receive the post categories.' );
273+
$this->assertSame( self::$post_id, $args[0][1], 'The filter should receive the post ID.' );
274+
}
275+
276+
/**
277+
* Tests that the the_category filter receives the list, separator and parents, and can replace the list.
278+
*
279+
* @ticket 65819
280+
*/
281+
public function test_the_category_filter() {
282+
$filter = new MockAction();
283+
add_filter( 'the_category', array( $filter, 'filter' ), 10, 3 );
284+
add_filter(
285+
'the_category',
286+
static function () {
287+
return 'filtered';
288+
},
289+
20
290+
);
291+
292+
$this->assertSame( 'filtered', get_the_category_list( ', ', 'multiple', self::$post_id ) );
293+
$this->assertSame(
294+
array(
295+
array(
296+
'<a href="' . $this->category_link( self::$parent_id ) . '">Parent</a>, <a href="' . $this->category_link( self::$child_id ) . '" rel="category">Child</a>, <a href="' . $this->category_link( self::$other_id ) . '" rel="category">Other</a>',
297+
', ',
298+
'multiple',
299+
),
300+
),
301+
$filter->get_args()
302+
);
303+
}
304+
}

0 commit comments

Comments
 (0)