Tests_User_Capabilities   F
last analyzed

Complexity

Total Complexity 122

Size/Duplication

Total Lines 1763
Duplicated Lines 10.15 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 179
loc 1763
rs 3.9999
c 0
b 0
f 0
wmc 122
lcom 1
cbo 11

78 Methods

Rating   Name   Duplication   Size   Complexity  
A wpSetUpBeforeClass() 0 11 1
A setUp() 0 6 1
A _flush_roles() 0 7 1
A _meta_yes_you_can() 0 3 1
A _meta_no_you_cant() 0 3 1
A _meta_filter() 0 3 1
B _getSingleSitePrimitiveCaps() 0 78 1
B _getMultiSitePrimitiveCaps() 0 79 1
B _getSingleSiteMetaCaps() 0 30 1
B _getMultiSiteMetaCaps() 0 30 1
A getAllCapsAndRoles() 0 3 1
A getPrimitiveCapsAndRoles() 0 7 2
A getMetaCapsAndRoles() 0 7 2
A test_single_and_multisite_cap_tests_match() 0 13 1
B test_all_caps_of_users_are_being_tested() 0 29 2
B testPrimitiveCapsTestsAreCorrect() 0 30 1
A testMetaCapsTestsAreCorrect() 0 72 1
A test_unfiltered_upload_caps() 0 10 2
A test_user_can_edit_comment_on_own_post() 0 12 1
A test_user_can_edit_comment_on_others_post() 0 15 1
A test_super_admin_caps() 0 21 2
B test_role_add_cap() 0 39 3
B test_role_remove_cap() 0 34 1
B test_user_add_cap() 0 37 4
B test_user_remove_cap() 0 30 1
B test_user_level_update() 0 24 1
B test_user_remove_all_caps() 0 38 2
B test_post_meta_caps() 0 79 2
A authorless_post_statuses() 0 3 1
A test_authorless_post() 0 20 1
A test_create_posts_caps() 0 62 1
B test_page_meta_caps() 0 34 1
A test_default_taxonomy_term_cannot_be_deleted() 0 16 2
B test_taxonomy_caps_map_correctly_to_their_meta_cap() 0 29 3
A dataTaxonomies() 0 13 1
A test_negative_caps() 0 11 1
A test_set_role_same_role() 0 8 1
B test_current_user_can_for_blog() 0 24 2
A test_borked_current_user_can_for_blog() 0 12 1
A _nullify_current_user() 0 8 2
A _nullify_current_user_and_keep_nullifying_user() 0 3 1
A test_current_user_edit_caps() 0 16 1
A test_subscriber_cant_edit_posts() 0 9 1
A test_multisite_administrator_can_not_edit_users() 0 8 1
A test_user_can_edit_self() 0 6 2
A test_only_admins_can_delete_users_on_single_site() 0 8 1
B test_contributor_cannot_edit_scheduled_post() 0 28 1
A test_require_edit_others_posts_if_post_type_doesnt_exist() 0 18 2
B test_cpt_with_page_capability_type() 0 42 1
A testNonLoggedInUsersHaveNoCapabilities() 0 17 2
A test_wp_roles_init_action() 0 21 1
A _hook_wp_roles_init() 0 3 1
A test_wp_roles_reinit_deprecated() 0 4 1
A test_no_one_can_edit_user_meta_for_non_existent_term() 0 4 1
A test_user_cannot_edit_user_meta() 0 4 1
A test_no_one_can_delete_user_meta_for_non_existent_term() 0 4 1
A test_user_cannot_delete_user_meta() 0 4 1
A test_no_one_can_add_user_meta_for_non_existent_term() 0 4 1
A test_user_cannot_add_user_meta() 0 4 1
B test_all_roles_and_caps() 9 32 4
B test_link_manager_caps() 17 34 6
A data_user_with_role_can_edit_own_post() 13 13 2
A data_user_with_role_can_edit_others_posts() 13 13 2
A test_bogus_role() 4 16 2
B test_user_subscriber_contributor() 9 28 3
B test_add_empty_role() 4 29 2
B test_add_role() 10 41 3
A test_taxonomy_capabilities_are_correct() 6 23 3
A test_taxonomy_capabilities_with_custom_caps_are_correct() 6 22 2
A test_only_admins_and_super_admins_can_remove_users() 12 12 2
A test_only_super_admins_can_delete_users_on_multisite() 9 9 1
A test_only_admins_and_super_admins_can_promote_users() 12 12 2
A test_multisite_administrator_with_manage_network_users_can_edit_users() 13 13 1
A test_multisite_administrator_with_manage_network_users_can_not_edit_super_admin() 12 12 1
A test_user_can_edit_user_meta() 7 7 2
A test_user_can_delete_user_meta() 7 7 2
A test_user_can_add_user_meta() 7 7 2
A test_only_super_admins_can_remove_themselves_on_multisite() 9 9 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Tests_User_Capabilities often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Tests_User_Capabilities, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
// Test roles and capabilities via the WP_User class
4
5
/**
6
 * @group user
7
 * @group capabilities
8
 */
