walker.php 6.1 KB

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