WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/05/2017 09:30:30 PM (12 months ago)
Author:
SergeyBiryukov
Message:

Menus: Make sure current-menu-parent and current-menu-ancestor classes are properly set for parent items of post type archive submenu items.

Props mrwweb, ajoah, welcher.
Fixes #39800.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r40676 r41008  
    554554        $this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
    555555    }
     556
     557    /**
     558     * @ticket 39800
     559     */
     560    function test_parent_ancestor_for_post_archive() {
     561
     562        register_post_type( 'books', array( 'label' => 'Books', 'public' => true, 'has_archive' => true ) );
     563
     564        $first_page_id  = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Top Level Page' ) );
     565        $second_page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Second Level Page' ) );
     566
     567
     568        $first_menu_id = wp_update_nav_menu_item( $this->menu_id, 0, array(
     569            'menu-item-type'      => 'post_type',
     570            'menu-item-object'    => 'page',
     571            'menu-item-object-id' => $first_page_id,
     572            'menu-item-status'    => 'publish',
     573        ));
     574
     575        $second_menu_id = wp_update_nav_menu_item( $this->menu_id, 0, array(
     576            'menu-item-type'      => 'post_type',
     577            'menu-item-object'    => 'page',
     578            'menu-item-object-id' => $second_page_id,
     579            'menu-item-status'    => 'publish',
     580            'menu-item-parent-id' => $first_menu_id
     581        ));
     582
     583        wp_update_nav_menu_item( $this->menu_id, 0, array(
     584            'menu-item-type'      => 'post_type_archive',
     585            'menu-item-object'    => 'books',
     586            'menu-item-status'    => 'publish',
     587            'menu-item-parent-id' => $second_menu_id
     588        ));
     589
     590        $this->go_to( get_post_type_archive_link( 'books' ) );
     591
     592        $menu_items = wp_get_nav_menu_items( $this->menu_id );
     593        _wp_menu_item_classes_by_context( $menu_items );
     594
     595        $top_page_menu_item       = $menu_items[0];
     596        $secondary_page_menu_item = $menu_items[1];
     597        $post_archive_menu_item   = $menu_items[2];
     598
     599        $this->assertFalse( $top_page_menu_item->current_item_parent );
     600        $this->assertTrue( $top_page_menu_item->current_item_ancestor );
     601        $this->assertContains( 'current-menu-ancestor', $top_page_menu_item->classes );
     602
     603        $this->assertTrue( $secondary_page_menu_item->current_item_parent );
     604        $this->assertTrue( $secondary_page_menu_item->current_item_ancestor  );
     605        $this->assertContains( 'current-menu-parent', $secondary_page_menu_item->classes );
     606        $this->assertContains( 'current-menu-ancestor', $secondary_page_menu_item->classes );
     607
     608        $this->assertFalse( $post_archive_menu_item->current_item_parent );
     609        $this->assertFalse( $post_archive_menu_item->current_item_ancestor  );
     610
     611        $this->assertNotContains( 'current-menu-parent', $post_archive_menu_item->classes );
     612        $this->assertNotContains( 'current-menu-ancestor', $post_archive_menu_item->classes );
     613    }
     614
    556615}
Note: See TracChangeset for help on using the changeset viewer.