9
class Tests_User_Capabilities extends WP_UnitTestCase {
10
11
	protected static $users = array(
12
		'administrator' => null,
13
		'editor'        => null,
14
		'author'        => null,
15
		'contributor'   => null,
16
		'subscriber'    => null,
17
	);
18
	protected static $super_admin = null;
19
20
	public static function wpSetUpBeforeClass( $factory ) {
21
		self::$users = array(
22
			'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ),
23
			'editor'        => $factory->user->create_and_get( array( 'role' => 'editor' ) ),
24
			'author'        => $factory->user->create_and_get( array( 'role' => 'author' ) ),
25
			'contributor'   => $factory->user->create_and_get( array( 'role' => 'contributor' ) ),
26
			'subscriber'    => $factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
27
		);
28
		self::$super_admin = $factory->user->create_and_get( array( 'role' => 'contributor' ) );
29
		grant_super_admin( self::$super_admin->ID );
30
	}
31
32
	function setUp() {
33
		parent::setUp();
34
		// keep track of users we create
35
		$this->_flush_roles();
36
37
	}
38
39
	function _flush_roles() {
40
		// we want to make sure we're testing against the db, not just in-memory data
41
		// this will flush everything and reload it from the db
42
		unset($GLOBALS['wp_user_roles']);
43
		global $wp_roles;
44
		$wp_roles = new WP_Roles();
45
	}
46
47
	function _meta_yes_you_can( $can, $key, $post_id, $user_id, $cap, $caps ) {
48
		return true;
49
	}
50
51
	function _meta_no_you_cant( $can, $key, $post_id, $user_id, $cap, $caps ) {
52
		return false;
53
	}
54
55
	function _meta_filter( $meta_value, $meta_key, $meta_type ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
56
		return $meta_value;
57
	}
58
59
	final private function _getSingleSitePrimitiveCaps() {
60
		return array(
61
62
			'unfiltered_html'        => array( 'administrator', 'editor' ),
63
64
			'activate_plugins'       => array( 'administrator' ),
65
			'create_users'           => array( 'administrator' ),
66
			'delete_plugins'         => array( 'administrator' ),
67
			'delete_themes'          => array( 'administrator' ),
68
			'delete_users'           => array( 'administrator' ),
69
			'edit_files'             => array( 'administrator' ),
70
			'edit_plugins'           => array( 'administrator' ),
71
			'edit_themes'            => array( 'administrator' ),
72
			'edit_users'             => array( 'administrator' ),
73
			'install_plugins'        => array( 'administrator' ),
74
			'install_themes'         => array( 'administrator' ),
75
			'update_core'            => array( 'administrator' ),
76
			'update_plugins'         => array( 'administrator' ),
77
			'update_themes'          => array( 'administrator' ),
78
			'edit_theme_options'     => array( 'administrator' ),
79
			'export'                 => array( 'administrator' ),
80
			'import'                 => array( 'administrator' ),
81
			'list_users'             => array( 'administrator' ),
82
			'manage_options'         => array( 'administrator' ),
83
			'promote_users'          => array( 'administrator' ),
84
			'remove_users'           => array( 'administrator' ),
85
			'switch_themes'          => array( 'administrator' ),
86
			'edit_dashboard'         => array( 'administrator' ),
87
88
			'moderate_comments'      => array( 'administrator', 'editor' ),
89
			'manage_categories'      => array( 'administrator', 'editor' ),
90
			'edit_others_posts'      => array( 'administrator', 'editor' ),
91
			'edit_pages'             => array( 'administrator', 'editor' ),
92
			'edit_others_pages'      => array( 'administrator', 'editor' ),
93
			'edit_published_pages'   => array( 'administrator', 'editor' ),
94
			'publish_pages'          => array( 'administrator', 'editor' ),
95
			'delete_pages'           => array( 'administrator', 'editor' ),
96
			'delete_others_pages'    => array( 'administrator', 'editor' ),
97
			'delete_published_pages' => array( 'administrator', 'editor' ),
98
			'delete_others_posts'    => array( 'administrator', 'editor' ),
99
			'delete_private_posts'   => array( 'administrator', 'editor' ),
100
			'edit_private_posts'     => array( 'administrator', 'editor' ),
101
			'read_private_posts'     => array( 'administrator', 'editor' ),
102
			'delete_private_pages'   => array( 'administrator', 'editor' ),
103
			'edit_private_pages'     => array( 'administrator', 'editor' ),
104
			'read_private_pages'     => array( 'administrator', 'editor' ),
105
106
			'edit_published_posts'   => array( 'administrator', 'editor', 'author' ),
107
			'upload_files'           => array( 'administrator', 'editor', 'author' ),
108
			'publish_posts'          => array( 'administrator', 'editor', 'author' ),
109
			'delete_published_posts' => array( 'administrator', 'editor', 'author' ),
110
111
			'edit_posts'             => array( 'administrator', 'editor', 'author', 'contributor' ),
112
			'delete_posts'           => array( 'administrator', 'editor', 'author', 'contributor' ),
113
114
			'read'                   => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
115
116
			'level_10'               => array( 'administrator' ),
117
			'level_9'                => array( 'administrator' ),
118
			'level_8'                => array( 'administrator' ),
119
			'level_7'                => array( 'administrator', 'editor' ),
120
			'level_6'                => array( 'administrator', 'editor' ),
121
			'level_5'                => array( 'administrator', 'editor' ),
122
			'level_4'                => array( 'administrator', 'editor' ),
123
			'level_3'                => array( 'administrator', 'editor' ),
124
			'level_2'                => array( 'administrator', 'editor', 'author' ),
125
			'level_1'                => array( 'administrator', 'editor', 'author', 'contributor' ),
126
			'level_0'                => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
127
128
			'administrator'          => array( 'administrator' ),
129
			'editor'                 => array( 'editor' ),
130
			'author'                 => array( 'author' ),
131
			'contributor'            => array( 'contributor' ),
132
			'subscriber'             => array( 'subscriber' ),
133
134
		);
135
136
	}
137
138
	final private function _getMultiSitePrimitiveCaps() {
139
		return array(
140
141
			'unfiltered_html'        => array(),
142
143
			'activate_plugins'       => array(),
144
			'create_users'           => array(),
145
			'delete_plugins'         => array(),
146
			'delete_themes'          => array(),
147
			'delete_users'           => array(),
148
			'edit_files'             => array(),
149
			'edit_plugins'           => array(),
150
			'edit_themes'            => array(),
151
			'edit_users'             => array(),
152
			'install_plugins'        => array(),
153
			'install_themes'         => array(),
154
			'update_core'            => array(),
155
			'update_plugins'         => array(),
156
			'update_themes'          => array(),
157
158
			'edit_theme_options'     => array( 'administrator' ),
159
			'export'                 => array( 'administrator' ),
160
			'import'                 => array( 'administrator' ),
161
			'list_users'             => array( 'administrator' ),
162
			'manage_options'         => array( 'administrator' ),
163
			'promote_users'          => array( 'administrator' ),
164
			'remove_users'           => array( 'administrator' ),
165
			'switch_themes'          => array( 'administrator' ),
166
			'edit_dashboard'         => array( 'administrator' ),
167
168
			'moderate_comments'      => array( 'administrator', 'editor' ),
169
			'manage_categories'      => array( 'administrator', 'editor' ),
170
			'edit_others_posts'      => array( 'administrator', 'editor' ),
171
			'edit_pages'             => array( 'administrator', 'editor' ),
172
			'edit_others_pages'      => array( 'administrator', 'editor' ),
173
			'edit_published_pages'   => array( 'administrator', 'editor' ),
174
			'publish_pages'          => array( 'administrator', 'editor' ),
175
			'delete_pages'           => array( 'administrator', 'editor' ),
176
			'delete_others_pages'    => array( 'administrator', 'editor' ),
177
			'delete_published_pages' => array( 'administrator', 'editor' ),
178
			'delete_others_posts'    => array( 'administrator', 'editor' ),
179
			'delete_private_posts'   => array( 'administrator', 'editor' ),
180
			'edit_private_posts'     => array( 'administrator', 'editor' ),
181
			'read_private_posts'     => array( 'administrator', 'editor' ),
182
			'delete_private_pages'   => array( 'administrator', 'editor' ),
183
			'edit_private_pages'     => array( 'administrator', 'editor' ),
184
			'read_private_pages'     => array( 'administrator', 'editor' ),
185
186
			'edit_published_posts'   => array( 'administrator', 'editor', 'author' ),
187
			'upload_files'           => array( 'administrator', 'editor', 'author' ),
188
			'publish_posts'          => array( 'administrator', 'editor', 'author' ),
189
			'delete_published_posts' => array( 'administrator', 'editor', 'author' ),
190
191
			'edit_posts'             => array( 'administrator', 'editor', 'author', 'contributor' ),
192
			'delete_posts'           => array( 'administrator', 'editor', 'author', 'contributor' ),
193
194
			'read'                   => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
195
196
			'level_10'               => array( 'administrator' ),
197
			'level_9'                => array( 'administrator' ),
198
			'level_8'                => array( 'administrator' ),
199
			'level_7'                => array( 'administrator', 'editor' ),
200
			'level_6'                => array( 'administrator', 'editor' ),
201
			'level_5'                => array( 'administrator', 'editor' ),
202
			'level_4'                => array( 'administrator', 'editor' ),
203
			'level_3'                => array( 'administrator', 'editor' ),
204
			'level_2'                => array( 'administrator', 'editor', 'author' ),
205
			'level_1'                => array( 'administrator', 'editor', 'author', 'contributor' ),
206
			'level_0'                => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
207
208
			'administrator'          => array( 'administrator' ),
209
			'editor'                 => array( 'editor' ),
210
			'author'                 => array( 'author' ),
211
			'contributor'            => array( 'contributor' ),
212
			'subscriber'             => array( 'subscriber' ),
213
214
		);
215
216
	}
217
218
	final private function _getSingleSiteMetaCaps() {
219
		return array(
220
			'create_sites'           => array(),
221
			'delete_sites'           => array(),
222
			'manage_network'         => array(),
223
			'manage_sites'           => array(),
224
			'manage_network_users'   => array(),
225
			'manage_network_plugins' => array(),
226
			'manage_network_themes'  => array(),
227
			'manage_network_options' => array(),
228
			'delete_site'            => array(),
229
			'upgrade_network'        => array(),
230
231
			'setup_network'          => array( 'administrator' ),
232
			'upload_plugins'         => array( 'administrator' ),
233
			'upload_themes'          => array( 'administrator' ),
234
			'customize'              => array( 'administrator' ),
235
			'add_users'              => array( 'administrator' ),
236
237
			'edit_categories'        => array( 'administrator', 'editor' ),
238
			'delete_categories'      => array( 'administrator', 'editor' ),
239
			'manage_post_tags'       => array( 'administrator', 'editor' ),
240
			'edit_post_tags'         => array( 'administrator', 'editor' ),
241
			'delete_post_tags'       => array( 'administrator', 'editor' ),
242
			'edit_css'               => array( 'administrator', 'editor' ),
243
244
			'assign_categories'      => array( 'administrator', 'editor', 'author', 'contributor' ),
245
			'assign_post_tags'       => array( 'administrator', 'editor', 'author', 'contributor' ),
246
		);
247
	}
248
249
	final private function _getMultiSiteMetaCaps() {
250
		return array(
251
			'create_sites'           => array(),
252
			'delete_sites'           => array(),
253
			'manage_network'         => array(),
254
			'manage_sites'           => array(),
255
			'manage_network_users'   => array(),
256
			'manage_network_plugins' => array(),
257
			'manage_network_themes'  => array(),
258
			'manage_network_options' => array(),
259
			'setup_network'          => array(),
260
			'upload_plugins'         => array(),
261
			'upload_themes'          => array(),
262
			'edit_css'               => array(),
263
			'upgrade_network'        => array(),
264
265
			'customize'              => array( 'administrator' ),
266
			'delete_site'            => array( 'administrator' ),
267
			'add_users'              => array( 'administrator' ),
268
269
			'edit_categories'        => array( 'administrator', 'editor' ),
270
			'delete_categories'      => array( 'administrator', 'editor' ),
271
			'manage_post_tags'       => array( 'administrator', 'editor' ),
272
			'edit_post_tags'         => array( 'administrator', 'editor' ),
273
			'delete_post_tags'       => array( 'administrator', 'editor' ),
274
275
			'assign_categories'      => array( 'administrator', 'editor', 'author', 'contributor' ),
276
			'assign_post_tags'       => array( 'administrator', 'editor', 'author', 'contributor' ),
277
		);
278
	}
279
280
	protected function getAllCapsAndRoles() {
281
		return $this->getPrimitiveCapsAndRoles() + $this->getMetaCapsAndRoles();
282
	}
283
284
	protected function getPrimitiveCapsAndRoles() {
285
		if ( is_multisite() ) {
286
			return $this->_getMultiSitePrimitiveCaps();
287
		} else {
288
			return $this->_getSingleSitePrimitiveCaps();
289
		}
290
	}
291
292
	protected function getMetaCapsAndRoles() {
293
		if ( is_multisite() ) {
294
			return $this->_getMultiSiteMetaCaps();
295
		} else {
296
			return $this->_getSingleSiteMetaCaps();
297
		}
298
	}
299
300
	// test the tests
301
	function test_single_and_multisite_cap_tests_match() {
302
		$single_primitive = array_keys( $this->_getSingleSitePrimitiveCaps() );
303
		$multi_primitive  = array_keys( $this->_getMultiSitePrimitiveCaps() );
304
		sort( $single_primitive );
305
		sort( $multi_primitive );
306
		$this->assertEquals( $single_primitive, $multi_primitive );
307
308
		$single_meta = array_keys( $this->_getSingleSiteMetaCaps() );
309
		$multi_meta  = array_keys( $this->_getMultiSiteMetaCaps() );
310
		sort( $single_meta );
311
		sort( $multi_meta );
312
		$this->assertEquals( $single_meta, $multi_meta );
313
	}
314
315
	// test the tests
316
	function test_all_caps_of_users_are_being_tested() {
317
		$caps = $this->getPrimitiveCapsAndRoles();
318
319
		// `manage_links` is a special case
320
		$this->assertSame( '0', get_option( 'link_manager_enabled' ) );
321
		// `unfiltered_upload` is a special case
322
		$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
323
324
		foreach ( self::$users as $role => $user ) {
325
326
			// make sure the user is valid
327
			$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
328
329
			$user_caps = $user->allcaps;
330
331
			unset(
332
				// `manage_links` is a special case
333
				$user_caps['manage_links'],
334
				// `unfiltered_upload` is a special case
335
				$user_caps['unfiltered_upload']
336
			);
337
338
			$diff = array_diff( array_keys( $user_caps ), array_keys( $caps ) );
339
340
			$this->assertEquals( array(), $diff, "User with {$role} role has capabilities that aren't being tested" );
341
342
		}
343
344
	}
345
346
	/**
347
	 * Test the tests. The administrator role has all primitive capabilities, therefore the
348
	 * primitive capabilitity tests can be tested by checking that the list of tested
349
	 * capabilities matches those of the administrator role.
350
	 *
351
	 * @group capTestTests
352
	 */
353
	public function testPrimitiveCapsTestsAreCorrect() {
354
		$actual   = $this->getPrimitiveCapsAndRoles();
355
		$admin    = get_role( 'administrator' );
356
		$expected = $admin->capabilities;
357
358
		unset(
359
			// Role names as capabilities are a special case:
360
			$actual['administrator'],
361
			$actual['editor'],
362
			$actual['author'],
363
			$actual['subscriber'],
364
			$actual['contributor']
365
		);
366
367
		unset(
368
			// `manage_links` is a special case in the caps tests:
369
			$expected['manage_links'],
370
			// `unfiltered_upload` is a special case in the caps tests:
371
			$expected['unfiltered_upload']
372
		);
373
374
		$expected = array_keys( $expected );
375
		$actual   = array_keys( $actual );
376
377
		$missing_primitive_cap_checks = array_diff( $expected, $actual );
378
		$this->assertSame( array(), $missing_primitive_cap_checks, 'These primitive capabilities are not tested' );
379
380
		$incorrect_primitive_cap_checks = array_diff( $actual, $expected );
381
		$this->assertSame( array(), $incorrect_primitive_cap_checks, 'These capabilities are not primitive' );
382
	}
383
384
	/**
385
	 * Test the tests. All meta capabilities should have a condition in the `map_meta_cap()`
386
	 * function that handles the capability.
387
	 *
388
	 * @group capTestTests
389
	 */
390
	public function testMetaCapsTestsAreCorrect() {
391
		$actual = $this->getMetaCapsAndRoles();
392
		$file   = file_get_contents( ABSPATH . WPINC . '/capabilities.php' );
393
394
		$matched = preg_match( '/^function map_meta_cap\((.*?)^\}/ms', $file, $function );
395
		$this->assertSame( 1, $matched );
396
		$this->assertNotEmpty( $function );
397
398
		$matched = preg_match_all( '/^[\t]case \'([^\']+)/m', $function[0], $cases );
399
		$this->assertNotEmpty( $matched );
400
		$this->assertNotEmpty( $cases );
401
402
		$expected = array_flip( $cases[1] );
403
404
		unset(
405
			// These primitive capabilities have a 'case' in `map_meta_cap()` but aren't meta capabilities:
406
			$expected['unfiltered_upload'],
407
			$expected['unfiltered_html'],
408
			$expected['edit_files'],
409
			$expected['edit_plugins'],
410
			$expected['edit_themes'],
411
			$expected['update_plugins'],
412
			$expected['delete_plugins'],
413
			$expected['install_plugins'],
414
			$expected['update_themes'],
415
			$expected['delete_themes'],
416
			$expected['install_themes'],
417
			$expected['update_core'],
418
			$expected['activate_plugins'],
419
			$expected['edit_users'],
420
			$expected['delete_users'],
421
			$expected['create_users'],
422
			$expected['manage_links'],
423
			// Singular object meta capabilities (where an object ID is passed) are not tested:
424
			$expected['remove_user'],
425
			$expected['promote_user'],
426
			$expected['edit_user'],
427
			$expected['delete_post'],
428
			$expected['delete_page'],
429
			$expected['edit_post'],
430
			$expected['edit_page'],
431
			$expected['read_post'],
432
			$expected['read_page'],
433
			$expected['publish_post'],
434
			$expected['edit_post_meta'],
435
			$expected['delete_post_meta'],
436
			$expected['add_post_meta'],
437
			$expected['edit_comment'],
438
			$expected['edit_comment_meta'],
439
			$expected['delete_comment_meta'],
440
			$expected['add_comment_meta'],
441
			$expected['edit_term'],
442
			$expected['delete_term'],
443
			$expected['assign_term'],
444
			$expected['edit_term_meta'],
445
			$expected['delete_term_meta'],
446
			$expected['add_term_meta'],
447
			$expected['delete_user'],
448
			$expected['edit_user_meta'],
449
			$expected['delete_user_meta'],
450
			$expected['add_user_meta']
451
		);
452
453
		$expected = array_keys( $expected );
454
		$actual   = array_keys( $actual );
455
456
		$missing_meta_cap_checks = array_diff( $expected, $actual );
457
		$this->assertSame( array(), $missing_meta_cap_checks, 'These meta capabilities are not tested' );
458
459
		$incorrect_meta_cap_checks = array_diff( $actual, $expected );
460
		$this->assertSame( array(), $incorrect_meta_cap_checks, 'These capabilities are not meta' );
461
	}
462
463
	// test the default roles and caps
464
	function test_all_roles_and_caps() {
465
		$caps = $this->getAllCapsAndRoles();
466
467
		foreach ( self::$users as $role => $user ) {
468
469
			// make sure the user is valid
470
			$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
471
472
			// make sure the role name is correct
473
			$this->assertEquals( array( $role ), $user->roles, "User should only have the {$role} role" );
474
475 View Code Duplication
			foreach ( $caps as $cap => $roles ) {
476
				if ( in_array( $role, $roles, true ) ) {
477
					$this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
478
					$this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
479
				} else {
480
					$this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
481
					$this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
482
				}
483
			}
484
485
		}
486
487
		$this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );
0 ignored issues
show
Bug introduced by
The variable $user seems to be defined by a foreach iteration on line 467. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
Bug introduced by
The variable $role seems to be defined by a foreach iteration on line 467. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
488
		$this->assertFalse( user_can( $user, 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );
489
490
		$this->assertFalse( $user->has_cap( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability" );
491
		$this->assertFalse( user_can( $user, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability" );
492
493
		$this->assertTrue( $user->has_cap( 'exist' ), "User with the {$role} role should have the exist capability" );
494
		$this->assertTrue( user_can( $user, 'exist' ), "User with the {$role} role should have the exist capability" );
495
	}
496
497
	// special case for the link manager
498
	function test_link_manager_caps() {
499
		$caps = array(
500
			'manage_links' => array( 'administrator', 'editor' ),
501
		);
502
503
		$this->assertSame( '0', get_option( 'link_manager_enabled' ) );
504
505
		// no-one should have access to the link manager by default
506 View Code Duplication
		foreach ( self::$users as $role => $user ) {
507
			foreach ( $caps as $cap => $roles ) {
508
				$this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
509
				$this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
510
			}
511
		}
512
513
		update_option( 'link_manager_enabled', '1' );
514
		$this->assertSame( '1', get_option( 'link_manager_enabled' ) );
515
516 View Code Duplication
		foreach ( self::$users as $role => $user ) {
517
			foreach ( $caps as $cap => $roles ) {
518
				if ( in_array( $role, $roles, true ) ) {
519
					$this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
520
					$this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
521
				} else {
522
					$this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
523
					$this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
524
				}
525
			}
526
		}
527
528
		update_option( 'link_manager_enabled', '0' );
529
		$this->assertSame( '0', get_option( 'link_manager_enabled' ) );
530
531
	}
532
533
	// special case for unfiltered uploads
534
	function test_unfiltered_upload_caps() {
535
		$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
536
537
		// no-one should have this cap
538
		foreach ( self::$users as $role => $user ) {
539
			$this->assertFalse( $user->has_cap( 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
540
			$this->assertFalse( user_can( $user, 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
541
		}
542
543
	}
544
545
	/**
546
	 * @dataProvider data_user_with_role_can_edit_own_post
547
	 *
548
	 * @param  string $role              User role name
549
	 * @param  bool   $can_edit_own_post Can users with this role edit their own posts?
550
	 */
551
	public function test_user_can_edit_comment_on_own_post( $role, $can_edit_own_post ) {
552
		$owner   = self::$users[ $role ];
553
		$post    = self::factory()->post->create_and_get( array(
554
			'post_author' => $owner->ID,
555
		) );
556
		$comment = self::factory()->comment->create_and_get( array(
557
			'comment_post_ID' => $post->ID,
558
		) );
559
560
		$owner_can_edit = user_can( $owner->ID, 'edit_comment', $comment->comment_ID );
561
		$this->assertSame( $can_edit_own_post, $owner_can_edit );
562
	}
563
564
	/**
565
	 * @dataProvider data_user_with_role_can_edit_others_posts
566
	 *
567
	 * @param  string $role                 User role name
568
	 * @param  bool   $can_edit_others_post Can users with this role edit others' posts?
569
	 */
570
	public function test_user_can_edit_comment_on_others_post( $role, $can_edit_others_post ) {
571
		$user    = self::$users[ $role ];
572
		$owner   = self::factory()->user->create_and_get( array(
573
			'role' => 'editor',
574
		) );
575
		$post    = self::factory()->post->create_and_get( array(
576
			'post_author' => $owner->ID,
577
		) );
578
		$comment = self::factory()->comment->create_and_get( array(
579
			'comment_post_ID' => $post->ID,
580
		) );
581
582
		$user_can_edit = user_can( $user->ID, 'edit_comment', $comment->comment_ID );
583
		$this->assertSame( $can_edit_others_post, $user_can_edit );
584
	}
585
586 View Code Duplication
	public function data_user_with_role_can_edit_own_post() {
587
		$data  = array();
588
		$caps  = $this->getPrimitiveCapsAndRoles();
589
590
		foreach ( self::$users as $role => $null ) {
591
			$data[] = array(
592
				$role,
593
				in_array( $role, $caps['edit_published_posts'], true ),
594
			);
595
		}
596
597
		return $data;
598
	}
599
600 View Code Duplication
	public function data_user_with_role_can_edit_others_posts() {
601
		$data  = array();
602
		$caps  = $this->getPrimitiveCapsAndRoles();
603
604
		foreach ( self::$users as $role => $null ) {
605
			$data[] = array(
606
				$role,
607
				in_array( $role, $caps['edit_others_posts'], true ),
608
			);
609
		}
610
611
		return $data;
612
	}
613
614
	/**
615
	 * @group ms-required
616
	 */
617
	function test_super_admin_caps() {
618
		$caps = $this->getAllCapsAndRoles();
619
		$user = self::$super_admin;
620
621
		$this->assertTrue( is_super_admin( $user->ID ) );
622
623
		foreach ( $caps as $cap => $roles ) {
624
			$this->assertTrue( $user->has_cap( $cap ), "Super Admins should have the {$cap} capability" );
625
			$this->assertTrue( user_can( $user, $cap ), "Super Admins should have the {$cap} capability" );
626
		}
627
628
		$this->assertTrue( $user->has_cap( 'start_a_fire' ), "Super admins should have all custom capabilities" );
629
		$this->assertTrue( user_can( $user, 'start_a_fire' ), "Super admins should have all custom capabilities" );
630
631
		$this->assertFalse( $user->has_cap( 'do_not_allow' ), 'Super Admins should not have the do_not_allow capability' );
632
		$this->assertFalse( user_can( $user, 'do_not_allow' ), 'Super Admins should not have the do_not_allow capability' );
633
634
		$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
635
		$this->assertFalse( $user->has_cap( 'unfiltered_upload' ), 'Super Admins should not have the unfiltered_upload capability' );
636
		$this->assertFalse( user_can( $user, 'unfiltered_upload' ), 'Super Admins should not have the unfiltered_upload capability' );
637
	}
638
639
	// a role that doesn't exist
640
	function test_bogus_role() {
641
		$user = self::factory()->user->create_and_get( array( 'role' => 'invalid_role' ) );
642
643
		// make sure the user is valid
644
		$this->assertTrue( $user->exists(), "User does not exist" );
645
646
		// make sure the role name is correct
647
		$this->assertEquals( array(), $user->roles, "User should not have any roles" );
648
649
		$caps = $this->getAllCapsAndRoles();
650
651 View Code Duplication
		foreach ( $caps as $cap => $roles ) {
652
			$this->assertFalse( $user->has_cap( $cap ), "User with an invalid role should not have the {$cap} capability" );
653
			$this->assertFalse( user_can( $user, $cap ), "User with an invalid role should not have the {$cap} capability" );
654
		}
655
	}
656
657
	// a user with multiple roles
658
	function test_user_subscriber_contributor() {
659
		$user = self::$users['subscriber'];
660
661
		// make sure the user is valid
662
		$this->assertTrue( $user->exists(), "User does not exist" );
663
664
		$user->add_role( 'contributor' );
665
666
		// user should have two roles now
667
		$this->assertEquals( array( 'subscriber', 'contributor' ), $user->roles );
668
669
		$caps = $this->getAllCapsAndRoles();
670
671 View Code Duplication
		foreach ( $caps as $cap => $roles ) {
672
			if ( array_intersect( $user->roles, $roles ) ) {
673
				$this->assertTrue( $user->has_cap( $cap ), "User should have the {$cap} capability" );
674
				$this->assertTrue( user_can( $user, $cap ), "User should have the {$cap} capability" );
675
			} else {
676
				$this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
677
				$this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
678
			}
679
		}
680
681
		$user->remove_role( 'contributor' );
682
		// user should have one role now
683
		$this->assertEquals( array( 'subscriber' ), $user->roles );
684
685
	}
686
687
	// newly added empty role
688
	function test_add_empty_role() {
689
		global $wp_roles;
690
691
		$role_name = 'janitor';
692
		add_role( $role_name, 'Janitor', array() );
693
694
		$this->_flush_roles();
695
		$this->assertTrue( $wp_roles->is_role( $role_name ) );
696
697
		$user = self::factory()->user->create_and_get( array( 'role' => $role_name ) );
698
699
		// make sure the user is valid
700
		$this->assertTrue( $user->exists(), "User does not exist" );
701
702
		// make sure the role name is correct
703
		$this->assertEquals( array( $role_name ), $user->roles );
704
705
		$caps = $this->getAllCapsAndRoles();
706
707 View Code Duplication
		foreach ( $caps as $cap => $roles ) {
708
			$this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
709
			$this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
710
		}
711
712
		// clean up
713
		remove_role( $role_name );
714
		$this->_flush_roles();
715
		$this->assertFalse( $wp_roles->is_role( $role_name ) );
716
	}
717
718
	// newly added role
719
	function test_add_role() {
720
		global $wp_roles;
721
722
		$role_name = 'janitor';
723
		$expected_caps = array(
724
			'edit_posts' => true,
725
			'edit_pages' => true,
726
			'level_0'    => true,
727
			'level_1'    => true,
728
			'level_2'    => true,
729
		);
730
		add_role( $role_name, 'Janitor', $expected_caps );
731
		$this->_flush_roles();
732
		$this->assertTrue( $wp_roles->is_role( $role_name ) );
733
734
		$user = self::factory()->user->create_and_get( array( 'role' => $role_name ) );
735
736
		// make sure the user is valid
737
		$this->assertTrue( $user->exists(), "User does not exist" );
738
739
		// make sure the role name is correct
740
		$this->assertEquals( array( $role_name ), $user->roles );
741
742
		$caps = $this->getPrimitiveCapsAndRoles();
743
744 View Code Duplication
		foreach ( $caps as $cap => $roles ) {
745
			// the user should have all the above caps
746
			if ( isset( $expected_caps[ $cap ] ) ) {
747
				$this->assertTrue( $user->has_cap( $cap ), "User should have the {$cap} capability" );
748
				$this->assertTrue( user_can( $user, $cap ), "User should have the {$cap} capability" );
749
			} else {
750
				$this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
751
				$this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
752
			}
753
		}
754
755
		// clean up
756
		remove_role( $role_name );
757
		$this->_flush_roles();
758
		$this->assertFalse( $wp_roles->is_role( $role_name ) );
759
	}
760
761
	function test_role_add_cap() {
762
		// change the capabilites associated with a role and make sure the change is reflected in has_cap()
763
764
		global $wp_roles;
765
		$role_name = 'janitor';
766
		add_role( $role_name, 'Janitor', array('level_1'=>true) );
767
		$this->_flush_roles();
768
		$this->assertTrue( $wp_roles->is_role($role_name) );
769
770
		// assign a user to that role
771
		$id = self::factory()->user->create( array( 'role' => $role_name ) );
772
773
		// now add a cap to the role
774
		$wp_roles->add_cap($role_name, 'sweep_floor');
775
		$this->_flush_roles();
776
777
		$user = new WP_User($id);
778
		$this->assertTrue($user->exists(), "Problem getting user $id");
779
		$this->assertEquals(array($role_name), $user->roles);
780
781
		// the user should have all the above caps
782
		$this->assertTrue($user->has_cap($role_name));
783
		$this->assertTrue($user->has_cap('level_1'));
784
		$this->assertTrue($user->has_cap('sweep_floor'));
785
786
		// shouldn't have any other caps
787
		$caps = $this->getAllCapsAndRoles();
788
		foreach ( $caps as $cap => $roles ) {
789
			if ( 'level_1' !== $cap ) {
790
				$this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
791
			}
792
		}
793
794
		// clean up
795
		remove_role($role_name);
796
		$this->_flush_roles();
797
		$this->assertFalse($wp_roles->is_role($role_name));
798
799
	}
800
801
	function test_role_remove_cap() {
802
		// change the capabilites associated with a role and make sure the change is reflected in has_cap()
803
804
		global $wp_roles;
805
		$role_name = 'janitor';
806
		add_role( $role_name, 'Janitor', array('level_1'=>true, 'sweep_floor'=>true, 'polish_doorknobs'=>true) );
807
		$this->_flush_roles();
808
		$this->assertTrue( $wp_roles->is_role($role_name) );
809
810
		// assign a user to that role
811
		$id = self::factory()->user->create( array( 'role' => $role_name ) );
812
813
		// now remove a cap from the role
814
		$wp_roles->remove_cap($role_name, 'polish_doorknobs');
815
		$this->_flush_roles();
816
817
		$user = new WP_User($id);
818
		$this->assertTrue($user->exists(), "Problem getting user $id");
819
		$this->assertEquals(array($role_name), $user->roles);
820
821
		// the user should have all the above caps
822
		$this->assertTrue($user->has_cap($role_name));
823
		$this->assertTrue($user->has_cap('level_1'));
824
		$this->assertTrue($user->has_cap('sweep_floor'));
825
826
		// shouldn't have the removed cap
827
		$this->assertFalse($user->has_cap('polish_doorknobs'));
828
829
		// clean up
830
		remove_role($role_name);
831
		$this->_flush_roles();
832
		$this->assertFalse($wp_roles->is_role($role_name));
833
834
	}
835
836
	function test_user_add_cap() {
837
		// add an extra capability to a user
838
839
		// there are two contributors
840
		$id_1 = self::factory()->user->create( array( 'role' => 'contributor' ) );
841
		$id_2 = self::factory()->user->create( array( 'role' => 'contributor' ) );
842
843
		// user 1 has an extra capability
844
		$user_1 = new WP_User($id_1);
845
		$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
846
		$user_1->add_cap('publish_posts');
847
848
		// re-fetch both users from the db
849
		$user_1 = new WP_User($id_1);
850
		$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
851
		$user_2 = new WP_User($id_2);
852
		$this->assertTrue($user_2->exists(), "Problem getting user $id_2");
853
854
		// make sure they're both still contributors
855
		$this->assertEquals(array('contributor'), $user_1->roles);
856
		$this->assertEquals(array('contributor'), $user_2->roles);
857
858
		// check the extra cap on both users
859
		$this->assertTrue($user_1->has_cap('publish_posts'));
860
		$this->assertFalse($user_2->has_cap('publish_posts'));
861
862
		// make sure the other caps didn't get messed up
863
		$caps = $this->getAllCapsAndRoles();
864
		foreach ( $caps as $cap => $roles ) {
865
			if ( in_array( 'contributor', $roles, true ) || 'publish_posts' === $cap ) {
866
				$this->assertTrue( $user_1->has_cap( $cap ), "User should have the {$cap} capability" );
867
			} else {
868
				$this->assertFalse( $user_1->has_cap( $cap ), "User should not have the {$cap} capability" );
869
			}
870
		}
871
872
	}
873
874
	function test_user_remove_cap() {
875
		// add an extra capability to a user then remove it
876
877
		// there are two contributors
878
		$id_1 = self::factory()->user->create( array( 'role' => 'contributor' ) );
879
		$id_2 = self::factory()->user->create( array( 'role' => 'contributor' ) );
880
881
		// user 1 has an extra capability
882
		$user_1 = new WP_User($id_1);
883
		$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
884
		$user_1->add_cap('publish_posts');
885
886
		// now remove the extra cap
887
		$user_1->remove_cap('publish_posts');
888
889
		// re-fetch both users from the db
890
		$user_1 = new WP_User($id_1);
891
		$this->assertTrue($user_1->exists(), "Problem getting user $id_1");
892
		$user_2 = new WP_User($id_2);
893
		$this->assertTrue($user_2->exists(), "Problem getting user $id_2");
894
895
		// make sure they're both still contributors
896
		$this->assertEquals(array('contributor'), $user_1->roles);
897
		$this->assertEquals(array('contributor'), $user_2->roles);
898
899
		// check the removed cap on both users
900
		$this->assertFalse($user_1->has_cap('publish_posts'));
901
		$this->assertFalse($user_2->has_cap('publish_posts'));
902
903
	}
904
905
	function test_user_level_update() {
906
		// make sure the user_level is correctly set and changed with the user's role
907
908
		// user starts as an author
909
		$id = self::factory()->user->create( array( 'role' => 'author' ) );
910
		$user = new WP_User($id);
911
		$this->assertTrue($user->exists(), "Problem getting user $id");
912
913
		// author = user level 2
914
		$this->assertEquals( 2, $user->user_level );
915
916
		// they get promoted to editor - level should get bumped to 7
917
		$user->set_role('editor');
918
		$this->assertEquals( 7, $user->user_level );
919
920
		// demoted to contributor - level is reduced to 1
921
		$user->set_role('contributor');
922
		$this->assertEquals( 1, $user->user_level );
923
924
		// if they have two roles, user_level should be the max of the two
925
		$user->add_role('editor');
926
		$this->assertEquals(array('contributor', 'editor'), $user->roles);
927
		$this->assertEquals( 7, $user->user_level );
928
	}
929
930
	function test_user_remove_all_caps() {
931
		// user starts as an author
932
		$id = self::factory()->user->create( array( 'role' => 'author' ) );
933
		$user = new WP_User($id);
934
		$this->assertTrue($user->exists(), "Problem getting user $id");
935
936
		// add some extra capabilities
937
		$user->add_cap('make_coffee');
938
		$user->add_cap('drink_coffee');
939
940
		// re-fetch
941
		$user = new WP_User($id);
942
		$this->assertTrue($user->exists(), "Problem getting user $id");
943
944
		$this->assertTrue($user->has_cap('make_coffee'));
945
		$this->assertTrue($user->has_cap('drink_coffee'));
946
947
		// all caps are removed
948
		$user->remove_all_caps();
949
950
		// re-fetch
951
		$user = new WP_User($id);
952
		$this->assertTrue($user->exists(), "Problem getting user $id");
953
954
		// all capabilities for the user should be gone
955
		foreach ( $this->getAllCapsAndRoles() as $cap => $roles ) {
956
			$this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
957
		}
958
959
		// the extra capabilities should be gone
960
		$this->assertFalse($user->has_cap('make_coffee'));
961
		$this->assertFalse($user->has_cap('drink_coffee'));
962
963
		// user level should be empty
964
		$this->assertEmpty( $user->user_level );
965
966
967
	}
968
969
	function test_post_meta_caps() {
970
		// simple tests for some common meta capabilities
971
972
		// Get our author
973
		$author = self::$users['author'];
974
975
		// make a post
976
		$post = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) );
977
978
		// the author of the post
979
		$this->assertTrue($author->exists(), "Problem getting user $author->ID");
980
981
		// add some other users
982
		$admin = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
983
		$author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
984
		$editor = new WP_User( self::factory()->user->create( array( 'role' => 'editor' ) ) );
985
		$contributor = new WP_User( self::factory()->user->create( array( 'role' => 'contributor' ) ) );
986
987
		// administrators, editors and the post owner can edit it
988
		$this->assertTrue($admin->has_cap('edit_post', $post));
989
		$this->assertTrue($author->has_cap('edit_post', $post));
990
		$this->assertTrue($editor->has_cap('edit_post', $post));
991
		// other authors and contributors can't
992
		$this->assertFalse($author_2->has_cap('edit_post', $post));
993
		$this->assertFalse($contributor->has_cap('edit_post', $post));
994
995
		// administrators, editors and the post owner can delete it
996
		$this->assertTrue($admin->has_cap('delete_post', $post));
997
		$this->assertTrue($author->has_cap('delete_post', $post));
998
		$this->assertTrue($editor->has_cap('delete_post', $post));
999
		// other authors and contributors can't
1000
		$this->assertFalse($author_2->has_cap('delete_post', $post));
1001
		$this->assertFalse($contributor->has_cap('delete_post', $post));
1002
1003
		// administrators, editors, and authors can publish it
1004
		$this->assertTrue($admin->has_cap('publish_post', $post));
1005
		$this->assertTrue($author->has_cap('publish_post', $post));
1006
		$this->assertTrue($editor->has_cap('publish_post', $post));
1007
		$this->assertTrue($author_2->has_cap('publish_post', $post));
1008
		// contributors can't
1009
		$this->assertFalse($contributor->has_cap('publish_post', $post));
1010
1011
		register_post_type( 'something', array( 'capabilities' => array( 'edit_posts' => 'draw_somethings' ) ) );
1012
		$something = get_post_type_object( 'something' );
1013
		$this->assertEquals( 'draw_somethings', $something->cap->edit_posts );
1014
		$this->assertEquals( 'draw_somethings', $something->cap->create_posts );
1015
1016
		register_post_type( 'something', array( 'capabilities' =>
1017
						array( 'edit_posts' => 'draw_somethings', 'create_posts' => 'create_somethings' ) ) );
1018
		$something = get_post_type_object( 'something' );
1019
		$this->assertEquals( 'draw_somethings', $something->cap->edit_posts );
1020
		$this->assertEquals( 'create_somethings', $something->cap->create_posts );
1021
		_unregister_post_type( 'something' );
1022
1023
		// Test meta authorization callbacks
1024
		if ( function_exists( 'register_meta') ) {
1025
			$this->assertTrue( $admin->has_cap('edit_post_meta',  $post) );
1026
			$this->assertTrue( $admin->has_cap('add_post_meta',  $post) );
1027
			$this->assertTrue( $admin->has_cap('delete_post_meta',  $post) );
1028
1029
			$this->assertFalse( $admin->has_cap('edit_post_meta', $post, '_protected') );
1030
			$this->assertFalse( $admin->has_cap('add_post_meta', $post, '_protected') );
1031
			$this->assertFalse( $admin->has_cap('delete_post_meta', $post, '_protected') );
1032
1033
			register_meta( 'post', '_protected', array( $this, '_meta_filter' ), array( $this, '_meta_yes_you_can' ) );
1034
			$this->assertTrue( $admin->has_cap('edit_post_meta',  $post, '_protected') );
1035
			$this->assertTrue( $admin->has_cap('add_post_meta',  $post, '_protected') );
1036
			$this->assertTrue( $admin->has_cap('delete_post_meta',  $post, '_protected') );
1037
1038
			$this->assertTrue( $admin->has_cap('edit_post_meta', $post, 'not_protected') );
1039
			$this->assertTrue( $admin->has_cap('add_post_meta', $post, 'not_protected') );
1040
			$this->assertTrue( $admin->has_cap('delete_post_meta', $post, 'not_protected') );
1041
1042
			register_meta( 'post', 'not_protected', array( $this, '_meta_filter' ), array( $this, '_meta_no_you_cant' ) );
1043
			$this->assertFalse( $admin->has_cap('edit_post_meta',  $post, 'not_protected') );
1044
			$this->assertFalse( $admin->has_cap('add_post_meta',  $post, 'not_protected') );
1045
			$this->assertFalse( $admin->has_cap('delete_post_meta',  $post, 'not_protected') );
1046
		}
1047
	}
1048
1049
	function authorless_post_statuses() {
1050
		return array( array( 'draft' ), array( 'private' ), array( 'publish' ) );
1051
	}
1052
1053
	/**
1054
	 * @ticket 27020
1055
	 * @dataProvider authorless_post_statuses
1056
	 */
1057
	function test_authorless_post( $status ) {
1058
		// Make a post without an author
1059
		$post = self::factory()->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
1060
1061
		// Add an editor and contributor
1062
		$editor = self::$users['editor'];
1063
		$contributor = self::$users['contributor'];
1064
1065
		// editor can publish, edit, view, and trash
1066
		$this->assertTrue( $editor->has_cap( 'publish_post', $post ) );
1067
		$this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
1068
		$this->assertTrue( $editor->has_cap( 'delete_post', $post ) );
1069
		$this->assertTrue( $editor->has_cap( 'read_post', $post ) );
1070
1071
		// a contributor cannot (except read a published post)
1072
		$this->assertFalse( $contributor->has_cap( 'publish_post', $post ) );
1073
		$this->assertFalse( $contributor->has_cap( 'edit_post', $post ) );
1074
		$this->assertFalse( $contributor->has_cap( 'delete_post', $post ) );
1075
		$this->assertEquals( $status === 'publish', $contributor->has_cap( 'read_post', $post ) );
1076
	}
1077
1078
	/**
1079
	 * @ticket 16714
1080
	 */
1081
	function test_create_posts_caps() {
1082
		$admin       = self::$users['administrator'];
1083
		$author      = self::$users['author'];
1084
		$editor      = self::$users['editor'];
1085
		$contributor = self::$users['contributor'];
1086
		$subscriber  = self::$users['subscriber'];
1087
1088
		// create_posts isn't a real cap.
1089
		$this->assertFalse($admin->has_cap('create_posts'));
1090
		$this->assertFalse($author->has_cap('create_posts'));
1091
		$this->assertFalse($editor->has_cap('create_posts'));
1092
		$this->assertFalse($contributor->has_cap('create_posts'));
1093
		$this->assertFalse($subscriber->has_cap('create_posts'));
1094
1095
		register_post_type( 'foobar' );
1096
		$cap = get_post_type_object( 'foobar' )->cap;
1097
1098
		$this->assertEquals( 'edit_posts', $cap->create_posts );
1099
1100
		$this->assertTrue($admin->has_cap( $cap->create_posts ));
1101
		$this->assertTrue($author->has_cap( $cap->create_posts ));
1102
		$this->assertTrue($editor->has_cap( $cap->create_posts ));
1103
		$this->assertTrue($contributor->has_cap( $cap->create_posts ));
1104
		$this->assertFalse($subscriber->has_cap( $cap->create_posts ));
1105
1106
		_unregister_post_type( 'foobar' );
1107
1108
		// Primitive capability edit_foobars is not assigned to any users.
1109
		register_post_type( 'foobar', array( 'capability_type' => array( 'foobar', 'foobars' ) ) );
1110
		$cap = get_post_type_object( 'foobar' )->cap;
1111
1112
		$this->assertEquals( 'edit_foobars', $cap->create_posts );
1113
1114
		$this->assertFalse($admin->has_cap( $cap->create_posts ));
1115
		$this->assertFalse($author->has_cap( $cap->create_posts ));
1116
		$this->assertFalse($editor->has_cap( $cap->create_posts ));
1117
		$this->assertFalse($contributor->has_cap( $cap->create_posts ));
1118
		$this->assertFalse($subscriber->has_cap( $cap->create_posts ));
1119
1120
		// Add edit_foobars primitive cap to a user.
1121
		$admin->add_cap( 'edit_foobars', true );
1122
		$admin = new WP_User( $admin->ID );
1123
		$this->assertTrue($admin->has_cap( $cap->create_posts ));
1124
		$this->assertFalse($author->has_cap( $cap->create_posts ));
1125
		$this->assertFalse($editor->has_cap( $cap->create_posts ));
1126
		$this->assertFalse($contributor->has_cap( $cap->create_posts ));
1127
		$this->assertFalse($subscriber->has_cap( $cap->create_posts ));
1128
1129
		$admin->remove_cap( 'edit_foobars' );
1130
1131
		_unregister_post_type( 'foobar' );
1132
1133
		$cap = get_post_type_object( 'attachment' )->cap;
1134
		$this->assertEquals( 'upload_files', $cap->create_posts );
1135
		$this->assertEquals( 'edit_posts', $cap->edit_posts );
1136
1137
		$this->assertTrue( $author->has_cap( $cap->create_posts ) );
1138
		$this->assertTrue( $author->has_cap( $cap->edit_posts ) );
1139
		$this->assertTrue( $contributor->has_cap( $cap->edit_posts ) );
1140
		$this->assertFalse( $contributor->has_cap( $cap->create_posts ) );
1141
		$this->assertFalse( $subscriber->has_cap( $cap->create_posts ) );
1142
	}
1143
1144
	function test_page_meta_caps() {
1145
		// simple tests for some common meta capabilities
1146
1147
		// Get our author
1148
		$author = self::$users['author'];
1149
1150
		// make a page
1151
		$page = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) );
1152
1153
		// the author of the page
1154
		$this->assertTrue($author->exists(), "Problem getting user " . $author->ID);
1155
1156
		// add some other users
1157
		$admin = self::$users['administrator'];
1158
		$author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
1159
		$editor = self::$users['editor'];
1160
		$contributor = self::$users['contributor'];
1161
1162
		// administrators, editors and the post owner can edit it
1163
		$this->assertTrue($admin->has_cap('edit_page', $page));
1164
		$this->assertTrue($editor->has_cap('edit_page', $page));
1165
		// other authors and contributors can't
1166
		$this->assertFalse($author->has_cap('edit_page', $page));
1167
		$this->assertFalse($author_2->has_cap('edit_page', $page));
1168
		$this->assertFalse($contributor->has_cap('edit_page', $page));
1169
1170
		// administrators, editors and the post owner can delete it
1171
		$this->assertTrue($admin->has_cap('delete_page', $page));
1172
		$this->assertTrue($editor->has_cap('delete_page', $page));
1173
		// other authors and contributors can't
1174
		$this->assertFalse($author->has_cap('delete_page', $page));
1175
		$this->assertFalse($author_2->has_cap('delete_page', $page));
1176
		$this->assertFalse($contributor->has_cap('delete_page', $page));
1177
	}
1178
1179
	/**
1180
	 * @dataProvider dataTaxonomies
1181
	 *
1182
	 * @ticket 35614
1183
	 */
1184
	public function test_taxonomy_capabilities_are_correct( $taxonomy ) {
1185
		if ( ! taxonomy_exists( $taxonomy ) ) {
1186
			register_taxonomy( $taxonomy, 'post' );
1187
		}
1188
1189
		$tax  = get_taxonomy( $taxonomy );
1190
		$user = self::$users['administrator'];
1191
1192
		// Primitive capabilities for all taxonomies should match this:
1193
		$expected = array(
1194
			'manage_terms' => 'manage_categories',
1195
			'edit_terms'   => 'manage_categories',
1196
			'delete_terms' => 'manage_categories',
1197
			'assign_terms' => 'edit_posts',
1198
		);
1199
1200 View Code Duplication
		foreach ( $expected as $meta_cap => $primitive_cap ) {
1201
			$caps = map_meta_cap( $tax->cap->$meta_cap, $user->ID );
1202
			$this->assertEquals( array(
1203
				$primitive_cap,
1204
			), $caps, "Meta cap: {$meta_cap}" );
1205
		}
1206
	}
1207
1208
	/**
1209
	 * @dataProvider dataTaxonomies
1210
	 *
1211
	 * @ticket 35614
1212
	 */
1213
	public function test_default_taxonomy_term_cannot_be_deleted( $taxonomy ) {
1214
		if ( ! taxonomy_exists( $taxonomy ) ) {
1215
			register_taxonomy( $taxonomy, 'post' );
1216
		}
1217
1218
		$tax  = get_taxonomy( $taxonomy );
1219
		$user = self::$users['administrator'];
1220
		$term = self::factory()->term->create_and_get( array(
1221
			'taxonomy' => $taxonomy,
1222
		) );
1223
1224
		update_option( "default_{$taxonomy}", $term->term_id );
1225
1226
		$this->assertTrue( user_can( $user->ID, $tax->cap->delete_terms ) );
1227
		$this->assertFalse( user_can( $user->ID, 'delete_term', $term->term_id ) );
1228
	}
1229
1230
	/**
1231
	 * @dataProvider dataTaxonomies
1232
	 *
1233
	 * @ticket 35614
1234
	 */
1235
	public function test_taxonomy_caps_map_correctly_to_their_meta_cap( $taxonomy ) {
1236
		if ( ! taxonomy_exists( $taxonomy ) ) {
1237
			register_taxonomy( $taxonomy, 'post' );
1238
		}
1239
1240
		$tax  = get_taxonomy( $taxonomy );
1241
		$term = self::factory()->term->create_and_get( array(
1242
			'taxonomy' => $taxonomy,
1243
		) );
1244
1245
		foreach ( self::$users as $role => $user ) {
1246
			$this->assertSame(
1247
				user_can( $user->ID, 'edit_term', $term->term_id ),
1248
				user_can( $user->ID, $tax->cap->edit_terms ),
1249
				"Role: {$role}"
1250
			);
1251
			$this->assertSame(
1252
				user_can( $user->ID, 'delete_term', $term->term_id ),
1253
				user_can( $user->ID, $tax->cap->delete_terms ),
1254
				"Role: {$role}"
1255
			);
1256
			$this->assertSame(
1257
				user_can( $user->ID, 'assign_term', $term->term_id ),
1258
				user_can( $user->ID, $tax->cap->assign_terms ),
1259
				"Role: {$role}"
1260
			);
1261
		}
1262
1263
	}
1264
1265
	public function dataTaxonomies() {
1266
		return array(
1267
			array(
1268
				'post_tag',
1269
			),
1270
			array(
1271
				'category',
1272
			),
1273
			array(
1274
				'standard_custom_taxo',
1275
			),
1276
		);
1277
	}
1278
1279
	/**
1280
	 * @ticket 35614
1281
	 */
1282
	public function test_taxonomy_capabilities_with_custom_caps_are_correct() {
1283
		$expected = array(
1284
			'manage_terms' => 'one',
1285
			'edit_terms'   => 'two',
1286
			'delete_terms' => 'three',
1287
			'assign_terms' => 'four',
1288
		);
1289
		$taxonomy = 'custom_cap_taxo';
1290
		register_taxonomy( $taxonomy, 'post', array(
1291
			'capabilities' => $expected,
1292
		) );
1293
1294
		$tax  = get_taxonomy( $taxonomy );
1295
		$user = self::$users['administrator'];
1296
1297 View Code Duplication
		foreach ( $expected as $meta_cap => $primitive_cap ) {
1298
			$caps = map_meta_cap( $tax->cap->$meta_cap, $user->ID );
1299
			$this->assertEquals( array(
1300
				$primitive_cap,
1301
			), $caps, "Meta cap: {$meta_cap}" );
1302
		}
1303
	}
1304
1305
	/**
1306
	 * @ticket 21786
1307
	 */
1308
	function test_negative_caps() {
1309
		$author = self::$users['author'];
1310
1311
		$author->add_cap( 'foo', false );
1312
		$this->assertTrue ( isset( $author->caps['foo'] ) );
1313
		$this->assertFalse( user_can( $author->ID, 'foo' ) );
1314
1315
		$author->remove_cap( 'foo' );
1316
		$this->assertFalse ( isset( $author->caps['foo'] ) );
1317
		$this->assertFalse( user_can( $author->ID, 'foo' ) );
1318
	}
1319
1320
	/**
1321
	 * @ticket 18932
1322
	 */
1323
	function test_set_role_same_role() {
1324
		$user = self::$users['administrator'];
1325
		$caps = $user->caps;
1326
		$this->assertNotEmpty( $user->caps );
1327
		$user->set_role( 'administrator' );
1328
		$this->assertNotEmpty( $user->caps );
1329
		$this->assertEquals( $caps, $user->caps );
1330
	}
1331
1332
	function test_current_user_can_for_blog() {
1333
		global $wpdb;
1334
1335
		$user = self::$users['administrator'];
1336
		$old_uid = get_current_user_id();
1337
		wp_set_current_user( $user->ID );
1338
1339
		$this->assertTrue( current_user_can_for_blog( get_current_blog_id(), 'edit_posts' ) );
1340
		$this->assertFalse( current_user_can_for_blog( get_current_blog_id(), 'foo_the_bar' ) );
1341
		if ( ! is_multisite() ) {
1342
			$this->assertTrue( current_user_can_for_blog( 12345, 'edit_posts' ) );
1343
			return;
1344
		}
1345
1346
		$suppress = $wpdb->suppress_errors();
1347
		$this->assertFalse( current_user_can_for_blog( 12345, 'edit_posts' ) );
1348
		$wpdb->suppress_errors( $suppress );
1349
1350
		$blog_id = self::factory()->blog->create( array( 'user_id' => $user->ID ) );
1351
		$this->assertTrue( current_user_can_for_blog( $blog_id, 'edit_posts' ) );
1352
		$this->assertFalse( current_user_can_for_blog( $blog_id, 'foo_the_bar' ) );
1353
1354
		wp_set_current_user( $old_uid );
1355
	}
1356
1357
	/**
1358
	 * @group ms-required
1359
	 */
1360
	function test_borked_current_user_can_for_blog() {
1361
		$orig_blog_id = get_current_blog_id();
1362
		$blog_id = self::factory()->blog->create();
1363
1364
		$this->_nullify_current_user();
1365
1366
		add_action( 'switch_blog', array( $this, '_nullify_current_user_and_keep_nullifying_user' ) );
1367
1368
		current_user_can_for_blog( $blog_id, 'edit_posts' );
1369
1370
		$this->assertEquals( $orig_blog_id, get_current_blog_id() );
1371
	}
1372
1373
	function _nullify_current_user() {
1374
		// Prevents fatal errors in ::tearDown()'s and other uses of restore_current_blog()
1375
		$function_stack = wp_debug_backtrace_summary( null, 0, false );
1376
		if ( in_array( 'restore_current_blog', $function_stack ) ) {
1377
			return;
1378
		}
1379
		$GLOBALS['current_user'] = null;
1380
	}
1381
1382
	function _nullify_current_user_and_keep_nullifying_user() {
1383
		add_action( 'set_current_user', array( $this, '_nullify_current_user' ) );
1384
	}
1385
1386
	/**
1387
	 * @ticket 28374
1388
	 */
1389
	function test_current_user_edit_caps() {
1390
		$user = self::$users['contributor'];
1391
		wp_set_current_user( $user->ID );
1392
1393
		$user->add_cap( 'publish_posts' );
1394
		$this->assertTrue( $user->has_cap( 'publish_posts' ) );
1395
1396
		$user->add_cap( 'publish_pages' );
1397
		$this->assertTrue( $user->has_cap( 'publish_pages' ) );
1398
1399
		$user->remove_cap( 'publish_pages' );
1400
		$this->assertFalse( $user->has_cap( 'publish_pages' ) );
1401
1402
		$user->remove_cap( 'publish_posts' );
1403
		$this->assertFalse( $user->has_cap( 'publish_posts' ) );
1404
	}
1405
1406
	function test_subscriber_cant_edit_posts() {
1407
		$user = self::$users['subscriber'];
1408
		wp_set_current_user( $user->ID );
1409
1410
		$post = self::factory()->post->create( array( 'post_author' => 1 ) );
1411
1412
		$this->assertFalse( current_user_can( 'edit_post', $post ) );
1413
		$this->assertFalse( current_user_can( 'edit_post', $post + 1 ) );
1414
	}
1415
1416
	/**
1417
	 * @group ms-required
1418
	 */
1419
	function test_multisite_administrator_can_not_edit_users() {
1420
		$user = self::$users['administrator'];
1421
		$other_user = self::$users['subscriber'];
1422
1423
		wp_set_current_user( $user->ID );
1424
1425
		$this->assertFalse( current_user_can( 'edit_user', $other_user->ID ) );
1426
	}
1427
1428
	function test_user_can_edit_self() {
1429
		foreach ( self::$users as $role => $user ) {
1430
			wp_set_current_user( $user->ID );
1431
			$this->assertTrue( current_user_can( 'edit_user', $user->ID ), "User with role {$role} should have the capability to edit their own profile" );
1432
		}
1433
	}
1434
1435 View Code Duplication
	public function test_only_admins_and_super_admins_can_remove_users() {
1436
		if ( is_multisite() ) {
1437
			$this->assertTrue( user_can( self::$super_admin->ID,        'remove_user', self::$users['subscriber']->ID ) );
1438
		}
1439
1440
		$this->assertTrue( user_can( self::$users['administrator']->ID, 'remove_user', self::$users['subscriber']->ID ) );
1441
1442
		$this->assertFalse( user_can( self::$users['editor']->ID,       'remove_user', self::$users['subscriber']->ID ) );
1443
		$this->assertFalse( user_can( self::$users['author']->ID,       'remove_user', self::$users['subscriber']->ID ) );
1444
		$this->assertFalse( user_can( self::$users['contributor']->ID,  'remove_user', self::$users['subscriber']->ID ) );
1445
		$this->assertFalse( user_can( self::$users['subscriber']->ID,   'remove_user', self::$users['subscriber']->ID ) );
1446
	}
1447
1448
	/**
1449
	 * @group ms-required
1450
	 */
1451 View Code Duplication
	public function test_only_super_admins_can_delete_users_on_multisite() {
1452
		$this->assertTrue( user_can( self::$super_admin->ID,             'delete_user', self::$users['subscriber']->ID ) );
1453
1454
		$this->assertFalse( user_can( self::$users['administrator']->ID, 'delete_user', self::$users['subscriber']->ID ) );
1455
		$this->assertFalse( user_can( self::$users['editor']->ID,        'delete_user', self::$users['subscriber']->ID ) );
1456
		$this->assertFalse( user_can( self::$users['author']->ID,        'delete_user', self::$users['subscriber']->ID ) );
1457
		$this->assertFalse( user_can( self::$users['contributor']->ID,   'delete_user', self::$users['subscriber']->ID ) );
1458
		$this->assertFalse( user_can( self::$users['subscriber']->ID,    'delete_user', self::$users['subscriber']->ID ) );
1459
	}
1460
1461
	/**
1462
	 * @group ms-excluded
1463
	 */
1464
	public function test_only_admins_can_delete_users_on_single_site() {
1465
		$this->assertTrue( user_can( self::$users['administrator']->ID, 'delete_user', self::$users['subscriber']->ID ) );
1466
1467
		$this->assertFalse( user_can( self::$users['editor']->ID,       'delete_user', self::$users['subscriber']->ID ) );
1468
		$this->assertFalse( user_can( self::$users['author']->ID,       'delete_user', self::$users['subscriber']->ID ) );
1469
		$this->assertFalse( user_can( self::$users['contributor']->ID,  'delete_user', self::$users['subscriber']->ID ) );
1470
		$this->assertFalse( user_can( self::$users['subscriber']->ID,   'delete_user', self::$users['subscriber']->ID ) );
1471
	}
1472
1473 View Code Duplication
	public function test_only_admins_and_super_admins_can_promote_users() {
1474
		if ( is_multisite() ) {
1475
			$this->assertTrue( user_can( self::$super_admin->ID,              'promote_user', self::$users['subscriber']->ID ) );
1476
		}
1477
1478
		$this->assertTrue( user_can( self::$users['administrator']->ID, 'promote_user', self::$users['subscriber']->ID ) );
1479
1480
		$this->assertFalse( user_can( self::$users['editor']->ID,       'promote_user', self::$users['subscriber']->ID ) );
1481
		$this->assertFalse( user_can( self::$users['author']->ID,       'promote_user', self::$users['subscriber']->ID ) );
1482
		$this->assertFalse( user_can( self::$users['contributor']->ID,  'promote_user', self::$users['subscriber']->ID ) );
1483
		$this->assertFalse( user_can( self::$users['subscriber']->ID,   'promote_user', self::$users['subscriber']->ID ) );
1484
	}
1485
1486
	/**
1487
	 * @ticket 33694
1488
	 */
1489
	function test_contributor_cannot_edit_scheduled_post() {
1490
1491
		// Add a contributor
1492
		$contributor = self::$users['contributor'];
1493
1494
		// Give them a scheduled post
1495
		$post = $this->factory->post->create_and_get( array(
1496
			'post_author' => $contributor->ID,
1497
			'post_status' => 'future',
1498
		) );
1499
1500
		// Ensure contributor can't edit or trash the post
1501
		$this->assertFalse( user_can( $contributor->ID, 'edit_post', $post->ID ) );
1502
		$this->assertFalse( user_can( $contributor->ID, 'delete_post', $post->ID ) );
1503
1504
		// Test the tests
1505
		$this->assertTrue( defined( 'EMPTY_TRASH_DAYS' ) );
1506
		$this->assertNotEmpty( EMPTY_TRASH_DAYS );
1507
1508
		// Trash it
1509
		$trashed = wp_trash_post( $post->ID );
1510
		$this->assertNotEmpty( $trashed );
1511
1512
		// Ensure contributor can't edit, un-trash, or delete the post
1513
		$this->assertFalse( user_can( $contributor->ID, 'edit_post', $post->ID ) );
1514
		$this->assertFalse( user_can( $contributor->ID, 'delete_post', $post->ID ) );
1515
1516
	}
1517
1518
	/**
1519
	 * @group ms-required
1520
	 */
1521 View Code Duplication
	function test_multisite_administrator_with_manage_network_users_can_edit_users() {
1522
		$user = self::$users['administrator'];
1523
		$user->add_cap( 'manage_network_users' );
1524
		$other_user = self::$users['subscriber'];
1525
1526
		wp_set_current_user( $user->ID );
1527
1528
		$can_edit_user = current_user_can( 'edit_user', $other_user->ID );
1529
1530
		$user->remove_cap( 'manage_network_users' );
1531
1532
		$this->assertTrue( $can_edit_user );
1533
	}
1534
1535
	/**
1536
	 * @group ms-required
1537
	 */
1538 View Code Duplication
	function test_multisite_administrator_with_manage_network_users_can_not_edit_super_admin() {
1539
		$user = self::$users['administrator'];
1540
		$user->add_cap( 'manage_network_users' );
1541
1542
		wp_set_current_user( $user->ID );
1543
1544
		$can_edit_user = current_user_can( 'edit_user', self::$super_admin->ID );
1545
1546
		$user->remove_cap( 'manage_network_users' );
1547
1548
		$this->assertFalse( $can_edit_user );
1549
	}
1550
1551
	/**
1552
	 * @ticket 16956
1553
	 * @expectedIncorrectUsage map_meta_cap
1554
	 */
1555
	function test_require_edit_others_posts_if_post_type_doesnt_exist() {
1556
		register_post_type( 'existed' );
1557
		$post_id = self::factory()->post->create( array( 'post_type' => 'existed' ) );
1558
		_unregister_post_type( 'existed' );
1559
1560
		$subscriber_id = self::$users['subscriber']->ID;
1561
		$editor_id = self::$users['editor']->ID;
1562
1563
		foreach ( array( 'delete_post', 'edit_post', 'read_post', 'publish_post' ) as $cap ) {
1564
			wp_set_current_user( $subscriber_id );
1565
			$this->assertSame( array( 'edit_others_posts' ), map_meta_cap( $cap, $subscriber_id, $post_id ) );
1566
			$this->assertFalse( current_user_can( $cap, $post_id ) );
1567
1568
			wp_set_current_user( $editor_id );
1569
			$this->assertSame( array( 'edit_others_posts' ), map_meta_cap( $cap, $editor_id, $post_id ) );
1570
			$this->assertTrue( current_user_can( $cap, $post_id ) );
1571
		}
1572
	}
1573
1574
	/**
1575
	 * @ticket 17253
1576
	 */
1577
	function test_cpt_with_page_capability_type() {
1578
1579
		register_post_type( 'page_capability', array(
1580
			'capability_type' => 'page',
1581
		) );
1582
1583
		$cpt = get_post_type_object( 'page_capability' );
1584
1585
		$admin       = self::$users['administrator'];
1586
		$editor      = self::$users['editor'];
1587
		$author      = self::$users['author'];
1588
		$contributor = self::$users['contributor'];
1589
1590
		$this->assertEquals( 'edit_pages', $cpt->cap->edit_posts );
1591
		$this->assertTrue( user_can( $admin->ID, $cpt->cap->edit_posts ) );
1592
		$this->assertTrue( user_can( $editor->ID, $cpt->cap->edit_posts ) );
1593
		$this->assertFalse( user_can( $author->ID, $cpt->cap->edit_posts ) );
1594
		$this->assertFalse( user_can( $contributor->ID, $cpt->cap->edit_posts ) );
1595
1596
		$admin_post = self::factory()->post->create_and_get( array(
1597
			'post_author' => $admin->ID,
1598
			'post_type'   => 'page_capability',
1599
		) );
1600
1601
		$this->assertTrue( user_can( $admin->ID, 'edit_post', $admin_post->ID ) );
1602
		$this->assertTrue( user_can( $editor->ID, 'edit_post', $admin_post->ID ) );
1603
		$this->assertFalse( user_can( $author->ID, 'edit_post', $admin_post->ID ) );
1604
		$this->assertFalse( user_can( $contributor->ID, 'edit_post', $admin_post->ID ) );
1605
1606
		$author_post = self::factory()->post->create_and_get( array(
1607
			'post_author' => $author->ID,
1608
			'post_type'   => 'page_capability',
1609
		) );
1610
1611
		$this->assertTrue( user_can( $admin->ID, 'edit_post', $author_post->ID ) );
1612
		$this->assertTrue( user_can( $editor->ID, 'edit_post', $author_post->ID ) );
1613
		$this->assertFalse( user_can( $author->ID, 'edit_post', $author_post->ID ) );
1614
		$this->assertFalse( user_can( $contributor->ID, 'edit_post', $author_post->ID ) );
1615
1616
		_unregister_post_type( 'page_capability' );
1617
1618
	}
1619
1620
	public function testNonLoggedInUsersHaveNoCapabilities() {
1621
1622
		$this->assertFalse( is_user_logged_in() );
1623
1624
		$caps = $this->getAllCapsAndRoles();
1625
1626
		foreach ( $caps as $cap => $roles ) {
1627
			$this->assertFalse( current_user_can( $cap ), "Non-logged-in user should not have the {$cap} capability" );
1628
		}
1629
1630
		// Special cases for link manager and unfiltered uploads:
1631
		$this->assertFalse( current_user_can( 'manage_links' ), "Non-logged-in user should not have the manage_links capability" );
1632
		$this->assertFalse( current_user_can( 'unfiltered_upload' ), "Non-logged-in user should not have the unfiltered_upload capability" );
1633
1634
		$this->assertFalse( current_user_can( 'start_a_fire' ), "Non-logged-in user should not have a custom capability" );
1635
		$this->assertFalse( current_user_can( 'do_not_allow' ), "Non-logged-in user should not have the do_not_allow capability" );
1636
	}
1637
1638
	protected $_role_test_wp_roles_role;
1639
	/**
1640
	 * @ticket 23016
1641
	 */
1642
	public function test_wp_roles_init_action() {
1643
		$this->_role_test_wp_roles_init = array(
1644
			'role' => 'test_wp_roles_init',
1645
			'info' => array(
1646
				'name' => 'Test WP Roles Init',
1647
				'capabilities' => array( 'testing_magic' => true ),
1648
			),
1649
		);
1650
		add_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ), 10, 1 );
1651
1652
		$wp_roles = new WP_Roles();
1653
1654
		remove_action( 'wp_roles_init', array( $this, '_hook_wp_roles_init' ) );
1655
1656
		$expected = new WP_Role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['capabilities'] );
1657
1658
		$role = $wp_roles->get_role( $this->_role_test_wp_roles_init['role'] );
1659
1660
		$this->assertEquals( $expected, $role );
1661
		$this->assertContains( $this->_role_test_wp_roles_init['info']['name'], $wp_roles->role_names );
1662
	}
1663
1664
	public function _hook_wp_roles_init( $wp_roles ) {
1665
		$wp_roles->add_role( $this->_role_test_wp_roles_init['role'], $this->_role_test_wp_roles_init['info']['name'], $this->_role_test_wp_roles_init['info']['capabilities'] );
1666
	}
1667
1668
	/**
1669
	 * @ticket 23016
1670
	 * @expectedDeprecated WP_Roles::reinit
1671
	 */
1672
	public function test_wp_roles_reinit_deprecated() {
1673
		$wp_roles = new WP_Roles();
1674
		$wp_roles->reinit();
0 ignored issues
show
Deprecated Code introduced by
The method WP_Roles::reinit() has been deprecated with message: 4.7.0 Use new WP_Roles()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1675
	}
1676
1677
	/**
1678
	 * @ticket 38412
1679
	 */
1680
	public function test_no_one_can_edit_user_meta_for_non_existent_term() {
1681
		wp_set_current_user( self::$super_admin->ID );
1682
		$this->assertFalse( current_user_can( 'edit_user_meta', 999999 ) );
1683
	}
1684
1685
	/**
1686
	 * @ticket 38412
1687
	 */
1688 View Code Duplication
	public function test_user_can_edit_user_meta() {
1689
		wp_set_current_user( self::$users['administrator']->ID );
1690
		if ( is_multisite() ) {
1691
			grant_super_admin( self::$users['administrator']->ID );
1692
		}
1693
		$this->assertTrue( current_user_can( 'edit_user_meta', self::$users['subscriber']->ID, 'foo' ) );
1694
	}
1695
1696
	/**
1697
	 * @ticket 38412
1698
	 */
1699
	public function test_user_cannot_edit_user_meta() {
1700
		wp_set_current_user( self::$users['editor']->ID );
1701
		$this->assertFalse( current_user_can( 'edit_user_meta', self::$users['subscriber']->ID, 'foo' ) );
1702
	}
1703
1704
	/**
1705
	 * @ticket 38412
1706
	 */
1707
	public function test_no_one_can_delete_user_meta_for_non_existent_term() {
1708
		wp_set_current_user( self::$super_admin->ID );
1709
		$this->assertFalse( current_user_can( 'delete_user_meta', 999999, 'foo' ) );
1710
	}
1711
1712
	/**
1713
	 * @ticket 38412
1714
	 */
1715 View Code Duplication
	public function test_user_can_delete_user_meta() {
1716
		wp_set_current_user( self::$users['administrator']->ID );
1717
		if ( is_multisite() ) {
1718
			grant_super_admin( self::$users['administrator']->ID );
1719
		}
1720
		$this->assertTrue( current_user_can( 'delete_user_meta', self::$users['subscriber']->ID, 'foo' ) );
1721
	}
1722
1723
	/**
1724
	 * @ticket 38412
1725
	 */
1726
	public function test_user_cannot_delete_user_meta() {
1727
		wp_set_current_user( self::$users['editor']->ID );
1728
		$this->assertFalse( current_user_can( 'delete_user_meta', self::$users['subscriber']->ID, 'foo' ) );
1729
	}
1730
1731
	/**
1732
	 * @ticket 38412
1733
	 */
1734
	public function test_no_one_can_add_user_meta_for_non_existent_term() {
1735
		wp_set_current_user( self::$super_admin->ID );
1736
		$this->assertFalse( current_user_can( 'add_user_meta', 999999, 'foo' ) );
1737
	}
1738
1739
	/**
1740
	 * @ticket 38412
1741
	 */
1742 View Code Duplication
	public function test_user_can_add_user_meta() {
1743
		wp_set_current_user( self::$users['administrator']->ID );
1744
		if ( is_multisite() ) {
1745
			grant_super_admin( self::$users['administrator']->ID );
1746
		}
1747
		$this->assertTrue( current_user_can( 'add_user_meta', self::$users['subscriber']->ID, 'foo' ) );
1748
	}
1749
1750
	/**
1751
	 * @ticket 38412
1752
	 */
1753
	public function test_user_cannot_add_user_meta() {
1754
		wp_set_current_user( self::$users['editor']->ID );
1755
		$this->assertFalse( current_user_can( 'add_user_meta', self::$users['subscriber']->ID, 'foo' ) );
1756
	}
1757
1758
	/**
1759
	 * @ticket 39063
1760
	 * @group ms-required
1761
	 */
1762 View Code Duplication
	public function test_only_super_admins_can_remove_themselves_on_multisite() {
1763
		$this->assertTrue( user_can( self::$super_admin->ID, 'remove_user', self::$super_admin->ID ) );
1764
1765
		$this->assertFalse( user_can( self::$users['administrator']->ID, 'remove_user', self::$users['administrator']->ID ) );
1766
		$this->assertFalse( user_can( self::$users['editor']->ID,        'remove_user', self::$users['editor']->ID ) );
1767
		$this->assertFalse( user_can( self::$users['author']->ID,        'remove_user', self::$users['author']->ID ) );
1768
		$this->assertFalse( user_can( self::$users['contributor']->ID,   'remove_user', self::$users['contributor']->ID ) );
1769
		$this->assertFalse( user_can( self::$users['subscriber']->ID,    'remove_user', self::$users['subscriber']->ID ) );
1770
	}
1771
}
1772