forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.php
More file actions
574 lines (478 loc) · 15.9 KB
/
types.php
File metadata and controls
574 lines (478 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
/**
* @group post
*/
class Tests_Post_Types extends WP_UnitTestCase {
/**
* Post type.
*
* @since 4.5.0
* @var string
*/
public $post_type;
/**
* Set up.
*
* @since 4.5.0
*/
function setUp() {
parent::setUp();
$this->post_type = rand_str( 20 );
}
function test_register_post_type() {
$this->assertNull( get_post_type_object( 'foo' ) );
register_post_type( 'foo' );
$pobj = get_post_type_object( 'foo' );
$this->assertInstanceOf( 'WP_Post_Type', $pobj );
$this->assertEquals( 'foo', $pobj->name );
// Test some defaults
$this->assertFalse( is_post_type_hierarchical( 'foo' ) );
$this->assertEquals( array(), get_object_taxonomies( 'foo' ) );
_unregister_post_type( 'foo' );
}
/**
* @ticket 31134
*
* @expectedIncorrectUsage register_post_type
*/
function test_register_post_type_with_too_long_name() {
// post type too long
$this->assertInstanceOf( 'WP_Error', register_post_type( 'abcdefghijklmnopqrstuvwxyz0123456789' ) );
}
/**
* @ticket 31134
*
* @expectedIncorrectUsage register_post_type
*/
function test_register_post_type_with_empty_name() {
// post type too short
$this->assertInstanceOf( 'WP_Error', register_post_type( '' ) );
}
/**
* @ticket 35985
* @covers ::register_post_type()
*/
function test_register_post_type_exclude_from_search_should_default_to_opposite_value_of_public() {
/*
* 'public' Default is false
* 'exclude_from_search' Default is null (opposite 'public')
*/
$args = $this->register_post_type( array( 'public' => $public = false ) );
$this->assertNotEquals( $public, $args->exclude_from_search );
}
/**
* @ticket 35985
* @covers ::register_post_type()
*/
function test_register_post_type_publicly_queryable_should_default_to_value_of_public() {
/*
* 'public' Default is false
* 'publicly_queryable' Default is null ('public')
*/
$args = $this->register_post_type( array( 'public' => $public = false ) );
$this->assertSame( $public, $args->publicly_queryable );
}
/**
* @ticket 35985
* @covers ::register_post_type()
*/
function test_register_post_type_show_ui_should_default_to_value_of_public() {
/*
* 'public' Default is false
* 'show_ui' Default is null ('public')
*/
$args = $this->register_post_type( array( 'public' => $public = false ) );
$this->assertSame( $public, $args->show_ui );
}
/**
* @ticket 35985
* @covers ::register_post_type()
*/
function test_register_post_type_show_in_menu_should_default_to_value_of_show_ui() {
/*
* 'public' Default is false
* 'show_ui' Default is null ('public')
* 'show_in_menu Default is null ('show_ui' > 'public')
*/
$args = $this->register_post_type( array( 'public' => $public = false ) );
// Should fall back to 'show_ui'.
$this->assertSame( $args->show_ui, $args->show_in_menu );
// Should fall back to 'show_ui', then 'public'.
$this->assertSame( $public, $args->show_in_menu );
}
/**
* @ticket 35985
* @covers ::register_post_type()
*/
function test_register_post_type_show_in_nav_menus_should_default_to_value_of_public() {
/*
* 'public' Default is false
* 'show_in_nav_menus' Default is null ('public')
*/
$args = $this->register_post_type( array( 'public' => $public = false ) );
$this->assertSame( $public, $args->show_in_nav_menus );
}
/**
* @ticket 35985
* @covers ::register_post_type()
*/
function test_register_post_type_show_in_admin_bar_should_default_to_value_of_show_in_menu() {
/*
* 'public' Default is false
* 'show_in_menu' Default is null ('show_ui' > 'public')
* 'show_in_admin_bar' Default is null ('show_in_menu' > 'show_ui' > 'public')
*/
$args = $this->register_post_type( array( 'public' => $public = false ) );
// Should fall back to 'show_in_menu'.
$this->assertSame( $args->show_in_menu, $args->show_in_admin_bar );
// Should fall back to 'show_ui'.
$this->assertSame( $args->show_ui, $args->show_in_admin_bar );
// Should fall back to 'public'.
$this->assertSame( $public, $args->show_in_admin_bar );
}
function test_register_taxonomy_for_object_type() {
global $wp_taxonomies;
register_post_type( 'bar' );
register_taxonomy_for_object_type( 'post_tag', 'bar' );
$this->assertEquals( array( 'post_tag' ), get_object_taxonomies( 'bar' ) );
register_taxonomy_for_object_type( 'category', 'bar' );
$this->assertEquals( array( 'category', 'post_tag' ), get_object_taxonomies( 'bar' ) );
$this->assertTrue( is_object_in_taxonomy( 'bar', 'post_tag' ) );
$this->assertTrue( is_object_in_taxonomy( 'bar', 'post_tag' ) );
// Clean up. Remove the 'bar' post type from these taxonomies.
$GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' );
$GLOBALS['wp_taxonomies']['category']->object_type = array( 'post' );
$this->assertFalse( is_object_in_taxonomy( 'bar', 'post_tag' ) );
$this->assertFalse( is_object_in_taxonomy( 'bar', 'post_tag' ) );
_unregister_post_type( 'bar' );
}
function test_post_type_exists() {
$this->assertFalse( post_type_exists( 'notaposttype' ) );
$this->assertTrue( post_type_exists( 'post' ) );
}
function test_post_type_supports() {
$this->assertTrue( post_type_supports( 'post', 'post-formats' ) );
$this->assertFalse( post_type_supports( 'page', 'post-formats' ) );
$this->assertFalse( post_type_supports( 'notaposttype', 'post-formats' ) );
$this->assertFalse( post_type_supports( 'post', 'notafeature' ) );
$this->assertFalse( post_type_supports( 'notaposttype', 'notafeature' ) );
}
/**
* @ticket 21586
*/
function test_post_type_with_no_support() {
register_post_type( 'foo', array( 'supports' => array() ) );
$this->assertTrue( post_type_supports( 'foo', 'editor' ) );
$this->assertTrue( post_type_supports( 'foo', 'title' ) );
_unregister_post_type( 'foo' );
register_post_type( 'foo', array( 'supports' => false ) );
$this->assertFalse( post_type_supports( 'foo', 'editor' ) );
$this->assertFalse( post_type_supports( 'foo', 'title' ) );
_unregister_post_type( 'foo' );
}
/**
* @ticket 23302
*/
function test_post_type_with_no_feed() {
global $wp_rewrite;
$old_permastruct = get_option( 'permalink_structure' );
update_option( 'permalink_structure', '%postname%' );
register_post_type( 'foo', array( 'rewrite' => array( 'feeds' => false ) ) );
$this->assertFalse( $wp_rewrite->extra_permastructs['foo']['feed'] );
update_option( 'permalink_structure', $old_permastruct );
_unregister_post_type( 'foo' );
}
/**
* @ticket 30013
*/
public function test_get_post_type_object_with_non_scalar_values() {
$this->assertFalse( post_type_exists( 'foo' ) );
register_post_type( 'foo' );
$this->assertTrue( post_type_exists( 'foo' ) );
$this->assertNotNull( get_post_type_object( 'foo' ) );
$this->assertNull( get_post_type_object( array() ) );
$this->assertNull( get_post_type_object( array( 'foo' ) ) );
$this->assertNull( get_post_type_object( new stdClass ) );
_unregister_post_type( 'foo' );
$this->assertFalse( post_type_exists( 'foo' ) );
}
/**
* @ticket 33023
*/
public function test_get_post_type_object_casting() {
register_post_type( 'foo' );
$before = get_post_type_object( 'foo' )->labels;
get_post_type_labels( get_post_type_object( 'foo' ) );
$after = get_post_type_object( 'foo' )->labels;
$this->assertEquals( $before, $after );
_unregister_post_type( 'foo' );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type() {
register_post_type( 'foo' );
$this->assertTrue( unregister_post_type( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_unknown_post_type() {
$this->assertWPError( unregister_post_type( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_twice() {
register_post_type( 'foo' );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertWPError( unregister_post_type( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_disallow_builtin_post_type() {
$this->assertWPError( unregister_post_type( 'post' ) );
$this->assertWPError( unregister_post_type( 'page' ) );
$this->assertWPError( unregister_post_type( 'attachment' ) );
$this->assertWPError( unregister_post_type( 'revision' ) );
$this->assertWPError( unregister_post_type( 'nav_menu_item' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_query_vars() {
global $wp;
register_post_type(
'foo',
array(
'public' => true,
'query_var' => 'bar',
)
);
$this->assertInternalType( 'int', array_search( 'bar', $wp->public_query_vars ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( array_search( 'bar', $wp->public_query_vars ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_rewrite_tags() {
$this->set_permalink_structure( '/%postname%' );
global $wp_rewrite;
register_post_type(
'foo',
array(
'public' => true,
'query_var' => 'bar',
)
);
$count_before = count( $wp_rewrite->rewritereplace );
$this->assertContains( '%foo%', $wp_rewrite->rewritecode );
$this->assertContains( 'bar=', $wp_rewrite->queryreplace );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertNotContains( '%foo%', $wp_rewrite->rewritecode );
$this->assertNotContains( 'bar=', $wp_rewrite->queryreplace );
$this->assertSame( -- $count_before, count( $wp_rewrite->rewritereplace ) ); // Array was reduced by one value.
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_rewrite_rules() {
$this->set_permalink_structure( '/%postname%' );
global $wp_rewrite;
register_post_type(
'foo',
array(
'public' => true,
'has_archive' => true,
)
);
$this->assertContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertNotContains( 'index.php?post_type=foo', $wp_rewrite->extra_rules_top );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_custom_meta_capabilities() {
global $post_type_meta_caps;
register_post_type(
'foo',
array(
'public' => true,
'capability_type' => 'bar',
'map_meta_cap' => true,
)
);
$this->assertSame( 'read_post', $post_type_meta_caps['read_bar'] );
$this->assertSame( 'delete_post', $post_type_meta_caps['delete_bar'] );
$this->assertSame( 'edit_post', $post_type_meta_caps['edit_bar'] );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( isset( $post_type_meta_caps['read_bar'] ) );
$this->assertFalse( isset( $post_type_meta_caps['delete_bar'] ) );
$this->assertFalse( isset( $post_type_meta_caps['edit_bar'] ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_post_type_supports() {
global $_wp_post_type_features;
register_post_type(
'foo',
array(
'public' => true,
'supports' => array( 'editor', 'author', 'title' ),
)
);
$this->assertEqualSetsWithIndex(
array(
'editor' => true,
'author' => true,
'title' => true,
),
$_wp_post_type_features['foo']
);
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( isset( $_wp_post_type_features['foo'] ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_post_type_from_taxonomies() {
global $wp_taxonomies;
register_post_type(
'foo',
array(
'public' => true,
'taxonomies' => array( 'category', 'post_tag' ),
)
);
$this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['category']->object_type, true ) );
$this->assertInternalType( 'int', array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( array_search( 'foo', $wp_taxonomies['category']->object_type, true ) );
$this->assertFalse( array_search( 'foo', $wp_taxonomies['post_tag']->object_type, true ) );
$this->assertEmpty( get_object_taxonomies( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_the_future_post_hooks() {
global $wp_filter;
register_post_type(
'foo',
array(
'public' => true,
)
);
$this->assertArrayHasKey( 'future_foo', $wp_filter );
$this->assertSame( 1, count( $wp_filter['future_foo']->callbacks ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertArrayNotHasKey( 'future_foo', $wp_filter );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_meta_box_callback() {
global $wp_filter;
register_post_type(
'foo',
array(
'public' => true,
'register_meta_box_cb' => '__return_empty_string',
)
);
$this->assertArrayHasKey( 'add_meta_boxes_foo', $wp_filter );
$this->assertSame( 1, count( $wp_filter['add_meta_boxes_foo']->callbacks ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertArrayNotHasKey( 'add_meta_boxes_foo', $wp_filter );
}
/**
* @ticket 14761
*/
public function test_unregister_post_type_removes_post_type_from_global() {
global $wp_post_types;
register_post_type(
'foo',
array(
'public' => true,
)
);
$this->assertInternalType( 'object', $wp_post_types['foo'] );
$this->assertInternalType( 'object', get_post_type_object( 'foo' ) );
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( isset( $wp_post_types['foo'] ) );
$this->assertNull( get_post_type_object( 'foo' ) );
}
/**
* @ticket 14761
*/
public function test_post_type_does_not_exist_after_unregister_post_type() {
register_post_type(
'foo',
array(
'public' => true,
)
);
$this->assertTrue( unregister_post_type( 'foo' ) );
$this->assertFalse( post_type_exists( 'foo' ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_single_feature() {
$this->assertContains( 'post', get_post_types_by_support( 'title' ) );
$this->assertContains( 'page', get_post_types_by_support( 'title' ) );
$this->assertContains( 'attachment', get_post_types_by_support( 'title' ) );
$this->assertContains( 'nav_menu_item', get_post_types_by_support( 'title' ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_multiple_features() {
$this->assertContains( 'post', get_post_types_by_support( array( 'thumbnail', 'author' ) ) );
$this->assertContains( 'page', get_post_types_by_support( array( 'thumbnail', 'author' ) ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_or_operator() {
$this->assertContains( 'post', get_post_types_by_support( array( 'post-formats', 'page-attributes' ), 'or' ) );
$this->assertContains( 'page', get_post_types_by_support( array( 'post-formats', 'page-attributes' ), 'or' ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_not_operator() {
$this->assertContains( 'attachment', get_post_types_by_support( array( 'thumbnail' ), 'not' ) );
$this->assertContains( 'revision', get_post_types_by_support( array( 'thumbnail' ), 'not' ) );
$this->assertContains( 'nav_menu_item', get_post_types_by_support( array( 'thumbnail' ), 'not' ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_excluding_features() {
$this->assertEqualSets( array(), get_post_types_by_support( array( 'post-formats', 'page-attributes' ) ) );
}
/**
* @ticket 34010
*/
public function test_get_post_types_by_support_non_existant_feature() {
$this->assertEqualSets( array(), get_post_types_by_support( 'somefeature' ) );
}
/**
* Serves as a helper to register a post type for tests.
*
* Uses `$this->post_type` initialized in setUp().
*
* @since 4.5.0
*
* @param array $args register_post_type() arguments.
* @return WP_Post_Type Post type object for `$this->post_type`.
*/
public function register_post_type( $args = array() ) {
register_post_type( $this->post_type, $args );
return get_post_type_object( $this->post_type );
}
}