forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpScriptModules.php
More file actions
979 lines (854 loc) · 30.8 KB
/
wpScriptModules.php
File metadata and controls
979 lines (854 loc) · 30.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
<?php
/**
* Unit tests covering WP_Script_Modules functionality.
*
* @package WordPress
* @subpackage Script Modules
*
* @since 6.5.0
*
* @group script-modules
*
* @coversDefaultClass WP_Script_Modules
*/
class Tests_Script_Modules_WpScriptModules extends WP_UnitTestCase {
/**
* Instance of WP_Script_Modules.
*
* @var WP_Script_Modules
*/
protected $script_modules;
/**
* Set up.
*/
public function set_up() {
parent::set_up();
// Set up the WP_Script_Modules instance.
$this->script_modules = new WP_Script_Modules();
}
/**
* Gets a list of the enqueued script modules.
*
* @return array Enqueued script module URLs, keyed by script module identifier.
*/
private function get_enqueued_script_modules() {
$script_modules_markup = get_echo( array( $this->script_modules, 'print_enqueued_script_modules' ) );
$p = new WP_HTML_Tag_Processor( $script_modules_markup );
$enqueued_script_modules = array();
while ( $p->next_tag( array( 'tag' => 'SCRIPT' ) ) ) {
if ( 'module' === $p->get_attribute( 'type' ) ) {
$id = preg_replace( '/-js-module$/', '', $p->get_attribute( 'id' ) );
$enqueued_script_modules[ $id ] = $p->get_attribute( 'src' );
}
}
return $enqueued_script_modules;
}
/**
* Gets the script modules listed in the import map.
*
* @return array Import map entry URLs, keyed by script module identifier.
*/
private function get_import_map() {
$import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
preg_match( '/<script type="importmap" id="wp-importmap">.*?(\{.*\}).*?<\/script>/s', $import_map_markup, $import_map_string );
return isset( $import_map_string[1] )
? json_decode( $import_map_string[1], true )['imports']
: array();
}
/**
* Gets a list of preloaded script modules.
*
* @return array Preloaded script module URLs, keyed by script module identifier.
*/
private function get_preloaded_script_modules() {
$preloaded_markup = get_echo( array( $this->script_modules, 'print_script_module_preloads' ) );
$p = new WP_HTML_Tag_Processor( $preloaded_markup );
$preloaded_script_modules = array();
while ( $p->next_tag( array( 'tag' => 'LINK' ) ) ) {
if ( 'modulepreload' === $p->get_attribute( 'rel' ) ) {
$id = preg_replace( '/-js-modulepreload$/', '', $p->get_attribute( 'id' ) );
$preloaded_script_modules[ $id ] = $p->get_attribute( 'href' );
}
}
return $preloaded_script_modules;
}
/**
* Tests that a script module gets enqueued correctly after being registered.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_enqueue_script_module() {
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->register( 'bar', '/bar.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'bar' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 2, $enqueued_script_modules );
$this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
$this->assertStringStartsWith( '/bar.js', $enqueued_script_modules['bar'] );
}
/**
* Tests that a script module can be dequeued after being enqueued.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::dequeue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_dequeue_script_module() {
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->register( 'bar', '/bar.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'bar' );
$this->script_modules->dequeue( 'foo' ); // Dequeued.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->assertArrayHasKey( 'bar', $enqueued_script_modules );
}
/**
* Tests that a script module can be deregistered
* after being enqueued, and that will be removed
* from the enqueue list too.
*
* @ticket 60463
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::deregister()
* @covers ::get_enqueued_script_modules()
*/
public function test_wp_deregister_script_module() {
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->register( 'bar', '/bar.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'bar' );
$this->script_modules->deregister( 'foo' ); // Dequeued.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->assertArrayHasKey( 'bar', $enqueued_script_modules );
}
/**
* Tests that a script module is not deregistered
* if it has not been registered before, causing
* no errors.
*
* @ticket 60463
*
* @covers ::deregister()
* @covers ::get_enqueued_script_modules()
*/
public function test_wp_deregister_unexistent_script_module() {
$this->script_modules->deregister( 'unexistent' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 0, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'unexistent', $enqueued_script_modules );
}
/**
* Tests that a script module is not deregistered
* if it has been deregistered previously, causing
* no errors.
*
* @ticket 60463
*
* @covers ::get_enqueued_script_modules()
* @covers ::register()
* @covers ::deregister()
* @covers ::enqueue()
*/
public function test_wp_deregister_already_deregistered_script_module() {
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->deregister( 'foo' ); // Dequeued.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 0, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->script_modules->deregister( 'foo' ); // Dequeued.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 0, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
}
/**
* Tests that a script module can be enqueued before it is registered, and will
* be handled correctly once registered.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_enqueue_script_module_works_before_register() {
$this->script_modules->enqueue( 'foo' );
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->enqueue( 'bar' ); // Not registered.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
$this->assertArrayNotHasKey( 'bar', $enqueued_script_modules );
}
/**
* Tests that a script module can be dequeued before it is registered and
* ensures that it is not enqueued after registration.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::dequeue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_dequeue_script_module_works_before_register() {
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'bar' );
$this->script_modules->dequeue( 'foo' );
$this->script_modules->register( 'foo', '/foo.js' );
$this->script_modules->register( 'bar', '/bar.js' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->assertArrayHasKey( 'bar', $enqueued_script_modules );
}
/**
* Tests that dependencies for a registered module are added to the import map
* when the script module is enqueued.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_import_map()
*/
public function test_wp_import_map_dependencies() {
$this->script_modules->register( 'foo', '/foo.js', array( 'dep' ) );
$this->script_modules->register( 'dep', '/dep.js' );
$this->script_modules->register( 'no-dep', '/no-dep.js' );
$this->script_modules->enqueue( 'foo' );
$import_map = $this->get_import_map();
$this->assertCount( 1, $import_map );
$this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
$this->assertArrayNotHasKey( 'no-dep', $import_map );
}
/**
* Tests that dependencies are not duplicated in the import map when multiple
* script modules require the same dependency.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_import_map()
*/
public function test_wp_import_map_no_duplicate_dependencies() {
$this->script_modules->register( 'foo', '/foo.js', array( 'dep' ) );
$this->script_modules->register( 'bar', '/bar.js', array( 'dep' ) );
$this->script_modules->register( 'dep', '/dep.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'bar' );
$import_map = $this->get_import_map();
$this->assertCount( 1, $import_map );
$this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
}
/**
* Tests that all recursive dependencies (both static and dynamic) are
* included in the import map.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_import_map()
*/
public function test_wp_import_map_recursive_dependencies() {
$this->script_modules->register(
'foo',
'/foo.js',
array(
'static-dep',
array(
'id' => 'dynamic-dep',
'import' => 'dynamic',
),
)
);
$this->script_modules->register(
'static-dep',
'/static-dep.js',
array(
array(
'id' => 'nested-static-dep',
'import' => 'static',
),
array(
'id' => 'nested-dynamic-dep',
'import' => 'dynamic',
),
)
);
$this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js' );
$this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
$this->script_modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
$this->script_modules->register( 'no-dep', '/no-dep.js' );
$this->script_modules->enqueue( 'foo' );
$import_map = $this->get_import_map();
$this->assertStringStartsWith( '/static-dep.js', $import_map['static-dep'] );
$this->assertStringStartsWith( '/dynamic-dep.js', $import_map['dynamic-dep'] );
$this->assertStringStartsWith( '/nested-static-dep.js', $import_map['nested-static-dep'] );
$this->assertStringStartsWith( '/nested-dynamic-dep.js', $import_map['nested-dynamic-dep'] );
$this->assertArrayNotHasKey( 'no-dep', $import_map );
}
/**
* Tests that the import map is not printed at all if there are no
* dependencies.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_import_map()
*/
public function test_wp_import_map_doesnt_print_if_no_dependencies() {
$this->script_modules->register( 'foo', '/foo.js' ); // No deps.
$this->script_modules->enqueue( 'foo' );
$import_map_markup = get_echo( array( $this->script_modules, 'print_import_map' ) );
$this->assertEmpty( $import_map_markup );
}
/**
* Tests that only static dependencies are preloaded and dynamic ones are
* excluded.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_script_module_preloads()
*/
public function test_wp_enqueue_preloaded_static_dependencies() {
$this->script_modules->register(
'foo',
'/foo.js',
array(
'static-dep',
array(
'id' => 'dynamic-dep',
'import' => 'dynamic',
),
)
);
$this->script_modules->register(
'static-dep',
'/static-dep.js',
array(
array(
'id' => 'nested-static-dep',
'import' => 'static',
),
array(
'id' => 'nested-dynamic-dep',
'import' => 'dynamic',
),
)
);
$this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js' );
$this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
$this->script_modules->register( 'nested-dynamic-dep', '/nested-dynamic-dep.js' );
$this->script_modules->register( 'no-dep', '/no-dep.js' );
$this->script_modules->enqueue( 'foo' );
$preloaded_script_modules = $this->get_preloaded_script_modules();
$this->assertCount( 2, $preloaded_script_modules );
$this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
$this->assertStringStartsWith( '/nested-static-dep.js', $preloaded_script_modules['nested-static-dep'] );
$this->assertArrayNotHasKey( 'dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'nested-dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'no-dep', $preloaded_script_modules );
}
/**
* Tests that static dependencies of dynamic dependencies are not preloaded.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_script_module_preloads()
*/
public function test_wp_dont_preload_static_dependencies_of_dynamic_dependencies() {
$this->script_modules->register(
'foo',
'/foo.js',
array(
'static-dep',
array(
'id' => 'dynamic-dep',
'import' => 'dynamic',
),
)
);
$this->script_modules->register( 'static-dep', '/static-dep.js' );
$this->script_modules->register( 'dynamic-dep', '/dynamic-dep.js', array( 'nested-static-dep' ) );
$this->script_modules->register( 'nested-static-dep', '/nested-static-dep.js' );
$this->script_modules->register( 'no-dep', '/no-dep.js' );
$this->script_modules->enqueue( 'foo' );
$preloaded_script_modules = $this->get_preloaded_script_modules();
$this->assertCount( 1, $preloaded_script_modules );
$this->assertStringStartsWith( '/static-dep.js', $preloaded_script_modules['static-dep'] );
$this->assertArrayNotHasKey( 'dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'nested-dynamic-dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'no-dep', $preloaded_script_modules );
}
/**
* Tests that preloaded dependencies don't include enqueued script modules.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_script_module_preloads()
*/
public function test_wp_preloaded_dependencies_filter_enqueued_script_modules() {
$this->script_modules->register(
'foo',
'/foo.js',
array(
'dep',
'enqueued-dep',
)
);
$this->script_modules->register( 'dep', '/dep.js' );
$this->script_modules->register( 'enqueued-dep', '/enqueued-dep.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'enqueued-dep' ); // Not preloaded.
$preloaded_script_modules = $this->get_preloaded_script_modules();
$this->assertCount( 1, $preloaded_script_modules );
$this->assertArrayHasKey( 'dep', $preloaded_script_modules );
$this->assertArrayNotHasKey( 'enqueued-dep', $preloaded_script_modules );
}
/**
* Tests that enqueued script modules with dependants correctly add both the
* script module and its dependencies to the import map.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_import_map()
*/
public function test_wp_enqueued_script_modules_with_dependants_add_import_map() {
$this->script_modules->register(
'foo',
'/foo.js',
array(
'dep',
'enqueued-dep',
)
);
$this->script_modules->register( 'dep', '/dep.js' );
$this->script_modules->register( 'enqueued-dep', '/enqueued-dep.js' );
$this->script_modules->enqueue( 'foo' );
$this->script_modules->enqueue( 'enqueued-dep' ); // Also in the import map.
$import_map = $this->get_import_map();
$this->assertCount( 2, $import_map );
$this->assertArrayHasKey( 'dep', $import_map );
$this->assertArrayHasKey( 'enqueued-dep', $import_map );
}
/**
* Tests the functionality of the `get_src` method to ensure
* proper URLs with version strings are returned.
*
* @ticket 56313
*
* @covers ::get_src()
*/
public function test_get_src() {
$get_src = new ReflectionMethod( $this->script_modules, 'get_src' );
$get_src->setAccessible( true );
$this->script_modules->register(
'module_with_version',
'http://example.com/module.js',
array(),
'1.0'
);
$result = $get_src->invoke( $this->script_modules, 'module_with_version' );
$this->assertSame( 'http://example.com/module.js?ver=1.0', $result );
$this->script_modules->register(
'module_without_version',
'http://example.com/module.js',
array(),
null
);
$result = $get_src->invoke( $this->script_modules, 'module_without_version' );
$this->assertSame( 'http://example.com/module.js', $result );
$this->script_modules->register(
'module_with_wp_version',
'http://example.com/module.js',
array(),
false
);
$result = $get_src->invoke( $this->script_modules, 'module_with_wp_version' );
$this->assertSame( 'http://example.com/module.js?ver=' . get_bloginfo( 'version' ), $result );
$this->script_modules->register(
'module_with_existing_query_string',
'http://example.com/module.js?foo=bar',
array(),
'1.0'
);
$result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' );
$this->assertSame( 'http://example.com/module.js?foo=bar&ver=1.0', $result );
// Filter the version to include the ID in the final URL, to test the filter, this should affect the tests below.
add_filter(
'script_module_loader_src',
function ( $src, $id ) {
return add_query_arg( 'script_module_id', urlencode( $id ), $src );
},
10,
2
);
$result = $get_src->invoke( $this->script_modules, 'module_without_version' );
$this->assertSame( 'http://example.com/module.js?script_module_id=module_without_version', $result );
$result = $get_src->invoke( $this->script_modules, 'module_with_existing_query_string' );
$this->assertSame( 'http://example.com/module.js?foo=bar&ver=1.0&script_module_id=module_with_existing_query_string', $result );
}
/**
* Tests that the correct version is propagated to the import map, enqueued
* script modules and preloaded script modules.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
* @covers ::print_import_map()
* @covers ::print_script_module_preloads()
* @covers ::get_version_query_string()
*/
public function test_version_is_propagated_correctly() {
$this->script_modules->register(
'foo',
'/foo.js',
array(
'dep',
),
'1.0'
);
$this->script_modules->register( 'dep', '/dep.js', array(), '2.0' );
$this->script_modules->enqueue( 'foo' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertSame( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
$import_map = $this->get_import_map();
$this->assertSame( '/dep.js?ver=2.0', $import_map['dep'] );
$preloaded_script_modules = $this->get_preloaded_script_modules();
$this->assertSame( '/dep.js?ver=2.0', $preloaded_script_modules['dep'] );
}
/**
* Tests that a script module is not registered when calling enqueue without a
* valid src.
*
* @ticket 56313
*
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_enqueue_script_module_doesnt_register_without_a_valid_src() {
$this->script_modules->enqueue( 'foo' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 0, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
}
/**
* Tests that a script module is registered when calling enqueue with a valid
* src.
*
* @ticket 56313
*
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_enqueue_script_module_registers_with_valid_src() {
$this->script_modules->enqueue( 'foo', '/foo.js' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
}
/**
* Tests that a script module is registered when calling enqueue with a valid
* src the second time.
*
* @ticket 56313
*
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
*/
public function test_wp_enqueue_script_module_registers_with_valid_src_the_second_time() {
$this->script_modules->enqueue( 'foo' ); // Not valid src.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 0, $enqueued_script_modules );
$this->assertArrayNotHasKey( 'foo', $enqueued_script_modules );
$this->script_modules->enqueue( 'foo', '/foo.js' ); // Valid src.
$enqueued_script_modules = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertStringStartsWith( '/foo.js', $enqueued_script_modules['foo'] );
}
/**
* Tests that a script module is registered with all the params when calling
* enqueue.
*
* @ticket 56313
*
* @covers ::register()
* @covers ::enqueue()
* @covers ::print_enqueued_script_modules()
* @covers ::print_import_map()
*/
public function test_wp_enqueue_script_module_registers_all_params() {
$this->script_modules->enqueue( 'foo', '/foo.js', array( 'dep' ), '1.0' );
$this->script_modules->register( 'dep', '/dep.js' );
$enqueued_script_modules = $this->get_enqueued_script_modules();
$import_map = $this->get_import_map();
$this->assertCount( 1, $enqueued_script_modules );
$this->assertSame( '/foo.js?ver=1.0', $enqueued_script_modules['foo'] );
$this->assertCount( 1, $import_map );
$this->assertStringStartsWith( '/dep.js', $import_map['dep'] );
}
/**
* @ticket 61510
*/
public function test_print_script_module_data_prints_enqueued_module_data() {
$this->script_modules->enqueue( '@test/module', '/example.js' );
add_action(
'script_module_data_@test/module',
function ( $data ) {
$data['foo'] = 'bar';
return $data;
}
);
$actual = get_echo( array( $this->script_modules, 'print_script_module_data' ) );
$expected = <<<HTML
<script type="application/json" id="wp-script-module-data-@test/module">
{"foo":"bar"}
</script>
HTML;
$this->assertSame( $expected, $actual );
}
/**
* @ticket 61510
*/
public function test_print_script_module_data_prints_dependency_module_data() {
$this->script_modules->register( '@test/dependency', '/dependency.js' );
$this->script_modules->enqueue( '@test/module', '/example.js', array( '@test/dependency' ) );
add_action(
'script_module_data_@test/dependency',
function ( $data ) {
$data['foo'] = 'bar';
return $data;
}
);
$actual = get_echo( array( $this->script_modules, 'print_script_module_data' ) );
$expected = <<<HTML
<script type="application/json" id="wp-script-module-data-@test/dependency">
{"foo":"bar"}
</script>
HTML;
$this->assertSame( $expected, $actual );
}
/**
* @ticket 61510
*/
public function test_print_script_module_data_does_not_print_nondependency_module_data() {
$this->script_modules->register( '@test/other', '/dependency.js' );
$this->script_modules->enqueue( '@test/module', '/example.js' );
add_action(
'script_module_data_@test/other',
function ( $data ) {
$data['foo'] = 'bar';
return $data;
}
);
$actual = get_echo( array( $this->script_modules, 'print_script_module_data' ) );
$this->assertSame( '', $actual );
}
/**
* @ticket 61510
*/
public function test_print_script_module_data_does_not_print_empty_data() {
$this->script_modules->enqueue( '@test/module', '/example.js' );
add_action(
'script_module_data_@test/module',
function ( $data ) {
return $data;
}
);
$actual = get_echo( array( $this->script_modules, 'print_script_module_data' ) );
$this->assertSame( '', $actual );
}
/**
* @ticket 61510
*
* @dataProvider data_special_chars_script_encoding
* @param string $input Raw input string.
* @param string $expected Expected output string.
* @param string $charset Blog charset option.
*/
public function test_print_script_module_data_encoding( $input, $expected, $charset ) {
add_filter(
'pre_option_blog_charset',
function () use ( $charset ) {
return $charset;
}
);
$this->script_modules->enqueue( '@test/module', '/example.js' );
add_action(
'script_module_data_@test/module',
function ( $data ) use ( $input ) {
$data[''] = $input;
return $data;
}
);
$actual = get_echo( array( $this->script_modules, 'print_script_module_data' ) );
$expected = <<<HTML
<script type="application/json" id="wp-script-module-data-@test/module">
{"":"{$expected}"}
</script>
HTML;
$this->assertSame( $expected, $actual );
}
/**
* Data provider.
*
* @return array
*/
public static function data_special_chars_script_encoding(): array {
return array(
// UTF-8
'Solidus' => array( '/', '/', 'UTF-8' ),
'Double quote' => array( '"', '\\"', 'UTF-8' ),
'Single quote' => array( '\'', '\'', 'UTF-8' ),
'Less than' => array( '<', '\u003C', 'UTF-8' ),
'Greater than' => array( '>', '\u003E', 'UTF-8' ),
'Ampersand' => array( '&', '&', 'UTF-8' ),
'Newline' => array( "\n", "\\n", 'UTF-8' ),
'Tab' => array( "\t", "\\t", 'UTF-8' ),
'Form feed' => array( "\f", "\\f", 'UTF-8' ),
'Carriage return' => array( "\r", "\\r", 'UTF-8' ),
'Line separator' => array( "\u{2028}", "\u{2028}", 'UTF-8' ),
'Paragraph separator' => array( "\u{2029}", "\u{2029}", 'UTF-8' ),
/*
* The following is the Flag of England emoji
* PHP: "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}"
*/
'Flag of england' => array( '🏴', '🏴', 'UTF-8' ),
'Malicious script closer' => array( '</script>', '\u003C/script\u003E', 'UTF-8' ),
'Entity-encoded malicious script closer' => array( '</script>', '</script>', 'UTF-8' ),
// Non UTF-8
'Solidus' => array( '/', '/', 'iso-8859-1' ),
'Less than' => array( '<', '\u003C', 'iso-8859-1' ),
'Greater than' => array( '>', '\u003E', 'iso-8859-1' ),
'Ampersand' => array( '&', '&', 'iso-8859-1' ),
'Newline' => array( "\n", "\\n", 'iso-8859-1' ),
'Tab' => array( "\t", "\\t", 'iso-8859-1' ),
'Form feed' => array( "\f", "\\f", 'iso-8859-1' ),
'Carriage return' => array( "\r", "\\r", 'iso-8859-1' ),
'Line separator' => array( "\u{2028}", "\u2028", 'iso-8859-1' ),
'Paragraph separator' => array( "\u{2029}", "\u2029", 'iso-8859-1' ),
/*
* The following is the Flag of England emoji
* PHP: "\u{1F3F4}\u{E0067}\u{E0062}\u{E0065}\u{E006E}\u{E0067}\u{E007F}"
*/
'Flag of england' => array( '🏴', "\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f", 'iso-8859-1' ),
'Malicious script closer' => array( '</script>', '\u003C/script\u003E', 'iso-8859-1' ),
'Entity-encoded malicious script closer' => array( '</script>', '</script>', 'iso-8859-1' ),
);
}
/**
* @ticket 61510
*
* @dataProvider data_invalid_script_module_data
* @param mixed $data Data to return in filter.
*/
public function test_print_script_module_data_does_not_print_invalid_data( $data ) {
$this->script_modules->enqueue( '@test/module', '/example.js' );
add_action(
'script_module_data_@test/module',
function ( $_ ) use ( $data ) {
return $data;
}
);
$actual = get_echo( array( $this->script_modules, 'print_script_module_data' ) );
$this->assertSame( '', $actual );
}
/**
* Data provider.
*
* @return array
*/
public static function data_invalid_script_module_data(): array {
return array(
'null' => array( null ),
'stdClass' => array( new stdClass() ),
'number 1' => array( 1 ),
'string' => array( 'string' ),
);
}
/**
* @ticket 61500
*/
public function test_included_module_appears_in_importmap() {
$this->script_modules->register( 'dependency', '/dep.js' );
$this->script_modules->register( 'example', '/example.js', array( 'dependency' ) );
// Nothing printed now.
$this->assertSame( array(), $this->get_enqueued_script_modules() );
$this->assertSame( array(), $this->get_preloaded_script_modules() );
$this->assertSame( array(), $this->get_import_map() );
// After including, the importmap should be populated.
$this->script_modules->include_in_import_map( 'example' );
$this->assertSame( array(), $this->get_enqueued_script_modules() );
$this->assertSame( array(), $this->get_preloaded_script_modules() );
$import_map = $this->get_import_map();
$this->assertCount( 2, $import_map );
$this->assertArrayHasKey( 'example', $import_map );
$this->assertArrayHasKey( 'dependency', $import_map );
}
/**
* @ticket 61500
*/
public function test_included_modules_concat_With_enqueued_dependencies() {
$this->script_modules->register( 'dependency-enqueued', '/dep.js' );
$this->script_modules->register(
'enqueued',
'/example.js',
array(
array(
'id' => 'dependency-enqueued',
'import' => 'dynamic',
),
)
);
$this->script_modules->enqueue( 'enqueued' );
$this->script_modules->register( 'dependency', '/dep.js' );
$this->script_modules->register( 'example', '/example.js', array( 'dependency' ) );
// Only dependency-enqueued should be printed.
$enqueued = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued );
$this->assertArrayHasKey( 'enqueued', $enqueued );
$this->assertSame( array(), $this->get_preloaded_script_modules() );
$import_map = $this->get_import_map();
$this->assertCount( 1, $import_map );
$this->assertArrayHasKey( 'dependency-enqueued', $import_map );
// After including, the importmap should be populated.
$this->script_modules->include_in_import_map( 'example' );
$enqueued = $this->get_enqueued_script_modules();
$this->assertCount( 1, $enqueued );
$this->assertArrayHasKey( 'enqueued', $enqueued );
$this->assertSame( array(), $this->get_preloaded_script_modules() );
$import_map = $this->get_import_map();
$this->assertCount( 3, $import_map );
$this->assertArrayHasKey( 'dependency-enqueued', $import_map );
$this->assertArrayHasKey( 'dependency', $import_map );
$this->assertArrayHasKey( 'example', $import_map );
}
}