| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group post |
| 5 | * @group navmenus |
| 6 | * @group taxonomy |
| 7 | * @group walker |
| 8 | */ |
| 9 | class Tests_Walker extends WP_UnitTestCase { |
| 10 | |
| 11 | function setUp() { |
| 12 | |
| 13 | $this->walker = new Walker_Test(); |
| 14 | |
| 15 | parent::setUp(); |
| 16 | |
| 17 | } |
| 18 | |
| 19 | function test_single_item() { |
| 20 | |
| 21 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ) ); |
| 22 | $output = $this->walker->walk( $items, 0 ); |
| 23 | |
| 24 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 25 | $this->assertEquals( '<li>1</li>', $output ); |
| 26 | |
| 27 | } |
| 28 | |
| 29 | function test_single_item_flat() { |
| 30 | |
| 31 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ) ); |
| 32 | $output = $this->walker->walk( $items, -1 ); |
| 33 | |
| 34 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 35 | $this->assertEquals( '<li>1</li>', $output ); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | function test_single_item_depth_1() { |
| 40 | |
| 41 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ) ); |
| 42 | $output = $this->walker->walk( $items, 1 ); |
| 43 | |
| 44 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 45 | $this->assertEquals( '<li>1</li>', $output ); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | function test_multiple_items_single_level() { |
| 50 | |
| 51 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ), (object) array( 'id' => 2, 'parent' => 0 ) ); |
| 52 | |
| 53 | $output = $this->walker->walk( $items, 0 ); |
| 54 | |
| 55 | $this->assertEquals( 2, $this->walker->get_number_of_root_elements( $items ) ); |
| 56 | $this->assertEquals( '<li>1</li><li>2</li>', $output ); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | function test_multiple_items_multiple_levels() { |
| 61 | |
| 62 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ), (object) array( 'id' => 2, 'parent' => 1 ) ); |
| 63 | |
| 64 | $output = $this->walker->walk( $items, 0 ); |
| 65 | |
| 66 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 67 | $this->assertEquals( '<li>1<ul><li>2</li></ul></li>', $output ); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | function test_multiple_items_multiple_levels_flat() { |
| 72 | |
| 73 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ), (object) array( 'id' => 2, 'parent' => 1 ) ); |
| 74 | |
| 75 | $output = $this->walker->walk( $items, -1 ); |
| 76 | |
| 77 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 78 | $this->assertEquals( '<li>1</li><li>2</li>', $output ); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | function test_multiple_items_multiple_levels_depth_1() { |
| 83 | |
| 84 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ), (object) array( 'id' => 2, 'parent' => 1 ) ); |
| 85 | |
| 86 | $output = $this->walker->walk( $items, 1 ); |
| 87 | |
| 88 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 89 | $this->assertEquals( '<li>1</li>', $output ); |
| 90 | |
| 91 | } |
| 92 | |
| 93 | function test_multiple_items_multiple_levels_depth_2() { |
| 94 | |
| 95 | $items = array( (object) array( 'id' => 1, 'parent' => 0 ), (object) array( 'id' => 2, 'parent' => 1 ), (object) array( 'id' => 3, 'parent' => 2 ) ); |
| 96 | |
| 97 | $output = $this->walker->walk( $items, 2 ); |
| 98 | |
| 99 | $this->assertEquals( 1, $this->walker->get_number_of_root_elements( $items ) ); |
| 100 | $this->assertEquals( '<li>1<ul><li>2</li></ul></li>', $output ); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | function test_multiple_items_recursive() { |
| 105 | |
| 106 | $items = array( (object) array( 'id' => 1, 'parent' => 2 ), (object) array( 'id' => 2, 'parent' => 1 ) ); |
| 107 | |
| 108 | $output = $this->walker->walk( $items, 0 ); |
| 109 | |
| 110 | $this->assertEquals( 0, $this->walker->get_number_of_root_elements( $items ) ); |
| 111 | $this->assertEquals( '<li>1<ul><li>2</li></ul></li>', $output ); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | function test_single_item_child() { |
| 116 | |
| 117 | $items = array( (object) array( 'id' => 1, 'parent' => 3 ) ); |
| 118 | |
| 119 | $output = $this->walker->walk( $items, 0 ); |
| 120 | |
| 121 | $this->assertEquals( 0, $this->walker->get_number_of_root_elements( $items ) ); |
| 122 | $this->assertEquals( '<li>1</li>', $output ); |
| 123 | |
| 124 | } |
| 125 | |
| 126 | function test_single_item_missing_parent_depth_1() { |
| 127 | |
| 128 | $items = array( (object) array( 'id' => 1, 'parent' => 3 ) ); |
| 129 | |
| 130 | $output = $this->walker->walk( $items, 1 ); |
| 131 | |
| 132 | $this->assertEquals( 0, $this->walker->get_number_of_root_elements( $items ) ); |
| 133 | |
| 134 | // It's not clear what the output of this "should" be |
| 135 | |
| 136 | // Currently the item is simply returned |
| 137 | $this->assertEquals( '<li>1</li>', $output ); |
| 138 | |
| 139 | // But as we've only asked for the first depth maybe nothing should be returned? |
| 140 | //$this->assertEquals( '', $output ); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | function test_multiple_items_missing_parents() { |
| 145 | |
| 146 | $items = array( (object) array( 'id' => 4, 'parent' => 1 ), (object) array( 'id' => 5, 'parent' => 2 ), (object) array( 'id' => 6, 'parent' => 3 ) ); |
| 147 | |
| 148 | $output = $this->walker->walk( $items, 0 ); |
| 149 | |
| 150 | $this->assertEquals( 0, $this->walker->get_number_of_root_elements( $items ) ); |
| 151 | $this->assertEquals( '<li>4</li><li>5</li><li>6</li>', $output ); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | function test_multiple_items_missing_parents_depth_1() { |
| 156 | |
| 157 | $items = array( (object) array( 'id' => 4, 'parent' => 1 ), (object) array( 'id' => 5, 'parent' => 2 ), (object) array( 'id' => 6, 'parent' => 3 ) ); |
| 158 | |
| 159 | $output = $this->walker->walk( $items, 1 ); |
| 160 | |
| 161 | $this->assertEquals( 0, $this->walker->get_number_of_root_elements( $items ) ); |
| 162 | |
| 163 | // It's not clear what the output of this "should" be |
| 164 | |
| 165 | // Currently the first item is simply returned |
| 166 | $this->assertEquals( '<li>4</li>', $output ); |
| 167 | |
| 168 | // But as we've only asked for the first depth maybe nothing should be returned? |
| 169 | //$this->assertEquals( '', $output ); |
| 170 | |
| 171 | // Or maybe all items which are missing parents should simply be treat top level? |
| 172 | //$this->assertEquals( '<li>4</li><li>5</li><li>6</li>', $output ); |
| 173 | |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | class Walker_Test extends Walker { |
| 179 | |
| 180 | var $tree_type = 'test'; |
| 181 | var $db_fields = array ( 'parent' => 'parent', 'id' => 'id' ); |
| 182 | |
| 183 | function start_lvl( &$output, $depth = 0, $args = array() ) { |
| 184 | $output .= '<ul>'; |
| 185 | } |
| 186 | |
| 187 | function end_lvl( &$output, $depth = 0, $args = array() ) { |
| 188 | $output .= '</ul>'; |
| 189 | } |
| 190 | |
| 191 | function start_el( &$output, $item, $depth = 0, $args = array(), $current_page = 0 ) { |
| 192 | $output .= '<li>' . $item->id; |
| 193 | } |
| 194 | |
| 195 | function end_el( &$output, $page, $depth = 0, $args = array() ) { |
| 196 | $output .= '</li>'; |
| 197 | } |
| 198 | |
| 199 | } |