Skip to content

Instantly share code, notes, and snippets.

@christophherr
Last active November 2, 2016 03:30
Show Gist options
  • Save christophherr/3b1ffcc38b5c392cec82e7d6208fdbc6 to your computer and use it in GitHub Desktop.
Save christophherr/3b1ffcc38b5c392cec82e7d6208fdbc6 to your computer and use it in GitHub Desktop.
/**
* @ticket 38486
*/
function test_class_applied_to_page_for_posts_on_custom_post_type() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
update_option( 'page_for_posts', $page_id );
$cpt_id = self::factory()->post->create( array( 'post_type' => 'cpt', 'post_name' => 'cpt-name' ) );
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id,
'menu-item-status' => 'publish',
));
$permalink = get_permalink( $cpt_id );
$this->go_to( $permalink );
$menu_items = wp_get_nav_menu_items( $this->menu_id );
_wp_menu_item_classes_by_context( $menu_items );
$classes = $menu_items[0]->classes;
delete_option( 'page_for_posts' );
$this->assertContains( 'current_page_parent', $classes );
}
/**
* @ticket 38486
*/
function test_class_applied_to_page_for_posts_on_custom_post_type_archive() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
update_option( 'page_for_posts', $page_id );
register_post_type( 'cpt', array( 'has_archive' => true ) );
$cpt_id = self::factory()->post->create( array( 'post_type' => 'cpt', 'post_name' => 'cpt-name' ) );
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id,
'menu-item-status' => 'publish',
));
$cpt_archive = get_post_type_archive_link( 'cpt' );
$this->go_to( $cpt_archive );
$menu_items = wp_get_nav_menu_items( $this->menu_id );
_wp_menu_item_classes_by_context( $menu_items );
$classes = $menu_items[0]->classes;
delete_option( 'page_for_posts' );
$this->assertContains( 'current_page_parent', $classes );
}
/**
* @ticket 38486
*/
function test_class_applied_to_page_for_posts_on_post() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
update_option( 'page_for_posts', $page_id );
$post_id = self::factory()->post->create_and_get();
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id,
'menu-item-status' => 'publish',
));
$permalink = get_permalink( $post_id );
$this->go_to( $permalink );
$menu_items = wp_get_nav_menu_items( $this->menu_id );
_wp_menu_item_classes_by_context( $menu_items );
$classes = $menu_items[0]->classes;
delete_option( 'page_for_posts' );
$this->assertContains( 'current_page_parent', $classes );
}
/**
* @ticket 38486
*/
function test_class_applied_to_page_for_posts_on_tag_archive() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
update_option( 'page_for_posts', $page_id );
$tag_id = self::factory()->tag->create_and_get( array( 'name' => 'Tag', 'slug' => 'tag' ) );
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id,
'menu-item-status' => 'publish',
));
$tag_archive = get_term_link( $tag_id );
$this->go_to( $tag_archive );
$menu_items = wp_get_nav_menu_items( $this->menu_id );
_wp_menu_item_classes_by_context( $menu_items );
$classes = $menu_items[0]->classes;
delete_option( 'page_for_posts' );
$this->assertContains( 'current_page_parent', $classes );
}
/**
* @ticket 38486
*/
function test_class_applied_to_page_for_posts_on_category_archive() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
update_option( 'page_for_posts', $page_id );
$cat_id = self::factory()->category->create_and_get( array( 'name' => 'Category', 'slug' => 'category' ) );
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id,
'menu-item-status' => 'publish',
));
$cat_archive = get_term_link( $cat_id );
$this->go_to( $cat_archive );
$menu_items = wp_get_nav_menu_items( $this->menu_id );
_wp_menu_item_classes_by_context( $menu_items );
$classes = $menu_items[0]->classes;
delete_option( 'page_for_posts' );
$this->assertContains( 'current_page_parent', $classes );
}
/**
* @ticket 38486
*/
function test_class_not_applied_to_page_for_posts_on_page() {
$page_id1 = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Home Page' ) );
$page_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Test Page' ) );
update_option( 'page_for_posts', $page_id1 );
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id1,
'menu-item-status' => 'publish',
));
wp_update_nav_menu_item( $this->menu_id, 0, array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id2,
'menu-item-status' => 'publish',
));
$permalink = get_permalink( $page_id2 );
$this->go_to( $permalink );
$menu_items = wp_get_nav_menu_items( $this->menu_id );
_wp_menu_item_classes_by_context( $menu_items );
$homepage_classes = $menu_items[0]->classes;
$testpage_classes = $menu_items[1]->classes;
delete_option( 'page_for_posts' );
$this->assertNotContains( 'current_page_parent', $homepage_classes );
$this->assertNotContains( 'current-menu-item', $homepage_classes );
$this->assertContains( 'current-menu-item', $testpage_classes );
$this->assertNotContains( 'current_page_parent', $testpage_classes );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment