| 1 | <?php |
| 2 | /** |
| 3 | * Test cases for the `wp_nav_menu_item_post_type_meta_box()` function. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage UnitTests |
| 7 | * @since 5.2.0 |
| 8 | */ |
| 9 | |
| 10 | require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; |
| 11 | |
| 12 | /** |
| 13 | * Tests_Menu_WpNavMenuItemPostTypeMetaBox class. |
| 14 | * |
| 15 | * @group navmenus |
| 16 | * @covers ::wp_nav_menu_item_post_type_meta_box() |
| 17 | * |
| 18 | * @since 5.2.0 |
| 19 | */ |
| 20 | class Tests_Menu_WpNavMenuItemPostTypeMetaBox extends WP_UnitTestCase { |
| 21 | |
| 22 | /** |
| 23 | * The function should contain no items without pages. |
| 24 | * |
| 25 | * @ticket 37782 |
| 26 | */ |
| 27 | public function test_should_contain_no_items_without_pages() { |
| 28 | |
| 29 | $item = apply_filters( 'nav_menu_meta_box_object', get_post_type_object( 'page' ) ); |
| 30 | |
| 31 | $box = array( |
| 32 | 'id' => 'add-' . $item->name, |
| 33 | 'title' => $item->labels->name, |
| 34 | 'callback' => 'wp_nav_menu_item_post_type_meta_box', |
| 35 | 'args' => $item, |
| 36 | ); |
| 37 | |
| 38 | $output = get_echo( 'wp_nav_menu_item_post_type_meta_box', array( null, $box ) ); |
| 39 | |
| 40 | $this->assertContains( 'No items', $output ); |
| 41 | $this->assertSame( 0, substr_count( $output, '<li>' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * The function should include a page only once, when viewing all pages. |
| 46 | * |
| 47 | * @ticket 37782 |
| 48 | */ |
| 49 | public function test_should_contain_page_only_once_when_viewing_all() { |
| 50 | $page = $this->factory()->post->create_and_get( |
| 51 | array( |
| 52 | 'post_title' => 'My Test Page', |
| 53 | 'post_type' => 'page', |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | $item = apply_filters( 'nav_menu_meta_box_object', get_post_type_object( 'page' ) ); |
| 58 | |
| 59 | $box = array( |
| 60 | 'id' => 'add-' . $item->name, |
| 61 | 'title' => $item->labels->name, |
| 62 | 'callback' => 'wp_nav_menu_item_post_type_meta_box', |
| 63 | 'args' => $item, |
| 64 | ); |
| 65 | |
| 66 | $output = get_echo( 'wp_nav_menu_item_post_type_meta_box', array( null, $box ) ); |
| 67 | |
| 68 | // Clean up. |
| 69 | wp_delete_post( $page->ID ); |
| 70 | |
| 71 | // Extract the "View All" part. |
| 72 | preg_match( '~<div id="page-all"[^>]*>.*?</div>~i', str_replace( "\n", '', $output ), $matches ); |
| 73 | |
| 74 | $this->assertTrue( isset( $matches[0] ) ); |
| 75 | $this->assertSame( 2, substr_count( $matches[0], '<li>' ) ); |
| 76 | $this->assertContains( sprintf( '> %s</label>', $page->post_title ), $matches[0] ); |
| 77 | $this->assertContains( '> Home</label>', $matches[0] ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * The function should include the front page only once, when viewing all pages. |
| 82 | * |
| 83 | * @ticket 37782 |
| 84 | */ |
| 85 | public function test_should_contain_front_page_only_once_when_viewing_all() { |
| 86 | $page = $this->factory()->post->create_and_get( |
| 87 | array( |
| 88 | 'post_title' => 'My Test Page', |
| 89 | 'post_type' => 'page', |
| 90 | ) |
| 91 | ); |
| 92 | |
| 93 | $item = apply_filters( 'nav_menu_meta_box_object', get_post_type_object( 'page' ) ); |
| 94 | |
| 95 | $box = array( |
| 96 | 'id' => 'add-' . $item->name, |
| 97 | 'title' => $item->labels->name, |
| 98 | 'callback' => 'wp_nav_menu_item_post_type_meta_box', |
| 99 | 'args' => $item, |
| 100 | ); |
| 101 | |
| 102 | // Front page setup. |
| 103 | update_option( 'show_on_front', 'page' ); |
| 104 | update_option( 'page_on_front', $page->ID ); |
| 105 | |
| 106 | $output = get_echo( 'wp_nav_menu_item_post_type_meta_box', array( null, $box ) ); |
| 107 | |
| 108 | // Clean up. |
| 109 | delete_option( 'show_on_front' ); |
| 110 | delete_option( 'page_on_front' ); |
| 111 | wp_delete_post( $page->ID ); |
| 112 | |
| 113 | // Extract the "View All" part. |
| 114 | preg_match( '~<div id="page-all"[^>]*>.*?</div>~i', str_replace( "\n", '', $output ), $matches ); |
| 115 | |
| 116 | $this->assertTrue( isset( $matches[0] ) ); |
| 117 | $this->assertSame( 1, substr_count( $matches[0], '<li>' ) ); |
| 118 | $this->assertContains( sprintf( '> Home: %s</label>', $page->post_title ), $matches[0] ); |
| 119 | $this->assertNotContains( sprintf( '> %s</label>', $page->post_title ), $matches[0] ); |
| 120 | } |
| 121 | } |