Repository navigation
Expand file tree
/
Copy pathentity-fields-api-test.php
More file actions
1918 lines (1739 loc) · 77.8 KB
/
Copy pathentity-fields-api-test.php
File metadata and controls
1918 lines (1739 loc) · 77.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Tests for the entity fields API.
*
* @package gutenberg
*
* @covers ::_wp_add_entity_field_modules_to_editor_script
* @covers ::_wp_add_entity_modules_to_editor_script
* @covers ::wp_register_entity_fields
* @covers ::wp_update_entity_fields
* @covers ::wp_unregister_entity_fields
* @covers ::wp_register_entity_field_collection
* @covers ::gutenberg_override_wp_entity_fields_registry
* @covers ::gutenberg_register_core_post_type_supports_fields
* @covers ::gutenberg_register_core_field_collections
* @covers WP_Entity_Fields_Registry::initialize
* @covers WP_Entity_Fields_Registry::register
* @covers WP_Entity_Fields_Registry::unregister
* @covers WP_Entity_Fields_Registry::register_collection
* @covers WP_Entity_Fields_Registry::get_collection
* @covers WP_Entity_Fields_Registry::get_collection_fields
* @covers WP_Entity_Fields_Registry_Gutenberg::get_instance
*/
class Tests_Entity_Fields_API extends WP_UnitTestCase {
/**
* The ids of the default fields every post type gets, whatever it
* supports, but for the design post types, which exclude some of them.
*
* @var string[]
*/
const EVERY_POST_TYPE_FIELDS = array( 'date', 'last_edited_date', 'password', 'scheduled_date', 'status', 'template' );
/**
* The ids of the default fields the design post types exclude: those
* about publishing a post, and the template that renders it.
*
* @var string[]
*/
const DESIGN_EXCLUDED_FIELDS = array( 'date', 'password', 'scheduled_date', 'slug', 'status', 'template' );
/**
* The callbacks a test hooked to `wp_entity_fields_api_init`, as callback and
* priority pairs, removed on tear down.
*
* @var array[]
*/
private $callbacks = array();
/**
* Whether a test unhooked the loader of the core collections, hooked
* back on tear down.
*
* @var bool
*/
private $core_collections_unhooked = false;
/**
* The post types a test registered, unregistered on tear down.
*
* @var string[]
*/
private $post_types = array();
/**
* Tears down each test.
*
* Resetting the registry drops the fields a test registered along with
* the defaults; the next read fires `wp_entity_fields_api_init` again and
* registers the defaults anew.
*/
public function tear_down() {
foreach ( $this->callbacks as list( $callback, $priority ) ) {
remove_action( 'wp_entity_fields_api_init', $callback, $priority );
}
$this->callbacks = array();
if ( $this->core_collections_unhooked ) {
add_action( 'wp_entity_fields_api_init', 'gutenberg_register_core_field_collections', 0 );
$this->core_collections_unhooked = false;
}
foreach ( $this->post_types as $post_type ) {
unregister_post_type( $post_type );
}
$this->post_types = array();
self::reset_registry();
parent::tear_down();
}
/**
* Resets the registry: drops the singleton instance and puts an empty
* plugin registry in its place, as `init` does, so its first read fires
* `wp_entity_fields_api_init` again.
*/
private static function reset_registry() {
$instance = new ReflectionProperty( WP_Entity_Fields_Registry::class, 'instance' );
if ( PHP_VERSION_ID < 80100 ) {
$instance->setAccessible( true );
}
$instance->setValue( null, null );
WP_Entity_Fields_Registry_Gutenberg::get_instance();
}
/**
* Hooks a callback to `wp_entity_fields_api_init` for the duration of the test
* and fires the action anew, so the registry reflects it right away.
*
* Registering and unregistering only run on the action, so this is how a
* test alters the registry, as a plugin does. The registry is reset and
* read, which fires the action: the defaults are registered again and
* the callbacks hooked so far replay, in order. The callbacks are
* removed on tear down.
*
* @param callable $callback The callback.
* @param int $priority The priority. Default 10, after the defaults.
*/
private function on_entity_fields_api_init( $callback, $priority = 10 ) {
add_action( 'wp_entity_fields_api_init', $callback, $priority );
$this->callbacks[] = array( $callback, $priority );
self::reset_registry();
WP_Entity_Fields_Registry::get_instance()->get_all_registered();
}
/**
* Unregisters the fields of every entity with a script module, so a
* test can start from a registry without modules. The registry is reset
* on tear down.
*/
private function unregister_all_field_modules() {
$this->on_entity_fields_api_init(
static function () {
foreach ( wp_get_all_registered_entity_field_modules() as $kind => $entities ) {
foreach ( array_keys( $entities ) as $name ) {
wp_unregister_entity_fields( $kind, $name );
}
}
}
);
}
/**
* Returns the module ids among the module dependencies of a script.
*
* @param WP_Scripts $scripts The scripts registry.
* @param string $handle The script handle.
* @return string[] The module ids, in order.
*/
private function get_module_dependency_ids( WP_Scripts $scripts, $handle ) {
$dependencies = $scripts->get_data( $handle, 'module_dependencies' );
return array_map(
static function ( $dependency ) {
return is_array( $dependency ) ? $dependency['id'] : $dependency;
},
is_array( $dependencies ) ? $dependencies : array()
);
}
/**
* Registers fields on `wp_entity_fields_api_init` for the duration of the test,
* with the `test-plugin` origin: the registry is reset on tear down.
*
* @param string $kind The entity kind.
* @param string $name The entity name.
* @param array[] $fields The field definitions.
* @param string|null $module The script module id, if any.
* @return string[] The ids of the fields registered.
*/
private function register_fields( $kind, $name, $fields, $module = null ) {
$registered = array();
$this->on_entity_fields_api_init(
static function () use ( &$registered, $kind, $name, $fields, $module ) {
$registered = wp_register_entity_fields( 'test-plugin', $kind, $name, $fields, $module );
}
);
return $registered;
}
/**
* Registers the fields of fixture collections instead of the core
* collections, for the duration of the test: the loader of the core
* collections is unhooked, then hooked back on tear down.
*
* @param string $fixture The folder of the collections, in
* data/core-fields/collections.
* @param string[] $collections The collections to register, in order.
* @return bool[] What wp_register_entity_field_collection() returned for
* each collection, keyed by collection.
*/
private function register_fixture_collections( $fixture, $collections ) {
remove_action( 'wp_entity_fields_api_init', 'gutenberg_register_core_field_collections', 0 );
$this->core_collections_unhooked = true;
$directory = __DIR__ . '/data/core-fields/collections/' . $fixture;
$results = array();
$this->on_entity_fields_api_init(
static function () use ( $directory, $collections, &$results ) {
foreach ( $collections as $collection ) {
$results[ $collection ] = wp_register_entity_field_collection( $directory . '/' . $collection );
}
},
0
);
return $results;
}
/**
* Registers the fixture post types a test uses, exposed in the REST API
* unless hidden. They are unregistered on tear down.
*
* @param array<string, array> $post_types The supports, keyed by post type.
* @param string[] $hidden The post types not exposed in
* the REST API.
*/
private function register_post_types( $post_types, $hidden = array() ) {
foreach ( $post_types as $post_type => $supports ) {
$this->post_types[] = $post_type;
register_post_type(
$post_type,
array(
'show_in_rest' => ! in_array( $post_type, $hidden, true ),
'supports' => $supports,
)
);
}
}
/**
* The ids of the fields registered on a post type, but for the defaults
* every post type gets, see EVERY_POST_TYPE_FIELDS.
*
* @param string $post_type The post type.
* @return string[] The ids, in registration order.
*/
private static function get_support_field_ids( $post_type ) {
return self::without_every_post_type_fields( array_column( wp_get_registered_entity_fields( 'postType', $post_type ), 'id' ) );
}
/**
* Removes the defaults every post type gets from a list of field ids.
*
* @param string[] $ids The field ids.
* @return string[] The ids, but for those of EVERY_POST_TYPE_FIELDS.
*/
private static function without_every_post_type_fields( $ids ) {
return array_values( array_diff( $ids, self::EVERY_POST_TYPE_FIELDS ) );
}
/**
* Builds a minimal field definition.
*
* @param string $id The field id.
* @return array The field definition.
*/
private function field( $id ) {
return array(
'id' => $id,
'type' => 'text',
'label' => $id,
);
}
/**
* The modules are added on `admin_footer`, which fires before the import
* map is printed, both in the admin template and in the pages rendered
* outside it.
*/
public function test_the_modules_are_added_on_admin_footer() {
$this->assertSame( 10, has_action( 'admin_footer', '_wp_add_entity_field_modules_to_editor_script' ) );
$this->assertFalse( has_action( 'admin_init', '_wp_add_entity_field_modules_to_editor_script' ) );
}
/**
* A page that registers the editor script without loading it is left
* untouched, and the registry is not read.
*/
public function test_nothing_happens_when_the_editor_script_is_not_enqueued() {
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->add_data( 'wp-editor', 'module_dependencies', array() );
$fired = did_action( 'wp_entity_fields_api_init' );
self::reset_registry();
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertSame( array(), $scripts->get_data( 'wp-editor', 'module_dependencies' ) );
$this->assertSame( $fired, did_action( 'wp_entity_fields_api_init' ), 'The registry is not read.' );
}
/**
* The modules are added when the editor script is only a dependency of
* the scripts the page enqueues, as on the pages booted by
* `@wordpress/boot`.
*/
public function test_the_modules_are_added_when_the_editor_script_is_a_dependency() {
$this->unregister_all_field_modules();
$this->register_fields( 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' );
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->add_data( 'wp-editor', 'module_dependencies', array() );
$scripts->add( 'boot', '/boot.js', array( 'wp-editor' ) );
$scripts->enqueue( 'boot' );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertSame( array( 'plugin/color' ), $this->get_module_dependency_ids( $scripts, 'wp-editor' ) );
}
/**
* A scripts registry without the editor script is left untouched, even
* when field script modules are registered.
*/
public function test_nothing_happens_without_the_editor_script() {
$this->register_fields( 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' );
$scripts = new WP_Scripts();
// The default scripts register the real editor script.
$scripts->remove( 'wp-editor' );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertFalse( $scripts->query( 'wp-editor', 'registered' ) );
$this->assertFalse( $scripts->get_data( 'wp-editor', 'module_dependencies' ) );
}
/**
* The dependencies of the editor script stay untouched when no entity has
* a script module registered.
*/
public function test_nothing_happens_without_registered_field_modules() {
$this->unregister_all_field_modules();
$this->assertSame( array(), wp_get_all_registered_entity_field_modules(), 'No field script module is registered.' );
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->enqueue( 'wp-editor' );
$scripts->add_data( 'wp-editor', 'module_dependencies', array() );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertSame( array(), $scripts->get_data( 'wp-editor', 'module_dependencies' ) );
}
/**
* The script module of a field becomes a dynamic dependency of the editor
* script.
*/
public function test_field_script_modules_become_dynamic_dependencies_of_the_editor_script() {
$this->unregister_all_field_modules();
$this->register_fields( 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' );
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->enqueue( 'wp-editor' );
// Only the dependencies added by the function are of interest.
$scripts->add_data( 'wp-editor', 'module_dependencies', array() );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertSame(
array(
array(
'id' => 'plugin/color',
'import' => 'dynamic',
),
),
$scripts->get_data( 'wp-editor', 'module_dependencies' )
);
}
/**
* The dependencies already declared are kept, and the modules are added
* once: a second run adds nothing.
*/
public function test_declared_dependencies_are_kept_and_modules_are_added_once() {
$this->register_fields( 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' );
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->enqueue( 'wp-editor' );
$scripts->add_data( 'wp-editor', 'module_dependencies', array( '@wordpress/existing' ) );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$dependencies = $scripts->get_data( 'wp-editor', 'module_dependencies' );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertSame( $dependencies, $scripts->get_data( 'wp-editor', 'module_dependencies' ), 'A second run adds nothing.' );
$ids = $this->get_module_dependency_ids( $scripts, 'wp-editor' );
$this->assertSame( '@wordpress/existing', $ids[0], 'The declared dependency is kept first.' );
$this->assertSame( 1, count( array_keys( $ids, 'plugin/color', true ) ), 'The module is declared once.' );
$this->assertContains(
array(
'id' => 'plugin/color',
'import' => 'dynamic',
),
$dependencies
);
}
/**
* A module registered for several entities is declared once.
*/
public function test_a_module_shared_by_several_entities_is_declared_once() {
$this->register_fields( 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/fields' );
$this->register_fields( 'taxonomy', 'category', array( $this->field( 'color' ) ), 'plugin/fields' );
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->enqueue( 'wp-editor' );
// Only the dependencies added by the function are of interest.
$scripts->add_data( 'wp-editor', 'module_dependencies', array() );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$ids = $this->get_module_dependency_ids( $scripts, 'wp-editor' );
$this->assertSame( array( 'plugin/fields' ), array_values( array_intersect( $ids, array( 'plugin/fields' ) ) ) );
}
/**
* The script module of the default post fields is declared by default,
* since the author field of every post type supporting authors ships its
* JavaScript parts in it.
*/
public function test_the_default_fields_module_is_a_dependency_of_the_editor_script() {
$scripts = new WP_Scripts();
$scripts->add( 'wp-editor', '/editor.js' );
$scripts->enqueue( 'wp-editor' );
$scripts->add_data( 'wp-editor', 'module_dependencies', array() );
_wp_add_entity_field_modules_to_editor_script( $scripts );
$this->assertContains(
array(
'id' => '@wordpress/core-fields/post_type_supports',
'import' => 'dynamic',
),
$scripts->get_data( 'wp-editor', 'module_dependencies' )
);
}
/**
* The script modules of the fields reach the import map of a page that
* loads the editor script.
*/
public function test_field_script_modules_reach_the_import_map() {
global $wp_scripts;
$original_scripts = $wp_scripts;
$wp_scripts = new WP_Scripts();
try {
// Only the test module is of interest: the built modules may not be
// registered in the test environment.
$this->unregister_all_field_modules();
$this->register_fields( 'postType', 'page', array( $this->field( 'color' ) ), 'plugin/color' );
$wp_scripts->add( 'wp-editor', '/editor.js' );
wp_register_script_module( 'plugin/color', '/color.js' );
$wp_scripts->enqueue( 'wp-editor' );
_wp_add_entity_field_modules_to_editor_script( $wp_scripts );
$processor = new WP_HTML_Tag_Processor( get_echo( array( wp_script_modules(), 'print_import_map' ) ) );
$this->assertTrue( $processor->next_tag( 'SCRIPT' ), 'An import map is printed.' );
$this->assertSame( 'importmap', $processor->get_attribute( 'type' ) );
$import_map = json_decode( $processor->get_modifiable_text(), true );
} finally {
$wp_scripts = $original_scripts;
wp_deregister_script_module( 'plugin/color' );
}
$this->assertArrayHasKey( 'plugin/color', $import_map['imports'] );
}
/**
* Any post type supporting authors, comments, or notes, including a
* custom one, gets the default fields: the defaults derive from the post
* types registered when the action fires.
*/
public function test_a_custom_post_type_gets_the_default_fields_of_its_supports() {
register_post_type(
'gutenberg_book',
array(
'show_in_rest' => true,
'supports' => array(
'title',
'author',
'comments',
'editor' => array( 'notes' => true ),
),
)
);
try {
self::reset_registry();
$fields = wp_get_registered_entity_fields( 'postType', 'gutenberg_book' );
$modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'gutenberg_book' ), 'fields', 'id' );
} finally {
unregister_post_type( 'gutenberg_book' );
}
$this->assertSame( array( 'author', 'comment_status', 'discussion', 'notesCount', 'post-content-info', 'title' ), self::without_every_post_type_fields( array_column( $fields, 'id' ) ) );
$this->assertSame( array( 'core' ), array_unique( array_column( array_column( $fields, 'origin' ), 'registeredBy' ) ), 'The fields carry the origin of their collection.' );
$this->assertSame(
array( '@wordpress/core-fields/post_type_supports' => array_column( $fields, 'id' ) ),
$modules,
'Every default field is registered with the module of its folder.'
);
}
/**
* The notes field follows the `notes` argument of the `editor` support,
* which WordPress stores as a list of argument arrays; a bare `editor`
* support does not enable it.
*/
public function test_the_notes_field_follows_the_editor_support_argument() {
register_post_type(
'gutenberg_book',
array(
'show_in_rest' => true,
'supports' => array( 'editor' => array( 'notes' => true ) ),
)
);
register_post_type(
'gutenberg_note',
array(
'show_in_rest' => true,
'supports' => array( 'editor' ),
)
);
try {
self::reset_registry();
$with_notes = array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_book' ), 'id' );
$without_notes = array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_note' ), 'id' );
} finally {
unregister_post_type( 'gutenberg_book' );
unregister_post_type( 'gutenberg_note' );
}
$this->assertContains( 'notesCount', $with_notes );
$this->assertNotContains( 'notesCount', $without_notes );
}
/**
* The defaults derived from a single support, each with the supports
* that give it to a post type.
*
* @return array[] The id of each field and the supports that give it.
*/
public function data_default_fields_and_their_supports() {
return array(
'discussion' => array( 'discussion', array( 'comments', 'trackbacks' ) ),
'excerpt' => array( 'excerpt', array( 'excerpt' ) ),
'parent' => array( 'parent', array( 'page-attributes' ) ),
'ping_status' => array( 'ping_status', array( 'trackbacks' ) ),
'title' => array( 'title', array( 'title' ) ),
'post-content-info' => array( 'post-content-info', array( 'editor' ) ),
);
}
/**
* The defaults every post type gets, whatever it supports, each with
* whether the design post types exclude it.
*
* @return array[] The id of each field and whether the design post
* types exclude it.
*/
public function data_default_fields_of_every_post_type() {
return array(
'date' => array( 'date', true ),
'last_edited_date' => array( 'last_edited_date', false ),
'password' => array( 'password', true ),
'scheduled_date' => array( 'scheduled_date', true ),
'status' => array( 'status', true ),
'template' => array( 'template', true ),
);
}
/**
* A default field of every post type is registered on a post type
* supporting nothing, and on the design post types unless they exclude
* it.
*
* @dataProvider data_default_fields_of_every_post_type
*
* @param string $field_id The id of the field.
* @param bool $excluded_for_design The design post types exclude it.
*/
public function test_a_default_field_of_every_post_type( $field_id, $excluded_for_design ) {
$this->assertContains( $field_id, self::EVERY_POST_TYPE_FIELDS, 'The tests know the field applies to every post type.' );
$this->register_post_types( array( 'gutenberg_plain' => array( 'title' ) ) );
self::reset_registry();
$this->assertContains( $field_id, array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_plain' ), 'id' ) );
foreach ( array( 'wp_template', 'wp_template_part', 'wp_block', 'wp_navigation' ) as $post_type ) {
$ids = array_column( wp_get_registered_entity_fields( 'postType', $post_type ), 'id' );
if ( $excluded_for_design ) {
$this->assertNotContains( $field_id, $ids, "$post_type excludes the field." );
} else {
$this->assertContains( $field_id, $ids, "$post_type gets the field." );
}
}
}
/**
* A default field is registered on the post types with one of its
* supports, and only on them.
*
* @dataProvider data_default_fields_and_their_supports
*
* @param string $field_id The id of the field.
* @param string[] $supports The supports that give the field.
*/
public function test_a_default_field_follows_its_supports( $field_id, $supports ) {
$with_support = array();
foreach ( $supports as $index => $support ) {
$with_support[ "gutenberg_with_$index" ] = array( 'title', $support );
}
$this->register_post_types( $with_support + array( 'gutenberg_without' => array( 'author' ) ) );
self::reset_registry();
foreach ( array_keys( $with_support ) as $post_type ) {
$this->assertContains( $field_id, array_column( wp_get_registered_entity_fields( 'postType', $post_type ), 'id' ), "$post_type gets the field." );
}
$this->assertNotContains( $field_id, array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_without' ), 'id' ) );
}
/**
* The build copies the collections as they are: the configurations and
* the fields come out byte-identical.
*/
public function test_the_build_copies_the_collections_as_they_are() {
$source = __DIR__ . '/../packages/core-fields/src';
$build = __DIR__ . '/../build/scripts/core-fields';
$files = array_merge( glob( $source . '/*/index.php' ), glob( $source . '/*/*/field.php' ) );
$this->assertNotEmpty( $files );
foreach ( $files as $file ) {
$this->assertFileEquals( $file, $build . substr( $file, strlen( $source ) ) );
}
}
/**
* The build prefixes the functions the core-fields loader defines, and
* nothing else: the functions of the Fields API keep their names.
*/
public function test_the_build_prefixes_the_functions_of_the_core_fields_loader() {
$source = file_get_contents( __DIR__ . '/../packages/core-fields/src/index.php' );
$built = file_get_contents( __DIR__ . '/../build/scripts/core-fields/index.php' );
$this->assertStringContainsString( 'function gutenberg_register_core_post_type_supports_fields()', $built );
$this->assertStringContainsString( 'function gutenberg_register_core_field_collections()', $built );
$this->assertStringContainsString( "wp_get_entity_field_collection_fields( __DIR__ . '/post_type_supports' );", $built );
$this->assertStringContainsString( 'gutenberg_register_core_post_type_supports_fields();', $built );
$this->assertStringContainsString( "wp_register_entity_field_collection( __DIR__ . '/wp_template' );", $built );
$this->assertStringContainsString( "add_action( 'wp_entity_fields_api_init', 'gutenberg_register_core_field_collections', 0 );", $built );
$this->assertStringNotContainsString( 'gutenberg_register_field_collection', $built );
$this->assertStringNotContainsString( 'gutenberg_get_field_collection_fields', $built );
$this->assertSame(
strtr(
$source,
array(
'wp_register_core_post_type_supports_fields' => 'gutenberg_register_core_post_type_supports_fields',
'wp_register_core_field_collections' => 'gutenberg_register_core_field_collections',
)
),
$built,
'Only the function names change.'
);
}
/**
* The default fields come from the `field.php` files of the
* `post_type_supports` folder.
*/
public function test_the_default_fields_come_from_the_post_type_supports_folder() {
$fields = array_column( wp_get_registered_entity_fields( 'postType', 'post' ), null, 'id' );
$this->assertSame( 'integer', $fields['author']['type'] );
$this->assertSame( 'core', $fields['author']['origin']['registeredBy'] );
$this->assertSame( 'radio', $fields['comment_status']['Edit'] );
$this->assertSame( 'boolean', $fields['ping_status']['type'] );
$this->assertSame( 'Discussion', $fields['discussion']['label'] );
$this->assertSame( 'textarea', $fields['excerpt']['Edit']['control'] );
$this->assertTrue( $fields['post-content-info']['readOnly'] );
}
/**
* A collection lists a folder per field: the id of a field is the name
* of its folder unless its `field.php` sets one, and the fields are in
* the alphabetical order of their folders, keyed by id.
*/
public function test_a_collection_has_a_field_per_folder() {
$fields = wp_get_entity_field_collection_fields( __DIR__ . '/data/core-fields/fixture' );
$this->assertSame( array( 'zeta', 'beta' ), array_keys( $fields ), 'The folder name is the id, unless the field sets one; the folders set the order.' );
$this->assertSame( array( 'zeta', 'beta' ), array_column( $fields, 'id' ), 'Each field has its id.' );
$this->assertSame( 'id', array_keys( $fields['beta'] )[0], 'The id comes first.' );
$this->assertSame( 'Zeta', $fields['zeta']['label'] );
}
/**
* A missing collection has no fields.
*/
public function test_a_missing_collection_has_no_fields() {
$this->assertSame( array(), wp_get_entity_field_collection_fields( __DIR__ . '/data/core-fields/missing' ) );
}
/**
* The core fields are registered on `wp_entity_fields_api_init` at priority 0,
* before the plugins hooking the action at the default priority.
*/
public function test_the_core_collections_are_registered_at_priority_zero() {
$this->assertSame( 0, has_action( 'wp_entity_fields_api_init', 'gutenberg_register_core_field_collections' ) );
// Core trunk ships the unprefixed functions too, so only check the prefixed one.
$this->assertTrue( function_exists( 'gutenberg_register_core_post_type_supports_fields' ), 'The Gutenberg build prefixes the function.' );
}
/**
* The defaults make no exception for any post type: on their own, they
* register the author field on templates, template parts, and
* attachments, which support authors.
*/
public function test_the_defaults_make_no_exception() {
remove_action( 'wp_entity_fields_api_init', 'gutenberg_register_core_field_collections', 0 );
$this->core_collections_unhooked = true;
$this->on_entity_fields_api_init( 'gutenberg_register_core_post_type_supports_fields', 0 );
foreach ( array( 'wp_template', 'wp_template_part', 'attachment' ) as $post_type ) {
$fields = array_column( wp_get_registered_entity_fields( 'postType', $post_type ), null, 'id' );
$this->assertArrayHasKey( 'author', $fields, "The defaults register the author field on $post_type." );
$this->assertSame( 'integer', $fields['author']['type'] );
}
}
/**
* Template parts support authors but their author is the theme, plugin,
* site, or user that provides them: they opt out of the default author
* field and their collection has its own, with its own script module.
*/
public function test_template_parts_get_their_own_author_field() {
$this->assertTrue( post_type_supports( 'wp_template_part', 'author' ), 'The post type supports authors.' );
$fields = array_column( wp_get_registered_entity_fields( 'postType', 'wp_template_part' ), null, 'id' );
$this->assertSame( array( 'author', 'title' ), self::get_support_field_ids( 'wp_template_part' ), 'The fields are the ones of the collection, not the default author and title fields.' );
$this->assertArrayNotHasKey( 'type', $fields['author'], 'The template part author is not the integer post author.' );
$this->assertArrayHasKey( 'title', $fields, 'The collection registers the template part title.' );
$this->assertSame( 'Title', $fields['title']['label'], 'The title is the one of the collection.' );
$this->assertSame(
array( 'author', 'title' ),
array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template_part' ), 'fields', 'id' )['@wordpress/core-fields/wp_template_part']
);
}
/**
* Patterns have their description, title, and sync status in their
* collection instead of the default excerpt and title fields.
*/
public function test_patterns_get_their_own_fields() {
$fields = array_column( wp_get_registered_entity_fields( 'postType', 'wp_block' ), null, 'id' );
$this->assertArrayHasKey( 'excerpt', $fields );
$this->assertArrayHasKey( 'sync-status', $fields );
$this->assertArrayHasKey( 'title', $fields );
$this->assertSame( 'Description', $fields['excerpt']['label'] );
$this->assertSame( 'Title', $fields['title']['label'] );
$this->assertTrue( $fields['sync-status']['readOnly'] );
$this->assertSame(
array( 'excerpt', 'sync-status', 'title' ),
array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_block' ), 'fields', 'id' )['@wordpress/core-fields/wp_block']
);
}
/**
* The slug field is registered on the viewable post types, which have a
* permalink, but for the design ones.
*/
public function test_the_viewable_post_types_get_the_slug_field() {
register_post_type(
'gutenberg_book',
array(
'public' => true,
'show_in_rest' => true,
)
);
register_post_type(
'gutenberg_note',
array(
'public' => false,
'show_in_rest' => true,
)
);
$this->post_types[] = 'gutenberg_book';
$this->post_types[] = 'gutenberg_note';
self::reset_registry();
$this->assertContains( 'slug', array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_book' ), 'id' ) );
$this->assertNotContains( 'slug', array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_note' ), 'id' ), 'A post type that is not viewable has no permalink.' );
$this->assertContains( 'slug', array_column( wp_get_registered_entity_fields( 'postType', 'page' ), 'id' ) );
$this->assertNotContains( 'slug', array_column( wp_get_registered_entity_fields( 'postType', 'wp_template' ), 'id' ) );
}
/**
* Only posts have sticky posts, so only they get the sticky field.
*/
public function test_only_posts_get_the_sticky_field() {
$this->register_post_types( array( 'gutenberg_book' => array( 'title', 'editor' ) ) );
self::reset_registry();
$this->assertContains( 'sticky', array_column( wp_get_registered_entity_fields( 'postType', 'post' ), 'id' ) );
$this->assertNotContains( 'sticky', array_column( wp_get_registered_entity_fields( 'postType', 'page' ), 'id' ) );
$this->assertNotContains( 'sticky', array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_book' ), 'id' ) );
}
/**
* The featured media field needs the post type to support `thumbnail`
* and the theme to support post thumbnails for that post type, which a
* theme may opt into for some post types only.
*/
public function test_the_featured_media_field_follows_the_post_type_support_and_the_theme() {
$this->register_post_types(
array(
'gutenberg_book' => array( 'title', 'thumbnail' ),
'gutenberg_note' => array( 'title', 'thumbnail' ),
'gutenberg_page' => array( 'title' ),
)
);
$original_support = get_theme_support( 'post-thumbnails' );
try {
// Naming post types only narrows the support when the theme does
// not already support post thumbnails for every post type.
remove_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails', array( 'gutenberg_book' ) );
self::reset_registry();
$this->assertContains( 'featured_media', self::get_support_field_ids( 'gutenberg_book' ) );
$this->assertNotContains(
'featured_media',
self::get_support_field_ids( 'gutenberg_note' ),
'The theme did not opt the post type into post thumbnails.'
);
$this->assertNotContains(
'featured_media',
self::get_support_field_ids( 'gutenberg_page' ),
'The post type does not support thumbnails.'
);
// A theme may also opt every post type in at once.
remove_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails' );
self::reset_registry();
$this->assertContains( 'featured_media', self::get_support_field_ids( 'gutenberg_note' ) );
remove_theme_support( 'post-thumbnails' );
self::reset_registry();
$this->assertNotContains(
'featured_media',
self::get_support_field_ids( 'gutenberg_book' ),
'A theme without post thumbnails has no featured image to set.'
);
} finally {
remove_theme_support( 'post-thumbnails' );
if ( is_array( $original_support ) ) {
add_theme_support( 'post-thumbnails', ...$original_support );
} elseif ( true === $original_support ) {
add_theme_support( 'post-thumbnails' );
}
}
}
/**
* The format field needs the post type to support `post-formats` and
* the theme to support post formats: a theme without formats has none
* to assign. Its elements are the formats of the theme, plus
* `standard`, which the themes route exposes for every theme
* supporting formats, sorted by label.
*
* It has a test of its own rather than a row in
* data_default_fields_and_their_supports(), whose fields follow a post
* type support alone.
*/
public function test_the_format_field_follows_the_post_type_support_and_the_theme() {
$this->register_post_types(
array(
'gutenberg_book' => array( 'title', 'post-formats' ),
'gutenberg_note' => array( 'title' ),
)
);
$original_support = get_theme_support( 'post-formats' );
try {
add_theme_support( 'post-formats', array( 'gallery', 'aside' ) );
self::reset_registry();
$fields = array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_book' ), null, 'id' );
$this->assertArrayHasKey( 'format', $fields );
$this->assertNotContains(
'format',
array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_note' ), 'id' ),
'A post type that does not support post formats has no format field.'
);
$this->assertSame(
array( 'aside', 'gallery', 'standard' ),
array_column( $fields['format']['elements'], 'value' ),
'The elements are the formats of the theme, plus standard, sorted by label.'
);
remove_theme_support( 'post-formats' );
self::reset_registry();
$this->assertNotContains(
'format',
array_column( wp_get_registered_entity_fields( 'postType', 'gutenberg_book' ), 'id' ),
'A theme without post formats has none to assign.'
);
} finally {
remove_theme_support( 'post-formats' );
if ( is_array( $original_support ) ) {
add_theme_support( 'post-formats', ...$original_support );
}
}
}
/**
* Pages support titles but their title shows the page's role on the
* site: they opt out of the default title field and their collection has
* its own, with its own script module.
*/
public function test_pages_get_their_own_title_field() {
$fields = array_column( wp_get_registered_entity_fields( 'postType', 'page' ), null, 'id' );
$modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'page' ), 'fields', 'id' );
$this->assertFalse( $fields['title']['enableHiding'], 'The page title is the one of the collection.' );
$this->assertSame( 'core', $fields['title']['origin']['registeredBy'] );
$this->assertSame( array( 'title' ), $modules['@wordpress/core-fields/page'] );
$this->assertNotContains( 'title', $modules['@wordpress/core-fields/post_type_supports'], 'The default title field is not registered.' );
}
/**
* Patterns support excerpts but their excerpt is their description,
* which their collection defines with a field of the same id, so they
* opt out of the default excerpt field.
*/
public function test_patterns_do_not_get_the_default_excerpt_field() {
$this->assertTrue( post_type_supports( 'wp_block', 'excerpt' ), 'The post type supports excerpts.' );
$modules = array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_block' ), 'fields', 'id' );
$this->assertContains( 'excerpt', $modules['@wordpress/core-fields/wp_block'], 'The excerpt is the description field of the collection.' );
$this->assertNotContains( 'excerpt', $modules['@wordpress/core-fields/post_type_supports'], 'The default excerpt field is not registered.' );
}
/**
* Navigation menus support the editor, but their content is blocks
* laying out a site rather than text to read, so they opt out of the
* content information field. Patterns keep it.
*/
public function test_navigation_menus_do_not_get_the_content_information_field() {
$this->assertTrue( post_type_supports( 'wp_navigation', 'editor' ), 'The post type supports the editor.' );
$this->assertNotContains( 'post-content-info', array_column( wp_get_registered_entity_fields( 'postType', 'wp_navigation' ), 'id' ) );
$this->assertContains( 'post-content-info', array_column( wp_get_registered_entity_fields( 'postType', 'wp_block' ), 'id' ) );
}
/**
* Templates support authors but their author is the theme, plugin, site,
* or user that provides them: they opt out of the default author field
* and their collection has its own, with its own script module.
*/
public function test_templates_get_their_own_author_field() {
$this->assertTrue( post_type_supports( 'wp_template', 'author' ), 'The post type supports authors.' );
$fields = array_column( wp_get_registered_entity_fields( 'postType', 'wp_template' ), null, 'id' );
$this->assertSame( array( 'author', 'default_comment_status', 'description', 'description_readonly', 'posts_page_title', 'posts_per_page', 'title' ), self::get_support_field_ids( 'wp_template' ), 'The fields are the ones of the collection, not the default author and title fields.' );
$this->assertSame( 'Template', $fields['title']['label'], 'The title is the one of the collection.' );
$this->assertArrayNotHasKey( 'type', $fields['author'], 'The template author is not the integer post author.' );
$this->assertSame( 'core', $fields['author']['origin']['registeredBy'] );
$this->assertSame(
array( 'author', 'default_comment_status', 'description', 'description_readonly', 'posts_page_title', 'posts_per_page', 'title' ),
array_column( wp_get_registered_entity_field_modules( 'postType', 'wp_template' ), 'fields', 'id' )['@wordpress/core-fields/wp_template'],
'The fields of templates ship their JavaScript parts in the module of the collection.'
);
}
/**
* A plugin can replace the author field of templates.
*/
public function test_a_plugin_can_replace_the_author_field_of_templates() {
$this->on_entity_fields_api_init(
static function () {
wp_unregister_entity_fields( 'postType', 'wp_template', array( 'author' ) );
wp_register_entity_fields(
'my-plugin',
'postType',
'wp_template',
array(
array(
'id' => 'author',
'type' => 'text',
'label' => 'Maker',
),
)
);
}
);