|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for remove_menu_page() and remove_submenu_page(). |
| 5 | + * |
| 6 | + * @group admin |
| 7 | + * @group plugins |
| 8 | + */ |
| 9 | +class Tests_Admin_Includes_Plugin_RemoveMenuPage extends WP_UnitTestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * Whether the $menu global was set before the test. |
| 13 | + * |
| 14 | + * @var bool |
| 15 | + */ |
| 16 | + private $had_menu; |
| 17 | + |
| 18 | + /** |
| 19 | + * Original $menu global value. |
| 20 | + * |
| 21 | + * @var mixed |
| 22 | + */ |
| 23 | + private $orig_menu; |
| 24 | + |
| 25 | + /** |
| 26 | + * Whether the $submenu global was set before the test. |
| 27 | + * |
| 28 | + * @var bool |
| 29 | + */ |
| 30 | + private $had_submenu; |
| 31 | + |
| 32 | + /** |
| 33 | + * Original $submenu global value. |
| 34 | + * |
| 35 | + * @var mixed |
| 36 | + */ |
| 37 | + private $orig_submenu; |
| 38 | + |
| 39 | + public function set_up() { |
| 40 | + parent::set_up(); |
| 41 | + |
| 42 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 43 | + |
| 44 | + $this->had_menu = array_key_exists( 'menu', $GLOBALS ); |
| 45 | + $this->orig_menu = $this->had_menu ? $GLOBALS['menu'] : null; |
| 46 | + $this->had_submenu = array_key_exists( 'submenu', $GLOBALS ); |
| 47 | + $this->orig_submenu = $this->had_submenu ? $GLOBALS['submenu'] : null; |
| 48 | + |
| 49 | + $GLOBALS['menu'] = array( |
| 50 | + 2 => array( 'Dashboard', 'read', 'index.php', '', 'menu-top menu-icon-dashboard', 'menu-dashboard', 'dashicons-dashboard' ), |
| 51 | + 5 => array( 'Posts', 'edit_posts', 'edit.php', '', 'menu-top menu-icon-post', 'menu-posts', 'dashicons-admin-post' ), |
| 52 | + 25 => array( 'My Plugin', 'manage_options', 'my-plugin', 'My Plugin', 'menu-top', 'toplevel_page_my-plugin', 'dashicons-admin-generic' ), |
| 53 | + 26 => array( 'My Plugin Copy', 'manage_options', 'my-plugin', 'My Plugin Copy', 'menu-top', 'toplevel_page_my-plugin', 'dashicons-admin-generic' ), |
| 54 | + 30 => array( 'Numeric', 'manage_options', '123', 'Numeric', 'menu-top', 'toplevel_page_123', 'dashicons-admin-generic' ), |
| 55 | + ); |
| 56 | + |
| 57 | + $GLOBALS['submenu'] = array( |
| 58 | + 'edit.php' => array( |
| 59 | + 5 => array( 'All Posts', 'edit_posts', 'edit.php' ), |
| 60 | + 10 => array( 'Add Post', 'edit_posts', 'post-new.php' ), |
| 61 | + 15 => array( 'Categories', 'manage_categories', 'edit-tags.php?taxonomy=category' ), |
| 62 | + 16 => array( 'Categories Copy', 'manage_categories', 'edit-tags.php?taxonomy=category' ), |
| 63 | + ), |
| 64 | + 'my-plugin' => array( |
| 65 | + 0 => array( 'Settings', 'manage_options', 'my-plugin' ), |
| 66 | + 1 => array( 'Numeric', 'manage_options', '123' ), |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function tear_down() { |
| 72 | + if ( $this->had_menu ) { |
| 73 | + $GLOBALS['menu'] = $this->orig_menu; |
| 74 | + } else { |
| 75 | + unset( $GLOBALS['menu'] ); |
| 76 | + } |
| 77 | + |
| 78 | + if ( $this->had_submenu ) { |
| 79 | + $GLOBALS['submenu'] = $this->orig_submenu; |
| 80 | + } else { |
| 81 | + unset( $GLOBALS['submenu'] ); |
| 82 | + } |
| 83 | + |
| 84 | + parent::tear_down(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Tests that remove_menu_page() removes the menu item and returns it. |
| 89 | + * |
| 90 | + * @ticket 65819 |
| 91 | + * |
| 92 | + * @covers ::remove_menu_page |
| 93 | + */ |
| 94 | + public function test_remove_menu_page_removes_and_returns_menu_item() { |
| 95 | + $removed = remove_menu_page( 'edit.php' ); |
| 96 | + |
| 97 | + $this->assertSame( |
| 98 | + array( 'Posts', 'edit_posts', 'edit.php', '', 'menu-top menu-icon-post', 'menu-posts', 'dashicons-admin-post' ), |
| 99 | + $removed, |
| 100 | + 'The removed menu item should be returned.' |
| 101 | + ); |
| 102 | + $this->assertSame( array( 2, 25, 26, 30 ), array_keys( $GLOBALS['menu'] ), 'Only the matching menu item should be removed, keeping the other positions.' ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Tests that remove_menu_page() only removes the first menu item with a matching slug. |
| 107 | + * |
| 108 | + * @ticket 65819 |
| 109 | + * |
| 110 | + * @covers ::remove_menu_page |
| 111 | + */ |
| 112 | + public function test_remove_menu_page_only_removes_first_matching_menu_item() { |
| 113 | + $removed = remove_menu_page( 'my-plugin' ); |
| 114 | + |
| 115 | + $this->assertSame( 'My Plugin', $removed[0], 'The first matching menu item should be returned.' ); |
| 116 | + $this->assertSame( array( 2, 5, 26, 30 ), array_keys( $GLOBALS['menu'] ), 'The second matching menu item should be kept.' ); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Tests that remove_menu_page() does not remove the submenu of the removed menu item. |
| 121 | + * |
| 122 | + * @ticket 65819 |
| 123 | + * |
| 124 | + * @covers ::remove_menu_page |
| 125 | + */ |
| 126 | + public function test_remove_menu_page_keeps_submenu_items() { |
| 127 | + $submenu = $GLOBALS['submenu']; |
| 128 | + |
| 129 | + remove_menu_page( 'edit.php' ); |
| 130 | + |
| 131 | + $this->assertSame( $submenu, $GLOBALS['submenu'] ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Tests that remove_menu_page() returns false and keeps the menu when no item matches. |
| 136 | + * |
| 137 | + * @ticket 65819 |
| 138 | + * |
| 139 | + * @covers ::remove_menu_page |
| 140 | + * |
| 141 | + * @dataProvider data_menu_slugs_without_match |
| 142 | + * |
| 143 | + * @param mixed $menu_slug Menu slug. |
| 144 | + */ |
| 145 | + public function test_remove_menu_page_returns_false_without_match( $menu_slug ) { |
| 146 | + $menu = $GLOBALS['menu']; |
| 147 | + |
| 148 | + $this->assertFalse( remove_menu_page( $menu_slug ) ); |
| 149 | + $this->assertSame( $menu, $GLOBALS['menu'], 'The menu should not change.' ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Data provider. |
| 154 | + * |
| 155 | + * @return array[] |
| 156 | + */ |
| 157 | + public static function data_menu_slugs_without_match() { |
| 158 | + return array( |
| 159 | + 'unknown slug' => array( 'does-not-exist' ), |
| 160 | + 'empty slug' => array( '' ), |
| 161 | + 'partial slug' => array( 'edit' ), |
| 162 | + 'different case' => array( 'EDIT.PHP' ), |
| 163 | + 'submenu slug' => array( 'post-new.php' ), |
| 164 | + 'integer for string slug' => array( 123 ), |
| 165 | + 'null' => array( null ), |
| 166 | + 'false' => array( false ), |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Tests that remove_menu_page() returns false when the menu is empty. |
| 172 | + * |
| 173 | + * @ticket 65819 |
| 174 | + * |
| 175 | + * @covers ::remove_menu_page |
| 176 | + */ |
| 177 | + public function test_remove_menu_page_returns_false_for_empty_menu() { |
| 178 | + $GLOBALS['menu'] = array(); |
| 179 | + |
| 180 | + $this->assertFalse( remove_menu_page( 'edit.php' ) ); |
| 181 | + $this->assertSame( array(), $GLOBALS['menu'] ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Tests that remove_submenu_page() removes the submenu item and returns it. |
| 186 | + * |
| 187 | + * @ticket 65819 |
| 188 | + * |
| 189 | + * @covers ::remove_submenu_page |
| 190 | + */ |
| 191 | + public function test_remove_submenu_page_removes_and_returns_submenu_item() { |
| 192 | + $removed = remove_submenu_page( 'edit.php', 'post-new.php' ); |
| 193 | + |
| 194 | + $this->assertSame( array( 'Add Post', 'edit_posts', 'post-new.php' ), $removed, 'The removed submenu item should be returned.' ); |
| 195 | + $this->assertSame( array( 5, 15, 16 ), array_keys( $GLOBALS['submenu']['edit.php'] ), 'Only the matching submenu item should be removed, keeping the other positions.' ); |
| 196 | + $this->assertSame( array( 0, 1 ), array_keys( $GLOBALS['submenu']['my-plugin'] ), 'Other submenus should not change.' ); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Tests that remove_submenu_page() only removes the first submenu item with a matching slug. |
| 201 | + * |
| 202 | + * @ticket 65819 |
| 203 | + * |
| 204 | + * @covers ::remove_submenu_page |
| 205 | + */ |
| 206 | + public function test_remove_submenu_page_only_removes_first_matching_submenu_item() { |
| 207 | + $removed = remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=category' ); |
| 208 | + |
| 209 | + $this->assertSame( 'Categories', $removed[0], 'The first matching submenu item should be returned.' ); |
| 210 | + $this->assertSame( array( 5, 10, 16 ), array_keys( $GLOBALS['submenu']['edit.php'] ), 'The second matching submenu item should be kept.' ); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Tests that remove_submenu_page() keeps an empty submenu after removing its last item. |
| 215 | + * |
| 216 | + * @ticket 65819 |
| 217 | + * |
| 218 | + * @covers ::remove_submenu_page |
| 219 | + */ |
| 220 | + public function test_remove_submenu_page_keeps_empty_submenu() { |
| 221 | + remove_submenu_page( 'my-plugin', 'my-plugin' ); |
| 222 | + remove_submenu_page( 'my-plugin', '123' ); |
| 223 | + |
| 224 | + $this->assertSame( array(), $GLOBALS['submenu']['my-plugin'] ); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Tests that remove_submenu_page() does not remove the parent menu item. |
| 229 | + * |
| 230 | + * @ticket 65819 |
| 231 | + * |
| 232 | + * @covers ::remove_submenu_page |
| 233 | + */ |
| 234 | + public function test_remove_submenu_page_keeps_menu_items() { |
| 235 | + $menu = $GLOBALS['menu']; |
| 236 | + |
| 237 | + remove_submenu_page( 'my-plugin', 'my-plugin' ); |
| 238 | + |
| 239 | + $this->assertSame( $menu, $GLOBALS['menu'] ); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Tests that remove_submenu_page() returns false and keeps the submenus when no item matches. |
| 244 | + * |
| 245 | + * @ticket 65819 |
| 246 | + * |
| 247 | + * @covers ::remove_submenu_page |
| 248 | + * |
| 249 | + * @dataProvider data_submenu_slugs_without_match |
| 250 | + * |
| 251 | + * @param mixed $menu_slug Parent menu slug. |
| 252 | + * @param mixed $submenu_slug Submenu slug. |
| 253 | + */ |
| 254 | + public function test_remove_submenu_page_returns_false_without_match( $menu_slug, $submenu_slug ) { |
| 255 | + $submenu = $GLOBALS['submenu']; |
| 256 | + |
| 257 | + $this->assertFalse( remove_submenu_page( $menu_slug, $submenu_slug ) ); |
| 258 | + $this->assertSame( $submenu, $GLOBALS['submenu'], 'The submenus should not change.' ); |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Data provider. |
| 263 | + * |
| 264 | + * @return array[] |
| 265 | + */ |
| 266 | + public static function data_submenu_slugs_without_match() { |
| 267 | + return array( |
| 268 | + 'unknown parent slug' => array( 'does-not-exist', 'post-new.php' ), |
| 269 | + 'parent without a submenu' => array( 'index.php', 'index.php' ), |
| 270 | + 'empty parent slug' => array( '', 'post-new.php' ), |
| 271 | + 'unknown submenu slug' => array( 'edit.php', 'does-not-exist' ), |
| 272 | + 'empty submenu slug' => array( 'edit.php', '' ), |
| 273 | + 'submenu of another parent' => array( 'my-plugin', 'post-new.php' ), |
| 274 | + 'different case' => array( 'edit.php', 'POST-NEW.PHP' ), |
| 275 | + 'integer for string slug' => array( 'my-plugin', 123 ), |
| 276 | + 'null submenu slug' => array( 'edit.php', null ), |
| 277 | + ); |
| 278 | + } |
| 279 | +} |
0 commit comments