forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapabilities.php
More file actions
754 lines (592 loc) · 27.8 KB
/
capabilities.php
File metadata and controls
754 lines (592 loc) · 27.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
<?php
// Test roles and capabilities via the WP_User class
/**
* @group user
* @group capabilities
*/
class Tests_User_Capabilities extends WP_UnitTestCase {
protected $user_ids = array();
function setUp() {
parent::setUp();
// keep track of users we create
$this->_flush_roles();
$this->orig_users = get_users();
}
function _flush_roles() {
// we want to make sure we're testing against the db, not just in-memory data
// this will flush everything and reload it from the db
unset($GLOBALS['wp_user_roles']);
global $wp_roles;
if ( is_object( $wp_roles ) )
$wp_roles->_init();
}
function _meta_yes_you_can( $can, $key, $post_id, $user_id, $cap, $caps ) {
return true;
}
function _meta_no_you_cant( $can, $key, $post_id, $user_id, $cap, $caps ) {
return false;
}
function _meta_filter( $meta_value, $meta_key, $meta_type ) {
return $meta_value;
}
// test the default roles
function test_user_administrator() {
$id = $this->factory->user->create( array( 'role' => 'administrator' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// make sure the role name is correct
$this->assertEquals(array('administrator'), $user->roles);
// check a few of the main capabilities
$this->assertTrue($user->has_cap('switch_themes'));
$this->assertTrue($user->has_cap('list_users'));
$this->assertTrue($user->has_cap('manage_options'));
$this->assertTrue($user->has_cap('level_10'));
}
function test_user_editor() {
$id = $this->factory->user->create( array( 'role' => 'editor' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// make sure the role name is correct
$this->assertEquals(array('editor'), $user->roles);
// check a few of the main capabilities
$this->assertTrue($user->has_cap('moderate_comments'));
$this->assertTrue($user->has_cap('manage_categories'));
$this->assertTrue($user->has_cap('upload_files'));
$this->assertTrue($user->has_cap('level_7'));
// and a few capabilities this user doesn't have
$this->assertFalse($user->has_cap('switch_themes'));
$this->assertFalse($user->has_cap('edit_users'));
$this->assertFalse($user->has_cap('level_8'));
}
function test_user_author() {
$id = $this->factory->user->create( array( 'role' => 'author' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// make sure the role name is correct
$this->assertEquals(array('author'), $user->roles);
// check a few of the main capabilities
$this->assertTrue($user->has_cap('edit_posts'));
$this->assertTrue($user->has_cap('edit_published_posts'));
$this->assertTrue($user->has_cap('upload_files'));
$this->assertTrue($user->has_cap('level_2'));
// and a few capabilities this user doesn't have
$this->assertFalse($user->has_cap('moderate_comments'));
$this->assertFalse($user->has_cap('manage_categories'));
$this->assertFalse($user->has_cap('level_3'));
}
function test_user_contributor() {
$id = $this->factory->user->create( array( 'role' => 'contributor' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// make sure the role name is correct
$this->assertEquals(array('contributor'), $user->roles);
// check a few of the main capabilities
$this->assertTrue($user->has_cap('edit_posts'));
$this->assertTrue($user->has_cap('read'));
$this->assertTrue($user->has_cap('level_1'));
$this->assertTrue($user->has_cap('level_0'));
// and a few capabilities this user doesn't have
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('edit_published_posts'));
$this->assertFalse($user->has_cap('level_2'));
}
function test_user_subscriber() {
$id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// make sure the role name is correct
$this->assertEquals(array('subscriber'), $user->roles);
// check a few of the main capabilities
$this->assertTrue($user->has_cap('read'));
$this->assertTrue($user->has_cap('level_0'));
// and a few capabilities this user doesn't have
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('edit_posts'));
$this->assertFalse($user->has_cap('level_1'));
}
// a role that doesn't exist
function test_bogus_role() {
_disable_wp_die();
$id = $this->factory->user->create( array( 'role' => rand_str() ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// user has no role and no capabilities
$this->assertEquals(array(), $user->roles);
$this->assertFalse($user->has_cap('level_0'));
_enable_wp_die();
}
// a user with multiple roles
function test_user_subscriber_contributor() {
$id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
$user->add_role('contributor');
// nuke and re-fetch the object to make sure it was stored
$user = NULL;
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// user should have two roles now
$this->assertEquals(array('subscriber', 'contributor'), $user->roles);
// with contributor capabilities
$this->assertTrue($user->has_cap('edit_posts'));
$this->assertTrue($user->has_cap('read'));
$this->assertTrue($user->has_cap('level_1'));
$this->assertTrue($user->has_cap('level_0'));
// but not these
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('edit_published_posts'));
$this->assertFalse($user->has_cap('level_2'));
}
function test_add_empty_role() {
// add_role($role, $display_name, $capabilities = '')
// randomly named role with no capabilities
global $wp_roles;
$role_name = rand_str();
add_role($role_name, 'Janitor', array());
$this->_flush_roles();
$this->assertTrue($wp_roles->is_role($role_name));
$id = $this->factory->user->create( array( 'role' => $role_name ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
$this->assertEquals(array($role_name), $user->roles);
// user shouldn't have any capabilities; test a quick sample
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('edit_published_posts'));
$this->assertFalse($user->has_cap('level_1'));
$this->assertFalse($user->has_cap('level_0'));
// clean up
remove_role($role_name);
$this->_flush_roles();
$this->assertFalse($wp_roles->is_role($role_name));
}
function test_add_role() {
// add_role($role, $display_name, $capabilities = '')
// randomly named role with a few capabilities
global $wp_roles;
$role_name = rand_str();
add_role($role_name, 'Janitor', array('edit_posts'=>true, 'edit_pages'=>true, 'level_0'=>true, 'level_1'=>true, 'level_2'=>true));
$this->_flush_roles();
$this->assertTrue($wp_roles->is_role($role_name));
$id = $this->factory->user->create( array( 'role' => $role_name ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
$this->assertEquals(array($role_name), $user->roles);
// the user should have all the above caps
$this->assertTrue($user->has_cap($role_name));
$this->assertTrue($user->has_cap('edit_posts'));
$this->assertTrue($user->has_cap('edit_pages'));
$this->assertTrue($user->has_cap('level_0'));
$this->assertTrue($user->has_cap('level_1'));
$this->assertTrue($user->has_cap('level_2'));
// shouldn't have any other caps
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('edit_published_posts'));
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('level_3'));
// clean up
remove_role($role_name);
$this->_flush_roles();
$this->assertFalse($wp_roles->is_role($role_name));
}
function test_role_add_cap() {
// change the capabilites associated with a role and make sure the change is reflected in has_cap()
global $wp_roles;
$role_name = rand_str();
add_role( $role_name, 'Janitor', array('level_1'=>true) );
$this->_flush_roles();
$this->assertTrue( $wp_roles->is_role($role_name) );
// assign a user to that role
$id = $this->factory->user->create( array( 'role' => $role_name ) );
// now add a cap to the role
$wp_roles->add_cap($role_name, 'sweep_floor');
$this->_flush_roles();
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
$this->assertEquals(array($role_name), $user->roles);
// the user should have all the above caps
$this->assertTrue($user->has_cap($role_name));
$this->assertTrue($user->has_cap('level_1'));
$this->assertTrue($user->has_cap('sweep_floor'));
// shouldn't have any other caps
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('edit_published_posts'));
$this->assertFalse($user->has_cap('upload_files'));
$this->assertFalse($user->has_cap('level_4'));
// clean up
remove_role($role_name);
$this->_flush_roles();
$this->assertFalse($wp_roles->is_role($role_name));
}
function test_role_remove_cap() {
// change the capabilites associated with a role and make sure the change is reflected in has_cap()
global $wp_roles;
$role_name = rand_str();
add_role( $role_name, 'Janitor', array('level_1'=>true, 'sweep_floor'=>true, 'polish_doorknobs'=>true) );
$this->_flush_roles();
$this->assertTrue( $wp_roles->is_role($role_name) );
// assign a user to that role
$id = $this->factory->user->create( array( 'role' => $role_name ) );
// now remove a cap from the role
$wp_roles->remove_cap($role_name, 'polish_doorknobs');
$this->_flush_roles();
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
$this->assertEquals(array($role_name), $user->roles);
// the user should have all the above caps
$this->assertTrue($user->has_cap($role_name));
$this->assertTrue($user->has_cap('level_1'));
$this->assertTrue($user->has_cap('sweep_floor'));
// shouldn't have the removed cap
$this->assertFalse($user->has_cap('polish_doorknobs'));
// clean up
remove_role($role_name);
$this->_flush_roles();
$this->assertFalse($wp_roles->is_role($role_name));
}
function test_user_add_cap() {
// add an extra capability to a user
// there are two contributors
$id_1 = $this->factory->user->create( array( 'role' => 'contributor' ) );
$id_2 = $this->factory->user->create( array( 'role' => 'contributor' ) );
// user 1 has an extra capability
$user_1 = new WP_User($id_1);
$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
$user_1->add_cap('publish_posts');
// re-fetch both users from the db
$user_1 = new WP_User($id_1);
$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
$user_2 = new WP_User($id_2);
$this->assertTrue($user_2->exists(), "Problem getting user $id_2");
// make sure they're both still contributors
$this->assertEquals(array('contributor'), $user_1->roles);
$this->assertEquals(array('contributor'), $user_2->roles);
// check the extra cap on both users
$this->assertTrue($user_1->has_cap('publish_posts'));
$this->assertFalse($user_2->has_cap('publish_posts'));
// make sure the other caps didn't get messed up
$this->assertTrue($user_1->has_cap('edit_posts'));
$this->assertTrue($user_1->has_cap('read'));
$this->assertTrue($user_1->has_cap('level_1'));
$this->assertTrue($user_1->has_cap('level_0'));
$this->assertFalse($user_1->has_cap('upload_files'));
$this->assertFalse($user_1->has_cap('edit_published_posts'));
$this->assertFalse($user_1->has_cap('level_2'));
}
function test_user_remove_cap() {
// add an extra capability to a user then remove it
// there are two contributors
$id_1 = $this->factory->user->create( array( 'role' => 'contributor' ) );
$id_2 = $this->factory->user->create( array( 'role' => 'contributor' ) );
// user 1 has an extra capability
$user_1 = new WP_User($id_1);
$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
$user_1->add_cap('publish_posts');
// now remove the extra cap
$user_1->remove_cap('publish_posts');
// re-fetch both users from the db
$user_1 = new WP_User($id_1);
$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
$user_2 = new WP_User($id_2);
$this->assertTrue($user_2->exists(), "Problem getting user $id_2");
// make sure they're both still contributors
$this->assertEquals(array('contributor'), $user_1->roles);
$this->assertEquals(array('contributor'), $user_2->roles);
// check the removed cap on both users
$this->assertFalse($user_1->has_cap('publish_posts'));
$this->assertFalse($user_2->has_cap('publish_posts'));
}
function test_user_level_update() {
// make sure the user_level is correctly set and changed with the user's role
// user starts as an author
$id = $this->factory->user->create( array( 'role' => 'author' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// author = user level 2
$this->assertEquals( 2, $user->user_level );
// they get promoted to editor - level should get bumped to 7
$user->set_role('editor');
$this->assertEquals( 7, $user->user_level );
// demoted to contributor - level is reduced to 1
$user->set_role('contributor');
$this->assertEquals( 1, $user->user_level );
// if they have two roles, user_level should be the max of the two
$user->add_role('editor');
$this->assertEquals(array('contributor', 'editor'), $user->roles);
$this->assertEquals( 7, $user->user_level );
}
function test_user_remove_all_caps() {
// user starts as an author
$id = $this->factory->user->create( array( 'role' => 'author' ) );
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// add some extra capabilities
$user->add_cap('make_coffee');
$user->add_cap('drink_coffee');
// re-fetch
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
$this->assertTrue($user->has_cap('make_coffee'));
$this->assertTrue($user->has_cap('drink_coffee'));
// all caps are removed
$user->remove_all_caps();
// re-fetch
$user = new WP_User($id);
$this->assertTrue($user->exists(), "Problem getting user $id");
// capabilities for the author role should be gone
# $this->assertFalse($user->has_cap('edit_posts'));
# $this->assertFalse($user->has_cap('edit_published_posts'));
# $this->assertFalse($user->has_cap('upload_files'));
# $this->assertFalse($user->has_cap('level_2'));
// the extra capabilities should be gone
$this->assertFalse($user->has_cap('make_coffee'));
$this->assertFalse($user->has_cap('drink_coffee'));
// user level should be empty
$this->assertEmpty( $user->user_level );
}
function test_post_meta_caps() {
// simple tests for some common meta capabilities
// Make our author
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
// make a post
$post = $this->factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) );
// the author of the post
$this->assertTrue($author->exists(), "Problem getting user $author->ID");
// add some other users
$admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
// administrators, editors and the post owner can edit it
$this->assertTrue($admin->has_cap('edit_post', $post));
$this->assertTrue($author->has_cap('edit_post', $post));
$this->assertTrue($editor->has_cap('edit_post', $post));
// other authors and contributors can't
$this->assertFalse($author_2->has_cap('edit_post', $post));
$this->assertFalse($contributor->has_cap('edit_post', $post));
// administrators, editors and the post owner can delete it
$this->assertTrue($admin->has_cap('delete_post', $post));
$this->assertTrue($author->has_cap('delete_post', $post));
$this->assertTrue($editor->has_cap('delete_post', $post));
// other authors and contributors can't
$this->assertFalse($author_2->has_cap('delete_post', $post));
$this->assertFalse($contributor->has_cap('delete_post', $post));
// administrators, editors, and authors can publish it
$this->assertTrue($admin->has_cap('publish_post', $post));
$this->assertTrue($author->has_cap('publish_post', $post));
$this->assertTrue($editor->has_cap('publish_post', $post));
$this->assertTrue($author_2->has_cap('publish_post', $post));
// contributors can't
$this->assertFalse($contributor->has_cap('publish_post', $post));
register_post_type( 'something', array( 'capabilities' => array( 'edit_posts' => 'draw_somethings' ) ) );
$something = get_post_type_object( 'something' );
$this->assertEquals( 'draw_somethings', $something->cap->edit_posts );
$this->assertEquals( 'draw_somethings', $something->cap->create_posts );
register_post_type( 'something', array( 'capabilities' =>
array( 'edit_posts' => 'draw_somethings', 'create_posts' => 'create_somethings' ) ) );
$something = get_post_type_object( 'something' );
$this->assertEquals( 'draw_somethings', $something->cap->edit_posts );
$this->assertEquals( 'create_somethings', $something->cap->create_posts );
_unregister_post_type( 'something' );
// Test meta authorization callbacks
if ( function_exists( 'register_meta') ) {
$this->assertTrue( $admin->has_cap('edit_post_meta', $post) );
$this->assertTrue( $admin->has_cap('add_post_meta', $post) );
$this->assertTrue( $admin->has_cap('delete_post_meta', $post) );
$this->assertFalse( $admin->has_cap('edit_post_meta', $post, '_protected') );
$this->assertFalse( $admin->has_cap('add_post_meta', $post, '_protected') );
$this->assertFalse( $admin->has_cap('delete_post_meta', $post, '_protected') );
register_meta( 'post', '_protected', array( $this, '_meta_filter' ), array( $this, '_meta_yes_you_can' ) );
$this->assertTrue( $admin->has_cap('edit_post_meta', $post, '_protected') );
$this->assertTrue( $admin->has_cap('add_post_meta', $post, '_protected') );
$this->assertTrue( $admin->has_cap('delete_post_meta', $post, '_protected') );
$this->assertTrue( $admin->has_cap('edit_post_meta', $post, 'not_protected') );
$this->assertTrue( $admin->has_cap('add_post_meta', $post, 'not_protected') );
$this->assertTrue( $admin->has_cap('delete_post_meta', $post, 'not_protected') );
register_meta( 'post', 'not_protected', array( $this, '_meta_filter' ), array( $this, '_meta_no_you_cant' ) );
$this->assertFalse( $admin->has_cap('edit_post_meta', $post, 'not_protected') );
$this->assertFalse( $admin->has_cap('add_post_meta', $post, 'not_protected') );
$this->assertFalse( $admin->has_cap('delete_post_meta', $post, 'not_protected') );
}
}
function authorless_post_statuses() {
return array( array( 'draft' ), array( 'private' ), array( 'publish' ) );
}
/**
* @ticket 27020
* @dataProvider authorless_post_statuses
*/
function test_authorless_post( $status ) {
// Make a post without an author
$post = $this->factory->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
// Add an editor and contributor
$editor = $this->factory->user->create_and_get( array( 'role' => 'editor' ) );
$contributor = $this->factory->user->create_and_get( array( 'role' => 'contributor' ) );
// editor can edit, view, and trash
$this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
$this->assertTrue( $editor->has_cap( 'delete_post', $post ) );
$this->assertTrue( $editor->has_cap( 'read_post', $post ) );
// a contributor cannot (except read a published post)
$this->assertFalse( $contributor->has_cap( 'edit_post', $post ) );
$this->assertFalse( $contributor->has_cap( 'delete_post', $post ) );
$this->assertEquals( $status === 'publish', $contributor->has_cap( 'read_post', $post ) );
}
/**
* @ticket 16714
*/
function test_create_posts_caps() {
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
// create_posts isn't a real cap.
$this->assertFalse($admin->has_cap('create_posts'));
$this->assertFalse($author->has_cap('create_posts'));
$this->assertFalse($editor->has_cap('create_posts'));
$this->assertFalse($author_2->has_cap('create_posts'));
$this->assertFalse($contributor->has_cap('create_posts'));
register_post_type( 'foobar' );
$cap = get_post_type_object( 'foobar' )->cap;
$this->assertEquals( 'edit_posts', $cap->create_posts );
$this->assertTrue($admin->has_cap( $cap->create_posts ));
$this->assertTrue($admin->has_cap( $cap->create_posts ));
$this->assertTrue($author->has_cap( $cap->create_posts ));
$this->assertTrue($editor->has_cap( $cap->create_posts ));
$this->assertTrue($author_2->has_cap( $cap->create_posts ));
$this->assertTrue($contributor->has_cap( $cap->create_posts ));
_unregister_post_type( 'foobar' );
// Primitive capability edit_foobars is not assigned to any users.
register_post_type( 'foobar', array( 'capability_type' => array( 'foobar', 'foobars' ) ) );
$cap = get_post_type_object( 'foobar' )->cap;
$this->assertEquals( 'edit_foobars', $cap->create_posts );
$this->assertFalse($admin->has_cap( $cap->create_posts ));
$this->assertFalse($author->has_cap( $cap->create_posts ));
$this->assertFalse($editor->has_cap( $cap->create_posts ));
$this->assertFalse($author_2->has_cap( $cap->create_posts ));
$this->assertFalse($contributor->has_cap( $cap->create_posts ));
// Add edit_foobars primitive cap to a user.
$admin->add_cap( 'edit_foobars', true );
$admin = new WP_User( $admin->ID );
$this->assertTrue($admin->has_cap( $cap->create_posts ));
$this->assertFalse($author->has_cap( $cap->create_posts ));
$this->assertFalse($editor->has_cap( $cap->create_posts ));
$this->assertFalse($author_2->has_cap( $cap->create_posts ));
$this->assertFalse($contributor->has_cap( $cap->create_posts ));
_unregister_post_type( 'foobar' );
$cap = get_post_type_object( 'attachment' )->cap;
$this->assertEquals( 'upload_files', $cap->create_posts );
$this->assertEquals( 'edit_posts', $cap->edit_posts );
$this->assertTrue( $author->has_cap( $cap->create_posts ) );
$this->assertTrue( $author->has_cap( $cap->edit_posts ) );
$this->assertTrue( $contributor->has_cap( $cap->edit_posts ) );
$this->assertFalse( $contributor->has_cap( $cap->create_posts ) );
}
function test_page_meta_caps() {
// simple tests for some common meta capabilities
// Make our author
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
// make a page
$page = $this->factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) );
// the author of the page
$this->assertTrue($author->exists(), "Problem getting user " . $author->ID);
// add some other users
$admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) );
$contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
// administrators, editors and the post owner can edit it
$this->assertTrue($admin->has_cap('edit_page', $page));
$this->assertTrue($editor->has_cap('edit_page', $page));
// other authors and contributors can't
$this->assertFalse($author->has_cap('edit_page', $page));
$this->assertFalse($author_2->has_cap('edit_page', $page));
$this->assertFalse($contributor->has_cap('edit_page', $page));
// administrators, editors and the post owner can delete it
$this->assertTrue($admin->has_cap('delete_page', $page));
$this->assertTrue($editor->has_cap('delete_page', $page));
// other authors and contributors can't
$this->assertFalse($author->has_cap('delete_page', $page));
$this->assertFalse($author_2->has_cap('delete_page', $page));
$this->assertFalse($contributor->has_cap('delete_page', $page));
}
/**
* @ticket 21786
*/
function test_negative_caps() {
$author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
$author->add_cap( 'foo', false );
$this->assertTrue ( isset( $author->caps['foo'] ) );
$author->remove_cap( 'foo' );
$this->assertFalse ( isset( $author->caps['foo'] ) );
}
/**
* @ticket 18932
*/
function test_set_role_same_role() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$caps = $user->caps;
$this->assertNotEmpty( $user->caps );
$user->set_role( 'administrator' );
$this->assertNotEmpty( $user->caps );
$this->assertEquals( $caps, $user->caps );
}
function test_current_user_can_for_blog() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
$old_uid = get_current_user_id();
wp_set_current_user( $user->ID );
$this->assertTrue( current_user_can_for_blog( get_current_blog_id(), 'edit_posts' ) );
$this->assertFalse( current_user_can_for_blog( get_current_blog_id(), 'foo_the_bar' ) );
if ( ! is_multisite() ) {
$this->assertTrue( current_user_can_for_blog( 12345, 'edit_posts' ) );
return;
}
$this->assertFalse( current_user_can_for_blog( 12345, 'edit_posts' ) );
$blog_id = $this->factory->blog->create( array( 'user_id' => $user->ID ) );
$this->assertTrue( current_user_can_for_blog( $blog_id, 'edit_posts' ) );
$this->assertFalse( current_user_can_for_blog( $blog_id, 'foo_the_bar' ) );
wp_set_current_user( $old_uid );
}
function test_borked_current_user_can_for_blog() {
if ( ! is_multisite() ) {
$this->markTestSkipped( 'Test only runs in multisite' );
return;
}
$orig_blog_id = get_current_blog_id();
$blog_id = $this->factory->blog->create();
$this->_nullify_current_user();
add_action( 'switch_blog', array( $this, '_nullify_current_user_and_keep_nullifying_user' ) );
current_user_can_for_blog( $blog_id, 'edit_posts' );
$this->assertEquals( $orig_blog_id, get_current_blog_id() );
}
function _nullify_current_user() {
// Prevents fatal errors in ::tearDown()'s and other uses of restore_current_blog()
$function_stack = wp_debug_backtrace_summary( null, 0, false );
if ( in_array( 'restore_current_blog', $function_stack ) ) {
return;
}
$GLOBALS['current_user'] = null;
}
function _nullify_current_user_and_keep_nullifying_user() {
add_action( 'set_current_user', array( $this, '_nullify_current_user' ) );
}
/**
* @ticket 28374
*/
function test_current_user_edit_caps() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) );
wp_set_current_user( $user->ID );
$user->add_cap( 'publish_posts' );
$user->add_cap( 'publish_pages' );
$this->assertTrue( $user->has_cap( 'publish_posts' ) );
$this->assertTrue( $user->has_cap( 'publish_pages' ) );
$user->remove_cap( 'publish_pages' );
$this->assertFalse( $user->has_cap( 'publish_pages' ) );
}
function test_subscriber_cant_edit_posts() {
$user = new WP_User( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
wp_set_current_user( $user->ID );
$post = $this->factory->post->create( array( 'post_author' => 1 ) );
$this->assertFalse( current_user_can( 'edit_post', $post ) );
$this->assertFalse( current_user_can( 'edit_post', $post + 1 ) );
}
}