11<?php
22class Enqueue_TestCase extends WP_UnitTestCase {
3- public function setUp (): void {
4- tangible_fields ()->enqueued = [
3+ public function setUp (): void {
4+ tangible_fields ()->enqueued = [
55 'fields ' => [],
66 'elements ' => []
77 ];
8- tangible_fields ()->is_enqueued = false ;
9- tangible_fields ()->set_context ('default ' );
8+ tangible_fields ()->is_enqueued = false ;
9+ tangible_fields ()->set_context ('default ' );
1010
11- $ GLOBALS ['wp_styles ' ] = new WP_Styles ();
12- $ GLOBALS ['wp_scripts ' ] = new WP_Scripts ();
13- }
11+ $ GLOBALS ['wp_styles ' ] = new WP_Styles ();
12+ $ GLOBALS ['wp_scripts ' ] = new WP_Scripts ();
13+ }
1414
1515 function _test_items_enqueue_data () {
1616 return [
@@ -22,52 +22,58 @@ function _test_items_enqueue_data() {
2222 /**
2323 * @dataProvider _test_items_enqueue_data
2424 */
25- public function test_items_enqueue (string $ type , array $ args ) {
26- global $ wp_version ;
25+ public function test_items_enqueue (string $ type , array $ args ) {
26+ global $ wp_version ;
2727 $ this ->assertEmpty (tangible_fields ()->enqueued [ $ type ], "There should not be enqueued $ type yet " );
28- $ this ->assertFalse (tangible_fields ()->is_enqueued );
28+ $ this ->assertFalse (tangible_fields ()->is_enqueued );
2929
30- tangible_fields ()->maybe_enqueue_scripts ();
31- $ this ->assertFalse (tangible_fields ()->is_enqueued );
30+ tangible_fields ()->maybe_enqueue_scripts ();
31+ $ this ->assertFalse (tangible_fields ()->is_enqueued );
3232
33- tangible_fields ()->enqueue_item ('test ' , $ type , $ args );
33+ tangible_fields ()->enqueue_item ('test ' , $ type , $ args );
3434 $ args ['context ' ] = 'default ' ;
35- $ this ->assertEquals (['test ' => $ args ], tangible_fields ()->enqueued [ $ type ]);
36-
37- $ this ->assertFalse (tangible_fields ()->is_enqueued );
38- $ this ->assertFalse (wp_styles ()->query ('tangible-fields-default ' ));
39- $ this ->assertFalse (wp_scripts ()->query ('tangible-fields ' ));
40- $ this ->assertFalse (wp_scripts ()->get_data ('tangible-fields ' , 'data ' ));
41-
42- tangible_fields ()->maybe_enqueue_scripts ();
43-
44- $ this ->assertTrue (tangible_fields ()->is_enqueued );
45- $ this ->assertEquals ('tangible-fields-default ' , wp_styles ()->query ('tangible-fields-default ' )->handle );
46- $ this ->assertEquals ('tangible-fields ' , wp_scripts ()->query ('tangible-fields ' )->handle );
47-
48- if ( version_compare ($ wp_version , '6.3 ' , '> ' ) ) {
49- $ data = wp_scripts ()->get_inline_script_data ('tangible-fields ' , 'before ' , false );
50- } else {
51- $ data = wp_scripts ()->print_inline_script ('tangible-fields ' , 'before ' , false );
52- }
53- $ this ->assertGreaterThan (0 , preg_match ('#^var TangibleFields = (.+?);$# ' , $ data , $ matches ), 'wp_add_inline_script does not have TangibleFields ' );
54- $ data = json_decode ($ matches [1 ], true );
5535
56- $ this ->assertEquals ([
36+ if ( $ type === 'elements ' ) {
37+ $ this ->assertEquals (['test ' => [ $ args ]], tangible_fields ()->enqueued [ $ type ]);
38+ }
39+ else {
40+ $ this ->assertEquals (['test ' => $ args ], tangible_fields ()->enqueued [ $ type ]);
41+ }
42+
43+ $ this ->assertFalse (tangible_fields ()->is_enqueued );
44+ $ this ->assertFalse (wp_styles ()->query ('tangible-fields-default ' ));
45+ $ this ->assertFalse (wp_scripts ()->query ('tangible-fields ' ));
46+ $ this ->assertFalse (wp_scripts ()->get_data ('tangible-fields ' , 'data ' ));
47+
48+ tangible_fields ()->maybe_enqueue_scripts ();
49+
50+ $ this ->assertTrue (tangible_fields ()->is_enqueued );
51+ $ this ->assertEquals ('tangible-fields-default ' , wp_styles ()->query ('tangible-fields-default ' )->handle );
52+ $ this ->assertEquals ('tangible-fields ' , wp_scripts ()->query ('tangible-fields ' )->handle );
53+
54+ if ( version_compare ($ wp_version , '6.3 ' , '> ' ) ) {
55+ $ data = wp_scripts ()->get_inline_script_data ('tangible-fields ' , 'before ' , false );
56+ } else {
57+ $ data = wp_scripts ()->print_inline_script ('tangible-fields ' , 'before ' , false );
58+ }
59+ $ this ->assertGreaterThan (0 , preg_match ('#^var TangibleFields = (.+?);$# ' , $ data , $ matches ), 'wp_add_inline_script does not have TangibleFields ' );
60+ $ data = json_decode ($ matches [1 ], true );
61+
62+ $ this ->assertEquals ([
5763 'api ' ,
5864 'fields ' ,
5965 'elements ' ,
6066 'dynamics ' ,
6167 'mimetypes '
6268 ], array_keys ($ data ));
63- }
69+ }
6470
6571 /**
66- * @dataProvider _test_items_enqueue_data
67- */
68- public function test_fields_enqueue_conditions (string $ type , array $ args ) {
69- global $ wp_version ;
70- tangible_fields ()->enqueue_item ('test ' , $ type , $ args );
72+ * @dataProvider _test_items_enqueue_data
73+ */
74+ public function test_fields_enqueue_conditions (string $ type , array $ args ) {
75+ global $ wp_version ;
76+ tangible_fields ()->enqueue_item ('test ' , $ type , $ args );
7177
7278 $ args ['condition ' ] = [
7379 'action ' => 'show ' ,
@@ -78,18 +84,23 @@ public function test_fields_enqueue_conditions(string $type, array $args) {
7884 ],
7985 ];
8086
81- tangible_fields ()->enqueue_item ('field ' , $ type , $ args );
87+ tangible_fields ()->enqueue_item ('field ' , $ type , $ args );
88+ tangible_fields ()->maybe_enqueue_scripts ();
8289
83- tangible_fields ()->maybe_enqueue_scripts ();
90+ if ( version_compare ($ wp_version , '6.3 ' , '> ' ) ) {
91+ preg_match ('#^var TangibleFields = (.+?);$# ' , wp_scripts ()->get_inline_script_data ('tangible-fields ' , 'before ' , false ), $ matches );
92+ } else {
93+ preg_match ('#^var TangibleFields = (.+?);$# ' , wp_scripts ()->print_inline_script ('tangible-fields ' , 'before ' , false ), $ matches );
94+ }
8495
85- if ( version_compare ($ wp_version , '6.3 ' , '> ' ) ) {
86- preg_match ('#^var TangibleFields = (.+?);$# ' , wp_scripts ()->get_inline_script_data ('tangible-fields ' , 'before ' , false ), $ matches );
87- } else {
88- preg_match ('#^var TangibleFields = (.+?);$# ' , wp_scripts ()->print_inline_script ('tangible-fields ' , 'before ' , false ), $ matches );
89- }
90- $ data = json_decode ($ matches [1 ], true );
96+ $ data = json_decode ($ matches [1 ], true );
9197
92- $ this ->assertArrayNotHasKey ('condition ' , $ data [ $ type ]['test ' ]);
93- $ this ->assertEquals ($ args ['condition ' ], $ data [ $ type ]['field ' ]['condition ' ]);
94- }
98+ $ this ->assertArrayNotHasKey ('condition ' , $ data [ $ type ]['test ' ]);
99+ $ this ->assertEquals (
100+ $ args ['condition ' ],
101+ $ type === 'elements '
102+ ? $ data [ $ type ]['field ' ][0 ]['condition ' ]
103+ : $ data [ $ type ]['field ' ]['condition ' ]
104+ );
105+ }
95106}
0 commit comments