forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpPostType.php
More file actions
442 lines (383 loc) · 12.8 KB
/
wpPostType.php
File metadata and controls
442 lines (383 loc) · 12.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
<?php
/**
* @group post
*/
class Tests_Post_WP_Post_Type extends WP_UnitTestCase {
public function test_instances() {
global $wp_post_types;
foreach ( $wp_post_types as $post_type ) {
$this->assertInstanceOf( 'WP_Post_Type', $post_type );
}
}
public function test_add_supports_defaults() {
$post_type = 'cpt';
$post_type_object = new WP_Post_Type( $post_type );
$post_type_object->add_supports();
$post_type_supports = get_all_post_type_supports( $post_type );
$post_type_object->remove_supports();
$post_type_supports_after = get_all_post_type_supports( $post_type );
$this->assertSameSets(
array(
'title' => true,
'editor' => true,
),
$post_type_supports
);
$this->assertSameSets( array(), $post_type_supports_after );
}
public function test_add_supports_custom() {
$post_type = 'cpt';
$post_type_object = new WP_Post_Type(
$post_type,
array(
'supports' => array(
'editor',
'comments',
'revisions',
),
)
);
$post_type_object->add_supports();
$post_type_supports = get_all_post_type_supports( $post_type );
$post_type_object->remove_supports();
$post_type_supports_after = get_all_post_type_supports( $post_type );
$this->assertSameSets(
array(
'editor' => true,
'comments' => true,
'revisions' => true,
),
$post_type_supports
);
$this->assertSameSets( array(), $post_type_supports_after );
}
/**
* Test that supports can optionally receive nested args.
*
* @ticket 40413
*/
public function test_add_supports_custom_with_args() {
$post_type = 'cpt';
$post_type_object = new WP_Post_Type(
$post_type,
array(
'supports' => array(
'support_with_args' => array(
'arg1',
'arg2',
),
'support_without_args',
),
)
);
$post_type_object->add_supports();
$post_type_supports = get_all_post_type_supports( $post_type );
$post_type_object->remove_supports();
$post_type_supports_after = get_all_post_type_supports( $post_type );
$this->assertSameSets(
array(
'support_with_args' => array(
array(
'arg1',
'arg2',
),
),
'support_without_args' => true,
),
$post_type_supports
);
$this->assertSameSets( array(), $post_type_supports_after );
}
public function test_does_not_add_query_var_if_not_public() {
$this->set_permalink_structure( '/%postname%' );
/* @var WP $wp */
global $wp;
$post_type = 'cpt';
$post_type_object = new WP_Post_Type(
$post_type,
array(
'rewrite' => false,
'query_var' => 'foobar',
)
);
$post_type_object->add_rewrite_rules();
$this->assertNotContains( 'foobar', $wp->public_query_vars );
}
public function test_adds_query_var_if_public() {
$this->set_permalink_structure( '/%postname%' );
/* @var WP $wp */
global $wp;
$post_type = 'cpt';
$post_type_object = new WP_Post_Type(
$post_type,
array(
'public' => true,
'rewrite' => false,
'query_var' => 'foobar',
)
);
$post_type_object->add_rewrite_rules();
$in_array = in_array( 'foobar', $wp->public_query_vars, true );
$post_type_object->remove_rewrite_rules();
$in_array_after = in_array( 'foobar', $wp->public_query_vars, true );
$this->assertTrue( $in_array );
$this->assertFalse( $in_array_after );
}
public function test_adds_rewrite_rules() {
$this->set_permalink_structure( '/%postname%' );
/* @var WP_Rewrite $wp_rewrite */
global $wp_rewrite;
$post_type = 'cpt';
$post_type_object = new WP_Post_Type(
$post_type,
array(
'public' => true,
'rewrite' => true,
)
);
$post_type_object->add_rewrite_rules();
$rewrite_tags = $wp_rewrite->rewritecode;
$post_type_object->remove_rewrite_rules();
$rewrite_tags_after = $wp_rewrite->rewritecode;
$this->assertNotFalse( array_search( "%$post_type%", $rewrite_tags, true ) );
$this->assertFalse( array_search( "%$post_type%", $rewrite_tags_after, true ) );
}
public function test_register_meta_boxes() {
$post_type = 'cpt';
$post_type_object = new WP_Post_Type( $post_type, array( 'register_meta_box_cb' => '__return_false' ) );
$post_type_object->register_meta_boxes();
$has_action = has_action( "add_meta_boxes_$post_type", '__return_false' );
$post_type_object->unregister_meta_boxes();
$has_action_after = has_action( "add_meta_boxes_$post_type", '__return_false' );
$this->assertSame( 10, $has_action );
$this->assertFalse( $has_action_after );
}
public function test_adds_future_post_hook() {
$post_type = 'cpt';
$post_type_object = new WP_Post_Type( $post_type );
$post_type_object->add_hooks();
$has_action = has_action( "future_$post_type", '_future_post_hook' );
$post_type_object->remove_hooks();
$has_action_after = has_action( "future_$post_type", '_future_post_hook' );
$this->assertSame( 5, $has_action );
$this->assertFalse( $has_action_after );
}
public function test_register_taxonomies() {
global $wp_post_types;
$post_type = 'cpt';
$post_type_object = new WP_Post_Type( $post_type, array( 'taxonomies' => array( 'post_tag' ) ) );
$wp_post_types[ $post_type ] = $post_type_object;
$post_type_object->register_taxonomies();
$taxonomies = get_object_taxonomies( $post_type );
$post_type_object->unregister_taxonomies();
$taxonomies_after = get_object_taxonomies( $post_type );
unset( $wp_post_types[ $post_type ] );
$this->assertSameSets( array( 'post_tag' ), $taxonomies );
$this->assertSameSets( array(), $taxonomies_after );
}
public function test_applies_registration_args_filters() {
$post_type = 'cpt';
$action = new MockAction();
add_filter( 'register_post_type_args', array( $action, 'filter' ) );
add_filter( "register_{$post_type}_post_type_args", array( $action, 'filter' ) );
new WP_Post_Type( $post_type );
new WP_Post_Type( 'random' );
$this->assertSame( 3, $action->get_call_count() );
}
/**
* @ticket 56922
*
* @dataProvider data_should_have_correct_custom_revisions_and_autosaves_controllers_properties
*
* @covers WP_Post_Type::set_props
*
* @param string $property_name Property name.
* @param string $property_value Property value.
* @param string|bool $expected_property_value Expected property value.
*/
public function test_should_have_correct_custom_revisions_and_autosaves_controllers_properties( $property_name, $property_value, $expected_property_value ) {
$properties = null === $property_value ? array() : array( $property_name => $property_value );
$post_type = new WP_Post_Type( 'test_post_type', $properties );
$this->assertObjectHasProperty( $property_name, $post_type, "The WP_Post_Type object does not have the expected {$property_name} property." );
$this->assertSame(
$expected_property_value,
$post_type->$property_name,
sprintf( 'Expected the property "%s" to have the %s value.', $property_name, var_export( $expected_property_value, true ) )
);
}
/**
* Data provider for test_should_allow_to_set_custom_revisions_and_autosaves_controllers_properties.
*
* @return array[] Arguments {
* @type string $property_name Property name.
* @type string $property_value Property value.
* @type string|bool $expected_property_value Expected property value.
* }
*/
public function data_should_have_correct_custom_revisions_and_autosaves_controllers_properties() {
return array(
'autosave_rest_controller_class property' => array(
'autosave_rest_controller_class',
'My_Custom_Template_Autosaves_Controller',
'My_Custom_Template_Autosaves_Controller',
),
'autosave_rest_controller_class property (null value)' => array(
'autosave_rest_controller_class',
null,
false,
),
'revisions_rest_controller_class property' => array(
'revisions_rest_controller_class',
'My_Custom_Template_Revisions_Controller',
'My_Custom_Template_Revisions_Controller',
),
'revisions_rest_controller_class property (null value)' => array(
'revisions_rest_controller_class',
null,
false,
),
);
}
/**
* @ticket 56922
*
* @covers WP_Post_Type::get_revisions_rest_controller
*
* @dataProvider data_get_revisions_rest_controller_should_return_correct_values
*
* @param bool $show_in_rest Enables "show_in_rest" support.
* @param bool $supports_revisions Enables revisions support.
* @param string|bool $revisions_rest_controller_class Custom revisions REST controller class.
* @param string|null $expected_value Expected value.
*/
public function test_get_revisions_rest_controller_should_return_correct_values( $show_in_rest, $supports_revisions, $revisions_rest_controller_class, $expected_value ) {
$post_type = 'test_post_type';
$properties = array(
'show_in_rest' => $show_in_rest,
'supports' => $supports_revisions ? array( 'revisions' ) : array(),
'revisions_rest_controller_class' => $revisions_rest_controller_class,
);
register_post_type( $post_type, $properties );
$post_type = get_post_type_object( $post_type );
$controller = $post_type->get_revisions_rest_controller();
if ( $expected_value ) {
$this->assertInstanceOf( $expected_value, $controller );
return;
}
$this->assertSame( $expected_value, $controller );
}
/**
* Data provider for test_get_revisions_rest_controller_should_return_correct_values.
*
* @return array[] Arguments {
* @type bool $show_in_rest Enables "show_in_rest" support.
* @type bool $supports_revisions Enables revisions support.
* @type string|bool $revisions_rest_controller_class Custom revisions REST controller class.
* @type string|null $expected_value Expected value.
* }
*/
public function data_get_revisions_rest_controller_should_return_correct_values() {
return array(
'disable show_in_rest' => array(
false,
false,
false,
null,
),
'disable revisions support' => array(
true,
false,
false,
null,
),
'default rest revisions controller' => array(
true,
true,
false,
WP_REST_Revisions_Controller::class,
),
'incorrect rest revisions controller' => array(
true,
true,
stdClass::class,
null,
),
'correct rest revisions controller' => array(
true,
true,
WP_REST_Template_Revisions_Controller::class,
WP_REST_Template_Revisions_Controller::class,
),
);
}
/**
* @ticket 56922
*
* @covers WP_Post_Type::get_autosave_rest_controller
*
* @dataProvider data_get_autosave_rest_controller_should_return_correct_values
*
* @param bool $show_in_rest Enables "show_in_rest" support.
* @param string $post_type Post type.
* @param string|bool $autosave_rest_controller_class Custom autosave REST controller class.
* @param string|null $expected_value Expected value.
*/
public function test_get_autosave_rest_controller_should_return_correct_values( $show_in_rest, $post_type, $autosave_rest_controller_class, $expected_value ) {
$properties = array(
'show_in_rest' => $show_in_rest,
'autosave_rest_controller_class' => $autosave_rest_controller_class,
);
register_post_type( $post_type, $properties );
$post_type = get_post_type_object( $post_type );
$controller = $post_type->get_autosave_rest_controller();
if ( $expected_value ) {
$this->assertInstanceOf( $expected_value, $controller );
return;
}
$this->assertSame( $expected_value, $controller );
}
/**
* Data provider for test_get_autosave_rest_controller_should_return_correct_values.
*
* @return array[] Arguments {
* @type bool $show_in_rest Enables "show_in_rest" support.
* @type string $post_type Post type.
* @type string|bool $autosave_rest_controller_class Custom autosave REST controller class.
* @type string|null $expected_value Expected value.
* }
*/
public function data_get_autosave_rest_controller_should_return_correct_values() {
return array(
'disable show_in_rest' => array(
false,
'attachment',
false,
null,
),
'invalid post type' => array(
true,
'attachment',
false,
null,
),
'default rest autosave controller' => array(
true,
'test_post_type',
false,
WP_REST_Autosaves_Controller::class,
),
'incorrect rest autosave controller' => array(
true,
'test_post_type',
stdClass::class,
null,
),
'correct rest autosave controller' => array(
true,
'test_post_type',
WP_REST_Template_Autosaves_Controller::class,
WP_REST_Template_Autosaves_Controller::class,
),
);
}
}