Skip to content

Commit 80188ad

Browse files
committed
Tests: Add coverage for walk_category_dropdown_tree().
Covers the default dropdown walker, invalid-walker fallback, and a supplied custom walker. Developed in: #14376 Props shailu25, mukesh27. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64468 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6c132f5 commit 80188ad

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
/**
4+
* Tests for walk_category_dropdown_tree().
5+
*
6+
* @group taxonomy
7+
* @group category
8+
* @group walker
9+
*
10+
* @covers ::walk_category_dropdown_tree
11+
*/
12+
class Tests_Category_WalkCategoryDropdownTree 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_dropdown_tree() uses Walker_CategoryDropdown by default
52+
* when no custom walker is provided.
53+
*
54+
* @ticket 65819
55+
*/
56+
public function test_uses_default_walker_category_dropdown() {
57+
$elements = array( self::$parent, self::$child );
58+
$args = array(
59+
'selected' => 0,
60+
'show_count' => 0,
61+
'value_field' => 'term_id',
62+
);
63+
64+
$default_walker = new Walker_CategoryDropdown();
65+
$expected = $default_walker->walk( $elements, 0, $args );
66+
67+
$this->assertStringContainsString( 'value="' . self::$child->term_id . '"', $expected );
68+
$this->assertSame( $expected, walk_category_dropdown_tree( $elements, 0, $args ) );
69+
}
70+
71+
/**
72+
* Tests that walk_category_dropdown_tree() falls back to Walker_CategoryDropdown
73+
* when the 'walker' argument is not a Walker instance.
74+
*
75+
* @ticket 65819
76+
*/
77+
public function test_falls_back_to_default_walker_when_walker_arg_is_invalid() {
78+
$elements = array( self::$parent, self::$child );
79+
$args = array(
80+
'selected' => 0,
81+
'show_count' => 0,
82+
'value_field' => 'term_id',
83+
'walker' => new stdClass(),
84+
);
85+
86+
$default_walker = new Walker_CategoryDropdown();
87+
$expected = $default_walker->walk( $elements, 0, $args );
88+
89+
$this->assertStringContainsString( 'value="' . self::$child->term_id . '"', $expected );
90+
$this->assertSame( $expected, walk_category_dropdown_tree( $elements, 0, $args ) );
91+
}
92+
93+
/**
94+
* Tests that walk_category_dropdown_tree() uses a custom Walker instance
95+
* when passed in $args['walker'].
96+
*
97+
* @ticket 65819
98+
*/
99+
public function test_uses_custom_walker_instance_when_provided() {
100+
$custom_walker = new class() extends Walker {
101+
public $tree_type = 'category';
102+
public $db_fields = array(
103+
'parent' => 'parent',
104+
'id' => 'term_id',
105+
);
106+
107+
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
108+
$output .= 'option:' . $data_object->slug . '|';
109+
}
110+
};
111+
112+
$elements = array( self::$parent, self::$child );
113+
$args = array(
114+
'walker' => $custom_walker,
115+
);
116+
117+
$this->assertSame(
118+
'option:parent-category|option:child-category|',
119+
walk_category_dropdown_tree( $elements, 0, $args )
120+
);
121+
}
122+
}

0 commit comments

Comments
 (0)