Make WordPress Core

Ticket #15667: 15667.diff

File 15667.diff, 10.3 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/class-wp-walker.php

     
    219219                 * When none of the elements is top level.
    220220                 * Assume the first one must be root of the sub elements.
    221221                 */
    222                 if ( empty($top_level_elements) ) {
     222                if ( empty($top_level_elements) && $max_depth !== 1 ) {
    223223
    224224                        $first = array_slice( $elements, 0, 1 );
    225225                        $root = $first[0];
  • tests/phpunit/tests/post/getPages.php

     
    132132                $this->assertEquals( $inc, $exc_result );
    133133        }
    134134
     135        /*
     136         * @ticket 15667
     137         */
     138        function test_get_pages_exclude_with_children() {
     139
     140                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     141                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     142                $page_2       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     143                $page_2_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     144
     145                // If hierarchical is false get_pages returns the children even though the parents are excluded
     146                $pages = get_pages( 'hierarchical=0&exclude=' . implode( ',', array( $page_1, $page_2 ) ) );
     147                $this->assertEqualSets( array( $page_1_child, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     148
     149                // If hierarchical is true get_pages doesn't return the children if the parents are excluded
     150                $pages = get_pages( 'hierarchical=1&exclude=' . implode( ',', array( $page_1, $page_2 ) ) );
     151                $this->assertEmpty( $pages );
     152
     153        }
     154
     155        /*
     156         * @ticket 15667
     157         */
     158        function test_get_pages_exclude_tree() {
     159
     160                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     161                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     162                $page_2       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     163                $page_2_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     164
     165                // exclude_tree always excludes the children regardless of what hierarchical is set to
     166                $pages = get_pages( 'hierarchical=0&exclude_tree=' . $page_1 );
     167                $this->assertEqualSets( array( $page_2, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     168                $pages = get_pages( 'hierarchical=1&exclude_tree=' . $page_1 );
     169                $this->assertEqualSets( array( $page_2, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     170
     171                // You can only pass a single ID to exclude_tree
     172                $pages = get_pages( 'exclude_tree=' . implode( ',', array( $page_1, $page_2 ) ) );
     173                $this->assertEqualSets( array( $page_2, $page_2_child ), wp_list_pluck( $pages, 'ID' ) );
     174
     175        }
     176
    135177        /**
    136178         * @ticket 9470
    137179         */
  • tests/phpunit/tests/post/wpListPages.php

     
     1<?php
     2
     3class Tests_Post_ListPages extends WP_UnitTestCase {
     4
     5        function setUp() {
     6                parent::setUp();
     7        }
     8
     9        function test_wp_list_pages_exclude_all_parents() {
     10
     11                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     12                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     13                $page_2       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     14                $page_2_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
     15
     16                // excluding the parents should cause wp_list_pages to return nothing
     17                $pages = wp_list_pages( 'echo=0&exclude=' . implode( ',', array( $page_1, $page_2 ) ) );
     18
     19                $this->assertEmpty( $pages );
     20
     21        }
     22
     23        function test_wp_list_pages_exclude_tree() {
     24
     25                $page_1       = $this->factory->post->create( array( 'post_type' => 'page' ) );
     26                $page_1_child = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
     27
     28                // excluding the parent should cause wp_list_pages to return nothing
     29                $pages = wp_list_pages( 'echo=0&exclude_tree=' . $page_1 );
     30
     31                $this->assertEmpty( $pages );
     32
     33        }
     34
     35}
  • tests/phpunit/tests/walker.php

     
     1<?php
     2
     3/**
     4 * @group post
     5 * @group navmenus
     6 * @group taxonomy
     7 * @group walker
     8 */
     9class 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
     178class 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}