forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminbar.php
More file actions
348 lines (278 loc) · 10.2 KB
/
adminbar.php
File metadata and controls
348 lines (278 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* @group admin-bar
* @group toolbar
* @group admin
*/
class Tests_AdminBar extends WP_UnitTestCase {
protected static $editor_id;
protected static $admin_id;
protected static $no_role_id;
protected static $post_id;
protected static $blog_id;
protected static $user_ids = array();
public static function setUpBeforeClass() {
require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
parent::setUpBeforeClass();
}
public static function wpSetUpBeforeClass( $factory ) {
self::$user_ids[] = self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$user_ids[] = self::$no_role_id = $factory->user->create( array( 'role' => '' ) );
}
public static function wpTearDownAfterClass() {
foreach ( self::$user_ids as $id ) {
self::delete_user( $id );
}
}
/**
* @ticket 21117
*/
function test_content_post_type() {
wp_set_current_user( self::$editor_id );
register_post_type( 'content', array( 'show_in_admin_bar' => true ) );
$admin_bar = new WP_Admin_Bar;
wp_admin_bar_new_content_menu( $admin_bar );
$nodes = $admin_bar->get_nodes();
$this->assertFalse( $nodes['new-content']->parent );
$this->assertEquals( 'new-content', $nodes['add-new-content']->parent );
_unregister_post_type( 'content' );
}
/**
* @ticket 21117
*/
function test_merging_existing_meta_values() {
wp_set_current_user( self::$editor_id );
$admin_bar = new WP_Admin_Bar;
$admin_bar->add_node( array(
'id' => 'test-node',
'meta' => array( 'class' => 'test-class' ),
) );
$node1 = $admin_bar->get_node( 'test-node' );
$this->assertEquals( array( 'class' => 'test-class' ), $node1->meta );
$admin_bar->add_node( array(
'id' => 'test-node',
'meta' => array( 'some-meta' => 'value' ),
) );
$node2 = $admin_bar->get_node( 'test-node' );
$this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node2->meta );
}
/**
* @ticket 25162
*/
public function test_admin_bar_contains_correct_links_for_users_with_no_role() {
if ( is_multisite() ) {
$this->markTestSkipped( 'Test does not run in multisite' );
}
$this->assertFalse( user_can( self::$no_role_id, 'read' ) );
wp_set_current_user( self::$no_role_id );
$wp_admin_bar = $this->get_standard_admin_bar();
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
// Site menu points to the home page instead of the admin URL
$this->assertEquals( home_url( '/' ), $node_site_name->href );
// No profile links as the user doesn't have any permissions on the site
$this->assertFalse( $node_my_account->href );
$this->assertFalse( $node_user_info->href );
$this->assertNull( $node_edit_profile );
}
/**
* @ticket 25162
*/
public function test_admin_bar_contains_correct_links_for_users_with_role() {
if ( is_multisite() ) {
$this->markTestSkipped( 'Test does not run in multisite' );
}
$this->assertTrue( user_can( self::$editor_id, 'read' ) );
wp_set_current_user( self::$editor_id );
$wp_admin_bar = $this->get_standard_admin_bar();
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
// Site menu points to the admin URL
$this->assertEquals( admin_url( '/' ), $node_site_name->href );
$profile_url = admin_url( 'profile.php' );
// Profile URLs point to profile.php
$this->assertEquals( $profile_url, $node_my_account->href );
$this->assertEquals( $profile_url, $node_user_info->href );
$this->assertEquals( $profile_url, $node_edit_profile->href );
}
/**
* @ticket 25162
* @group multisite
*/
public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_blog() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Test only runs in multisite' );
}
$blog_id = self::factory()->blog->create( array(
'user_id' => self::$admin_id,
) );
$this->assertTrue( user_can( self::$admin_id, 'read' ) );
$this->assertTrue( user_can( self::$editor_id, 'read' ) );
$this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) );
$this->assertFalse( is_user_member_of_blog( self::$editor_id, $blog_id ) );
wp_set_current_user( self::$editor_id );
switch_to_blog( $blog_id );
$wp_admin_bar = $this->get_standard_admin_bar();
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
// get primary blog
$primary = get_active_blog_for_user( self::$editor_id );
$this->assertInternalType( 'object', $primary );
// No Site menu as the user isn't a member of this blog
$this->assertNull( $node_site_name );
$primary_profile_url = get_admin_url( $primary->blog_id, 'profile.php' );
// Ensure the user's primary blog is not the same as the main site
$this->assertNotEquals( $primary_profile_url, admin_url( 'profile.php' ) );
// Profile URLs should go to the user's primary blog
$this->assertEquals( $primary_profile_url, $node_my_account->href );
$this->assertEquals( $primary_profile_url, $node_user_info->href );
$this->assertEquals( $primary_profile_url, $node_edit_profile->href );
restore_current_blog();
}
/**
* @ticket 25162
* @group multisite
*/
public function test_admin_bar_contains_correct_links_for_users_with_no_role_on_network() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Test only runs in multisite' );
}
$this->assertTrue( user_can( self::$admin_id, 'read' ) );
$this->assertFalse( user_can( self::$no_role_id, 'read' ) );
$blog_id = self::factory()->blog->create( array(
'user_id' => self::$admin_id,
) );
$this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) );
$this->assertFalse( is_user_member_of_blog( self::$no_role_id, $blog_id ) );
$this->assertTrue( is_user_member_of_blog( self::$no_role_id, get_current_blog_id() ) );
// Remove `$nobody` from the current blog, so they're not a member of any blog
$removed = remove_user_from_blog( self::$no_role_id, get_current_blog_id() );
$this->assertTrue( $removed );
$this->assertFalse( is_user_member_of_blog( self::$no_role_id, get_current_blog_id() ) );
wp_set_current_user( self::$no_role_id );
switch_to_blog( $blog_id );
$wp_admin_bar = $this->get_standard_admin_bar();
$node_site_name = $wp_admin_bar->get_node( 'site-name' );
$node_my_account = $wp_admin_bar->get_node( 'my-account' );
$node_user_info = $wp_admin_bar->get_node( 'user-info' );
$node_edit_profile = $wp_admin_bar->get_node( 'edit-profile' );
// get primary blog
$primary = get_active_blog_for_user( self::$no_role_id );
$this->assertNull( $primary );
// No Site menu as the user isn't a member of this site
$this->assertNull( $node_site_name );
$user_profile_url = user_admin_url( 'profile.php' );
$this->assertNotEquals( $user_profile_url, admin_url( 'profile.php' ) );
// Profile URLs should go to the user's primary blog
$this->assertEquals( $user_profile_url, $node_my_account->href );
$this->assertEquals( $user_profile_url, $node_user_info->href );
$this->assertEquals( $user_profile_url, $node_edit_profile->href );
restore_current_blog();
}
protected function get_standard_admin_bar() {
global $wp_admin_bar;
_wp_admin_bar_init();
$this->assertTrue( is_admin_bar_showing() );
$this->assertInstanceOf( 'WP_Admin_Bar', $wp_admin_bar );
do_action_ref_array( 'admin_bar_menu', array( &$wp_admin_bar ) );
return $wp_admin_bar;
}
/**
* @ticket 32495
*
* @dataProvider data_admin_bar_nodes_with_tabindex_meta
*
* @param array $node_data The data for a node, passed to `WP_Admin_Bar::add_node()`.
* @param string $expected_html The expected HTML when admin menu is rendered.
*/
public function test_admin_bar_with_tabindex_meta( $node_data, $expected_html ) {
$admin_bar = new WP_Admin_Bar();
$admin_bar->add_node( $node_data );
$admin_bar_html = get_echo( array( $admin_bar, 'render' ) );
$this->assertContains( $expected_html, $admin_bar_html );
}
/**
* Data provider for test_admin_bar_with_tabindex_meta().
*
* @return array {
* @type array {
* @type array $node_data The data for a node, passed to `WP_Admin_Bar::add_node()`.
* @type string $expected_html The expected HTML when admin bar is rendered.
* }
* }
*/
public function data_admin_bar_nodes_with_tabindex_meta() {
return array(
array(
// No tabindex.
array(
'id' => 'test-node',
),
'<div class="ab-item ab-empty-item">',
),
array(
// Empty string.
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => '' ),
),
'<div class="ab-item ab-empty-item">',
),
array(
// Integer 1 as string.
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => '1' ),
),
'<div class="ab-item ab-empty-item" tabindex="1">',
),
array(
// Integer -1 as string.
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => '-1' ),
),
'<div class="ab-item ab-empty-item" tabindex="-1">',
),
array(
// Integer 0 as string.
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => '0' ),
),
'<div class="ab-item ab-empty-item" tabindex="0">',
),
array(
// Integer, 0.
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => 0 ),
),
'<div class="ab-item ab-empty-item" tabindex="0">',
),
array(
// Integer, 2.
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => 2 ),
),
'<div class="ab-item ab-empty-item" tabindex="2">',
),
array(
// Boolean, false
array(
'id' => 'test-node',
'meta' => array( 'tabindex' => false ),
),
'<div class="ab-item ab-empty-item">',
),
);
}
}