Skip to content

Commit b227d20

Browse files
committed
Tests: Cover admin page title resolution.
Test `get_admin_page_title()` with an existing title, top-level menus, core and plugin submenus, and post type parents. Verify title fallback behavior and restore the affected globals and query parameter. Developed in: #14627 Props shailu25, huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64749 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 63ff33c commit b227d20

1 file changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
3+
/**
4+
* Tests for get_admin_page_title().
5+
*
6+
* @group admin
7+
* @group plugins
8+
*
9+
* @covers ::get_admin_page_title
10+
*/
11+
class Tests_Admin_Includes_Plugin_GetAdminPageTitle extends WP_UnitTestCase {
12+
13+
/**
14+
* Names of the globals used by the function under test.
15+
*
16+
* @var string[]
17+
*/
18+
const GLOBALS_USED = array(
19+
'title',
20+
'menu',
21+
'submenu',
22+
'pagenow',
23+
'typenow',
24+
'plugin_page',
25+
'parent_file',
26+
'admin_page_hooks',
27+
'_wp_real_parent_file',
28+
'_wp_menu_nopriv',
29+
'_wp_submenu_nopriv',
30+
);
31+
32+
/**
33+
* Original values of the globals that were set before the test, keyed by name.
34+
*
35+
* @var array
36+
*/
37+
private $orig_globals = array();
38+
39+
/**
40+
* Whether $_GET['page'] was set before the test.
41+
*
42+
* @var bool
43+
*/
44+
private $had_get_page;
45+
46+
/**
47+
* Original $_GET['page'] value.
48+
*
49+
* @var mixed
50+
*/
51+
private $orig_get_page;
52+
53+
public function set_up() {
54+
parent::set_up();
55+
56+
require_once ABSPATH . 'wp-admin/includes/plugin.php';
57+
58+
$this->orig_globals = array();
59+
60+
foreach ( self::GLOBALS_USED as $name ) {
61+
if ( array_key_exists( $name, $GLOBALS ) ) {
62+
$this->orig_globals[ $name ] = $GLOBALS[ $name ];
63+
}
64+
65+
unset( $GLOBALS[ $name ] );
66+
}
67+
68+
$this->had_get_page = array_key_exists( 'page', $_GET );
69+
$this->orig_get_page = $this->had_get_page ? $_GET['page'] : null;
70+
unset( $_GET['page'] );
71+
72+
$GLOBALS['title'] = '';
73+
$GLOBALS['plugin_page'] = '';
74+
$GLOBALS['typenow'] = '';
75+
$GLOBALS['parent_file'] = '';
76+
$GLOBALS['admin_page_hooks'] = array();
77+
$GLOBALS['menu'] = array();
78+
$GLOBALS['submenu'] = array();
79+
}
80+
81+
public function tear_down() {
82+
foreach ( self::GLOBALS_USED as $name ) {
83+
if ( array_key_exists( $name, $this->orig_globals ) ) {
84+
$GLOBALS[ $name ] = $this->orig_globals[ $name ];
85+
} else {
86+
unset( $GLOBALS[ $name ] );
87+
}
88+
}
89+
90+
if ( $this->had_get_page ) {
91+
$_GET['page'] = $this->orig_get_page;
92+
} else {
93+
unset( $_GET['page'] );
94+
}
95+
96+
parent::tear_down();
97+
}
98+
99+
/**
100+
* @ticket 65819
101+
*/
102+
public function test_returns_existing_title_global() {
103+
$GLOBALS['title'] = 'Already Set Title';
104+
105+
$this->assertSame( 'Already Set Title', get_admin_page_title() );
106+
}
107+
108+
/**
109+
* @ticket 65819
110+
*/
111+
public function test_returns_top_level_menu_title_by_pagenow() {
112+
$GLOBALS['pagenow'] = 'profile.php';
113+
$GLOBALS['menu'] = array(
114+
70 => array( 'Profile', 'read', 'profile.php', 'Your Profile', 'menu-top', 'menu-profile', 'dashicons-admin-users' ),
115+
);
116+
117+
$this->assertSame( 'Your Profile', get_admin_page_title() );
118+
$this->assertSame( 'Your Profile', $GLOBALS['title'], 'The $title global should be updated.' );
119+
}
120+
121+
/**
122+
* @ticket 65819
123+
*/
124+
public function test_returns_menu_name_when_page_title_is_missing() {
125+
$GLOBALS['pagenow'] = 'profile.php';
126+
$GLOBALS['menu'] = array(
127+
70 => array( 'Profile', 'read', 'profile.php' ),
128+
);
129+
130+
$this->assertSame( 'Profile', get_admin_page_title() );
131+
}
132+
133+
/**
134+
* @ticket 65819
135+
*/
136+
public function test_returns_submenu_title_for_plugin_page() {
137+
$GLOBALS['pagenow'] = 'options-general.php';
138+
$GLOBALS['plugin_page'] = 'my-settings';
139+
$_GET['page'] = 'my-settings';
140+
$GLOBALS['menu'] = array(
141+
80 => array( 'Settings', 'manage_options', 'options-general.php', 'Settings', 'menu-top', 'menu-settings', 'dashicons-admin-settings' ),
142+
);
143+
$GLOBALS['submenu'] = array(
144+
'options-general.php' => array(
145+
10 => array( 'General', 'manage_options', 'options-general.php', 'General Settings' ),
146+
50 => array( 'My Settings', 'manage_options', 'my-settings', 'My Settings Page' ),
147+
),
148+
);
149+
150+
$this->assertSame( 'My Settings Page', get_admin_page_title() );
151+
$this->assertSame( 'My Settings Page', $GLOBALS['title'], 'The $title global should be updated.' );
152+
}
153+
154+
/**
155+
* @ticket 65819
156+
*/
157+
public function test_returns_submenu_title_for_core_page_by_pagenow() {
158+
$GLOBALS['pagenow'] = 'options-general.php';
159+
$GLOBALS['menu'] = array(
160+
80 => array( 'Settings', 'manage_options', 'options-general.php', 'Settings', 'menu-top', 'menu-settings', 'dashicons-admin-settings' ),
161+
);
162+
$GLOBALS['submenu'] = array(
163+
'options-general.php' => array(
164+
10 => array( 'General', 'manage_options', 'options-general.php', 'General Settings' ),
165+
),
166+
);
167+
168+
$this->assertSame( 'General Settings', get_admin_page_title() );
169+
}
170+
171+
/**
172+
* @ticket 65819
173+
*/
174+
public function test_returns_top_level_plugin_title_on_admin_php() {
175+
$GLOBALS['pagenow'] = 'admin.php';
176+
$GLOBALS['plugin_page'] = 'my-plugin';
177+
$GLOBALS['menu'] = array(
178+
25 => array( 'My Plugin', 'manage_options', 'my-plugin', 'My Plugin Title', 'menu-top', 'toplevel_page_my-plugin', 'dashicons-admin-generic' ),
179+
);
180+
// Use a distinct submenu slug/title so the expected title must come from the top-level menu fallback.
181+
$GLOBALS['submenu'] = array(
182+
'my-plugin' => array(
183+
0 => array( 'Settings', 'manage_options', 'my-plugin-settings', 'My Plugin Settings' ),
184+
),
185+
);
186+
187+
$this->assertSame( 'My Plugin Title', get_admin_page_title() );
188+
}
189+
190+
/**
191+
* @ticket 65819
192+
*/
193+
public function test_returns_submenu_title_for_post_type_parent() {
194+
$GLOBALS['pagenow'] = 'edit.php';
195+
$GLOBALS['typenow'] = 'page';
196+
$GLOBALS['plugin_page'] = 'my-page-tool';
197+
$GLOBALS['menu'] = array(
198+
20 => array( 'Pages', 'edit_pages', 'edit.php?post_type=page', 'Pages', 'menu-top', 'menu-pages', 'dashicons-admin-page' ),
199+
);
200+
$GLOBALS['submenu'] = array(
201+
'edit.php?post_type=page' => array(
202+
5 => array( 'All Pages', 'edit_pages', 'edit.php?post_type=page', 'Pages' ),
203+
15 => array( 'My Tool', 'edit_pages', 'my-page-tool', 'Page Tool Title' ),
204+
),
205+
);
206+
207+
$this->assertSame( 'Page Tool Title', get_admin_page_title() );
208+
}
209+
}

0 commit comments

Comments
 (0)