forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapMetaCap.php
More file actions
248 lines (198 loc) · 9.75 KB
/
mapMetaCap.php
File metadata and controls
248 lines (198 loc) · 9.75 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
<?php
/**
* @group user
* @group capabilities
*/
class Tests_User_MapMetaCap extends WP_UnitTestCase {
var $super_admins = null;
function setUp() {
parent::setUp();
$this->user_ids = array();
$this->user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
$this->author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
if ( isset( $GLOBALS['super_admins'] ) )
$this->super_admins = $GLOBALS['super_admins'];
$user = new WP_User( $this->user_id );
$GLOBALS['super_admins'] = array( $user->user_login );
$this->post_type = rand_str( 20 );
register_post_type( $this->post_type );
$this->post_id = wp_insert_post( array(
'post_title' => rand_str(),
'post_type' => $this->post_type,
'post_status' => 'private',
'post_author' => $this->author_id,
) );
}
function tearDown() {
$GLOBALS['super_admins'] = $this->super_admins;
unset( $GLOBALS['wp_post_types'][ $this->post_type ] );
parent::tearDown();
}
function test_capability_type_post_with_no_extra_caps() {
register_post_type( $this->post_type, array(
'capability_type' => 'post',
) );
$post_type_object = get_post_type_object( $this->post_type );
$this->assertTrue( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
}
function test_custom_capability_type_with_map_meta_cap() {
register_post_type( $this->post_type, array(
'capability_type' => 'book',
'map_meta_cap' => true,
) );
$post_type_object = get_post_type_object( $this->post_type );
$this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_books' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_books' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
}
function test_capability_type_post_with_one_renamed_cap() {
register_post_type( $this->post_type, array(
'capability_type' => 'post',
'capabilities' => array( 'edit_posts' => 'edit_books' ),
) );
$post_type_object = get_post_type_object( $this->post_type );
$this->assertFalse( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_post' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'edit_post' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_post' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_post' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_post' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_post' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
}
function test_capability_type_post_map_meta_cap_true_with_renamed_cap() {
register_post_type( $this->post_type, array(
'capability_type' => 'post',
'map_meta_cap' => true,
'capabilities' => array(
'edit_post' => 'edit_book', // maps back to itself.
'edit_others_posts' => 'edit_others_books',
),
) );
$post_type_object = get_post_type_object( $this->post_type );
$this->assertTrue( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
}
function test_capability_type_post_with_all_meta_caps_renamed() {
register_post_type( $this->post_type, array(
'capability_type' => 'post',
'capabilities' => array(
'edit_post' => 'edit_book',
'read_post' => 'read_book',
'delete_post' => 'delete_book',
),
) );
$post_type_object = get_post_type_object( $this->post_type );
$this->assertFalse( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_book' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'edit_book' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_book' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_book' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_book' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_book' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
}
function test_capability_type_post_with_all_meta_caps_renamed_mapped() {
register_post_type( $this->post_type, array(
'capability_type' => 'post',
'map_meta_cap' => true,
'capabilities' => array(
'edit_post' => 'edit_book',
'read_post' => 'read_book',
'delete_post' => 'delete_book',
),
) );
$post_type_object = get_post_type_object( $this->post_type );
$this->assertTrue( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
}
function test_unfiltered_html_cap() {
if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
$this->markTestSkipped( 'DISALLOW_UNFILTERED_HTML is defined.' );
if ( is_multisite() ) {
$this->assertEquals( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) );
$this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
} else {
$this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
}
}
/**
* @ticket 20488
*/
function test_file_edit_caps_not_reliant_on_unfiltered_html_constant() {
if ( defined( 'DISALLOW_FILE_MODS' ) || defined( 'DISALLOW_FILE_EDIT' ) )
$this->markTestSkipped('DISALLOW_FILE_MODS or DISALLOW_FILE_EDIT is defined.');
if ( defined( 'DISALLOW_UNFILTERED_HTML' ) ) {
if ( ! DISALLOW_UNFILTERED_HTML )
$this->markTestSkipped( 'DISALLOW_UNFILTERED_HTML is defined.' );
} else {
define( 'DISALLOW_UNFILTERED_HTML', true );
}
$this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', $this->user_id ) );
$this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', $this->user_id ) );
}
/**
* Test a post without an author.
*
* @ticket 27020
*/
function test_authorless_posts_capabilties() {
$post_id = $this->factory->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => 'publish' ) );
$editor = $this->factory->user->create( array( 'role' => 'editor' ) );
$this->assertEquals( array( 'edit_others_posts', 'edit_published_posts' ), map_meta_cap( 'edit_post', $editor, $post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_published_posts' ), map_meta_cap( 'delete_post', $editor, $post_id ) );
}
}