Skip to content

Commit 795df58

Browse files
committed
Tests: Add coverage for walk_category_tree().
Covers the default walker, invalid-walker fallback, and a supplied custom walker. Developed in: #14374 Props shailu25, mukesh27. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64466 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 023740f commit 795df58

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* Tests for walk_category_tree().
5+
*
6+
* @group taxonomy
7+
* @group category
8+
* @group walker
9+
*
10+
* @covers ::walk_category_tree
11+
*/
12+
class Tests_Category_WalkCategoryTree extends WP_UnitTestCase {
13+
14+
/**
15+
* Parent category object.
16+
*
17+
* @var WP_Term
18+
*/
19+
private static $parent;
20+
21+
/**
22+
* Child category object.
23+
*
24+
* @var WP_Term
25+
*/
26+
private static $child;
27+
28+
/**
29+
* Creates shared category fixtures.
30+
*
31+
* @param WP_UnitTest_Factory $factory Test factory.
32+
*/
33+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
34+
self::$parent = $factory->category->create_and_get(
35+
array(
36+
'name' => 'Parent Category',
37+
'slug' => 'parent-category',
38+
)
39+
);
40+
41+
self::$child = $factory->category->create_and_get(
42+
array(
43+
'name' => 'Child Category',
44+
'slug' => 'child-category',
45+
'parent' => self::$parent->term_id,
46+
)
47+
);
48+
}
49+
50+
/**
51+
* Tests that walk_category_tree() uses Walker_Category by default when no custom walker is provided.
52+
*
53+
* @ticket 65819
54+
*/
55+
public function test_uses_default_walker_category() {
56+
$elements = array( self::$parent, self::$child );
57+
$args = array(
58+
'style' => 'list',
59+
'use_desc_for_title' => 0,
60+
);
61+
62+
$default_walker = new Walker_Category();
63+
$expected = $default_walker->walk( $elements, 0, $args );
64+
65+
$this->assertStringContainsString( 'cat-item-' . self::$child->term_id, $expected );
66+
$this->assertSame( $expected, walk_category_tree( $elements, 0, $args ) );
67+
}
68+
69+
/**
70+
* Tests that walk_category_tree() falls back to Walker_Category when the 'walker' argument
71+
* is not a Walker instance.
72+
*
73+
* @ticket 65819
74+
*/
75+
public function test_falls_back_to_default_walker_when_walker_arg_is_invalid() {
76+
$elements = array( self::$parent, self::$child );
77+
$args = array(
78+
'style' => 'list',
79+
'use_desc_for_title' => 0,
80+
'walker' => new stdClass(),
81+
);
82+
83+
$default_walker = new Walker_Category();
84+
$expected = $default_walker->walk( $elements, 0, $args );
85+
86+
$this->assertStringContainsString( 'cat-item-' . self::$child->term_id, $expected );
87+
$this->assertSame( $expected, walk_category_tree( $elements, 0, $args ) );
88+
}
89+
90+
/**
91+
* Tests that walk_category_tree() uses a custom Walker instance when passed in $args['walker'].
92+
*
93+
* @ticket 65819
94+
*/
95+
public function test_uses_custom_walker_instance_when_provided() {
96+
$custom_walker = new class() extends Walker {
97+
public $tree_type = 'category';
98+
public $db_fields = array(
99+
'parent' => 'parent',
100+
'id' => 'term_id',
101+
);
102+
103+
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
104+
$output .= 'custom:' . $data_object->slug . '|';
105+
}
106+
};
107+
108+
$elements = array( self::$parent, self::$child );
109+
$args = array(
110+
'walker' => $custom_walker,
111+
);
112+
113+
$this->assertSame(
114+
'custom:parent-category|custom:child-category|',
115+
walk_category_tree( $elements, 0, $args )
116+
);
117+
}
118+
}

0 commit comments

Comments
 (0)