Make WordPress Core

Ticket #14477: 14477-41-broken-ut.2.diff

File 14477-41-broken-ut.2.diff, 5.3 KB (added by dd32, 9 years ago)
  • tests/phpunit/tests/post/getPages.php

     
    272272        }
    273273
    274274        /**
    275275         * @ticket 14477
    276276         */
    277277        function test_get_pages_interrupted_hierarchy() {
    278278                $page1 = $this->factory->post->create( array( 'post_type' => 'page' ) );
    279279                $page2 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page1 ) );
    280280                add_post_meta( $page2, 'color', 'red' );
    281281                $page3 = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $page2 ) );
    282282                add_post_meta( $page3, 'color', 'blue' );
    283283
    284284                $pages = get_pages( array( 'child_of' => $page1, 'meta_key' => 'color', 'meta_value' => 'blue' ) );
    285285                $this->assertEqualSets( array( $page3 ), wp_list_pluck( $pages, 'ID' ) );
    286286        }
     287
     288        /**
     289         * @group 14477
     290         */
     291        function test_get_pages_broken_childs() {
     292        /*
     293        - A       $parent_one
     294        - B       $parent_two
     295         + BA     $child_one
     296          + BAA   $grandchild_one
     297           + BAAA $great_grandchild_one
     298           + BAAB $great_grandchild_two
     299         + BB     $child_two
     300        */
     301
     302                $parent_one = $this->factory->post->create( array( 'post_type' => 'page' ) );
     303                $parent_two = $this->factory->post->create( array( 'post_type' => 'page' ) );
     304                        $child_one = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $parent_two ) );
     305                                $grandchild_one = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $child_one ) );
     306                                        $great_grandchild_one = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $grandchild_one ) );
     307                                        $great_grandchild_two = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $grandchild_one ) );
     308                        $child_two = $this->factory->post->create( array( 'post_type' => 'page', 'post_parent' => $parent_two ) );
     309
     310                $pages = get_pages( array(
     311                        'child_of' => $parent_two,
     312                ) );
     313
     314                $page_ids = wp_list_pluck( $pages, 'ID' );
     315
     316                // The expected structure, confirmed against 4.0
     317                $expected = array(
     318                        $child_one,
     319                                $grandchild_one,
     320                                        $great_grandchild_one,
     321                                        $great_grandchild_two,
     322                        $child_two
     323                );
     324
     325                // The invalid output which 4.1 creates
     326                $four_one_output = array(
     327                        $child_one,
     328                                $grandchild_one,
     329                                        $great_grandchild_one,
     330                                        $great_grandchild_two,
     331
     332                                $grandchild_one,
     333                                        $great_grandchild_one,
     334                                        $great_grandchild_two,
     335
     336                                        $great_grandchild_one,
     337                                        $great_grandchild_two,
     338                        $child_two
     339                );
     340
     341                // Not assertEqualsSets, we want this EXACT set and order, no duplicates
     342                //$this->assertEquals( $four_one_output, $page_ids );
     343                $this->assertEquals( $expected, $page_ids );
     344
     345        }
     346       
     347        /**
     348         * @ticket 14477
     349         */
     350        function test_get_page_children_order() {
     351
     352                $parent = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'BB Parent' ) );
     353                        $bba = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'BBA Child', 'post_parent' => $parent ) );
     354                        $bbb = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'BBB Child', 'post_parent' => $parent ) );
     355                                $aaaa = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'AAAA Grand Child', 'post_parent' => $bbb ) );
     356
     357
     358                $all_pages = get_pages();
     359                $pages_of_child = get_page_children( $parent, $all_pages );
     360
     361                $expected = array(
     362                        // 4.0 = Hierarchical
     363                        $bba,
     364                        $bbb,
     365                                $aaaa
     366                );
     367                $four_point_one = array(
     368                        // 4.1 = Alphabetical
     369                        $aaaa,
     370                        $bba,
     371                        $bbb
     372                );
     373                $actual = wp_list_pluck( $pages_of_child, 'ID' );
     374
     375                $this->assertEquals( $expected, $actual );
     376
     377                // Test that wp_list_pages() + Walker gets the hierarchy correct
     378                $list_pages_output = wp_list_pages( array(
     379                                                                'child_of' => $parent,
     380                                                                'echo' => false,
     381                                                                'title_li' => ''
     382                                                        ) );
     383
     384                // do a dance to remove all HTML and clean up the resulting whitespace mess...
     385                $list_pages_output = implode( "\n", array_filter( array_map( 'rtrim', explode( "\n", strip_tags( $list_pages_output ) ) ) ) );
     386                $expected = "BBA Child\n" .
     387                                        "BBB Child\n" .
     388                                        "\tAAAA Grand Child";
     389
     390                $four_one_actual = "AAAA Grand Child\n" .
     391                                                        "BBA Child\n" .
     392                                                        "BBB Child\n";
     393
     394                $this->assertEquals( $expected, $list_pages_output );
     395
     396        /*
     397                The cause of this mis-match only happens when wp_list_pages() is used with the child_of parameter, and the structure presented does not have a post with a parent of 0.
     398                In the event that the data doesn't have a post_parent=0 in the get_pages() result (Which it won't, as post_parent=$parent is the furthest it'll go when using child_of) then wp_list_pages() will use the first post_parent it comes accross.
     399                In 4.0, as the Hierarchical order is respected, the first post_parent will be the one we're interested in, and it'll "just work"
     400                In 4.1, the order is now Alphabetical, the first post_parent it finds is $bbb, which results in it displaying $aaa first, and then listing the orphans $bba and $bbb along side it.
     401
     402                This is ultimately a two fold issue, and can be fixed by fixing either issue
     403                 - Page_Walker is hard-coded to assume the top-most parent is 0, or failing that, the first post_parent it finds
     404                 - The order from get_page_children() is now different
     405
     406                */
     407
     408        }
     409
    287410}